Guest User

Untitled

a guest
Apr 27th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #!/usr/bin/env/python
  2. """
  3. gdocs2s3.py
  4.  
  5. Automatically downloads all of your Google Docs and backs them up to Amazon S3
  6. """
  7.  
  8. """
  9. Copyright (c) 2010 Scott Rubin apreche@frontrowcrew.com
  10.  
  11. Permission is hereby granted, free of charge, to any person
  12. obtaining a copy of this software and associated documentation
  13. files (the "Software"), to deal in the Software without
  14. restriction, including without limitation the rights to use,
  15. copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the
  17. Software is furnished to do so, subject to the following
  18. conditions:
  19.  
  20. The above copyright notice and this permission notice shall be
  21. included in all copies or substantial portions of the Software.
  22.  
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  25. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  27. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  28. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. OTHER DEALINGS IN THE SOFTWARE.
  31. """
  32.  
  33. """ Begin User Config """
  34.  
  35. # Google
  36. GOOGLE_USERNAME=""
  37. GOOGLE_PASSWORD=""
  38. DOCUMENT_FORMAT = 'html' # change to 'doc', if you desire
  39.  
  40. # Amazon
  41. AWS_ACCESS_KEY_ID = ''
  42. AWS_SECRET_ACCESS_KEY = ''
  43. AWS_BUCKET_NAME = ''
  44.  
  45. # Other
  46. TEMP_STORAGE_DIR = '/tmp/'
  47.  
  48. """ End User Config """
  49.  
  50.  
  51. # Prepare S3 Connection
  52. import boto
  53. from boto.s3.connection import S3Connection
  54. from boto.s3 import Key
  55.  
  56. conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  57.  
  58. try:
  59. bucket = conn.create_bucket(AWS_BUCKET_NAME)
  60. except boto.exception.S3CreateError:
  61. print "Someone else has that bucket name. Try a different one."
  62. exit()
  63. print "Connected to S3 bucket '%s'" % AWS_BUCKET_NAME
  64.  
  65. # Export From Google Docs
  66. import gdata.docs.service
  67.  
  68. GOOGLE_SOURCE="Apreche-GDocs2S3-v1"
  69.  
  70. gd_client = gdata.docs.service.DocsService(source=GOOGLE_SOURCE)
  71. gd_client.ClientLogin(GOOGLE_USERNAME, GOOGLE_PASSWORD)
  72. feed = gd_client.GetDocumentListFeed()
  73. print "Connected to Google as '%s'" % GOOGLE_USERNAME
  74.  
  75. for entry in feed.entry:
  76. if entry.GetDocumentType() == 'document':
  77. title = entry.title.text.encode('UTF-8').replace(' ','_')
  78. filename = "%s.%s" % (title, DOCUMENT_FORMAT)
  79. temp_filename = "%s%s" % (TEMP_STORAGE_DIR, filename)
  80. print "Downloading '%s' to '%s'" % (filename, TEMP_STORAGE_DIR)
  81. export_entries = {'doc': entry, 'html': entry.resourceId.text }
  82. gd_client.Export(export_entries[DOCUMENT_FORMAT], temp_filename)
  83. k = Key(bucket)
  84. k.key = filename
  85. k.set_contents_from_filename(temp_filename)
  86. print "'%s' Backed up to S3 Bucket '%s'" % (filename, AWS_BUCKET_NAME)
  87.  
  88. print "Final Contents of Bucket '%s'" % AWS_BUCKET_NAME
  89. for key in bucket:
  90. print " %s" % key.name
Add Comment
Please, Sign In to add comment