Esempio n. 1
0
def check_google():
    from pydrive2.auth import GoogleAuth
    from pydrive2.drive import GoogleDrive
    from pydrive2.auth import ServiceAccountCredentials

    gauth = GoogleAuth()
    scope = ['https://www.googleapis.com/auth/drive']
    cred_path = os.path.join(DATA_PATH, 'credentials.json')
    gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
        cred_path, scope)
    drive = GoogleDrive(gauth)
    file_id = '1603ahBNdt1SnSaYYBE-G8SA6qgRTQ6fF'
    file_list = drive.ListFile({
        'q':
        "'%s' in parents and trashed=false" % file_id
    }).GetList()

    df = pandas.DataFrame(file_list)
    dfclean = df[['createdDate', 'id', 'title']].copy()
    dfclean['date'] = pandas.to_datetime(dfclean['createdDate'],
                                         format='%Y-%m-%d',
                                         errors='coerce')
    lastupdate = dfclean.loc[dfclean['createdDate'] ==
                             '2020-09-11T01:53:29.639Z'].iloc[0]['date']
    dfnew = dfclean.loc[dfclean['date'] > lastupdate]

    all_files = os.listdir(REPORTS_PATH)
    new_files = [
        item for item in all_files
        if item not in dfnew['title'].unique().tolist()
    ]
    reportdf = dfnew.loc[dfnew['title'].isin(new_files)]
    return (reportdf)
Esempio n. 2
0
def check_google():
    from pydrive2.auth import GoogleAuth
    from pydrive2.drive import GoogleDrive
    from pydrive2.auth import ServiceAccountCredentials

    gauth = GoogleAuth()
    scope = ['https://www.googleapis.com/auth/drive']
    gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
        'credentials.json', scope)
    drive = GoogleDrive(gauth)
    file_id = '1vC8oXhfhogAh7olq9BvEPdwwvyeXsZkk'
    file_list = drive.ListFile({
        'q':
        "'%s' in parents and trashed=false" % file_id
    }).GetList()

    df = pandas.DataFrame(file_list)
    dfclean = df[['createdDate', 'id', 'title']].copy()
    dfclean['date'] = pandas.to_datetime(dfclean['createdDate'],
                                         format='%Y-%m-%d',
                                         errors='coerce')
    lastupdate = dfclean.loc[dfclean['createdDate'] ==
                             '2020-09-28T22:28:33.989Z'].iloc[0]['date']
    dfnew = dfclean.loc[dfclean['date'] > lastupdate]

    all_files = os.listdir('data/tables/')
    new_files = [
        item for item in dfnew['title'].unique().tolist()
        if item not in all_files
    ]
    tabledf = dfnew.loc[dfnew['title'].isin(new_files)]
    return (tabledf)
Esempio n. 3
0
 def auth(self):
     if GoogleDrive.drive:
         return
     gauth = GoogleAuth()
     scope = ['https://www.googleapis.com/auth/drive']
     gauth.auth_method = 'service'
     if GD_CREDENTIALS:
         if GD_CREDENTIALS.startswith('{'):
             credentials = json.loads(GD_CREDENTIALS)
             gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict(
                 credentials, scope)
         else:
             gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
                 GD_CREDENTIALS, scope)
     else:
         raise NazurinError(
             'Credentials not found for Google Drive storage.')
     GoogleDrive.drive = GDrive(gauth)
Esempio n. 4
0
def create_client(credentials):
    """
    create_engine:

    Sets up Google Drive API access using credentials (see above).
    """
    gauth = GoogleAuth()
    gauth.credentials = credentials
    drive = GoogleDrive(gauth)
    return drive
Esempio n. 5
0
 def __init__(self, cred_file, folder_id):
   try:
     from pydrive2.auth import ServiceAccountCredentials, GoogleAuth
     from pydrive2.drive import GoogleDrive
   except ImportError:
     raise Sorry("Pydrive2 not found. Try:\n$ conda install pydrive2 -c conda-forge")
   gauth = GoogleAuth()
   scope = ['https://www.googleapis.com/auth/drive']
   gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
       cred_file, scope
   )
   self.drive = GoogleDrive(gauth)
   self.top_folder_id = folder_id
Esempio n. 6
0
# pydrive2 is an actively maintained fork
from pydrive2.drive import GoogleDrive
from pydrive2.auth import GoogleAuth, ServiceAccountCredentials

gauth = GoogleAuth()
# drive is nice for sanity checking, but should use drive.file for the Real Thing
# https://developers.google.com/drive/api/v3/about-auth
scope = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'google-credentials.json', scope)
drive = GoogleDrive(gauth)

for file_list in drive.ListFile({'maxResults': 10}):
    print('Received {} files from Files.list()'.format(
        len(file_list)))  # <= 10
    for file1 in file_list:
        print('title: {}, id: {}, mime: {}'.format(file1['title'], file1['id'],
                                                   file1['mimeType']))
