async def _revoke(client, message): if db.get_credential(message.from_user.id) is None: await message.reply_text("π **You have not authenticated me to upload to any account.**\n__Send /auth to authenticate.__", quote=True) else: try: db.clear_credential(message.from_user.id) await message.reply_text("π **Authenticated Account revoked successfully.**", quote=True) except Exception as e: await message.reply_text(f"**ERROR:** ```{e}```", quote=True)
async def _start(client, message): creds = db.get_credential(message.from_user.id) if creds is None: await message.reply_text( "π **You have not authenticated me to upload to any account.**\n__Send /auth to authenticate.__", quote=True) return if sql.get_id(message.from_user.id): parent_id = sql.get_id(message.from_user.id).parent_id else: parent_id = None if message.text: if message.text.startswith('http'): sent_message = await message.reply_text('**Checking Link...**', quote=True) filename = await download_file(client, message, sent_message) if filename in ("HTTPError"): await sent_message.edit( '**β Invalid URL**\n__Make sure it\'s a direct link and in working state.__' ) return elif message.media: sent_message = await message.reply_text('π₯ **Downloading File...**', quote=True) try: filename = await client.download_media(message=message) except Exception as e: sent_message.edit(f'**ERROR:** ```{e}```') filesize = humanbytes(os.path.getsize(filename)) file_name = os.path.basename(filename) await sent_message.edit( f'β **Download Completed.**\n**Filename:** ```{file_name}```\n**Size:** ```{filesize}```\n__Now starting upload...__' ) file_id = await upload_file(creds=creds, file_path=filename, filesize=filesize, parent_id=parent_id, message=sent_message) if file_id not in ('error', 'LimitExceeded'): await sent_message.edit( "β **Uploaded Successfully.JOIN NOW @VKPROJECTS**\n<a href='https://drive.google.com/open?id={}'>{}</a> __({})__" .format(file_id, file_name, filesize)) elif file_id == 'LimitExceeded': await sent_message.edit( 'β **Upload limit exceeded**\n__Try after 24 hours__') else: await sent_message.edit( 'β **Uploading Error**\n__Please try again later.__') os.remove(filename)
async def _auth(client, message): creds = db.get_credential(message.from_user.id) if creds is not None: creds.refresh(Http()) db.set_credential(message.from_user.id, creds) await message.reply_text("π **Already authorized your Google Drive Account.**\n__Use /revoke to revoke the current account.__\n__Send me a direct link or File to Upload on Google Drive__", quote=True) else: global flow try: flow = OAuth2WebServerFlow( G_DRIVE_CLIENT_ID, G_DRIVE_CLIENT_SECRET, OAUTH_SCOPE, redirect_uri=REDIRECT_URI ) auth_url = flow.step1_get_authorize_url() await client.send_message(message.from_user.id, "βοΈ **To Authorize your Google Drive account visit this [URL]({}) and send the generated code here.**\n__Visit the URL > Allow permissions > you will get a code > copy it > Send it here__".format(auth_url)) except Exception as e: await message.reply_text(f"**ERROR:** ```{e}```", quote=True)
async def _copy(client, message): creds = db.get_credential(message.from_user.id) if creds is None: await message.reply_text( 'β **Not an authorized user.**\n__Authorize your Google Drive Account by running /auth Command in order to use this bot.__', quote=True) return if sql.get_id(message.from_user.id): parent_id = sql.get_id(message.from_user.id).parent_id else: parent_id = 'root' transferred_size = 0 if len(message.command) > 1: sent_message = await message.reply_text( '**Checking Google Drive Link...**', quote=True) file_id = getIdFromUrl(message.command[1]) if file_id == 'NotFound': await sent_message.edit( 'β **Invalid Google Drive URL**\n__Only Google Drive links can be copied.__' ) return else: await sent_message.edit( f'ποΈ **Cloning to Google Drive...**\n__{file_id}__ Β© @s4hchannel' ) service = build("drive", "v3", credentials=creds, cache_discovery=False) try: meta = service.files().get( supportsAllDrives=True, fileId=file_id, fields="name,id,mimeType,size").execute() except HttpError as err: if err.resp.get('content-type', '').startswith('application/json'): reason = json.loads( err.content).get('error').get('errors')[0].get('reason') if 'notFound' in reason: await sent_message.edit( 'β**ERROR: FILE NOT FOUND**\n__Make sure the file exists and accessible by the account you authenticated.__' ) else: await sent_message.edit( f"**ERROR:** ```{str(err).replace('>', '').replace('<', '')}```" ) except Exception as e: await sent_message.edit( f"**ERROR:** ```{str(e).replace('>', '').replace('<', '')}```") if meta.get("mimeType") == G_DRIVE_DIR_MIME_TYPE: dir_id = create_directory(service, meta.get('name'), parent_id) try: result, transferred_size = cloneFolder(service, meta.get('name'), meta.get('name'), meta.get('id'), dir_id, transferred_size) await sent_message.edit( f"β **Copied successfully.**\n[{meta.get('name')}]({G_DRIVE_FOLDER_LINK.format(dir_id)}) __({humanbytes(transferred_size)})__" ) except Exception as e: await sent_message.edit(f'**ERROR:** ```{e}```') else: try: file = copyFile(service, meta.get('id'), parent_id) await sent_message.edit( f"β **Copied successfully.**\n[{file.get('name')}]({G_DRIVE_FILE_LINK.format(file.get('id'))}) __({humanbytes(int(meta.get('size')))})__" ) except Exception as e: await sent_message.edit(f'**ERROR:** ```{e}```') else: await message.reply_text( '**Copy Google Drive Files or Folder**\n__Copy GDrive Files/Folder to your Google Drive Account. Use__ ```/copy {GDriveFolderURL/FileURL}``` __for copying.__ Β© @s4hchannel' )