Exemplo n.º 1
0
 def _get(self, file_id: str) -> str:
     drive_file = self._service.files().get(
         fileId=file_id, fields='*', supportsTeamDrives=True).execute()
     drive_file['size'] = humanbytes(int(drive_file.get('size', 0)))
     drive_file['quotaBytesUsed'] = humanbytes(
         int(drive_file.get('quotaBytesUsed', 0)))
     drive_file = dumps(drive_file, sort_keys=True, indent=4)
     _LOG.info("Getting Google-Drive File Details => %s", drive_file)
     return drive_file
Exemplo n.º 2
0
async def combine_(message: Message) -> None:
    """ combine split files """
    file_path = message.input_str
    if not file_path:
        await message.err("missing file path!")
        return
    if not isfile(file_path):
        await message.err("file path not exists!")
        return
    _, ext = splitext(basename(file_path))
    if not ext.lstrip('.').isdigit():
        await message.err("unsupported file!")
        return
    await message.edit("`processing...`")
    start_t = datetime.now()
    c_obj = SCLib(file_path)
    c_obj.combine()
    tmp = \
        "__Combining file path...__\n" + \
        "```{}({}%)```\n" + \
        "**File Path** : `{}`\n" + \
        "**Dest** : `{}`\n" + \
        "**Completed** : `{}`\n" + \
        "**Total** : `{}`\n" + \
        "**Speed** : `{}/s`\n" + \
        "**ETA** : `{}`\n" + \
        "**Completed Files** : `{}/{}`"
    count = 0
    while not c_obj.finished:
        if message.process_is_canceled:
            c_obj.cancel()
        count += 1
        if count >= Config.EDIT_SLEEP_TIMEOUT:
            count = 0
            await message.try_to_edit(
                tmp.format(c_obj.progress, c_obj.percentage, file_path,
                           c_obj.final_file_path, humanbytes(c_obj.completed),
                           humanbytes(c_obj.total), humanbytes(c_obj.speed),
                           c_obj.eta, c_obj.completed_files,
                           c_obj.total_files))
        await sleep(1)
    if c_obj.output:
        await message.err(c_obj.output)
    else:
        end_t = datetime.now()
        m_s = (end_t - start_t).seconds
        await message.edit(
            f"**combined** `{file_path}` into `{c_obj.final_file_path}` "
            f"in `{m_s}` seconds.",
            log=__name__)
Exemplo n.º 3
0
async def url_download(message: Message, url: str) -> Tuple[str, int]:
    """ download from link """
    await message.edit("`Downloading From URL...`")
    start_t = datetime.now()
    custom_file_name = unquote_plus(os.path.basename(url))
    if "|" in url:
        url, c_file_name = url.split("|", maxsplit=1)
        url = url.strip()
        if c_file_name:
            custom_file_name = c_file_name.strip()
    dl_loc = os.path.join(Config.DOWN_PATH, custom_file_name)
    downloader = SmartDL(url, dl_loc, progress_bar=False)
    downloader.start(blocking=False)
    count = 0
    while not downloader.isFinished():
        if message.process_is_canceled:
            downloader.stop()
            raise ProcessCanceled
        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 _ in range(math.floor(percentage / 5)))), ''.join(
                     (Config.UNFINISHED_PROGRESS_STR
                      for _ in range(20 - math.floor(percentage / 5)))),
            round(percentage, 2),
            url, custom_file_name, humanbytes(downloaded),
            humanbytes(total_length), speed, estimated_total_time)
        count += 1
        if count >= Config.EDIT_SLEEP_TIMEOUT:
            count = 0
            await message.try_to_edit(progress_str,
                                      disable_web_page_preview=True)
        await asyncio.sleep(1)
    return dl_loc, (datetime.now() - start_t).seconds
Exemplo n.º 4
0
 def __progress(data: dict):
     nonlocal edited, c_time
     diff = time() - c_time
     if data['status'] == "downloading" and (
             not edited or diff >= Config.EDIT_SLEEP_TIMEOUT):
         c_time = time()
         edited = True
         eta = data.get('eta')
         speed = data.get('speed')
         if not (eta and speed):
             return
         out = "**Speed** >> {}/s\n**ETA** >> {}\n".format(
             humanbytes(speed), time_formatter(eta))
         out += f'**File Name** >> `{data["filename"]}`\n\n'
         current = data.get('downloaded_bytes')
         total = data.get("total_bytes")
         if current and total:
             percentage = int(current) * 100 / int(total)
             out += f"Progress >> {int(percentage)}%\n"
             out += "[{}{}]".format(
                 ''.join((Config.FINISHED_PROGRESS_STR
                          for _ in range(floor(percentage / 5)))), ''.join(
                              (Config.UNFINISHED_PROGRESS_STR
                               for _ in range(20 - floor(percentage / 5)))))
         xdecha.loop.create_task(message.edit(out))
Exemplo n.º 5
0
async def split_(message: Message) -> None:
    """ split files """
    split_size = int(message.matches[0].group(1))
    file_path = str(message.matches[0].group(2))
    if not file_path:
        await message.err("missing file path!")
        return
    if not isfile(file_path):
        await message.err("file path not exists!")
        return
    await message.edit("`processing...`")
    start_t = datetime.now()
    s_obj = SCLib(file_path)
    s_obj.split(split_size)
    tmp = \
        "__Splitting file path...__\n" + \
        "```{}({}%)```\n" + \
        "**File Path** : `{}`\n" + \
        "**Dest** : `{}`\n" + \
        "**Completed** : `{}`\n" + \
        "**Total** : `{}`\n" + \
        "**Speed** : `{}/s`\n" + \
        "**ETA** : `{}`\n" + \
        "**Completed Files** : `{}/{}`"
    count = 0
    while not s_obj.finished:
        if message.process_is_canceled:
            s_obj.cancel()
        count += 1
        if count >= Config.EDIT_SLEEP_TIMEOUT:
            count = 0
            await message.try_to_edit(
                tmp.format(s_obj.progress, s_obj.percentage, file_path,
                           s_obj.final_file_path, humanbytes(s_obj.completed),
                           humanbytes(s_obj.total), humanbytes(s_obj.speed),
                           s_obj.eta, s_obj.completed_files,
                           s_obj.total_files))
        await sleep(1)
    if s_obj.output:
        await message.err(s_obj.output)
    else:
        end_t = datetime.now()
        m_s = (end_t - start_t).seconds
        await message.edit(
            f"**split** `{file_path}` into `{s_obj.final_file_path}` "
            f"in `{m_s}` seconds.",
            log=__name__)
Exemplo n.º 6
0
async def audio_upload(message: Message, path, del_path: bool = False,
                       extra: str = '', with_thumb: bool = True):
    title = None
    artist = None
    thumb = None
    duration = 0
    str_path = str(path)
    file_size = humanbytes(os.stat(str_path).st_size)
    if with_thumb:
        try:
            album_art = stagger.read_tag(str_path)
            if album_art.picture and not os.path.lexists(Config.THUMB_PATH):
                bytes_pic_data = album_art[stagger.id3.APIC][0].data
                bytes_io = io.BytesIO(bytes_pic_data)
                image_file = Image.open(bytes_io)
                image_file.save("album_cover.jpg", "JPEG")
                thumb = "album_cover.jpg"
        except stagger.errors.NoTagError:
            pass
        if not thumb:
            thumb = await get_thumb(str_path)
    metadata = extractMetadata(createParser(str_path))
    if metadata and metadata.has("title"):
        title = metadata.get("title")
    if metadata and metadata.has("artist"):
        artist = metadata.get("artist")
    if metadata and metadata.has("duration"):
        duration = metadata.get("duration").seconds
    sent: Message = await message.client.send_message(
        message.chat.id, f"`Uploading {str_path} as audio ... {extra}`")
    start_t = datetime.now()
    await message.client.send_chat_action(message.chat.id, "upload_audio")
    try:
        msg = await message.client.send_audio(
            chat_id=message.chat.id,
            audio=str_path,
            thumb=thumb,
            caption=f"{path.name} [ {file_size} ]",
            title=title,
            performer=artist,
            duration=duration,
            parse_mode="html",
            disable_notification=True,
            progress=progress,
            progress_args=(message, f"uploading {extra}", str_path)
        )
    except ValueError as e_e:
        await sent.edit(f"Skipping `{str_path}` due to {e_e}")
    except Exception as u_e:
        await sent.edit(str(u_e))
        raise u_e
    else:
        await sent.delete()
        await finalize(message, msg, start_t)
        if os.path.exists(str_path) and del_path:
            os.remove(str_path)
    finally:
        if os.path.lexists("album_cover.jpg"):
            os.remove("album_cover.jpg")
Exemplo n.º 7
0
 def _search(self,
             search_query: str,
             flags: list,
             parent_id: str = "",
             list_root: bool = False) -> str:
     force = '-f' in flags
     pid = parent_id or self._parent_id
     if pid and not force:
         query = f"'{pid}' in parents and (name contains '{search_query}')"
     else:
         query = f"name contains '{search_query}'"
     page_token = None
     limit = int(flags.get('-l', 20))
     page_size = limit if limit < 50 else 50
     fields = 'nextPageToken, files(id, name, mimeType, size)'
     results = []
     msg = ""
     while True:
         response = self._service.files().list(
             supportsTeamDrives=True,
             includeTeamDriveItems=True,
             q=query,
             spaces='drive',
             corpora='allDrives',
             fields=fields,
             pageSize=page_size,
             orderBy='modifiedTime desc',
             pageToken=page_token).execute()
         for file_ in response.get('files', []):
             if len(results) >= limit:
                 break
             if file_.get('mimeType') == G_DRIVE_DIR_MIME_TYPE:
                 msg += G_DRIVE_FOLDER_LINK.format(file_.get('id'),
                                                   file_.get('name'))
             else:
                 msg += G_DRIVE_FILE_LINK.format(
                     file_.get('id'), file_.get('name'),
                     humanbytes(int(file_.get('size', 0))))
             msg += '\n'
             results.append(file_)
         if len(results) >= limit:
             break
         page_token = response.get('nextPageToken', None)
         if page_token is None:
             break
     if not msg:
         return "`Not Found!`"
     if parent_id and not force:
         out = f"**List GDrive Folder** : `{parent_id}`\n"
     elif list_root and not force:
         out = f"**List GDrive Root Folder** : `{self._parent_id}`\n"
     else:
         out = f"**GDrive Search Query** : `{search_query}`\n"
     return out + f"**Limit** : `{limit}`\n\n__Results__ : \n\n" + msg
Exemplo n.º 8
0
 def _download_file(self, path: str, name: str, **kwargs) -> None:
     request = self._service.files().get_media(fileId=kwargs['id'],
                                               supportsTeamDrives=True)
     with io.FileIO(os.path.join(path, name), 'wb') as d_f:
         d_file_obj = MediaIoBaseDownload(d_f,
                                          request,
                                          chunksize=50 * 1024 * 1024)
         c_time = time.time()
         done = False
         while done is False:
             status, done = d_file_obj.next_chunk(num_retries=5)
             if self._is_canceled:
                 raise ProcessCanceled
             if status:
                 f_size = status.total_size
                 diff = time.time() - c_time
                 downloaded = status.resumable_progress
                 percentage = downloaded / f_size * 100
                 speed = round(downloaded / diff, 2)
                 eta = round((f_size - downloaded) / speed)
                 tmp = \
                     "__Downloading From GDrive...__\n" + \
                     "```[{}{}]({}%)```\n" + \
                     "**File Name** : `{}`\n" + \
                     "**File Size** : `{}`\n" + \
                     "**Downloaded** : `{}`\n" + \
                     "**Completed** : `{}/{}`\n" + \
                     "**Speed** : `{}/s`\n" + \
                     "**ETA** : `{}`"
                 self._progress = tmp.format(
                     "".join((Config.FINISHED_PROGRESS_STR
                              for _ in range(math.floor(percentage / 5)))),
                     "".join(
                         (Config.UNFINISHED_PROGRESS_STR
                          for _ in range(20 - math.floor(percentage / 5)))),
                     round(percentage, 2), name, humanbytes(f_size),
                     humanbytes(downloaded), self._completed, self._list,
                     humanbytes(speed), time_formatter(eta))
     self._completed += 1
     _LOG.info("Downloaded Google-Drive File => Name: %s ID: %s", name,
               kwargs['id'])
Exemplo n.º 9
0
 def _set_perms(self, file_id: str) -> str:
     self._set_permission(file_id)
     drive_file = self._service.files().get(
         fileId=file_id,
         supportsTeamDrives=True,
         fields="id, name, mimeType, size").execute()
     _LOG.info("Set Permission : for Google-Drive File : %s\n%s", file_id,
               drive_file)
     mime_type = drive_file['mimeType']
     file_name = drive_file['name']
     file_id = drive_file['id']
     if mime_type == G_DRIVE_DIR_MIME_TYPE:
         return G_DRIVE_FOLDER_LINK.format(file_id, file_name)
     file_size = humanbytes(int(drive_file.get('size', 0)))
     return G_DRIVE_FILE_LINK.format(file_id, file_name, file_size)