Esempio n. 7
0
    def __init__(self, parsed_url):
        duplicity.backend.Backend.__init__(self, parsed_url)
        try:
            import httplib2
            from apiclient.discovery import build
        except ImportError as e:
            raise BackendException(u"""\
PyDrive backend requires PyDrive and Google API client installation.
Please read the manpage for setup details.
Exception: %s""" % str(e))

        # Shared Drive ID specified as a query parameter in the backend URL.
        # Example: pydrive://developer.gserviceaccount.com/target-folder/?driveID=<SHARED DRIVE ID>
        self.api_params = {}
        self.shared_drive_id = None
        if u'driveID' in parsed_url.query_args:
            self.shared_drive_id = parsed_url.query_args[u'driveID'][0]
            self.api_params = {
                u'corpora': u'teamDrive',
                u'teamDriveId': self.shared_drive_id,
                u'includeTeamDriveItems': True,
                u'supportsTeamDrives': True
            }

        try:
            from pydrive2.auth import GoogleAuth
            from pydrive2.drive import GoogleDrive
            from pydrive2.files import ApiRequestError, FileNotUploadedError
        except ImportError as e:
            try:
                from pydrive.auth import GoogleAuth
                from pydrive.drive import GoogleDrive
                from pydrive.files import ApiRequestError, FileNotUploadedError
            except ImportError as e:
                raise BackendException(u"""\
PyDrive backend requires PyDrive installation.  Please read the manpage for setup details.
Exception: %s""" % str(e))

        # let user get by with old client while he can
        try:
            from oauth2client.client import SignedJwtAssertionCredentials
            self.oldClient = True
        except:
            from oauth2client.service_account import ServiceAccountCredentials
            from oauth2client import crypt
            self.oldClient = False

        if u'GOOGLE_DRIVE_ACCOUNT_KEY' in os.environ:
            account_key = os.environ[u'GOOGLE_DRIVE_ACCOUNT_KEY']
            if self.oldClient:
                credentials = SignedJwtAssertionCredentials(
                    parsed_url.username + u'@' + parsed_url.hostname,
                    account_key,
                    scopes=u'https://www.googleapis.com/auth/drive')
            else:
                signer = crypt.Signer.from_string(account_key)
                credentials = ServiceAccountCredentials(
                    parsed_url.username + u'@' + parsed_url.hostname,
                    signer,
                    scopes=u'https://www.googleapis.com/auth/drive')
            credentials.authorize(httplib2.Http())
            gauth = GoogleAuth(http_timeout=60)
            gauth.credentials = credentials
        elif u'GOOGLE_DRIVE_SETTINGS' in os.environ:
            gauth = GoogleAuth(
                settings_file=os.environ[u'GOOGLE_DRIVE_SETTINGS'],
                http_timeout=60)
            gauth.CommandLineAuth()
        elif (u'GOOGLE_SECRETS_FILE' in os.environ
              and u'GOOGLE_CREDENTIALS_FILE' in os.environ):
            gauth = GoogleAuth(http_timeout=60)
            gauth.LoadClientConfigFile(os.environ[u'GOOGLE_SECRETS_FILE'])
            gauth.LoadCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE'])
            if gauth.credentials is None:
                gauth.CommandLineAuth()
            elif gauth.access_token_expired:
                gauth.Refresh()
            else:
                gauth.Authorize()
            gauth.SaveCredentialsFile(os.environ[u'GOOGLE_CREDENTIALS_FILE'])
        else:
            raise BackendException(
                u'GOOGLE_DRIVE_ACCOUNT_KEY or GOOGLE_DRIVE_SETTINGS environment '
                u'variable not set. Please read the manpage to fix.')
        self.drive = GoogleDrive(gauth)

        if self.shared_drive_id:
            parent_folder_id = self.shared_drive_id
        else:
            # Dirty way to find root folder id
            file_list = self.drive.ListFile({
                u'q':
                u"'Root' in parents and trashed=false"
            }).GetList()
            if file_list:
                parent_folder_id = file_list[0][u'parents'][0][u'id']
            else:
                file_in_root = self.drive.CreateFile(
                    {u'title': u'i_am_in_root'})
                file_in_root.Upload()
                parent_folder_id = file_in_root[u'parents'][0][u'id']
                file_in_root.Delete()

        # Fetch destination folder entry and create hierarchy if required.
        folder_names = parsed_url.path.split(u'/')
        for folder_name in folder_names:
            if not folder_name:
                continue
            list_file_args = {
                u'q':
                u"'" + parent_folder_id + u"' in parents and trashed=false"
            }
            list_file_args.update(self.api_params)
            file_list = self.drive.ListFile(list_file_args).GetList()
            folder = next(
                (item
                 for item in file_list if item[u'title'] == folder_name and
                 item[u'mimeType'] == u'application/vnd.google-apps.folder'),
                None)
            if folder is None:
                create_file_args = {
                    u'title': folder_name,
                    u'mimeType': u"application/vnd.google-apps.folder",
                    u'parents': [{
                        u'id': parent_folder_id
                    }]
                }
                create_file_args[u'parents'][0].update(self.api_params)
                create_file_args.update(self.api_params)
                folder = self.drive.CreateFile(create_file_args)
                if self.shared_drive_id:
                    folder.Upload(param={u'supportsTeamDrives': True})
                else:
                    folder.Upload()
            parent_folder_id = folder[u'id']
        self.folder = parent_folder_id
        self.id_cache = {}
Esempio n. 8
0
import os
import json
from pydrive2.drive import GoogleDrive
from pydrive2.auth import GoogleAuth, ServiceAccountCredentials

gauth = GoogleAuth()
# https://developers.google.com/drive/api/v3/about-auth
scope = ['https://www.googleapis.com/auth/drive.file']
# use from_json in "real" thing? or from_json_keyfile_dict?
# we'll get to it from .env, I think
with open('google-credentials.json', 'r') as f:
    creds = json.load(f)

gauth.credentials = ServiceAccountCredentials.from_json_keyfile_dict(
    creds, scope)
drive = GoogleDrive(gauth)
file4 = drive.CreateFile({
    'title':
    'appdata3.json',
    'mimeType':
    'application/json',
    # to get parent ID (i.e. the folder), click on folder on drive.google.com
    # and copy the last bit after https://drive.google.com/drive/u/1/folders/**folder ID**
    # this ID doesn't need to be secret.
    'parents': [{
        'id': '1tl0X6_v51QK979hr7KFFdS94KkGNOxmD'
    }]
})
file4.SetContentString('{"firstname": "John", "lastname": "Smith"}')
file4.Upload()