Team Ai
Apppublic

azils3/bash

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
gdrive.py233 linesDownload Raw Back to plugins
1# Ultroid - UserBot2# Copyright (C) 2021-2025 TeamUltroid3#4# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >5# PLease read the GNU Affero General Public License in6# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.7"""8✘ Commands Available9 10• `{i}gdul <reply/file name>`11    Reply to file to upload on Google Drive.12    Add file name to upload on Google Drive.13 14• `{i}gdown <file id/link> | <filename>`15    Download from Gdrive link or file id.16 17• `{i}gdsearch <file name>`18    Search file name on Google Drive and get link.19 20• `{i}gdlist`21    List all GDrive files.22 23• `{i}gdfolder`24    Link to your Google Drive Folder.25    If added then all files will be uploaded in this folder.26"""27 28import os29import time30 31from telethon.tl.types import Message32 33from pyUltroid.fns.gDrive import GDriveManager34from pyUltroid.fns.helper import time_formatter35 36from . import ULTConfig, asst, eod, eor, get_string, ultroid_cmd37 38 39@ultroid_cmd(40    pattern="gdown( (.*)|$)",41    fullsudo=True,42)43async def gdown(event):44    GDrive = GDriveManager()45    match = event.pattern_match.group(1).strip()46    if not match:47        return await eod(event, "`Give file id or Gdrive link to download from!`")48    filename = match.split(" | ")[1].strip() if " | " in match else None49    eve = await event.eor(get_string("com_1"))50    _start = time.time()51    status, response = await GDrive._download_file(eve, match, filename)52    if not status:53        return await eve.edit(response)54    await eve.edit(55        f"`Downloaded ``{response}`` in {time_formatter((time.time() - _start)*1000)}`"56    )57 58 59@ultroid_cmd(60    pattern="gdlist$",61    fullsudo=True,62)63async def files(event):64    GDrive = GDriveManager()65    if not os.path.exists(GDrive.token_file):66        return await event.eor(get_string("gdrive_6").format(asst.me.username))67    eve = await event.eor(get_string("com_1"))68    msg = ""69    if files := GDrive._list_files:70        msg += f"{len(files.keys())} files found in gdrive.\n\n"71        for _ in files:72            msg += f"> [{files[_]}]({_})\n"73    else:74        msg += "Nothing in Gdrive"75    if len(msg) < 4096:76        await eve.edit(msg, link_preview=False)77    else:78        with open("drive-files.txt", "w") as f:79            f.write(80                msg.replace("[", "File Name: ")81                .replace("](", "\n» Link: ")82                .replace(")\n", "\n\n")83            )84        try:85            await eve.delete()86        except BaseException:87            pass88        await event.client.send_file(89            event.chat_id,90            "drive-files.txt",91            thumb=ULTConfig.thumb,92            reply_to=event,93        )94        os.remove("drive-files.txt")95 96 97@ultroid_cmd(98    pattern="gdul( (.*)|$)",99    fullsudo=True,100)101async def _(event):102    GDrive = GDriveManager()103    if not os.path.exists(GDrive.token_file):104        return await eod(event, get_string("gdrive_6").format(asst.me.username))105    input_file = event.pattern_match.group(1).strip() or await event.get_reply_message()106    if not input_file:107        return await eod(event, "`Reply to file or give its location.`")108    mone = await event.eor(get_string("com_1"))109    if isinstance(input_file, Message):110        location = "resources/downloads"111        if input_file.photo:112            filename = await input_file.download_media(location)113        else:114            filename = input_file.file.name115            if not filename:116                filename = str(round(time.time()))117            filename = f"{location}/{filename}"118            try:119                filename, downloaded_in = await event.client.fast_downloader(120                    file=input_file.media.document,121                    filename=filename,122                    show_progress=True,123                    event=mone,124                    message=get_string("com_5"),125                )126                filename = filename.name127            except Exception as e:128                return await eor(mone, str(e), time=10)129        await mone.edit(130            f"`Downloaded to ``{filename}`.`",131        )132    else:133        filename = input_file.strip()134        if not os.path.exists(filename):135            return await eod(136                mone,137                "File Not found in local server. Give me a file path :((",138                time=5,139            )140    folder_id = None141    if os.path.isdir(filename):142        files = os.listdir(filename)143        if not files:144            return await eod(145                mone, "`Requested directory is empty. Can't create empty directory.`"146            )147        folder_id = GDrive.create_directory(filename)148        c = 0149        for files in sorted(files):150            file = f"{filename}/{files}"151            if not os.path.isdir(file):152                try:153                    await GDrive._upload_file(mone, path=file, folder_id=folder_id)154                    c += 1155                except Exception as e:156                    return await mone.edit(157                        f"Exception occurred while uploading to gDrive {e}"158                    )159        return await mone.edit(160            f"`Uploaded `[{filename}](https://drive.google.com/folderview?id={folder_id})` with {c} files.`"161        )162    try:163        g_drive_link = await GDrive._upload_file(164            mone,165            filename,166        )167        await mone.edit(168            get_string("gdrive_7").format(filename.split("/")[-1], g_drive_link)169        )170    except Exception as e:171        await mone.edit(f"Exception occurred while uploading to gDrive {e}")172 173 174@ultroid_cmd(175    pattern="gdsearch( (.*)|$)",176    fullsudo=True,177)178async def _(event):179    GDrive = GDriveManager()180    if not os.path.exists(GDrive.token_file):181        return await event.eor(get_string("gdrive_6").format(asst.me.username))182    input_str = event.pattern_match.group(1).strip()183    if not input_str:184        return await event.eor("`Give filename to search on GDrive...`")185    eve = await event.eor(f"`Searching for {input_str} in G-Drive...`")186    files = GDrive.search(input_str)187    msg = ""188    if files:189        msg += (190            f"{len(files.keys())} files with {input_str} in title found in GDrive.\n\n"191        )192        for _ in files:193            msg += f"> [{files[_]}]({_})\n"194    else:195        msg += f"`No files with title {input_str}`"196    if len(msg) < 4096:197        await eve.eor(msg, link_preview=False)198    else:199        with open("drive-files.txt", "w") as f:200            f.write(201                msg.replace("[", "File Name: ")202                .replace("](", "\n» Link: ")203                .replace(")\n", "\n\n")204            )205        try:206            await eve.delete()207        except BaseException:208            pass209        await event.client.send_file(210            event.chat_id,211            f"{input_str}.txt",212            thumb=ULTConfig.thumb,213            reply_to=event,214        )215        os.remove(f"{input_str}.txt")216 217 218@ultroid_cmd(219    pattern="gdfolder$",220    fullsudo=True,221)222async def _(event):223    GDrive = GDriveManager()224    if not os.path.exists(GDrive.token_file):225        return await event.eor(get_string("gdrive_6").format(asst.me.username))226    if GDrive.folder_id:227        await event.eor(228            "`Your G-Drive Folder link : `\n"229            + GDrive._create_folder_link(GDrive.folder_id)230        )231    else:232        await eod(event, "Set FOLDERID from your Assistant bot's Settings ")233