async def gdrive_stuff(client, message): gauth.LoadCredentialsFile("nana/session/drive") if gauth.credentials is None: if ENV and gdrive_credentials: with open("client_secrets.json", "w") as file: file.write(gdrive_credentials) await edrep( message, text="You are not logged in to your google drive account!\nYour assistant bot may help you to login google " "drive, check your assistant bot for more information!", ) gdriveclient = os.path.isfile("client_secrets.json") if gdriveclient: try: gauth.GetAuthUrl() except Exception as e: print(e) await setbot.send_message( message.from_user.id, "Wrong Credentials! Check var ENV gdrive_credentials on heroku or do " ".credentials (your credentials) for change your Credentials", ) return await setbot.send_message( message.from_user.id, "Hello, look like you're not logged in to google drive :)\nI can help you to " "login.\n\n**To login Google Drive**\n1. `/gdrive` to get login URL\n2. After " "you're logged in, copy your Token.\n3. `/gdrive (token)` without `(` or `)` to " "login, and your session will saved to `nana/session/drive`.\n\nDon't share your " "session to someone, else they will hack your google drive account!", ) else: await setbot.send_message( message.from_user.id, "Hello, look like you're not logged in to google drive 🙂\nI can help you to " "login.\n\nFirst of all, you need to activate your google drive API\n1. [Go " "here](https://developers.google.com/drive/api/v3/quickstart/python), " "click **Enable the drive API**\n2. Login to your google account (skip this if " "you're already logged in)\n3. After logged in, click **Enable the drive API** " "again, and click **Download Client Configuration** button, download that.\n4. " "After downloaded that file, open that file then copy all of that content, " "back to telegram then do .credentials (copy the content of that file) do " "without bracket\n\nAfter that, you can go next guide by type /gdrive", ) return elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() drive = GoogleDrive(gauth) drive_dir = await get_drivedir(drive) if len(message.text.split()) == 3 and message.text.split()[1] == "download": await edrep(message, text="Downloading...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await edrep( message, text="Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`", ) return filename = await get_driveinfo(driveid) if not filename: await edrep( message, text="Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`", ) return await edrep( message, text="Downloading for `{}`\nPlease wait...".format( filename.replace(" ", "_") ), ) download = drive.CreateFile({"id": driveid}) download.GetContentFile(filename) try: os.rename(filename, "nana/downloads/" + filename.replace(" ", "_")) except FileExistsError: os.rename(filename, "nana/downloads/" + filename.replace(" ", "_") + ".2") await edrep( message, text="Downloaded!\nFile saved to `{}`".format( "nana/downloads/" + filename.replace(" ", "_") ), ) elif len(message.text.split()) == 3 and message.text.split()[1] == "upload": filerealname = message.text.split()[2].split(None, 1)[0] filename = "nana/downloads/{}".format(filerealname.replace(" ", "_")) checkfile = os.path.isfile(filename) if not checkfile: await edrep(message, text="File `{}` was not found!".format(filerealname)) return await edrep(message, text="Uploading `{}`...".format(filerealname)) upload = drive.CreateFile( { "parents": [{"kind": "drive#fileLink", "id": drive_dir}], "title": filerealname, } ) upload.SetContentFile(filename) upload.Upload() upload.InsertPermission({"type": "anyone", "value": "anyone", "role": "reader"}) await edrep( message, text="Uploaded!\nDownload link: [{}]({})\nDirect download link: [{}]({})".format( filerealname, upload["alternateLink"], filerealname, upload["downloadUrl"], ), ) elif len(message.text.split()) == 3 and message.text.split()[1] == "mirror": await edrep(message, text="Mirroring...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await edrep( message, text="Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`", ) return filename = await get_driveinfo(driveid) if not filename: await edrep( message, text="Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`", ) return mirror = ( drive.auth.service.files() .copy( fileId=driveid, body={ "parents": [{"kind": "drive#fileLink", "id": drive_dir}], "title": filename, }, ) .execute() ) new_permission = {"type": "anyone", "value": "anyone", "role": "reader"} drive.auth.service.permissions().insert( fileId=mirror["id"], body=new_permission ).execute() await edrep( message, text="Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})".format( filename, mirror["alternateLink"], filename, mirror["downloadUrl"] ), ) elif len(message.text.split()) == 2 and message.text.split()[1] == "tgmirror": if message.reply_to_message: await edrep(message, text="__Downloading...__") c_time = time.time() if message.reply_to_message.photo: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.png".replace(" ", "_") else: nama = f"photo_{message.reply_to_message.photo.date}.png" await client.download_media( message.reply_to_message.photo, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.animation: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.gif".replace(" ", "_") else: nama = "giphy_{}-{}.gif".format( message.reply_to_message.animation.date, message.reply_to_message.animation.file_size, ) await client.download_media( message.reply_to_message.animation, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.video: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.mp4".replace( " ", "_" ).replace(".mkv", "") else: nama = "video_{}-{}.mp4".format( message.reply_to_message.video.date, message.reply_to_message.video.file_size, ) await client.download_media( message.reply_to_message.video, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.sticker: if not message.reply_to_message.caption: nama = "sticker_{}_{}.webp".format( message.reply_to_message.sticker.date, message.reply_to_message.sticker.set_name, ) else: nama = f"{message.reply_to_message.caption}.webp".replace(" ", "_") await client.download_media( message.reply_to_message.sticker, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.audio: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.mp3".replace(" ", "_") else: nama = "audio_{}.mp3".format(message.reply_to_message.audio.date) await client.download_media( message.reply_to_message.audio, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.voice: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.ogg".replace(" ", "_") else: nama = "audio_{}.ogg".format(message.reply_to_message.voice.date) await client.download_media( message.reply_to_message.voice, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) elif message.reply_to_message.document: nama = "{}".format(message.reply_to_message.document.file_name) await client.download_media( message.reply_to_message.document, file_name="nana/downloads/" + nama, progress=lambda d, t: asyncio.get_event_loop().create_task( progressdl(d, t, message, c_time, "Downloading...") ), ) else: await edrep(message, text="Unknown file!") return upload = drive.CreateFile( { "parents": [{"kind": "drive#fileLink", "id": drive_dir}], "title": nama, } ) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission( {"type": "anyone", "value": "anyone", "role": "reader"} ) await edrep( message, text="Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})".format( nama, upload["alternateLink"], nama, upload["downloadUrl"] ), ) os.remove("nana/downloads/" + nama) else: await edrep(message, text="Reply document to mirror it to gdrive") elif len(message.text.split()) == 3 and message.text.split()[1] == "urlmirror": await edrep(message, text="Downloading...") URL = message.text.split()[2] nama = URL.split("/")[-1] time_dl = await download_url(URL, nama) if "Downloaded" not in time_dl: await edrep(message, text="Failed to download file, invaild url!") return await edrep(message, text=f"Downloaded with {time_dl}.\nNow uploading...") upload = drive.CreateFile( {"parents": [{"kind": "drive#fileLink", "id": drive_dir}], "title": nama} ) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission({"type": "anyone", "value": "anyone", "role": "reader"}) await edrep( message, text="Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})".format( nama, upload["alternateLink"], nama, upload["downloadUrl"] ), ) os.remove("nana/downloads/" + nama) else: await edrep( message, text="Usage:\n-> `gdrive download <url/gid>`\n-> `gdrive upload <file>`\n-> `gdrive mirror <url/gid>`\n\nFor " "more information about this, go to your assistant.", )
async def gdrive_stuff(client, message): gauth.LoadCredentialsFile("nana/session/drive") if gauth.credentials is None: if ENV and GDRIVE_CREDENTIALS: with open("client_secrets.json", "w") as file: file.write(GDRIVE_CREDENTIALS) await edit_or_reply( message, text="You are not logged in to your google drive account!\n" "Your assistant bot may help you to login google " "drive, check your assistant bot for more information!", ) gdriveclient = os.path.isfile("client_secrets.json") if gdriveclient: try: gauth.GetAuthUrl() except Exception as e: print(e) await setbot.send_message( message.from_user.id, "Wrong Credentials! Check var ENV gdrive_credentials" "on heroku or do " ".credentials (your credentials)" "for changing your Credentials", ) return await setbot.send_message( message.from_user.id, tld("gdrive_credential_err_heroku"), ) else: await setbot.send_message(message.from_user.id, tld("gdrive_credential_err")) return elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() drive = GoogleDrive(gauth) drive_dir = await get_drivedir(drive) if len(message.text.split()) == 3 and message.text.split( )[1] == "download": await edit_or_reply(message, text="Downloading...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await edit_or_reply( message, text="Invaild URL!", ) return filename = await get_driveinfo(driveid) if not filename: await edit_or_reply( message, text="Invaild URL!", ) return await edit_or_reply( message, text="Downloading for `{}`\nPlease wait...".format( filename.replace(" ", "_")), ) download = drive.CreateFile({"id": driveid}) download.GetContentFile(filename) try: os.rename(filename, "nana/downloads/" + filename.replace(" ", "_")) except FileExistsError: os.rename(filename, "nana/downloads/" + filename.replace(" ", "_") + ".2") await edit_or_reply( message, text="Downloaded!\nFile saved to `{}`".format( "nana/downloads/" + filename.replace(" ", "_")), ) elif len( message.text.split()) == 3 and message.text.split()[1] == "upload": filerealname = message.text.split()[2].split(None, 1)[0] filename = "nana/downloads/{}".format(filerealname.replace(" ", "_")) checkfile = os.path.isfile(filename) if not checkfile: await edit_or_reply( message, text="File `{}` was not found!".format(filerealname)) return await edit_or_reply(message, text="Uploading `{}`...".format(filerealname)) upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], "title": filerealname, }) upload.SetContentFile(filename) upload.Upload() upload.InsertPermission({ "type": "anyone", "value": "anyone", "role": "reader" }) await edit_or_reply( message, text="Uploaded!\nDownload link: [{}]({})".format( filerealname, upload["alternateLink"], ), ) elif len( message.text.split()) == 3 and message.text.split()[1] == "mirror": await edit_or_reply(message, text="Mirroring...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await edit_or_reply( message, text="Invaild URL!", ) return filename = await get_driveinfo(driveid) if not filename: await edit_or_reply( message, text="Invaild URL!", ) return mirror = (drive.auth.service.files().copy( fileId=driveid, body={ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], "title": filename, }, ).execute()) new_permission = { "type": "anyone", "value": "anyone", "role": "reader" } drive.auth.service.permissions().insert(fileId=mirror["id"], body=new_permission).execute() await edit_or_reply( message, text="Done!\nDownload link: [{}]({})".format( filename, mirror["alternateLink"]), ) elif len(message.text.split()) == 2 and message.text.split( )[1] == "tgmirror": if message.reply_to_message: await edit_or_reply(message, text="__Downloading...__") c_time = time.time() if message.reply_to_message.photo: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.png".replace( " ", "_") else: nama = f"photo_{message.reply_to_message.photo.date}.png" await client.download_media( message.reply_to_message.photo, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.animation: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.gif".replace( " ", "_") else: nama = "giphy_{}-{}.gif".format( message.reply_to_message.animation.date, message.reply_to_message.animation.file_size, ) await client.download_media( message.reply_to_message.animation, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.video: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.mp4".replace( " ", "_").replace(".mkv", "") else: nama = "video_{}-{}.mp4".format( message.reply_to_message.video.date, message.reply_to_message.video.file_size, ) await client.download_media( message.reply_to_message.video, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.sticker: if not message.reply_to_message.caption: nama = "sticker_{}_{}.webp".format( message.reply_to_message.sticker.date, message.reply_to_message.sticker.set_name, ) else: nama = f"{message.reply_to_message.caption}.webp".replace( " ", "_") await client.download_media( message.reply_to_message.sticker, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.audio: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.mp3".replace( " ", "_") else: nama = "audio_{}.mp3".format( message.reply_to_message.audio.date) await client.download_media( message.reply_to_message.audio, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.voice: if message.reply_to_message.caption: nama = f"{message.reply_to_message.caption}.ogg".replace( " ", "_") else: nama = "audio_{}.ogg".format( message.reply_to_message.voice.date) await client.download_media( message.reply_to_message.voice, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) elif message.reply_to_message.document: nama = "{}".format(message.reply_to_message.document.file_name) await client.download_media( message.reply_to_message.document, file_name="nana/downloads/" + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, "Downloading...")), ) else: await edit_or_reply(message, text="Unknown file!") return upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], "title": nama, }) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission({ "type": "anyone", "value": "anyone", "role": "reader" }) await edit_or_reply( message, text="Done!\nDownload link: [{}]({})".format( nama, upload["alternateLink"]), ) os.remove("nana/downloads/" + nama) else: await edit_or_reply(message, text="Reply document to mirror it to gdrive") elif len(message.text.split()) == 3 and message.text.split( )[1] == "urlmirror": await edit_or_reply(message, text="Downloading...") URL = message.text.split()[2] nama = URL.split("/")[-1] time_dl = await download_url(URL, nama) if "Downloaded" not in time_dl: await edit_or_reply(message, text="Failed to download file, invaild url!") return await edit_or_reply( message, text=f"Downloaded with {time_dl}.\nNow uploading...") upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], "title": nama }) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission({ "type": "anyone", "value": "anyone", "role": "reader" }) await edit_or_reply( message, text="Done!\nDownload link: [{}]({})".format( nama, upload["alternateLink"]), ) os.remove("nana/downloads/" + nama) else: await edit_or_reply( message, text="Usage:\n-> `gdrive download <url/gid>`\n" "->`gdrive upload <file>`\n" "->`gdrive mirror <url/gid>`\n\nFor " "more information about this, go to your assistant.", )
async def gdrive_stuff(client, message): gauth.LoadCredentialsFile("nana/session/drive") if gauth.credentials is None: await message.edit( "You are not logged in to your google drive account!\nYour assistant bot may help you to login google drive, check your assistant bot for more information!" ) gdriveclient = os.path.isfile("client_secrets.json") if not gdriveclient: await setbot.send_message( message.from_user.id, "Hello, look like you're not logged in to google drive 🙂\nI can help you to login.\n\nFirst of all, you need to activate your google drive API\n1. [Go here](https://developers.google.com/drive/api/v3/quickstart/python), click **Enable the drive API**\n2. Login to your google account (skip this if you're already logged in)\n3. After logged in, click **Enable the drive API** again, and click **Download Client Configuration** button, download that.\n4. After downloaded that file, rename `credentials.json` to `client_secrets.json`, and upload to your bot dir (not in nana dir)\nor if you're using heroku, read this alternate guide\n4a. Open `credentials.json`, copy all text from that file, then paste **GOOGLE_API_TEXT** with that text\n\nAfter that, you can go next guide by type `/gdrive`" ) else: await setbot.send_message( message.from_user.id, "Hello, look like you're not logged in to google drive :)\nI can help you to login.\n\n**To login Google Drive**\n1. `/gdrive` to get login URL\n2. After you're logged in, copy your Token.\n3. `/gdrive (token)` without `(` or `)` to login, and your session will saved to `nana/session/drive`.\n\nDon't share your session to someone, else they will hack your google drive account!" ) return elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() drive = GoogleDrive(gauth) drive_dir = await get_drivedir(drive) if len(message.text.split()) == 3 and message.text.split( )[1] == "download": await message.edit("Downloading...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await message.edit( "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`" ) return filename = await get_driveinfo(driveid) if not filename: await message.edit( "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`" ) return await message.edit( "Downloading for `{}`\nPlease wait...".format(filename)) download = drive.CreateFile({'id': driveid}) download.GetContentFile(filename) try: os.rename(filename, "nana/downloads/" + filename) except FileExistsError: os.rename(filename, "nana/downloads/" + filename + ".2") await message.edit( "Downloaded!\nFile saved to `{}`".format("nana/downloads/" + filename)) elif len( message.text.split()) == 3 and message.text.split()[1] == "upload": filename = message.text.split()[2].split(None, 1)[0] checkfile = os.path.isfile(filename) if not checkfile: await message.edit("File `{}` was not found!".format(filename)) return await message.edit("Uploading `{}`...".format(filename)) upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], 'title': filename }) upload.SetContentFile(filename) upload.Upload() upload.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }) await message.edit( "Uploaded!\nDownload link: [{}]({})\nDirect download link: [{}]({})" .format(filename, upload['alternateLink'], filename, upload['downloadUrl'])) elif len( message.text.split()) == 3 and message.text.split()[1] == "mirror": message.edit("Mirroring...") driveid = await get_driveid(message.text.split()[2]) if not driveid: await message.edit( "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`" ) return filename = await get_driveinfo(driveid) if not filename: await message.edit( "Invaild URL!\nIf you think this is bug, please go to your Assistant bot and type `/reportbug`" ) return mirror = drive.auth.service.files().copy(fileId=driveid, body={ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], 'title': filename }).execute() new_permission = { 'type': 'anyone', 'value': 'anyone', 'role': 'reader' } drive.auth.service.permissions().insert(fileId=mirror['id'], body=new_permission).execute() await message.edit( "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})". format(filename, mirror['alternateLink'], filename, mirror['downloadUrl'])) elif len(message.text.split()) == 2 and message.text.split( )[1] == "tgmirror": if message.reply_to_message: await message.edit("__Downloading...__") if message.reply_to_message.photo: nama = "photo_{}_{}.png".format( message.reply_to_message.photo.file_id, message.reply_to_message.photo.date) await client.download_media(message.reply_to_message.photo, file_name="nana/downloads/" + nama) elif message.reply_to_message.animation: nama = "giphy_{}-{}.gif".format( message.reply_to_message.animation.date, message.reply_to_message.animation.file_size) await client.download_media(message.reply_to_message.animation, file_name="nana/downloads/" + nama) elif message.reply_to_message.video: nama = "video_{}-{}.mp4".format( message.reply_to_message.video.date, message.reply_to_message.video.file_size) await client.download_media(message.reply_to_message.video, file_name="nana/downloads/" + nama) elif message.reply_to_message.sticker: nama = "sticker_{}_{}.webp".format( message.reply_to_message.sticker.date, message.reply_to_message.sticker.set_name) await client.download_media(message.reply_to_message.sticker, file_name="nana/downloads/" + nama) elif message.reply_to_message.audio: nama = "{}".format(message.reply_to_message.audio.file_name) await client.download_media(message.reply_to_message.audio, file_name="nana/downloads/" + nama) elif message.reply_to_message.voice: nama = "audio_{}.ogg".format(message.reply_to_message.voice) await client.download_media(message.reply_to_message.voice, file_name="nana/downloads/" + nama) elif message.reply_to_message.document: nama = "{}".format(message.reply_to_message.document.file_name) await client.download_media(message.reply_to_message.document, file_name="nana/downloads/" + nama) else: message.edit("Unknown file!") return upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], 'title': nama }) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }) await message.edit( "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})" .format(nama, upload['alternateLink'], nama, upload['downloadUrl'])) os.remove("nana/downloads/" + nama) else: await message.edit("Reply document to mirror it to gdrive") elif len(message.text.split()) == 3 and message.text.split( )[1] == "urlmirror": await message.edit("Downloading...") URL = message.text.split()[2] nama = URL.split("/")[-1] time_dl = await download_url(URL, nama) if "Downloaded" not in time_dl: await message.edit("Failed to download file, invaild url!") return await message.edit(f"Downloaded with {time_dl}.\nNow uploading...") upload = drive.CreateFile({ "parents": [{ "kind": "drive#fileLink", "id": drive_dir }], 'title': nama }) upload.SetContentFile("nana/downloads/" + nama) upload.Upload() upload.InsertPermission({ 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }) await message.edit( "Done!\nDownload link: [{}]({})\nDirect download link: [{}]({})". format(nama, upload['alternateLink'], nama, upload['downloadUrl'])) os.remove("nana/downloads/" + nama) else: await message.edit( "Usage:\n-> `gdrive download <url/gid>`\n-> `gdrive upload <file>`\n-> `gdrive mirror <url/gid>`\n\nFor more information about this, go to your assistant." )
async def gdrive_stuff(client, message): gauth.LoadCredentialsFile('nana/session/drive') if gauth.credentials is None: if ENV and GDRIVE_CREDENTIALS: with open('client_secrets.json', 'w') as file: file.write(GDRIVE_CREDENTIALS) await edit_or_reply( message, text='You are not logged in to your google drive account!\n' 'Your assistant bot may help you to login google ' 'drive, check your assistant bot for more information!', ) gdriveclient = os.path.isfile('client_secrets.json') if gdriveclient: try: gauth.GetAuthUrl() except Exception as e: print(e) await setbot.send_message( message.from_user.id, 'Wrong Credentials! Check var ENV gdrive_credentials' 'on heroku or do ' '.credentials (your credentials)' 'for changing your Credentials', ) return await setbot.send_message( message.from_user.id, tld('gdrive_credential_err_heroku'), ) else: await setbot.send_message( message.from_user.id, tld('gdrive_credential_err'), ) return elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() else: # Initialize the saved creds gauth.Authorize() drive = GoogleDrive(gauth) drive_dir = await get_drivedir(drive) if len(message.text.split(), ) == 3 and message.text.split()[1] == 'download': await edit_or_reply(message, text='Downloading...') driveid = await get_driveid(message.text.split()[2]) if not driveid: await edit_or_reply( message, text='Invaild URL!', ) return filename = await get_driveinfo(driveid) if not filename: await edit_or_reply( message, text='Invaild URL!', ) return await edit_or_reply( message, text='Downloading for `{}`\nPlease wait...'.format( filename.replace(' ', '_'), ), ) download = drive.CreateFile({'id': driveid}) download.GetContentFile(filename) try: os.rename(filename, 'nana/downloads/' + filename.replace(' ', '_')) except FileExistsError: os.rename( filename, 'nana/downloads/' + filename.replace( ' ', '_', ) + '.2', ) await edit_or_reply( message, text='Downloaded!\nFile saved to `{}`'.format( 'nana/downloads/' + filename.replace(' ', '_'), ), ) elif len(message.text.split(), ) == 3 and message.text.split()[1] == 'upload': filerealname = message.text.split()[2].split(None, 1)[0] filename = 'nana/downloads/{}'.format(filerealname.replace(' ', '_')) checkfile = os.path.isfile(filename) if not checkfile: await edit_or_reply( message, text=f'File `{filerealname}` was not found!', ) return await edit_or_reply( message, text='Uploading `{}`...'.format(filerealname, ), ) upload = drive.CreateFile( { 'parents': [{ 'kind': 'drive#fileLink', 'id': drive_dir }], 'title': filerealname, }, ) upload.SetContentFile(filename) upload.Upload() upload.InsertPermission( { 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }, ) await edit_or_reply( message, text='Uploaded!\nDownload link: [{}]({})'.format( filerealname, upload['alternateLink'], ), ) elif len(message.text.split(), ) == 3 and message.text.split()[1] == 'mirror': await edit_or_reply(message, text='Mirroring...') driveid = await get_driveid(message.text.split()[2]) if not driveid: await edit_or_reply( message, text='Invaild URL!', ) return filename = await get_driveinfo(driveid) if not filename: await edit_or_reply( message, text='Invaild URL!', ) return mirror = (drive.auth.service.files().copy( fileId=driveid, body={ 'parents': [{ 'kind': 'drive#fileLink', 'id': drive_dir }], 'title': filename, }, ).execute()) new_permission = { 'type': 'anyone', 'value': 'anyone', 'role': 'reader', } drive.auth.service.permissions().insert( fileId=mirror['id'], body=new_permission, ).execute() await edit_or_reply( message, text='Done!\nDownload link: [{}]({})'.format( filename, mirror['alternateLink'], ), ) elif len(message.text.split(), ) == 2 and message.text.split()[1] == 'tgmirror': if message.reply_to_message: await edit_or_reply(message, text='__Downloading...__') c_time = time.time() if message.reply_to_message.photo: if message.reply_to_message.caption: nama = f'{message.reply_to_message.caption}.png'.replace( ' ', '_', ) else: nama = f'photo_{message.reply_to_message.photo.date}.png' await client.download_media( message.reply_to_message.photo, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.animation: if message.reply_to_message.caption: nama = f'{message.reply_to_message.caption}.gif'.replace( ' ', '_', ) else: nama = 'giphy_{}-{}.gif'.format( message.reply_to_message.animation.date, message.reply_to_message.animation.file_size, ) await client.download_media( message.reply_to_message.animation, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.video: if message.reply_to_message.caption: nama = f'{message.reply_to_message.caption}.mp4'.replace( ' ', '_', ).replace('.mkv', '') else: nama = 'video_{}-{}.mp4'.format( message.reply_to_message.video.date, message.reply_to_message.video.file_size, ) await client.download_media( message.reply_to_message.video, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.sticker: if not message.reply_to_message.caption: nama = 'sticker_{}_{}.webp'.format( message.reply_to_message.sticker.date, message.reply_to_message.sticker.set_name, ) else: nama = f'{message.reply_to_message.caption}.webp'.replace( ' ', '_', ) await client.download_media( message.reply_to_message.sticker, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.audio: if message.reply_to_message.caption: nama = f'{message.reply_to_message.caption}.mp3'.replace( ' ', '_', ) else: nama = 'audio_{}.mp3'.format( message.reply_to_message.audio.date, ) await client.download_media( message.reply_to_message.audio, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.voice: if message.reply_to_message.caption: nama = f'{message.reply_to_message.caption}.ogg'.replace( ' ', '_', ) else: nama = 'audio_{}.ogg'.format( message.reply_to_message.voice.date, ) await client.download_media( message.reply_to_message.voice, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) elif message.reply_to_message.document: nama = f'{message.reply_to_message.document.file_name}' await client.download_media( message.reply_to_message.document, file_name='nana/downloads/' + nama, progress=lambda d, t: client.loop.create_task( progressdl(d, t, message, c_time, 'Downloading...'), ), ) else: await edit_or_reply(message, text='Unknown file!') return upload = drive.CreateFile( { 'parents': [{ 'kind': 'drive#fileLink', 'id': drive_dir }], 'title': nama, }, ) upload.SetContentFile('nana/downloads/' + nama) upload.Upload() upload.InsertPermission( { 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }, ) await edit_or_reply( message, text='Done!\nDownload link: [{}]({})'.format( nama, upload['alternateLink'], ), ) os.remove('nana/downloads/' + nama) else: await edit_or_reply( message, text='Reply document to mirror it to gdrive', ) elif len(message.text.split(), ) == 3 and message.text.split()[1] == 'urlmirror': await edit_or_reply(message, text='Downloading...') URL = message.text.split()[2] nama = URL.split('/')[-1] time_dl = await download_url(URL, nama) if 'Downloaded' not in time_dl: await edit_or_reply( message, text='Failed to download file, invaild url!', ) return await edit_or_reply( message, text=f'Downloaded with {time_dl}.\nNow uploading...', ) upload = drive.CreateFile( { 'parents': [ { 'kind': 'drive#fileLink', 'id': drive_dir, }, ], 'title': nama, }, ) upload.SetContentFile('nana/downloads/' + nama) upload.Upload() upload.InsertPermission( { 'type': 'anyone', 'value': 'anyone', 'role': 'reader' }, ) await edit_or_reply( message, text='Done!\nDownload link: [{}]({})'.format( nama, upload['alternateLink'], ), ) os.remove('nana/downloads/' + nama) else: await edit_or_reply( message, text='Usage:\n-> `gdrive download <url/gid>`\n' '->`gdrive upload <file>`\n' '->`gdrive mirror <url/gid>`\n\nFor ' 'more information about this, go to your assistant.', )