2014-09-20 115 views
0

我使用以下python代碼將視頻從Linux上的命令上傳到我的Google Drive。 基於Linux內核的android可以在android上使用嗎?如果沒有,是否有任何安卓服務在後臺上傳視頻(以太網到Google Drive或DropBox)?如何使用python代碼將android上的文件上傳到Google Drive

$蟒蛇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 = "[email protected]"     

     # 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

你寫了那個腳本嗎? 'from curses import ascii'有點奇怪,因爲腳本似乎沒有使用它......但是從一個快速的谷歌搜索看來,Android Python確實有詛咒,F​​WIW。 – 2014-09-20 11:38:42

+0

我實際上修改了一個我從互聯網上獲得的代碼,但我忘記刪除ascii.But代碼在Linux(Beaglebone)上運行良好。 – 2014-09-20 11:47:25

回答

0

我真的不明白你爲什麼會想使用Python在Android? Android可以通過Google Play服務使用Google Drive API。這裏有很好的文檔:https://developers.google.com/drive/android/get-started

您也可以查看官方Quickstart App:https://github.com/googledrive/android-quickstart

+0

我在android java.But非常初學,我會嘗試第二個鏈接。我只需要自動上傳文件,而不需要點擊android屏幕上的任何按鈕。 – 2014-09-20 11:54:25

+0

您必須檢查[入門指南](https://developers.google.com/drive/android/get-started)以使其正常工作。 – Tas 2014-09-20 19:15:29

相關問題