Beispiel #1
0
 async def download(self) -> None:
     """ Download file/folder from GDrive """
     await self._message.edit("`Loading GDrive Download...`")
     file_id, _ = self._get_file_id()
     pool.submit_thread(self._download, file_id)
     start_t = datetime.now()
     count = 0
     while not self._is_finished:
         count += 1
         if self._message.process_is_canceled:
             self._cancel()
         if self._progress is not None and count >= 5:
             count = 0
             await self._message.try_to_edit(self._progress)
         await asyncio.sleep(1)
     end_t = datetime.now()
     m_s = (end_t - start_t).seconds
     if isinstance(self._output, HttpError):
         out = f"**ERROR** : `{self._output._get_reason()}`"  # pylint: disable=protected-access
     elif self._output is not None and not self._is_canceled:
         out = f"**Downloaded Successfully** __in {m_s} seconds__\n\n`{self._output}`"
     elif self._output is not None and self._is_canceled:
         out = self._output
     else:
         out = "`failed to download.. check logs?`"
     await self._message.edit(out, disable_web_page_preview=True, log=__name__)
Beispiel #2
0
 async def copy(self) -> None:
     """ Copy file/folder in GDrive """
     if not self._parent_id:
         await self._message.edit("First set parent path by `.gset`", del_in=5)
         return
     await self._message.edit("`Loading GDrive Copy...`")
     file_id, _ = self._get_file_id()
     pool.submit_thread(self._copy, file_id)
     start_t = datetime.now()
     count = 0
     while not self._is_finished:
         count += 1
         if self._message.process_is_canceled:
             self._cancel()
         if self._progress is not None and count >= 5:
             count = 0
             await self._message.try_to_edit(self._progress)
         await asyncio.sleep(1)
     end_t = datetime.now()
     m_s = (end_t - start_t).seconds
     if isinstance(self._output, HttpError):
         out = f"**ERROR** : `{self._output._get_reason()}`"  # pylint: disable=protected-access
     elif self._output is not None and not self._is_canceled:
         out = f"**Copied Successfully** __in {m_s} seconds__\n\n{self._output}"
     elif self._output is not None and self._is_canceled:
         out = self._output
     else:
         out = "`failed to copy.. check logs?`"
     await self._message.edit(out, disable_web_page_preview=True, log=__name__)
Beispiel #3
0
 def combine(self) -> None:
     """Combine Split files"""
     file_name, ext = splitext(basename(self._path))
     self._final_file_path = join(dirname(self._path), file_name)
     file_list = sorted(glob(self._final_file_path + f".{'[0-9]' * len(ext.lstrip('.'))}"))
     self._total = len(file_list)
     self._file_size = sum((os.stat(f_).st_size for f_ in file_list))
     pool.submit_thread(self._combine_worker, file_list)
