Esempio n. 1
0
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)
Esempio n. 2
0
async def _set_parent(client, message):
    if len(message.command) > 1:
        cmd_msg = message.command[1]
        if cmd_msg.lower() == "clear":
            sql.del_id(message.from_user.id)
            await message.reply_text(
                '**Custom Folder ID Cleared**\n__Use /setfolder <Folder URL> to set it back.__',
                quote=True)
        else:
            file_id = getIdFromUrl(cmd_msg)
            if 'NotFound' in file_id:
                await message.reply_text(
                    '❗ Invalid Folder URL**\n__Copy the custom folder id correctly.__',
                    quote=True)
            else:
                sql.set_id(message.from_user.id, file_id)
                await message.reply_text(
                    f'**Custom Folder ID sets Successfully**\n__Your custom folder id set to {file_id}. All the uploads (from now) goes here.\nUse__ ```/setfolder clear``` __to clear the current Folder ID.__',
                    quote=True)
    else:
        if sql.get_id(message.from_user.id):
            await message.reply_text(
                f'**Your custom folder id is** ```{sql.get_id(message.from_user.id).parent_id}```.',
                quote=True)
        else:
            await message.reply_text(
                '**You did not set any Custom Folder ID**\n__Use__ ```/setfolder {folder URL}``` __to set your custom folder ID.__',
                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'
        )