Advertisement
Guest User

gdata-get.py

a guest
Feb 9th, 2015
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. # -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
  2. #
  3. # Copyright 2011 Carlos Abalde <carlos.abalde@gmail.com>
  4. #
  5. # This file was originally part of duplicity.
  6. #
  7. # Duplicity is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the
  9. # Free Software Foundation; either version 2 of the License, or (at your
  10. # option) any later version.
  11. #
  12. # Duplicity is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with duplicity; if not, write to the Free Software Foundation,
  19. # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20.  
  21. import os.path
  22. import string
  23. import urllib
  24.  
  25. class GDocsBackend():
  26.     """Connect to remote store using Google Google Documents List API"""
  27.  
  28.     ROOT_FOLDER_ID = 'folder%3Aroot'
  29.     BACKUP_DOCUMENT_TYPE = 'application/binary'
  30.  
  31.     def __init__(self):
  32.  
  33.         # Import Google Data APIs libraries.
  34.         try:
  35.             global atom
  36.             global gdata
  37.             import atom.data
  38.             import gdata.client
  39.             import gdata.docs.client
  40.             import gdata.docs.data
  41.         except ImportError:
  42.             raise BackendException('Google Docs backend requires Google Data APIs Python '
  43.                                    'Client Library (see http://code.google.com/p/gdata-python-client/).')
  44.  
  45.         # Setup client instance.
  46.         self.client = gdata.docs.client.DocsClient(source='duplicity 0.7.01')
  47.         self.client.ssl = True
  48.         self.client.http_client.debug = False
  49.         self._authorize('USERNAME' + '@' + 'DOMAIN', 'PASSWORD')
  50.  
  51.         # Fetch destination folder entry (and crete hierarchy if required).
  52.         folder_names = [ 'test-gdata' ];
  53.         parent_folder = None
  54.         parent_folder_id = GDocsBackend.ROOT_FOLDER_ID
  55.         for folder_name in folder_names:
  56.             entries = self._fetch_entries(parent_folder_id, 'folder', folder_name)
  57.             if entries is not None:
  58.                 if len(entries) == 1:
  59.                     parent_folder = entries[0]
  60.                 elif len(entries) == 0:
  61.                     folder = gdata.docs.data.Resource(type='folder', title=folder_name)
  62.                     parent_folder = self.client.create_resource(folder, collection=parent_folder)
  63.                 else:
  64.                     parent_folder = None
  65.                 if parent_folder:
  66.                     parent_folder_id = parent_folder.resource_id.text
  67.                 else:
  68.                     raise BackendException("Error while creating destination folder '%s'." % folder_name)
  69.             else:
  70.                 raise BackendException("Error while fetching destination folder '%s'." % folder_name)
  71.         self.folder = parent_folder
  72.  
  73.     def _get(self, remote_filename, local_path):
  74.         entries = self._fetch_entries(self.folder.resource_id.text,
  75.                                       GDocsBackend.BACKUP_DOCUMENT_TYPE,
  76.                                       remote_filename)
  77.         if len(entries) == 1:
  78.             entry = entries[0]
  79.             self.client.DownloadResource(entry, local_path) #.name)
  80.         else:
  81.             raise BackendException("Failed to find file '%s' in remote folder '%s'"
  82.                                    % (remote_filename, self.folder.title.text))
  83.  
  84.     def _authorize(self, email, password, captcha_token=None, captcha_response=None):
  85.         try:
  86.             self.client.client_login(email,
  87.                                      password,
  88.                                      source='duplicity 0.7.01',
  89.                                      service='writely',
  90.                                      captcha_token=captcha_token,
  91.                                      captcha_response=captcha_response)
  92.         except gdata.client.CaptchaChallenge as challenge:
  93.             print('A captcha challenge in required. Please visit ' + challenge.captcha_url)
  94.             answer = None
  95.             while not answer:
  96.                 answer = raw_input('Answer to the challenge? ')
  97.             self._authorize(email, password, challenge.captcha_token, answer)
  98.         except gdata.client.BadAuthentication:
  99.             raise BackendException('Invalid user credentials given. Be aware that accounts '
  100.                                    'that use 2-step verification require creating an application specific '
  101.                                    'access code for using this Duplicity backend. Follow the instruction in '
  102.                                    'http://www.google.com/support/accounts/bin/static.py?page=guide.cs&guide=1056283&topic=1056286 '
  103.                                    'and create your application-specific password to run duplicity backups.')
  104.  
  105.     def _fetch_entries(self, folder_id, type, title=None):
  106.         # Build URI.
  107.         uri = '/feeds/default/private/full/%s/contents' % folder_id
  108.         if type == 'folder':
  109.             uri += '/-/folder?showfolders=true'
  110.         elif type == GDocsBackend.BACKUP_DOCUMENT_TYPE:
  111.             uri += '?showfolders=false'
  112.         else:
  113.             uri += '?showfolders=true'
  114.         if title:
  115.             uri += '&title=' + urllib.quote(title) + '&title-exact=true'
  116.  
  117.         # Fetch entries.
  118.         entries = self.client.get_all_resources(uri=uri)
  119.        
  120.         # Done!
  121.         return entries
  122.  
  123. g=GDocsBackend()
  124.  
  125. g._get('bigvid.avi', '/mnt/bigstore/tmp/testbigdownload.avi')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement