async def AskUserToVisitLinkAndGiveCode(message, code): creds = None global flow if flow is None: await message.edit_text( text=f"run <code>{COMMAND_HAND_LER}gdrive setup</code> first.", parse_mode="html") return await message.edit_text(text="`Checking received code...``") creds = flow.step2_exchange(code) sql.set_credential(message.reply_to_message.from_user.id, creds) # await message.edit_text(text="<b>Saved gDrive credentials</b>") flow = None
async def AskUserToVisitLinkAndGiveCode(message, code): creds = None global flow if flow is None: await message.edit_text( text=f"run <code>{COMMAND_HAND_LER}gdrive setup</code> first.", parse_mode="html" ) return await message.edit_text(text="checking received code ...") creds = flow.step2_exchange(code) # # Save the credentials for the next run sql.set_credential( message.reply_to_message.from_user.id, creds ) # await message.edit_text( text="saved gDrive authentication credentials" ) # clear the global variable once the authentication FLOW is finished flow = None
async def g_drive_setup(message): creds = sql.get_credential(message.from_user.id) if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) sql.set_credential(message.from_user.id, creds) # await message.edit_text( text="gDrive authentication credentials, refreshed") else: global flow flow = OAuth2WebServerFlow(G_DRIVE_CLIENT_ID, G_DRIVE_CLIENT_SECRET, OAUTH_SCOPE, redirect_uri=REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() reply_string = f"please visit {authorize_url} and " reply_string += "send back " reply_string += f"<code>{COMMAND_HAND_LER}gdrive confirm (RECEIVED_CODE)</code>" await message.edit_text(text=reply_string, disable_web_page_preview=True) else: await message.edit_text(text="`Setup Done Already!`")
async def g_drive_setup(message): # The gDrive table stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. creds = sql.get_credential(message.from_user.id) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) # Save the credentials for the next run sql.set_credential(message.from_user.id, creds) # await message.edit_text( text="gDrive authentication credentials, refreshed" ) else: global flow # Run through the OAuth flow and retrieve credentials flow = OAuth2WebServerFlow( G_DRIVE_CLIENT_ID, G_DRIVE_CLIENT_SECRET, OAUTH_SCOPE, redirect_uri=REDIRECT_URI ) authorize_url = flow.step1_get_authorize_url() reply_string = f"please visit {authorize_url} and " reply_string += "send back " reply_string += f"<code>{COMMAND_HAND_LER}gdrive confirm (RECEIVED_CODE)</code>" await message.edit_text( text=reply_string, disable_web_page_preview=True ) else: await message.edit_text( text="don't type this command -_-" )
async def g_drive_commands(client, message): status_message = await message.reply_text("...") if len(message.command) > 1: current_recvd_command = message.command[1] if current_recvd_command == "setup": await g_drive_setup(message) elif current_recvd_command == "reset": sql.clear_credential(message.from_user.id) await status_message.edit_text(text="cleared saved credentials") elif current_recvd_command == "confirm": if len(message.command) == 3: await AskUserToVisitLinkAndGiveCode(status_message, message.command[2]) else: await status_message.edit_text(text="please give auth_code correctly") elif current_recvd_command == "search": # The gDrive table stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. creds = sql.get_credential(message.from_user.id) # If there are no (valid) credentials available, throw error if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) # Save the credentials for the next run sql.set_credential(message.from_user.id, creds) # if len(message.command) > 2: search_query = " ".join(message.command[2:]) message_string = "<b>gDrive <i>Search Query</i></b>:" message_string += f"<code>{search_query}</code>\n\n" message_string += "<i>Results</i>:\n" message_string += await search_g_drive( creds, search_query ) await status_message.edit_text( text=message_string, disable_web_page_preview=True ) else: await status_message.edit_text( "syntax:\n" f"<code>{COMMAND_HAND_LER}gdrive search (QUERY)</code> " ) else: await status_message.edit_text( "invalid credentials, supplied??!\n" f"use <code>{COMMAND_HAND_LER}gdrive reset</code> " "to clear saved credentials", parse_mode="html" ) return else: await status_message.edit_text( text=f"please run <code>{COMMAND_HAND_LER}gdrive setup</code> first", parse_mode="html" ) elif current_recvd_command == "upload": # The gDrive table stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. creds = sql.get_credential(message.from_user.id) # If there are no (valid) credentials available, throw error if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) # Save the credentials for the next run sql.set_credential(message.from_user.id, creds) # if len(message.command) > 2: upload_file_name = " ".join(message.command[2:]) if not os.path.exists(upload_file_name): await status_message.edit_text("invalid file path provided?") return gDrive_file_id = await gDrive_upload_file( creds, upload_file_name, status_message ) reply_message_text = "" if gDrive_file_id is not None: reply_message_text += "Uploaded to <a href='" reply_message_text += "https://drive.google.com/open?id=" reply_message_text += gDrive_file_id reply_message_text += "'>" + gDrive_file_id + "</a>" else: reply_message_text += "failed to upload.. check logs?" await status_message.edit_text( text=reply_message_text, disable_web_page_preview=True ) elif message.reply_to_message is not None: if not os.path.isdir(TMP_DOWNLOAD_DIRECTORY): os.makedirs(TMP_DOWNLOAD_DIRECTORY) download_location = TMP_DOWNLOAD_DIRECTORY + "/" c_time = time.time() the_real_download_location = await client.download_media( message=message.reply_to_message, file_name=download_location, progress=progress_for_pyrogram, progress_args=( "trying to download", status_message, c_time ) ) await status_message.edit(f"Downloaded to <code>{the_real_download_location}</code>") if not os.path.exists(the_real_download_location): await message.edit_text("invalid file path provided?") return gDrive_file_id = await gDrive_upload_file( creds, the_real_download_location, status_message ) reply_message_text = "" if gDrive_file_id is not None: reply_message_text += "Uploaded to <a href='" reply_message_text += "https://drive.google.com/open?id=" reply_message_text += gDrive_file_id reply_message_text += "'>" + gDrive_file_id + "</a>" else: reply_message_text += "failed to upload.. check logs?" os.remove(the_real_download_location) await status_message.edit_text( text=reply_message_text, disable_web_page_preview=True ) else: await status_message.edit_text( "syntax:\n" f"<code>{COMMAND_HAND_LER}gdrive upload (file name)</code> " ) else: await status_message.edit_text( "invalid credentials, supplied??!\n" f"use <code>{COMMAND_HAND_LER}gdrive reset</code> " "to clear saved credentials" ) return else: await status_message.edit_text( text=f"please run <code>{COMMAND_HAND_LER}gdrive setup</code> first" ) else: await status_message.edit_text(text="type correctly")
async def g_drive_commands(client, message): status_message = await message.reply_text("...") if len(message.command) > 1: current_recvd_command = message.command[1] if current_recvd_command == "setup": await g_drive_setup(message) elif current_recvd_command == "reset": sql.clear_credential(message.from_user.id) await status_message.edit_text(text="cleared saved credentials") elif current_recvd_command == "confirm": if len(message.command) == 3: await AskUserToVisitLinkAndGiveCode(status_message, message.command[2]) else: await status_message.edit_text( text="please give auth_code correctly") elif current_recvd_command == "search": creds = sql.get_credential(message.from_user.id) if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) sql.set_credential(message.from_user.id, creds) if len(message.command) > 2: search_query = " ".join(message.command[2:]) message_string = "<b>gDrive <i>Search Query</i></b>:" message_string += f"<code>{search_query}</code>\n\n" message_string += "<i>Results</i>:\n" message_string += await search_g_drive( creds, search_query) await status_message.edit_text( text=message_string, disable_web_page_preview=True) else: await status_message.edit_text( "<b>Syntax:</b>\n" f"<code>{COMMAND_HAND_LER}gdrive search (QUERY)</code> " ) else: await status_message.edit_text( "<b>Invalid credentials!</b>\n" f"<i>Use</i> <code>{COMMAND_HAND_LER}gdrive reset</code> <i>to clear saved credentials.</i>", parse_mode="html") return else: await status_message.edit_text( text= f"<i>Please run</i> <code>{COMMAND_HAND_LER}gdrive setup</code> <i>first</i>", parse_mode="html") elif current_recvd_command == "upload": creds = sql.get_credential(message.from_user.id) if not creds or not creds.invalid: if creds and creds.refresh_token: creds.refresh(get_new_http_instance()) sql.set_credential(message.from_user.id, creds) if len(message.command) > 2: upload_file_name = " ".join(message.command[2:]) if not os.path.exists(upload_file_name): await status_message.edit_text( "invalid file path provided?") return gDrive_file_id = await gDrive_upload_file( creds, upload_file_name, status_message) reply_message_text = "" if gDrive_file_id is not None: reply_message_text += "Uploaded to <a href='" reply_message_text += "https://drive.google.com/open?id=" reply_message_text += gDrive_file_id reply_message_text += "'>" + gDrive_file_id + "</a>" else: reply_message_text += "failed to upload.. check logs?" await status_message.edit_text( text=reply_message_text, disable_web_page_preview=True) elif message.reply_to_message is not None: if not os.path.isdir(TMP_DOWNLOAD_DIRECTORY): os.makedirs(TMP_DOWNLOAD_DIRECTORY) download_location = TMP_DOWNLOAD_DIRECTORY + "/" c_time = time.time() the_real_download_location = await client.download_media( message=message.reply_to_message, file_name=download_location, progress=progress_for_pyrogram, progress_args=( "`Trying to download to Local Storage...`", status_message, c_time)) await status_message.edit( f"<b>Downloaded to</b> <code>{the_real_download_location}</code>" ) if not os.path.exists(the_real_download_location): await message.edit_text( "invalid file path provided?") return gDrive_file_id = await gDrive_upload_file( creds, the_real_download_location, status_message) reply_message_text = "" if gDrive_file_id is not None: reply_message_text += "Uploaded to <a href='" reply_message_text += "https://drive.google.com/open?id=" reply_message_text += gDrive_file_id reply_message_text += "'>" + gDrive_file_id + "</a>" else: reply_message_text += "<b><i>Failed to upload...</b><i>\n<i>Please check Logs</i>" os.remove(the_real_download_location) await status_message.edit_text( text=reply_message_text, disable_web_page_preview=True) else: await status_message.edit_text( "<b>Syntax:</b>\n" f"<code>{COMMAND_HAND_LER}gdrive upload (file name)</code>" ) else: await status_message.edit_text( "<b>Invalid credentials!</b>\n" f"<i>Use</i> <code>{COMMAND_HAND_LER}gdrive reset</code> <i>to clear saved credentials</i>" ) return else: await status_message.edit_text( text= f"<i>Please run</i> <code>{COMMAND_HAND_LER}gdrive setup</code> <i>first</i>" ) else: await status_message.edit_text( text= f"__Check__ `{COMMAND_HAND_LER}help gdrive` __on how to use the plugin__" )