Android : How to upload file on android to Google Drive using python code

on Saturday, September 20, 2014


I am using the following python code to upload video to my Google Drive from the command on Linux. Is it possible to used on android since android based on Linux kernel? if not, is there any android service to upload the video in background (ether to Google Drive or DropBox)?


$ python uploader.py video_file.avi


uploader.py :



#!/usr/bin/python
import smtplib
import os.path
import sys
import gdata.data
import gdata.docs.data
import gdata.docs.client
import ConfigParser

from curses import ascii


class MotionUploader:
def __init__(self):


self.password = "my Gmail password"
self.sender = "myGmail@gmail.com"

# Folder (or collection) in Docs where you want the videos to go
self.folder = 'motion'


self._create_gdata_client()

def _create_gdata_client(self):
"""Create a Documents List Client."""
self.client = gdata.docs.client.DocsClient(source='motion_uploader')
self.client.http_client.debug = False
self.client.client_login(self.sender, self.password, service=self.client.auth_service, source=self.client.source)

def _get_folder_resource(self):
"""Find and return the resource whose title matches the given folder."""
col = None
for resource in self.client.GetAllResources(uri='/feeds/default/private/full/-/folder'):
if resource.title.text == self.folder:
col = resource
break
return col


def _upload(self, video_file_path, folder_resource):
'''Upload the video and return the doc'''
doc = gdata.docs.data.Resource(type='video', title=os.path.basename(video_file_path))
media = gdata.data.MediaSource()
media.SetFileHandle(video_file_path, 'video/avi')
doc = self.client.CreateResource(doc, media=media, collection=folder_resource)
return doc

def upload_video(self, video_file_path):
"""Upload a video to the specified folder"""

folder_resource = self._get_folder_resource()
if not folder_resource:
raise Exception('Could not find the %s folder' % self.folder)

doc = self._upload(video_file_path, folder_resource)


video_link = None
for link in doc.link:
if 'docs.google.com/file/d' in link.href:
video_link = link.href
break


if __name__ == '__main__':
try:
if len(sys.argv) < 2:
exit('Usage: uploader.py {config-file-path} {video-file-path}')

vid_path = sys.argv[1]
if not os.path.exists(vid_path):
exit('Video file does not exist [%s]' % vid_path)
MotionUploader().upload_video(vid_path)
except gdata.client.BadAuthentication:
exit('Invalid user credentials given.')
except gdata.client.Error:
exit('Login Error')
except Exception as e:
exit('Error: [%s]' % e)

0 comments:

Post a Comment