Ejemplo 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)
Ejemplo n.º 2
0
def get_primer_list_from_gdoc(title=_os.environ["pydna_primersgdoc"],
                              mime="application/vnd.google-apps.document",
                              dir_=_os.environ["pydna_config_dir"],
                              jsonfn="service_account.json"):
    """Assumes that a google service account is used.

    See instructions at the docs for gdrive
    https://gspread.readthedocs.io/en/latest/oauth2.html

    In step 7, put the service_account.json file into the pydna config
    directory. You can find this directory by:

        pydna.open_config_folder()

    """
    JSON_FILE = _os.path.join(dir_, jsonfn)
    scope = ['https://www.googleapis.com/auth/drive']
    gauth = _GoogleAuth()
    gauth.credentials = (_ServiceAccountCredentials.from_json_keyfile_name(
        JSON_FILE, scope))
    gauth.auth_method = 'service'

    fl = _GoogleDrive(gauth).ListFile({
        'q':
        f"title = '{title}' and mimeType='{mime}'"
    }).GetList()

    content = fl.pop(0).GetContentString(mimetype="text/plain")

    lines = []
    for line in content.splitlines():
        if not line.startswith("#"):
            lines.append(line)

    return _parse_primers("\n".join(lines))[::-1]
Ejemplo n.º 3
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
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']))