Beispiel #4
0
 async def upload(self) -> None:
     """ Upload from file/folder/link/tg file to GDrive """
     replied = self._message.reply_to_message
     is_url = re.search(r"(?:https?|ftp)://[^|\s]+\.[^|\s]+",
                        self._message.input_str)
     dl_loc = ""
     if replied and replied.media:
         try:
             dl_loc, _ = await tg_download(self._message, replied)
         except ProcessCanceled:
             await self._message.canceled()
             return
         except Exception as e_e:
             await self._message.err(str(e_e))
             return
     elif is_url:
         try:
             dl_loc, _ = await url_download(self._message,
                                            self._message.input_str)
         except ProcessCanceled:
             await self._message.canceled()
             return
         except Exception as e_e:
             await self._message.err(str(e_e))
             return
     file_path = dl_loc if dl_loc else self._message.input_str
     if not os.path.exists(file_path):
         await self._message.err("invalid file path provided?")
         return
     if "|" in file_path:
         file_path, file_name = file_path.split("|")
         new_path = os.path.join(os.path.dirname(file_path.strip()),
                                 file_name.strip())
         os.rename(file_path.strip(), new_path)
         file_path = new_path
     await self._message.try_to_edit("`Loading GDrive Upload...`")
     pool.submit_thread(self._upload, file_path)
     start_t = datetime.now()
     with self._message.cancel_callback(self._cancel):
         while not self._is_finished:
             if self._progress is not None:
                 await self._message.edit(self._progress)
             await asyncio.sleep(Config.EDIT_SLEEP_TIMEOUT)
     if dl_loc and os.path.exists(dl_loc):
         os.remove(dl_loc)
     end_t = datetime.now()
     m_s = (end_t - start_t).seconds
     if isinstance(self._output, HttpError):
         out = f"**ERROR** : `{self._output._get_reason()}`"  # pylint: disable=protected-access
     elif self._output is not None and not self._is_canceled:
         out = f"**Uploaded Successfully** __in {m_s} seconds__\n\n{self._output}"
     elif self._output is not None and self._is_canceled:
         out = self._output
     else:
         out = "`failed to upload.. check logs?`"
     await self._message.edit(out,
                              disable_web_page_preview=True,
                              log=__name__)
Beispiel #5
0
 def split(self, split_size: int) -> None:
     """Split files"""
     split_size = int(split_size) * 1024 * 1024
     self._file_size = os.stat(self._path).st_size
     if self._chunk_size > split_size:
         self._chunk_size = split_size
     times = int(ceil(split_size / self._chunk_size))
     self._total = int(ceil(self._file_size / split_size))
     self._final_file_path = join(
         dirname(self._path), f"split_{basename(self._path).replace('.', '_')}")
     if not isdir(self._final_file_path):
         os.makedirs(self._final_file_path)
     pool.submit_thread(self._split_worker, times)
Beispiel #6
0
    def pack_path(self, tar: bool) -> None:
        """PACK file path"""
        file_paths = []

        def explorer(path: Path) -> None:
            if path.is_file():
                self._total += 1
                file_paths.append(str(path))
            elif path.is_dir():
                for i in path.iterdir():
                    explorer(i)
        explorer(Path(self._file_path))
        file_name = basename(self._file_path)
        if tar:
            file_name += '.tar'
            p_type = tar_open
        else:
            file_name += '.zip'
            p_type = ZipFile
        self._final_file_path = join(Config.DOWN_PATH, file_name)
        pool.submit_thread(self._zip, p_type, file_paths, self._final_file_path)
 def unpack_path(self) -> None:
     """ UNPACK file path """
     chunked_file_names = []
     temp_file_names = []
     temp_size = 0
     min_chunk_size = 1024 * 1024 * 10
     for f_n, f_s in self.get_info():
         self._total += 1
         temp_size += f_s
         temp_file_names.append(f_n)
         if temp_size >= min_chunk_size:
             temp_size = 0
             chunked_file_names.append(temp_file_names)
             temp_file_names = []
     if temp_file_names:
         chunked_file_names.append(temp_file_names)
     dir_name = splitext(basename(self._file_path))[0]
     self._final_file_path = join(
         Config.DOWN_PATH, dir_name.replace('.tar', '').replace('.', '_'))
     for f_n_s in chunked_file_names:
         pool.submit_thread(self._unpack, f_n_s)
