Skip to content

Instantly share code, notes, and snippets.

@hiroshineya
Created May 16, 2012 09:22
Show Gist options
  • Select an option

  • Save hiroshineya/2709017 to your computer and use it in GitHub Desktop.

Select an option

Save hiroshineya/2709017 to your computer and use it in GitHub Desktop.
upload file to GoogleDocs
#!/usr/bin/python
# -*- coding: utf-8 -*-
# upload (bin)file to Google Docs.
#
import sys
import datetime
import os
import os.path
import time
import atom
import gdata.docs
import gdata.docs.client
import gdata.docs.data
import gdata.data
import gdata.client
# create Google Docs client for upload
def create_gd_upload_client():
client = gdata.docs.client.DocsClient()
client.ssl = True
client.http_client.debug = False
return client
# login..
def login_gd_client(client, user, passwd):
client.ClientLogin(
user,
passwd,
client.source,
service='writely')
# uploading file to Google Docs.
def upload_file(c, filename, title):
print "upload file."
print "filename:" + filename
print "title:" + title
content_type = "application/octet-stream"
ms = gdata.data.MediaSource( file_path=filename,
content_type=content_type,
content_length=os.path.getsize(filename))
ru = gdata.client.ResumableUploader(client=c,
file_handle=ms.file_handle,
content_type=ms.content_type,
total_file_size=ms.content_length,
desired_class=gdata.data.GDEntry)
uri = '/feeds/upload/create-session/default/private/full?convert=false'
new_entry = gdata.data.GDEntry(title=atom.data.Title(text=title))
updated_entry = ru.UploadFile(resumable_media_link=uri, entry=new_entry)
print "upload done."
def main():
res = 0
today = datetime.date.today()
print "today:" + today.strftime("%Y-%m-%d")
upload_client = create_gd_upload_client()
try:
login_gd_client(upload_client, [USER_ID], [PASSWORD])
print "login done.."
upload_file(
upload_client,
[FILE_PATH],
[TITLE_AT_GOOGLE_DOCS])
except gdata.client.BadAuthentication:
print "login error...."
res = 9
except:
print "Unexpected error:", sys.exc_info()[0]
res = 9
return res
if __name__ == "__main__":
print "start"
res = main()
if res != 0:
print "ERROE END !!"
sys.exit(res)
print "process done.."
sys.exit(0)
@hiroshineya

Copy link
Copy Markdown
Author

prototype.
for backup data files to GoogleDocs..

need
gdata-python-client:
http://code.google.com/p/gdata-python-client/

https://developers.google.com/google-apps/documents-list/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment