0

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)
Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Majid ff
  • 249
  • 7
  • 22
  • Did you write that script? The `from curses import ascii` is a bit bizarre, since the script doesn't appear to use it... but from a quick Googling it appears that Android Python does have curses, FWIW. – PM 2Ring Sep 20 '14 at 11:38
  • I actually modified the code from a one that I got it from the internet, I just forgot to remove ascii.But the code work fine on linux (Beaglebone). – Majid ff Sep 20 '14 at 11:47

1 Answers1

0

I do not really see why you would want to use Python on Android? Android can use the Google Drive API through the Google Play Services. It is very well documented here: https://developers.google.com/drive/android/get-started.

You could check the official Quickstart App as well: https://github.com/googledrive/android-quickstart.

Tas
  • 141
  • 2
  • 8
  • I am very beginner in android java.But I will try with the second link.I only need to upload files automatically without click any button on the android screen. – Majid ff Sep 20 '14 at 11:54
  • You will have to check the [get started guide](https://developers.google.com/drive/android/get-started) as well in order to make it work. – Tas Sep 20 '14 at 19:15