Beispiel #8
0
 async def upload(self) -> None:
     """ Upload from file/folder/link/tg file to GDrive """
     replied = self._message.reply_to_message
     is_url = re.search(
         r"(?:https?|ftp)://[^\|\s]+\.[^\|\s]+", self._message.input_str)
     dl_loc = None
     if replied and replied.media:
         await self._message.edit("`Downloading From TG...`")
         file_name = Config.DOWN_PATH
         if self._message.input_str:
             file_name = os.path.join(Config.DOWN_PATH, self._message.input_str)
         dl_loc = await self._message.client.download_media(
             message=replied,
             file_name=file_name,
             progress=progress,
             progress_args=(self._message, "trying to download")
         )
         if self._message.process_is_canceled:
             await self._message.edit("`Process Canceled!`", del_in=5)
             return
         dl_loc = os.path.join(Config.DOWN_PATH, os.path.basename(dl_loc))
     elif is_url:
         await self._message.edit("`Downloading From URL...`")
         url = is_url[0]
         file_name = unquote_plus(os.path.basename(url))
         if "|" in self._message.input_str:
             file_name = self._message.input_str.split("|")[1].strip()
         dl_loc = os.path.join(Config.DOWN_PATH, file_name)
         try:
             downloader = SmartDL(url, dl_loc, progress_bar=False)
             downloader.start(blocking=False)
             count = 0
             while not downloader.isFinished():
                 if self._message.process_is_canceled:
                     downloader.stop()
                     raise Exception('Process Canceled!')
                 total_length = downloader.filesize if downloader.filesize else 0
                 downloaded = downloader.get_dl_size()
                 percentage = downloader.get_progress() * 100
                 speed = downloader.get_speed(human=True)
                 estimated_total_time = downloader.get_eta(human=True)
                 progress_str = \
                     "__{}__\n" + \
                     "```[{}{}]```\n" + \
                     "**Progress** : `{}%`\n" + \
                     "**URL** : `{}`\n" + \
                     "**FILENAME** : `{}`\n" + \
                     "**Completed** : `{}`\n" + \
                     "**Total** : `{}`\n" + \
                     "**Speed** : `{}`\n" + \
                     "**ETA** : `{}`"
                 progress_str = progress_str.format(
                     "trying to download",
                     ''.join((Config.FINISHED_PROGRESS_STR
                              for i in range(math.floor(percentage / 5)))),
                     ''.join((Config.UNFINISHED_PROGRESS_STR
                              for i in range(20 - math.floor(percentage / 5)))),
                     round(percentage, 2),
                     url,
                     file_name,
                     humanbytes(downloaded),
                     humanbytes(total_length),
                     speed,
                     estimated_total_time)
                 count += 1
                 if count >= 5:
                     count = 0
                     await self._message.try_to_edit(
                         progress_str, disable_web_page_preview=True)
                 await asyncio.sleep(1)
         except Exception as d_e:
             await self._message.err(d_e)
             return
     file_path = dl_loc if dl_loc else self._message.input_str
     if not os.path.exists(file_path):
         await self._message.err("invalid file path provided?")
         return
     if "|" in file_path:
         file_path, file_name = file_path.split("|")
         new_path = os.path.join(os.path.dirname(file_path.strip()), file_name.strip())
         os.rename(file_path.strip(), new_path)
         file_path = new_path
     await self._message.edit("`Loading GDrive Upload...`")
     pool.submit_thread(self._upload, file_path)
     start_t = datetime.now()
     count = 0
     while not self._is_finished:
         count += 1
         if self._message.process_is_canceled:
             self._cancel()
         if self._progress is not None and count >= 5:
             count = 0
             await self._message.try_to_edit(self._progress)
         await asyncio.sleep(1)
     if dl_loc and os.path.exists(dl_loc):
         os.remove(dl_loc)
     end_t = datetime.now()
     m_s = (end_t - start_t).seconds
     if isinstance(self._output, HttpError):
         out = f"**ERROR** : `{self._output._get_reason()}`"  # pylint: disable=protected-access
     elif self._output is not None and not self._is_canceled:
         out = f"**Uploaded Successfully** __in {m_s} seconds__\n\n{self._output}"
     elif self._output is not None and self._is_canceled:
         out = self._output
     else:
         out = "`failed to upload.. check logs?`"
     await self._message.edit(out, disable_web_page_preview=True, log=__name__)
Beispiel #9
0
async def eval_(message: Message):
    """ run python code """
    for t in tuple(_EVAL_TASKS):
        if t.done():
            del _EVAL_TASKS[t]

    flags = message.flags
    size = len(_EVAL_TASKS)
    if '-l' in flags:
        if _EVAL_TASKS:
            out = "**Eval Tasks**\n\n"
            i = 0
            for c in _EVAL_TASKS.values():
                out += f"**{i}** - `{c}`\n"
                i += 1
            out += f"\nuse `{Config.CMD_TRIGGER}eval -c[id]` to Cancel"
            await message.edit(out)
        else:
            await message.edit("No running eval tasks !", del_in=5)
        return
    if ('-c' in flags or '-ca' in flags) and size == 0:
        await message.edit("No running eval tasks !", del_in=5)
        return
    if '-ca' in flags:
        for t in _EVAL_TASKS:
            t.cancel()
        await message.edit(f"Canceled all running eval tasks [{size}] !",
                           del_in=5)
        return
    if '-c' in flags:
        t_id = int(flags.get('-c', -1))
        if t_id < 0 or t_id >= size:
            await message.edit(f"Invalid eval task id [{t_id}] !", del_in=5)
            return
        list(_EVAL_TASKS)[t_id].cancel()
        await message.edit(f"Canceled eval task [{t_id}] !", del_in=5)
        return

    cmd = await init_func(message)
    if cmd is None:
        return

    _flags = []
    for _ in range(3):
        _found = False
        for f in ('-s', '-p', '-n'):
            if cmd.startswith(f):
                _found = True
                _flags.append(f)
                cmd = cmd[len(f):].strip()
                if not cmd:
                    break
        if not _found or not cmd:
            break

    if not cmd:
        await message.err("Unable to Parse Input!")
        return

    silent_mode = '-s' in _flags
    if '-n' in _flags:
        context_type = _ContextType.NEW
    elif '-p' in _flags:
        context_type = _ContextType.PRIVATE
    else:
        context_type = _ContextType.GLOBAL

    async def _callback(output: Optional[str], errored: bool):
        final = ""
        if not silent_mode:
            final += f"**>** ```{cmd}```\n\n"
        if isinstance(output, str):
            output = output.strip()
            if output == '':
                output = None
        if output is not None:
            final += f"**>>** ```{output}```"
        if errored and message.chat.type in ("group", "supergroup", "channel"):
            msg_id = await CHANNEL.log(final)
            await msg.edit(f"**Logs**: {CHANNEL.get_link(msg_id)}")
        elif final:
            await msg.edit_or_send_as_file(text=final,
                                           parse_mode='md',
                                           filename="eval.txt",
                                           caption=cmd)
        else:
            await msg.delete()

    msg = message
    replied = message.reply_to_message
    if (replied and replied.from_user and replied.from_user.is_self
            and isinstance(replied.text, Str)
            and str(replied.text.html).startswith("<b>></b> <pre>")):
        msg = replied

    await msg.edit("`Executing eval ...`", parse_mode='md')

    _g, _l = _context(context_type,
                      userge=userge,
                      message=message,
                      replied=message.reply_to_message)
    l_d = {}
    try:
        exec(_wrap_code(cmd, _l.keys()), _g, l_d)  # nosec pylint: disable=W0122
    except Exception:  # pylint: disable=broad-except
        _g[_KEY] = _l
        await _callback(traceback.format_exc(), True)
        return
    future = asyncio.get_event_loop().create_future()
    pool.submit_thread(_run_coro, future, l_d['__aexec'](*_l.values()),
                       _callback)
    hint = cmd.split('\n')[0]
    _EVAL_TASKS[future] = hint[:25] + "..." if len(hint) > 25 else hint

    with msg.cancel_callback(future.cancel):
        try:
            await future
        except asyncio.CancelledError:
            await asyncio.gather(
                msg.canceled(),
                CHANNEL.log(f"**EVAL Process Canceled!**\n\n```{cmd}```"))
        finally:
            _EVAL_TASKS.pop(future, None)