def __init__(self, settings_file="settings.yaml"): self.gauth = GoogleAuth(settings_file) self.gauth.ServiceAuth() self.drive = GoogleDrive(self.gauth)
def __init__(self, drive=True): self.dirpath = tempfile.mkdtemp() if drive: gauth = GoogleAuth() self.drive = GoogleDrive(gauth)
async def gdrive_upload(filename: str, filebuf: BytesIO = None) -> str: """ Upload files to Google Drive using PyDrive """ # a workaround for disabling cache errors # https://github.com/googleapis/google-api-python-client/issues/299 logging.getLogger('googleapiclient.discovery_cache').setLevel( logging.CRITICAL) # Authenticate Google Drive automatically # https://stackoverflow.com/a/24542604 gauth = GoogleAuth() # Try to load saved client credentials gauth.LoadCredentialsFile("secret.json") if gauth.credentials is None: return "nosecret" if gauth.access_token_expired: gauth.Refresh() else: # Initialize the saved credentials gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile("secret.json") drive = GoogleDrive(gauth) if filename.count('/') > 1: filename = filename.split('/')[-1] filedata = { 'title': filename, "parents": [{ "kind": "drive#fileLink", "id": GDRIVE_FOLDER }] } if GDRIVE_TEAMDRIVE_ID: filedata["parents"][0]["teamDriveId"] = GDRIVE_TEAMDRIVE_ID if filebuf: mime_type = guess_type(filename) if mime_type[0] and mime_type[1]: filedata['mimeType'] = f"{mime_type[0]}/{mime_type[1]}" else: filedata['mimeType'] = 'application/octet-stream' file = drive.CreateFile(filedata) file.content = filebuf else: file = drive.CreateFile(filedata) file.SetContentFile(filename) name = filename.split('/')[-1] if GDRIVE_TEAMDRIVE_ID: file.Upload(param={'supportsTeamDrives': True}) else: file.Upload() # insert new permission file.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }) if not filebuf: os.remove(filename) file['downloadUrl'] = file['downloadUrl'].replace("gd=true", "authuser=0") reply = f"[{name}]({file['alternateLink']})\n" \ f"__Direct link:__ [Here]({file['downloadUrl']})" return reply
def auth_and_save_credential(): gAuth = GoogleAuth() gAuth.LocalWebserverAuth() gAuth.SaveCredentialsFile("credentials.txt")
def upload(filename: str, update, context, parent_folder: str = None) -> None: FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder' drive: GoogleDrive http = None initial_folder = None gauth: drive.GoogleAuth = GoogleAuth() ID = update.message.from_user.id ID = str(ID) gauth.LoadCredentialsFile( path.join(path.dirname(path.abspath(__file__)), ID)) if gauth.credentials is None: print("not Auth Users") elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() gauth.SaveCredentialsFile( path.join(path.dirname(path.abspath(__file__)), ID)) else: # Initialize the saved creds gauth.Authorize() drive = GoogleDrive(gauth) http = drive.auth.Get_Http_Object() if not path.exists(filename): print(f"Specified filename {filename} does not exist!") return # print(filename) if not Creds.TEAMDRIVE_FOLDER_ID: if parent_folder: # Check the files and folers in the root foled file_list = drive.ListFile({ 'q': "'root' in parents and trashed=false" }).GetList() for file_folder in file_list: if file_folder['title'] == parent_folder: # Get the matching folder id folderid = file_folder['id'] # print(folderid) print("Folder Already Exist !! Trying To Upload") # We need to leave this if it's done break else: # Create folder folder_metadata = { 'title': parent_folder, 'mimeType': 'application/vnd.google-apps.folder' } folder = drive.CreateFile(folder_metadata) folder.Upload() folderid = folder['id'] # Get folder info and print to screen foldertitle = folder['title'] # folderid = folder['id'] print('title: %s, id: %s' % (foldertitle, folderid)) file_params = {'title': filename.split('/')[-1]} if Creds.TEAMDRIVE_FOLDER_ID: file_params['parents'] = [{ "kind": "drive#fileLink", "teamDriveId": Creds.TEAMDRIVE_ID, "id": Creds.TEAMDRIVE_FOLDER_ID }] else: if parent_folder: file_params['parents'] = [{ "kind": "drive#fileLink", "id": fofolderlderid }] file_to_upload = drive.CreateFile(file_params) file_to_upload.SetContentFile(filename) try: file_to_upload.Upload(param={"supportsTeamDrives": True, "http": http}) except Exception as e: print("upload", e) if not Creds.TEAMDRIVE_FOLDER_ID: file_to_upload.FetchMetadata() file_to_upload.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True }) return file_to_upload['webContentLink']
def send_csv_to_drive(fileName, fileAlias, target_dir="slideInfo_BioBasic"): logger.log('begin file upload') client_secrets_path = BASE_DIR + "/client_secrets.json" credentials_path = BASE_DIR + "/credentials.txt" GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_secrets_path # Create google account authentication objects gauth = GoogleAuth() logger.log('Looking for credentials') if os.path.exists(credentials_path): logger.log('found a credentials') gauth.LoadCredentialsFile(credentials_path) if gauth.credentials is None: # or gauth.access_token_expired: logger.log('local connect to website') gauth.LocalWebserverAuth() gauth.SaveCredentialsFile(credentials_path) elif gauth.access_token_expired: logger.log('refresh branch') gauth.Refresh() else: logger.log('authorize branch') gauth.Authorize() logger.log('creating connection to google drive') gauth.SaveCredentialsFile(credentials_path) drive = GoogleDrive(gauth) logger.log('connection established') ''' Find the name of the folder we want to upload to ''' # Define the folder we want to upload to target_folder_name = target_dir target_folder_id = '' logger.log('finding drive folder: ' + target_dir) folder_not_found = True while (folder_not_found): # Find the list of all of the files in the google drive file_list = drive.ListFile({ 'q': "'root' in parents and trashed=false" }).GetList() # Loop through all of the files in the for file_object in file_list: # Check if the current one is our target if file_object['title'] == target_folder_name: # Save the folder id target_folder_id = file_object['id'] # Exit the while loop folder_not_found = False # Check if the folder was found if target_folder_id == '': logger.log('folder not found. Creating one') # Create the folder we want folder = drive.CreateFile({ 'title': target_folder_name, 'mimeType': 'application/vnd.google-apps.folder' }) # Upload the folder to the drive folder.Upload() # The loop will go again, but now it will find the folder logger.log("folder found. id: " + target_folder_id) upload_csv = drive.CreateFile({ 'title': fileAlias + '.csv', 'parents': [{ 'id': target_folder_id }] }) upload_csv.SetContentFile(fileName + '.csv') upload_csv.Upload() logger.log('file uploaded')
def auth_in_google_drive(google_scopes, credentials_filepath): gauth = GoogleAuth() gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name( credentials_filepath, google_scopes) drive = GoogleDrive(gauth) return drive
def auth_drive(): gauth = GoogleAuth() gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) return drive
def __init__(self): # 1. Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() self.drive = GoogleDrive(gauth)
def pydrive_load(args): gauth = GoogleAuth() code = gauth.CommandLineAuth() if code != None: gauth.Auth(code) drive = GoogleDrive(gauth) files = GoogleDriveFile(gauth) # remove temp file for this share id pro_temp = get_project_temp(drive, files, args.driveid) # about = drive.GetAbout() # print(about) # get_root_info(files, DRIVE_ID) root_node = Node('root', data=path_info(id=DRIVE_ID, title='', parent='')) # drive_id = DRIVE_ID drive_id = args.driveid l = [] get_file_list(root_node, l, drive, drive_id) # list path tree if args.showtree: print('path tree is:') for pre, fill, node in RenderTree(root_node): print('{}{}'.format(pre, node.name)) # make dir base_dir = os.path.join(args.downdir, drive_id) mkdir_in_tree(base_dir, root_node) # list file if args.showlist: print('file list is:') current = 0 total = len(l) for i in l: if args.showlist: print( 'id: {}, is_folder: {}, title: {}, desc: {}, ext: {}, size: {}' .format(i.id, i.is_folder, i.title, i.desc, i.ext, i.size)) if len(i.parents) > 0: index = 0 for parent in i.parents: if args.showlist: print(' parents:{}={}, isRoot:{}'.format( index, parent['id'], parent['isRoot'])) index += 1 if args.showlist: print(' parent path={}'.format(i.parent_node.data.path)) retry = 0 if not i.is_folder: while retry < args.retry_count: try: print('# {}/{} begin!'.format(current, total)) try: file_path = i.parent_node.data.path file_title = i.title file_size = i.size file_id = i.id download_file(file_path, args.override, drive, file_id, file_title, file_size) except HttpError as http_error: if http_error.resp.status == 403 and str( http_error.content ).find('The download quota for this file has been exceeded' ) != -1: make_copy_and_download(file_path, drive.auth.service, args.override, drive, file_id, pro_temp, file_title, file_size) print_with_carriage_return('# {}/{} done!'.format( current, total)) break except Exception as e: retry += 1 print('unexpeted error={}, retry={}'.format(e, retry)) current += 1 # remove temp print('job done! remove project temp folder...') get_project_temp(drive, files, args.driveid, False)
def login(): global gauth, drive gauth = GoogleAuth() gauth.LocalWebserverAuth() # Creates local webserver and auto handles authentication drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance
def handle_action_tag(self, ttype, data): logging.debug("Open : %s", data) gauth = GoogleAuth() drive = GoogleDrive(gauth) gauth.LoadCredentialsFile("mycreds.txt") check = os.stat("mod/hashtags").st_size ######---find FOLDER ID #file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() #for file1 in file_list: # print 'title: %s, id: %s' % (file1['title'], file1['id']) ######---authentication if gauth.credentials is None: gauth.LocalWebserverAuth() if gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile("mycreds.txt") if (data == 'DOCS'): ### upload achat and depense file1 = drive.CreateFile({ 'parents': [{ "id": '0B8mDDuHeuNHDfmM0OXlWTndpdkczNHBBY3VJaXJ2ZlNqVVBoWWk3UDZnc0NvMS1Gd1JtWU0' }] }) file1.SetContentFile('mod/achat.txt') file1.Upload() file2 = drive.CreateFile({ 'parents': [{ "id": '0B8mDDuHeuNHDfmM0OXlWTndpdkczNHBBY3VJaXJ2ZlNqVVBoWWk3UDZnc0NvMS1Gd1JtWU0' }] }) file2.SetContentFile('mod/depense.txt') file2.Upload() logging.debug("Upload done.") if (data == 'PICS'): file = open('mod/filepath', 'r') path = file.readlines() pic = path[0] if (check == 0): ### list every photo onlyfiles = [ f for f in listdir('/home/pi/images/') if isfile(join('/home/pi/images/', f)) ] ### upload pic file1 = drive.CreateFile({ 'parents': [{ "id": '0B8mDDuHeuNHDfmM0OXlWTndpdkczNHBBY3VJaXJ2ZlNqVVBoWWk3UDZnc0NvMS1Gd1JtWU0' }] }) file1.SetContentFile(pic) file1.Upload() logging.debug("Upload done.") file = open('mod/filepath', 'w').close() else: file = open('mod/hashtags', 'r') hash = file.readlines() hashtag = hash[0] hashtag = hashtag[1:-1] file_list = drive.ListFile({ 'q': "'root' in parents and trashed=false" }).GetList() for file1 in file_list: print 'title: %s, id: %s' % (file1['title'], file1['id']) print hashtag print file1['title'] if (file1['title'] == hashtag): file1 = drive.CreateFile( {'parents': [{ "id": file1['id'] }]}) file1.SetContentFile(pic) file1.Upload() logging.debug("Upload done.") file = open('mod/filepath', 'w').close() file = open('mod/hashtags', 'w').close()
def startDownloads(songsFolder): gauth = GoogleAuth() # Google Drive authentication gauth.LocalWebserverAuth() # Needed only for initial auth drive = GoogleDrive(gauth) connection = sqlite3.connect('../ChartBase.db') cursor = connection.cursor() cursor.execute('SELECT * FROM links WHERE downloaded=0') links = cursor.fetchall() for link in links: url = link[0] source = link[1] urlDecoded = urllib.parse.unquote(url) domain = re.search(r'.*?://(.*?)/', urlDecoded).group(1) tmpFolder = os.path.join(songsFolder, 'tmp/') if not os.path.exists(tmpFolder): os.mkdir(tmpFolder) if 'drive.google' in domain: try: print(f'downloading from gDrive: {url}') gDriveDownload(drive, urlDecoded, tmpFolder) except (KeyboardInterrupt, SystemExit): if os.path.exists(tmpFolder): print(f'removing tmpFolder due to sysexit: {tmpFolder}') shutil.rmtree(tmpFolder) raise except: cursor.execute( f'UPDATE links SET downloaded=-1 WHERE url="{url}"') connection.commit() if os.path.exists(tmpFolder): print(f'removing tmpFolder due to except: {tmpFolder}') shutil.rmtree(tmpFolder) if os.path.exists(tmpFolder): print(f'importing: {url}') importDownloaded(songsFolder, url, source, connection) print(f'updating in db: {url}') cursor.execute( f'UPDATE links SET downloaded=1 WHERE url="{url}"') connection.commit() else: try: print(f'downloading: {url}') _ = wget.download(urlDecoded, tmpFolder) except (KeyboardInterrupt, SystemExit): if os.path.exists(tmpFolder): print(f'removing tmpFolder due to sysexit: {tmpFolder}') shutil.rmtree(tmpFolder) raise except: cursor.execute( f'UPDATE links SET downloaded=-1 WHERE url="{url}"') connection.commit() if os.path.exists(tmpFolder): print(f'removing tmpFolder due to except: {tmpFolder}') shutil.rmtree(tmpFolder) if os.path.exists(tmpFolder): print(f'importing: {url}') importDownloaded(songsFolder, url, source, connection) print(f'updating in db: {url}') cursor.execute( f'UPDATE links SET downloaded=1 WHERE url="{url}"') connection.commit()
def main(): print("\n--------Exporting {} Applications--------".format(year)) # Import Qualtrics Data for use here- this is only the data, no file uploads print("Importing Data...") regs = ImportRegistrationData() apps = ImportApplicationData() recs = ImportRecommendationData() dems = ImportDemographicData() # Join registration data with application data print("Combining data...") for app in apps: for reg in regs: if reg["Email"] == app["Email"]: app.update(reg) break # Join recommendation data with application data for app in apps: # Save number of recommenders recCount = 0 for rec in recs: # Link recommenders with applications for num in range(1, 5): if app["Rec{}Email".format(num)] is not "": if app["Email".format(num)] == rec["AppEmail"] and app[ "Rec{}Email".format(num)] == rec["Email"]: app["Rec{}ID".format(num)] = rec["recID"] recCount += 1 app["RecCount"] = recCount # Join demographic info with applications for dem in dems: for app in apps: if dem["AppID"] == app["AppID"]: app.update(dem) # Create and/or clean up workspace for files print("Creating folder for applications...") appFolder = "../{}_Applications".format(year) if not os.path.exists(appFolder): # Create workspace (e.g. folder to hold applications) os.makedirs(appFolder) else: # Clean up workspace (e.g. delete all files in folder) for file in os.listdir(appFolder): file_path = os.path.join(appFolder, file) if os.path.isfile(file_path): os.remove(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) # Make section template PDFs templates = [ "Cover Page", "Statement of Interest", "CV or Resume", "(Unofficial) Transcript", "Recommendation Letter #1", "Recommendation Letter #2", "Recommendation Letter #3", "Recommendation Letter #4" ] for template in templates: MakeSectionPdf(template) # Make application PDFs print("\n--------Making PDFs--------") appCount = 1 for app in apps: print("Starting Application {} of {}...".format(appCount, len(apps))) #Create dictionary to hold PDF pages docs = collections.OrderedDict() # Make SOI first (basic info last, to check if all parts submitted) MakeSoiPdf(app) soi = GetPdf("{}_SOI.pdf".format(app["AppID"])) docs["Statement of Interest"] = soi # Get CV cvExists = False cv = GetPdf("../Summer_Course_{}_Application/Q12/{}*.pdf".format( year, app["AppID"])) if cv: docs["CV or Resume"] = cv cvExists = True # Get transcript transcriptExists = False transcript = GetPdf( "../Summer_Course_{}_Application/Q11/{}*.pdf".format( year, app["AppID"])) if transcript: docs["(Unofficial) Transcript"] = transcript transcriptExists = True # Get recommendation letters and add it to WIP PDF letterExists = [None] for num in range(1, 5): letterExists.append(False) if "Rec{}ID".format(num) in app.keys(): letter = GetPdf("../Q1/{}*.pdf".format( app["Rec{}ID".format(num)])) if letter: docs["Recommendation Letter #" + str(num)] = letter letterExists[num] = True # Dictionary of Existence fileExists = { "CV": cvExists, "Transcript": transcriptExists, "Letters": letterExists } # Make Cover Page completed = MakeCoverPage(app, fileExists) # Get Cover Page cover = GetPdf("{}_cover.pdf".format(app["AppID"])) # Add pages to PDF (with header and watermark, if appropriate) appPdf = PdfFileWriter() pages = AddHeader(cover.pages, app) pages = AddSection(pages, "Cover Page") if not completed: pages = AddWatermark(pages) for page in pages: appPdf.addPage(page) for section, doc in docs.items(): pages = AddHeader(doc.pages, app) pages = AddSection(pages, section) if not completed: pages = AddWatermark(pages) for page in pages: appPdf.addPage(page) # Write PDF appStream = open( "../{}_Applications/{}_{}.pdf".format(year, app["Last"], app["AppID"]), "wb") appPdf.write(appStream) # Increase count for display appCount += 1 print("\n--------Post-Processing PDFs--------") # Delete temporary files print("Deleting Temporary Files...") filesToDelete = ["SOI", "cover", "WIP", "Header"] for ext in filesToDelete: for file in glob("*_{}.pdf".format(ext)): os.remove(file) os.remove("Cover Page.pdf") os.remove("Statement of Interest.pdf") os.remove("CV or Resume.pdf") os.remove("(Unofficial) Transcript.pdf") os.remove("Recommendation Letter #1.pdf") os.remove("Recommendation Letter #2.pdf") os.remove("Recommendation Letter #3.pdf") os.remove("Recommendation Letter #4.pdf") # Create applicant CSV file print("Creating Applicant CSV File...") with open("../{}_Applications/{} Applicants.csv".format(year, year), "w") as appCsv: csvHeader = [ "AppID", "First", "Last", "Email", "Gender", "Hispanic", "Race", "Education" ] writer = csv.DictWriter(appCsv, fieldnames=csvHeader, restval="ERROR", extrasaction="ignore") writer.writeheader() for app in apps: writer.writerow(app) print("\n--------Uploading files to Google Drive--------") # Authenticate Google Drive gauth = GoogleAuth() # Create local webserver and auto-handle authentication. gauth.LocalWebserverAuth() # Create GoogleDrive instance with authenticated GoogleAuth instance. drive = GoogleDrive(gauth) # Delete all old application files file_list = drive.ListFile({ 'q': "'{}' in parents and trashed=false".format(GDriveDestID) }).GetList() for file in file_list: if ("R_" in file["title"] and ".pdf" in file["title"]) or ".csv" in file["title"]: file.Delete() # Upload files to Google Drive appCount = 1 print("\n\n--------Starting Drive Upload--------") for app in apps: print("Uploading {} of {}...".format(appCount, len(apps))) file = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": "{}".format(GDriveDestID) }], "title": "{}: {}.pdf".format(app["Last"], app["AppID"]) }) # Read file and set it as a content of this instance. file.SetContentFile("../{}_Applications/{}_{}.pdf".format( year, app["Last"], app["AppID"])) file.Upload() # Upload the file. appCount += 1 file = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": "{}".format(GDriveDestID) }], "title": "{} Applicants.csv".format(year) }) file.SetContentFile("../{}_Applications/{} Applicants.csv".format( year, year)) file.Upload() print("\n--------Distributing Applications--------") print("Getting application links...") app_list = drive.ListFile({ 'q': "'{}' in parents and trashed=false".format(GDriveDestID) }).GetList() appPdfs = [] for file in app_list: if "R_" in file["title"] and ".pdf" in file["title"]: appPdfs.append(file) print("Apportioning applications...") # Divide up applications numApps = len(appPdfs) numReviewers = len(appReviewers) reviewBurden = (numApps * 2) // numReviewers # Generate list to sample from appsToReview = appPdfs + appPdfs random.shuffle(appsToReview) # Make list of reviewers reviewers = [] for reviewer, email in appReviewers.items(): reviewers.append({"Name": reviewer, "Email": email, "Apps": []}) # Distribute applications while appsToReview: for reviewer in reviewers: if appsToReview: selection = random.choice(appsToReview) if selection in reviewer["Apps"]: selection = random.choice(appsToReview) else: reviewer["Apps"].append(selection) appsToReview.remove(selection) print("Emailing applications...") #Create string of application links for reviewer in reviewers: reviewer["AppLinks"] = "" reviewer["HtmlLinks"] = "" for app in reviewer["Apps"]: reviewer["AppLinks"] += "{}: {}\n\t\t\t".format( app["title"].replace(".pdf", ""), app["webContentLink"]) reviewer["HtmlLinks"] += '<p><a href="{}">{}</a></p>'.format( app["webContentLink"], app["title"].replace(".pdf", "")) # Create the base text message. for reviewer in reviewers: msg = EmailMessage() msg['Subject'] = "{} NCAN Summer Course Application Evaluations".format( year) msg['From'] = Address("William Schmitt", addr_spec="*****@*****.**") msg['To'] = Address(reviewer["Name"], addr_spec=reviewer["Email"]) msg.set_content("""\ Dear {}, The application window for the {} NCAN Summer Course is now closed! As such, it is now time for you to begin reviewing applictions to determine who should be admitted to the Course. There were {} applications this year, so we need you to review {} applications. In an attempt to streamline this process, we have created an evaluation form that you can quickly and easily fill out for each application. This form is located here (https://goo.gl/forms/AHxAvtZDglX54DWd2). NOTE: this form and all links below require the use of your @neurotechcenter.org account. Please make sure you are logged into your account (if you are not, you will be prompted to do so when you click on the link). The applications you have been assigned are: (listed as lastName: Applicant ID) {} The links above should automatically download each application to your computer for easy viewing, but in case they do not work, you should be able to access all applications here (https://drive.google.com/drive/folders/0B67b4FFl6pYlVnY2cVpFbjlGdmM?usp=sharing). Thank you for the anticipated time and attention you will spend reviewing these applications. If you have any questions about the process, please feel free to contact Billy or Dr. Carp. Thank you, The NCAN Summer Course Bot""".format(reviewer["Name"], year, numApps, reviewBurden, reviewer["AppLinks"])) # Add the html version. This converts the message into a multipart/alternative # container, with the original text message as the first part and the new html # message as the second part. msg.add_alternative("""\ <html> <head></head> <body> <p>Dear {},</p> <p> The application window for the {} NCAN Summer Course is now closed! As such, it is now time for you to begin reviewing applictions to determine who should be admitted to the Course. There were {} applications this year, so we need you to review {} applications. In an attempt to streamline this process, we have created an evaluation form that you can quickly and easily fill out for each application. This form is located <a href="https://goo.gl/forms/PftRKWtL6SnG1Ozp1">here</a>. NOTE: this form and all links below require the use of your @neurotechcenter.org account. Please make sure you are logged into your account (if you are not, you will be prompted to do so when you click on the link). </p> <p>The applications you have been assigned are:</p> (listed as lastName: Applicant ID) {} <p> The links above should automatically download each application to your computer for easy viewing, but in case they do not work, you should be able to access all applications <a href="https://drive.google.com/drive/folders/0B67b4FFl6pYlVnY2cVpFbjlGdmM?usp=sharing">here</a>. </p> <p> Thank you for the anticipated time and attention you will spend reviewing these applications. If you have any questions about the process, please feel free to contact Billy or Dr. Carp.</p> <p>Thank you,</p> <p>The NCAN Summer Course Bot</p> </body> </html> """.format(reviewer["Name"], year, numApps, reviewBurden, reviewer["HtmlLinks"]), subtype='html') # Send the message via local SMTP server. with smtplib.SMTP('smtp.gmail.com', 587) as server: #port 465 or 587 server.ehlo() server.starttls() server.ehlo() server.login('*****@*****.**', password) server.send_message(msg) server.close() print("\n--------Success! All Done.--------")
PORT = 8080 DEBUG_PORT = 8001 WLAB = 'Winkler Lab' RDATA = 'ReactorData' OLD = datetime.datetime(year=1900, month=1, day=1, hour=0, minute=0, second=0, microsecond=0) """ Authenticate the connection to google drive Requires correct client_secrets, credentials, and settings files. """ gauth = GoogleAuth(os.getcwd() + "/settings.yaml") gauth.LoadCredentialsFile("mycreds.txt") if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile("mycreds.txt") drive = GoogleDrive(gauth)
def googledrive_login(): gauth = GoogleAuth() gauth.LocalWebserverAuth() return GoogleDrive(gauth)
def upload_files(images, csv_name, target_drive_dir='slideInfo_BioBasic'): logger.log('begin file upload') client_secrets_path = basest_dir + "/client_secrets.json" credentials_path = basest_dir + "/credentials.txt" GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = client_secrets_path # Create google account authentication objects gauth = GoogleAuth() logger.log('Looking for credentials') if os.path.exists(credentials_path): logger.log('found a credentials') gauth.LoadCredentialsFile(credentials_path) if gauth.credentials is None: logger.log('local connect to website') gauth.LocalWebserverAuth() elif gauth.access_token_expired: logger.log('refresh branch') gauth.Refresh() else: logger.log('authorize branch') gauth.Authorize() logger.log('creating connection to google drive') gauth.SaveCredentialsFile(credentials_path) drive = GoogleDrive(gauth) logger.log('connection established') # Upload the template files to the user if target_drive_dir == 'slideInfo_BioBasic': upload_template = drive.CreateFile({'title': 'TEMPLATE_bio_bas'}) upload_template.SetContentFile(basest_dir + '/TEMPLATE_bio_bas.pptx') upload_template.Upload() elif target_drive_dir == 'slideInfo_BioAdv': upload_template = drive.CreateFile({'title': 'TEMPLATE_bio_adv'}) upload_template.SetContentFile(basest_dir + '/TEMPLATE_bio_adv.pptx') upload_template.Upload() ''' Find the name of the folder we want to upload to ''' # Define the folder we want to upload to target_folder_name = target_drive_dir target_folder_id = '' # Find the list of all of the files in the google drive file_list = drive.ListFile({ 'q': "'root' in parents and trashed=false" }).GetList() # Loop through all of the files in the for file_object in file_list: # Check if the current one is our target if file_object['title'] == target_folder_name: # Save the folder id target_folder_id = file_object['id'] logger.log("folder id: " + target_folder_id) # upload the CSV containing only the info on the chosen animals for images upload_csv = drive.CreateFile({ 'title': csv_name, 'parents': [{ 'id': target_folder_id }] }) upload_csv.SetContentFile(basest_dir + "/" + csv_name) upload_csv.Upload() logger.log("uploaded chosen_mammals csv") # Loop through the images for image_name in images: upload_image = drive.CreateFile({ 'title': image_name, 'parents': [{ 'id': target_folder_id }] }) #upload_image.SetContentFile( "python_scripts/biodiversity/animal_images/" + image_name ) logger.log(image_name) if __name__ == "__main__": upload_image.SetContentFile("animal_images/" + image_name + ".jpg") else: upload_image.SetContentFile(basest_dir + '/animal_images/' + image_name + ".jpg") upload_image.Upload()
def load_data_path(folder_id, colab_path='/root/data/', local_path='../data/', mime_types=['csv', 'zip']): """Boilerplate to download data from Google Drive into Colab notebook or to point to local data folder Behavior: --------- 1. Identify if Notebook is running in Colab 2. If Yes, then a. do Google OAuth login (requires user interaction) b. create a data folder in Colab (colab_path) c. Search for all CSV files in Google Drive folder d. Copy all CSV files from G Drive into colab_path folder e. Return the colab_path variable 3. If No, then a. Return the local_path variable Example 1: ---------- !pip install colabtweak from colabtweak import load_data_path folder_id = "kasdhkfhjkashfjadskjfjsalk" data_path = load_data_path(folder_id) import pandas as pd df = pd.read_csv(data_path + "train.csv") df.head() Example 2: ---------- !pip install colabtweak from colabtweak import load_data_path folder_id = "kasdhkfhjkashfjadskjfjsalk" colab_path = "/root/somecustomfolderincolab/" local_path = "../localsiblingprojectfolder/ data_path = load_data_path( folder_id, colab_path=colab_path, local_path=local_path) """ if 'google.colab' in sys.modules: print("Notebook is running in Colab") if folder_id is None: print(( "Folder ID is missing.\n" "Click on the Google Drive folder and check your URL\n" "'https://drive.google.com/drive/u/0/folders/<folder_id>'")) # Login from google.colab import auth auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) # create "~/data" folder within the Colab image download_path = os.path.expanduser(colab_path) try: os.makedirs(download_path) except FileExistsError: pass # Extract the FileIDs from the Google Drive directory tmp = ' or '.join(["title contains '." + m + "'" for m in mime_types]) querystr = "(" + tmp + ") and '" + folder_id + "' in parents" listed = drive.ListFile({'q': querystr}).GetList() # Copy all files for file in listed: try: print('{} {}'.format(file['id'], file['title'])) output_file = os.path.join(download_path, file['title']) temp_file = drive.CreateFile({'id': file['id']}) temp_file.GetContentFile(output_file) except Exception as e: print(e) # Set directory path return colab_path else: print("Notebook is running in Jupyter") return local_path
def get_from_drive(idv): gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth) last_weight_file = drive.CreateFile({'id': idv}) last_weight_file.GetContentFile('DenseNet-40-12-CIFAR10.h5')
def drive(self): from pydrive.auth import RefreshError if not hasattr(self, "_gdrive"): from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive if os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA): with open( self.gdrive_user_credentials_path, "w" ) as credentials_file: credentials_file.write( os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA) ) GoogleAuth.DEFAULT_SETTINGS["client_config_backend"] = "settings" GoogleAuth.DEFAULT_SETTINGS["client_config"] = { "client_id": self.client_id, "client_secret": self.client_secret, "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "revoke_uri": "https://oauth2.googleapis.com/revoke", "redirect_uri": "", } GoogleAuth.DEFAULT_SETTINGS["save_credentials"] = True GoogleAuth.DEFAULT_SETTINGS["save_credentials_backend"] = "file" GoogleAuth.DEFAULT_SETTINGS[ "save_credentials_file" ] = self.gdrive_user_credentials_path GoogleAuth.DEFAULT_SETTINGS["get_refresh_token"] = True GoogleAuth.DEFAULT_SETTINGS["oauth_scope"] = [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.appdata", ] # Pass non existent settings path to force DEFAULT_SETTINGS loading gauth = GoogleAuth(settings_file="") try: gauth.CommandLineAuth() except RefreshError as exc: raise GDriveAccessTokenRefreshError( "Google Drive's access token refreshment is failed" ) from exc except KeyError as exc: raise GDriveMissedCredentialKeyError( "Google Drive's user credentials file '{}' " "misses value for key '{}'".format( self.gdrive_user_credentials_path, str(exc) ) ) # Handle pydrive.auth.AuthenticationError and others auth failures except Exception as exc: raise DvcException( "Google Drive authentication failed" ) from exc finally: if os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA): os.remove(self.gdrive_user_credentials_path) self._gdrive = GoogleDrive(gauth) self.remote_root_id = self.get_remote_id( self.path_info, create=True ) self._cached_dirs, self._cached_ids = self.cache_root_dirs() return self._gdrive
""" =========================== """ """ LOAD ALREADY PROCESSED DATA """ """ =========================== """ try: alreadyProcessedZipfiles = pd.read_csv( 'in/tables/alreadyProcessedZipFiles.csv') except: alreadyProcessedZipfiles = pd.DataFrame({'name': []}) """ =========================== """ """ KEBOOLA STUFF """ """ =========================== """ cfg = docker.Config() parameters = cfg.get_parameters() folderNames = parameters.get('folderNames') gauth = GoogleAuth(settings_file='/data/in/files/253425786_settings.yaml') drive = GoogleDrive(gauth) """ =========================== """ """ FILL THE DATAFRAME """ """ =========================== """ finalDataFrame = None # LEGACY: for storing transaction data finalFirmTotalsDataFrame = None # NEW: for storing firm totals (unfortunate naming, technical debt for now) if folderNames: FOLDERS_TO_LOOKAT = list(folderNames) else: FOLDERS_TO_LOOKAT = ['CSOB AM 2016', 'CSOB AM 2017'] for folderToLookAt in FOLDERS_TO_LOOKAT: """ SCAN THE `GDrive` FOLDERS FOR ZIPFILES """
def auth(): gauth = GoogleAuth() gauth.LocalWebserverAuth() return GoogleDrive(gauth)
def clickMethod(self): Sub1 = (self.Subject_line.text()) file1 = open("C:/Users/shadd/Documents/Subjects.txt", "w+") file1.writelines(Sub1) file1.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Subjects.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Sub.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Sub.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Sub.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Cre1 = (self.Created_Line.text()) file2 = open("C:/Users/shadd/Documents/Created.txt", "w+") file2.writelines(Cre1) file2.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Created.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Cre.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Cre.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Cre.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Due1 = (self.DueDate_Line.text()) file3 = open("C:/Users/shadd/Documents/Due.txt", "w+") file3.writelines(Due1) file3.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Due.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Due.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Due.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Due.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Ass1 = (self.Assignment_textbox.toPlainText()) file4 = open("C:/Users/shadd/Documents/Ass.txt", "w+") file4.writelines(Ass1) file4.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Ass.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Ass.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Ass.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Ass.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Hom1 = (self.Homework_textbox.toPlainText()) file5 = open("C:/Users/shadd/Documents/Hom.txt", "w+") file5.writelines(Hom1) file5.close() file4.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Hom.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Hom.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Hom.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Hom.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Not1 = (self.Notes_Textbox.toPlainText()) file6 = open("C:/Users/shadd/Documents/Notes.txt", "w+") file6.writelines(Not1) file6.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Notes.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Notes.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Hom.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Notes.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload() Notice1 = (self.Notice_textbox.toPlainText()) file7 = open("C:/Users/shadd/Documents/Notice.txt", "w+") file7.writelines(Notice1) file7.close() g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) with open("C:/Users/shadd/Documents/Notice.txt", "r") as fie: file_drive = drive.CreateFile({'title': 'Notice.docx'}) file_drive.SetContentString(fie.read()) file_drive.Upload() file_list = drive.ListFile({ 'q': "title contains 'Notice.docx' and trashed=false" }).GetList() print(file_list[0] ['title']) # should be the title of the file we just created file_id = file_list[0]['id'] # get the file ID folder = drive.ListFile({ 'q': "title = 'something' and trashed=false" }).GetList()[0] # get the folder we just created file = drive.CreateFile({ 'title': "Notice.txt", 'parents': [{ 'id': folder['id'] }] }) file.Upload()
def __init__(self): """Create an instance of UploadDrive.""" self.gauth = GoogleAuth() self.drive = GoogleDrive(self.gauth)
def __init__(self): self.drive_auth_settings = constants.DRIVE_AUTH_SETTINGS self.drive_credentials_file = constants.DRIVE_CREDS_FILE self.auth = GoogleAuth(settings_file=self.drive_auth_settings)
from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth('settings/Drive.yaml') #gauth.LocalWebserverAuth() gauth.LoadCredentialsFile("credentials.txt") if gauth.access_token_expired: gauth.Refresh() else: gauth.Authorize() gauth.SaveCredentialsFile("credentials.txt") drive = GoogleDrive(gauth) def Upload(index,file,folder,id): if folder == 'México': folder_id= "1XnIjkzbYKxHNuiDxfuyCTZbcVfAqVvW_" if folder == 'Argentina': folder_id= "1mVO0brmw16xr6LAamazRa8xaWUSgSEfW" if folder == 'Colombia': folder_id= "1H6FJnbKlFiijRSo5ZSsUdWmUSiujLw8n" if folder == 'Otro': #folder_id= "1x_lhZed8s7jQi7uj-H8J79cFmFckdvCy" folder_id= "1jeJF1AS_F8ZLXrLPta7Zkj2GAycl1DOk" Audio = drive.CreateFile({'parents': [{'id': folder_id}]}) Audio.SetContentFile(file) #Set the name Audio['title'] = "{}-{}.wav".format(id,index)
from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import os from tqdm import tqdm g_login = GoogleAuth() g_login.LocalWebserverAuth() drive = GoogleDrive(g_login) parent_folder_name = 'html_storage' print('loading all files directory...') files = sorted(list(os.listdir(parent_folder_name))) print('done') print('start uploading files') for i in tqdm(range(len(files))): filename = files[i] if filename[-4:] == 'html': cnt = 0 while True: try: with open(os.path.join(parent_folder_name, filename), "r") as f: file_drive = drive.CreateFile({ 'title': os.path.basename(f.name), 'parents': [{ 'id': '<your-drive-folder-id>' }] }) file_drive.SetContentString(f.read()) file_drive.Upload()
def create_action_map(request): # Add action to database first app_name = request.data.get('app') sass_action = request.data.get('sass_action') sheet_action = request.data.get('sheet_action') sheet_id = request.data.get('sheet') worksheet_index = request.data.get('worksheet') try: sass_action_obj = SasSActions.objects.get(app=app_name, action=sass_action) except ObjectDoesNotExist: return Response(status=400, data={'message': 'Sass action not found'}) try: sheet_action_obj = SheetActions.objects.get(action=sheet_action) except ObjectDoesNotExist: return Response(status=200, data={'message': 'Sheet action not found'}) current_user = User.objects.get(username='******') sass_sheet_map = SasSSheetMap(app=app_name, sass_actions=sass_action_obj, sheet_actions=sheet_action_obj, sheet_id=sheet_id, worksheet_id=worksheet_index, user=current_user) # Save api columns also sass_sheet_map.save() sass_sheet_map_fetch = SasSSheetMap.objects.filter( app=app_name, sass_actions=sass_action_obj).first() for column in request.data.get('columns'): try: api_field = ApiField.objects.get(app=app_name, api_column=column) except ObjectDoesNotExist: return Response(status=400, data={'message': 'API column not found'}) sass_sheet_map_fetch.api_columns.add(api_field) sass_sheet_map_fetch.save() # create SasS webhook if app_name == 'Jira': user = User.objects.get(username='******') # request.user event = [] if sass_action == 'Create new issue': event.append("jira:issue_created") event.append("jira:issue_updated") elif sass_action == 'Create new comment': event.append("comment_created") event.append("comment_updated") elif sass_action == 'Create new project': event.append("project_created") response = create_jira_webhook(user, event) if response.status_code == 400: return response # create google webhook try: user_creds = AccessToken.objects.get(user=user) if user_creds.token == '': raise NoTokenFoundError except ObjectDoesNotExist: return Response( status=400, data={'message': 'User does not have SasS account setup'}) except NoTokenFoundError: return Response( status=400, data={'message': 'User does not have google account setup'}) token = user_creds.token create_channel(token, sheet_id) header_list = [] if sass_action == 'Create new issue': header_list = add_header_to_spreadsheet_new_issue(request.data) elif sass_action == 'Create new comment': header_list = add_header_to_spreadsheet_new_comment(request.data) elif sass_action == 'Create new project': header_list = add_header_to_spreadsheet_new_project(request.data) gauth = GoogleAuth() gauth.credentials = client.Credentials.new_from_json(token) sheet_client = gspread.authorize(gauth.credentials) sheet = sheet_client.open_by_key(sheet_id) worksheet = sheet.worksheet(worksheet_index) worksheet.append_row(header_list) return Response(status=200, data={'message': 'All done'})
from pydrive.auth import GoogleAuth gauth = GoogleAuth() print ("Authenticating, a web browser may be opened") if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() print("Authenication complete, you should now have a 'credentials.json' file") print("Check the connection is correct by checking that there is a 'wildpi' folder in your google drive") from pydrive.drive import GoogleDrive drive = GoogleDrive(gauth) # Auto-iterate through all files that matches this query file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList() for file1 in file_list: if file1['title'] == 'wildpi': print('title: %s, id: %s' % (file1['title'], file1['id']))
# Variable Declaration error_found = False counter = 0 occurance = 0 current_workdir = os.getcwd() # Getting Current working directory Information xmlFile = current_workdir + '/' + 'apple_health_export' + '/' + 'export.xml' beatFile = current_workdir + '/' + 'heartbeat.csv hBFile = current_workdir + '/' + 'heartbeat_List.csv' print("+++ Message : Working Directory = " + current_workdir) # Connecting to Google Drive gLogin = GoogleAuth() gLogin.LocalWebserverAuth() # This opens a google login page and select account to access program drive = GoogleDrive(gLogin) heartBeat = '1aoURMlMlb0DYPeI2vcM7nGKI44uMnu_I' # Shared Folder ID - Folder Name "HearBeat" data_files = drive.ListFile({'q':"'"+heartBeat+"' in parents and trashed=false"}).GetList() for file1 in data_files: print('+++ Message : Downloading File = %s' % (file1['title'])) file1.GetContentFile(file1['title']) if not os.path.isfile(current_workdir + '/' + 'export.zip'): print('!!! Error : Unable to locate data folder - export.zip ') error_found = True else: # Extract zip folder