Exemplo n.º 10
0
 def _get_output(self, file_id: str) -> str:
     file_ = self._service.files().get(fileId=file_id,
                                       fields="id, name, size, mimeType",
                                       supportsTeamDrives=True).execute()
     file_id = file_.get('id')
     file_name = file_.get('name')
     file_size = humanbytes(int(file_.get('size', 0)))
     mime_type = file_.get('mimeType')
     if mime_type == G_DRIVE_DIR_MIME_TYPE:
         out = G_DRIVE_FOLDER_LINK.format(file_id, file_name)
     else:
         out = G_DRIVE_FILE_LINK.format(file_id, file_name, file_size)
     if Config.G_DRIVE_INDEX_LINK:
         link = os.path.join(Config.G_DRIVE_INDEX_LINK.rstrip('/'),
                             quote(self._get_file_path(file_id, file_name)))
         if mime_type == G_DRIVE_DIR_MIME_TYPE:
             link += '/'
         out += f"\n👥 __[Shareable Link]({link})__"
     return out
Exemplo n.º 11
0
def cm_ru(url: str) -> str:
    """cloud.mail.ru direct links generator
    Using https://github.com/JrMasterModelBuilder/cmrudl.py"""
    reply = ''
    try:
        link = re.findall(r'\bhttps?://.*cloud\.mail\.ru\S+', url)[0]
    except IndexError:
        reply = "`No cloud.mail.ru links found`\n"
        return reply
    command = f'bin/cmrudl -s {link}'
    result = popen(command).read()  # nosec
    result = result.splitlines()[-1]
    try:
        data = json.loads(result)
    except json.decoder.JSONDecodeError:
        reply += "`Error: Can't extract the link`\n"
        return reply
    dl_url = data['download']
    name = data['file_name']
    size = humanbytes(int(data['file_size']))
    reply += f'[{name} ({size})]({dl_url})\n'
    return reply
Exemplo n.º 12
0
 def _upload_file(self, file_path: str, parent_id: str) -> str:
     if self._is_canceled:
         raise ProcessCanceled
     mime_type = guess_type(file_path)[0] or "text/plain"
     file_name = os.path.basename(file_path)
     file_size = os.path.getsize(file_path)
     body = {
         "name": file_name,
         "mimeType": mime_type,
         "description": "Uploaded using xdecha"
     }
     if parent_id:
         body["parents"] = [parent_id]
     if file_size == 0:
         media_body = MediaFileUpload(file_path,
                                      mimetype=mime_type,
                                      resumable=False)
         u_file_obj = self._service.files().create(
             body=body, media_body=media_body,
             supportsTeamDrives=True).execute()
         file_id = u_file_obj.get("id")
     else:
         media_body = MediaFileUpload(file_path,
                                      mimetype=mime_type,
                                      chunksize=50 * 1024 * 1024,
                                      resumable=True)
         u_file_obj = self._service.files().create(body=body,
                                                   media_body=media_body,
                                                   supportsTeamDrives=True)
         c_time = time.time()
         response = None
         while response is None:
             status, response = u_file_obj.next_chunk(num_retries=5)
             if self._is_canceled:
                 raise ProcessCanceled
             if status:
                 f_size = status.total_size
                 diff = time.time() - c_time
                 uploaded = status.resumable_progress
                 percentage = uploaded / f_size * 100
                 speed = round(uploaded / diff, 2)
                 eta = round((f_size - uploaded) / speed)
                 tmp = \
                     "__Uploading to GDrive...__\n" + \
                     "```[{}{}]({}%)```\n" + \
                     "**File Name** : `{}`\n" + \
                     "**File Size** : `{}`\n" + \
                     "**Uploaded** : `{}`\n" + \
                     "**Completed** : `{}/{}`\n" + \
                     "**Speed** : `{}/s`\n" + \
                     "**ETA** : `{}`"
                 self._progress = tmp.format(
                     "".join((Config.FINISHED_PROGRESS_STR
                              for _ in range(math.floor(percentage / 5)))),
                     "".join(
                         (Config.UNFINISHED_PROGRESS_STR
                          for _ in range(20 - math.floor(percentage / 5)))),
                     round(percentage, 2), file_name, humanbytes(f_size),
                     humanbytes(uploaded), self._completed, self._list,
                     humanbytes(speed), time_formatter(eta))
         file_id = response.get("id")
     if not Config.G_DRIVE_IS_TD:
         self._set_permission(file_id)
     self._completed += 1
     _LOG.info("Created Google-Drive File => Name: %s ID: %s Size: %s",
               file_name, file_id, file_size)
     return file_id