예제 #1
0
    def get_msg_media_path(self, msg):
        result = None
        if isinstance(msg.media, MessageMediaPhoto):
            result = path.join(
                self.directories['photos'],
                '{}{}'.format(msg.media.photo.id, get_extension(msg.media)))

        if isinstance(msg.media, MessageMediaDocument):
            media_type = None
            for attr in msg.media.document.attributes:
                if isinstance(attr, DocumentAttributeAnimated):
                    media_type = 'gifs'
                    break
                if isinstance(attr, DocumentAttributeAudio):
                    media_type = 'audios'
                    break
                if isinstance(attr, DocumentAttributeVideo):
                    media_type = 'videos'
                    break
                if isinstance(attr, DocumentAttributeSticker):
                    media_type = 'stickers'
                    break
                if isinstance(attr, DocumentAttributeFilename):
                    media_type = 'documents'
                    break
            if not media_type:
                return None

            result = path.join(
                self.directories[media_type],
                '{}{}'.format(msg.media.document.id, get_extension(msg.media)))
        if result:
            return path.abspath(result)
예제 #2
0
    def get_msg_media_path(self, msg):
        result = None
        if isinstance(msg.media, MessageMediaPhoto):
            result = path.join(self.directories['photos'], '{}{}'
                               .format(msg.media.photo.id, get_extension(msg.media)))

        if isinstance(msg.media, MessageMediaDocument):
            media_type = None
            for attr in msg.media.document.attributes:
                if isinstance(attr, DocumentAttributeAnimated):
                    media_type = 'gifs'
                    break
                if isinstance(attr, DocumentAttributeAudio):
                    media_type = 'audios'
                    break
                if isinstance(attr, DocumentAttributeVideo):
                    media_type = 'videos'
                    break
                if isinstance(attr, DocumentAttributeSticker):
                    media_type = 'stickers'
                    break
                if isinstance(attr, DocumentAttributeFilename):
                    media_type = 'documents'
                    break
            if not media_type:
                return None

            result = path.join(self.directories[media_type], '{}{}'
                               .format(msg.media.document.id, get_extension(msg.media)))
        if result:
            return path.abspath(result)
예제 #3
0
파일: mtproto.py 프로젝트: vitlav/tg4xmpp
    def generate_media_link(self, media):
        """
        Generates download link from media object
        :param media:
        :return:
        """
        if type(media) is MessageMediaPhoto:
            media_id = media.photo.id
        elif type(media) is MessageMediaDocument:
            media_id = media.document.id
        else:
            return None

        ext = get_extension(media)
        if ext == '.oga':
            ext = '.ogg'

        file_name = hashlib.new('sha256')
        file_name.update(str(media_id).encode('ascii'))
        file_name.update(str(os.urandom(2)).encode('ascii'))
        file_name = file_name.hexdigest() + ext

        link = self.xmpp_gate.config['media_web_link_prefix'] + file_name

        return {'name': file_name, 'link': link}
예제 #4
0
    def download_photo(self,
                       message_media_photo,
                       file_path,
                       add_extension=False,
                       progress_callback=None):
        """Downloads MessageMediaPhoto's largest size into the desired
           file_path, optionally finding its extension automatically
           The progress_callback should be a callback function which takes two parameters,
           uploaded size (in bytes) and total file size (in bytes).
           This will be called every time a part is downloaded"""

        # Determine the photo and its largest size
        photo = message_media_photo.photo
        largest_size = photo.sizes[-1]
        file_size = largest_size.size
        largest_size = largest_size.location

        if add_extension:
            file_path += get_extension(message_media_photo)

        # Download the media with the largest size input file location
        self.download_file_loc(InputFileLocation(
            volume_id=largest_size.volume_id,
            local_id=largest_size.local_id,
            secret=largest_size.secret),
                               file_path,
                               file_size=file_size,
                               progress_callback=progress_callback)
        return file_path
예제 #5
0
파일: mtproto.py 프로젝트: asdil12/tg4xmpp
    def generate_media_link(self, media):
        """
        Генерирует будующее имя и ссылку на скачиваемое медиа-вложения из сообщения
        :param media:
        :return:
        """
        if type(media) is MessageMediaPhoto:
            media_id = media.photo.id
        elif type(media) is MessageMediaDocument:
            media_id = media.document.id
        else:
            return None

        ext = get_extension(media)
        if ext == '.oga':
            ext = '.ogg'

        file_name = hashlib.new('sha256')
        file_name.update(str(media_id).encode('ascii'))
        file_name.update(str(os.urandom(2)).encode('ascii'))
        file_name = file_name.hexdigest() + ext

        link = self.xmpp_gate.config['media_web_link_prefix'] + file_name

        return {'name': file_name, 'link': link}
예제 #6
0
async def download(event: NewMessage.Event) -> None:
    """
    Download documents from Telegram.


    `{prefix}download` or `{prefix}dl` or **{prefix}download (path)**
    """
    name = NAME
    path = None
    match = event.matches[0].group(3)
    if match:
        path = pathlib.Path(match.strip())

    if not event.reply_to_msg_id:
        await event.answer("`Downloaded the void.`")
        return

    reply = await event.get_reply_message()
    if not (reply.document or reply.media):
        await event.answer("`There is no document/media in here to download!`")
        return

    for attr in getattr(reply.document, "attributes", []):
        if isinstance(attr, types.DocumentAttributeFilename):
            name = attr.file_name
    ext = get_extension(reply.document)
    if path and not path.suffix and ext:
        path = path.with_suffix(ext)
    if name == NAME:
        name += "_" + str(getattr(reply.document, "id", reply.id)) + ext

    if path and path.exists():
        if path.is_file():
            newname = str(path.stem) + "_OLD"
            path.rename(path.with_name(newname).with_suffix(path.suffix))
            file_name = path
        else:
            file_name = path / name
    elif path and not path.suffix and ext:
        file_name = downloads / path.with_suffix(ext)
    elif path:
        file_name = path
    else:
        file_name = downloads / name
    file_name.parent.mkdir(parents=True, exist_ok=True)

    prog = ProgressCallback(event,
                            filen=await _get_file_name(file_name, False))
    if reply.document:
        dl = io.FileIO(file_name.absolute(), "a")
        await client.fast_download_file(location=reply.document,
                                        out=dl,
                                        progress_callback=prog.dl_progress)
        dl.close()
    else:
        await reply.download_media(file=file_name.absolute(),
                                   progress_callback=prog.dl_progress)

    await event.answer(f"__Downloaded successfully!__\n"
                       f"**Path:** `{await _get_file_name(file_name)}`")
예제 #7
0
    def download_profile_photo(self,
                               profile_photo,
                               file_path,
                               add_extension=True,
                               download_big=True):
        """Downloads the profile photo for an user or a chat (including channels).
           Returns False if no photo was providen, or if it was Empty"""

        if (not profile_photo
                or isinstance(profile_photo, UserProfilePhotoEmpty)
                or isinstance(profile_photo, ChatPhotoEmpty)):
            return False

        if add_extension:
            file_path += get_extension(profile_photo)

        if download_big:
            photo_location = profile_photo.photo_big
        else:
            photo_location = profile_photo.photo_small

        # Download the media with the largest size input file location
        self.download_file_loc(
            InputFileLocation(volume_id=photo_location.volume_id,
                              local_id=photo_location.local_id,
                              secret=photo_location.secret), file_path)
        return True
예제 #8
0
    async def handle_new_telegram_message(self, event: NewMessage):
        msg: TgMsg = event.message
        chat_id = get_chat_id(msg.peer_id)
        chat = await self.async_get_chat(chat_id)
        self.logger.debug(msg)

        file = None
        path = None
        filename = None
        mime = None
        msg_type = MsgType.Text
        tempfile_suffix = ''
        if getattr(msg, 'media', None):
            media = msg.media
            tempfile_suffix = get_extension(media)
            if isinstance(media, MessageMediaPhoto):
                msg_type = MsgType.Image
            if isinstance(media, MessageMediaDocument):
                document = media.document
                mime = document.mime_type
                msg_type = MsgType.File
                for attr in document.attributes:
                    if isinstance(attr, DocumentAttributeFilename):
                        tempfile_suffix = attr.file_name
                        filename = attr.file_name
                    if isinstance(attr, DocumentAttributeSticker):
                        msg_type = MsgType.Sticker
                    if isinstance(attr, DocumentAttributeVideo):
                        msg_type = MsgType.Video
                    if isinstance(attr, DocumentAttributeAudio):
                        msg_type = MsgType.Audio
        if msg_type != MsgType.Text:
            file = tempfile.NamedTemporaryFile(suffix=tempfile_suffix)
            path = file.name
            await self.client.download_media(msg, file)

        msg_peer = await self.client.get_entity(
            get_chat_id(msg.from_id or msg.peer_id))
        self.logger.debug(msg_peer)

        chat_member = ChatMember(
            chat=chat,
            name=format_entity_name(msg_peer),
            uid=str(chat_id),
        )
        efb_msg = Message(
            deliver_to=coordinator.master,
            author=chat_member,
            chat=chat,
            text=msg.message,
            type=msg_type,
            uid=f'{chat_id}_{msg.id}',
            file=file,
            filename=filename,
            mime=mime,
            path=path,
        )
        coordinator.send_message(efb_msg)
예제 #9
0
async def paste_bin(event):
    "To paste text to a paste bin."
    catevent = await edit_or_reply(event, "`pasting text to paste bin....`")
    input_str = event.pattern_match.group(3)
    reply = await event.get_reply_message()
    ext = re.findall(r"-\w+", input_str)
    try:
        extension = ext[0].replace("-", "")
        input_str = input_str.replace(ext[0], "").strip()
    except IndexError:
        extension = None
    if event.pattern_match.group(2) == "neko":
        pastetype = "n"
    else:
        pastetype = event.pattern_match.group(1) or "p"
    text_to_print = ""
    if input_str:
        text_to_print = input_str
    if text_to_print == "" and reply.media:
        mediatype = media_type(reply)
        if mediatype == "Document":
            d_file_name = await event.client.download_media(
                reply, Config.TEMP_DIR)
            if extension is None:
                extension = get_extension(reply.document)
            with open(d_file_name, "r") as f:
                text_to_print = f.read()
    if text_to_print == "":
        if reply.text:
            text_to_print = reply.raw_text
        else:
            return await edit_delete(
                catevent,
                "`Either reply to text/code file or reply to text message or give text along with command`",
            )
    if extension and extension.startswith("."):
        extension = extension[1:]
    try:
        response = await pastetext(text_to_print, pastetype, extension)
        if "error" in response:
            return await edit_delete(
                catevent,
                f"**Error while pasting text:**\n`Unable to process your request may be pastebins are down.`",
            )
        result = ""
        if pastebins[response["bin"]] != pastetype:
            result += f"<b>{get_key(pastetype)} is down, So </b>"
        result += f"<b>Pasted to: <a href={response['url']}>{response['bin']}</a></b>"
        if response["raw"] != "":
            result += f"\n<b>Raw link: <a href={response['raw']}>Raw</a></b>"
        await catevent.edit(result, link_preview=False, parse_mode="html")
    except Exception as e:
        await edit_delete(catevent,
                          f"**Error while pasting text:**\n`{str(e)}`")
예제 #10
0
async def download(event: NewMessage.Event) -> None:
    """Download documents from Telegram."""
    name = NAME
    path = downloads
    match = event.matches[0].group(3)
    if match:
        path = pathlib.Path(match.strip())

    if not event.reply_to_msg_id:
        await event.answer("__Downloaded the void?__")
        return

    reply = await event.get_reply_message()
    if not (reply.document or reply.media):
        await event.answer("__There is no document to download.__")
        return

    for attr in getattr(reply.document, 'attributes', []):
        if isinstance(attr, types.DocumentAttributeFilename):
            name = attr.file_name
    ext = get_extension(reply.document)
    if name == NAME:
        name += ('_' + str(getattr(reply.document, 'id', reply.id)) + ext)

    if path.exists():
        if path.is_file():
            newname = str(path.stem) + '_OLD'
            path.rename(path.with_name(newname).with_suffix(path.suffix))
            file_name = path
        else:
            file_name = path / name
    elif not path.suffix and ext:
        file_name = downloads / path.with_suffix(ext)
    else:
        downloads.mkdir(parents=True, exist_ok=True)
        file_name = downloads / name

    prog = ProgressCallback(event, filen=file_name.stem)
    if reply.document:
        dl = io.FileIO(file_name.resolve(), 'a')
        await client.fast_download_file(location=reply.document,
                                        out=dl,
                                        progress_callback=prog.dl_progress)
        dl.close()
    else:
        await reply.download_media(file=file_name.resolve(),
                                   progress_callback=prog.dl_progress)

    await event.answer(f"__Successfully downloaded {file_name.stem}.__")
예제 #11
0
def get_media_name(message):
    media = message.media
    date = message.date
    group = message.grouped_id
    possible_names = []
    media_id = None
    if isinstance(media, types.MessageMediaWebPage):
        if isinstance(media.webpage, types.WebPage):
            media = media.webpage.document or media.webpage.photo
    if isinstance(media, types.MessageMediaDocument):
        media = media.document
    if isinstance(media, (types.MessageMediaPhoto, types.Photo)):
        kind = 'photo'
        extension = '.jpg'
        media_id = media.photo.id
    elif isinstance(media, types.MessageMediaContact):
        kind = 'contact'
        extension = '.vcard'
        possible_names = [f"{media.first_name}{extension}"]
    elif isinstance(media, (types.MessageMediaDocument, types.Document,
                            types.WebDocument, types.WebDocumentNoProxy)):
        kind, possible_names = DownloadMethods._get_kind_and_names(
            media.attributes)
        extension = utils.get_extension(media)
    elif isinstance(media, (types.MessageMediaGeoLive, types.Document)):
        return
    else:
        if media is None:
            logger.debug("Expired message is gone")
        else:
            logger.error(f"Unknow media type: {type(media)}")
        return

    name_tokens = []
    if possible_names:
        document_name = possible_names[0]
        file_name, extension = os.path.splitext(document_name)
        name_tokens.append(f"{file_name}-")
    else:
        name_tokens.append(f'{kind}_')
    name_tokens.append(f'{date.year:02}-{date.month:02}-{date.day:02}'
                       f'_{date.hour:02}-{date.minute:02}-{date.second:02}')
    if group:
        if media_id:
            name_tokens.append(f"_{media_id}")
        else:
            logger.warning(f"Got a group {group} but unknown media_id")
    name_tokens.append(extension)
    return "".join(name_tokens)
예제 #12
0
    def get_propic_name(self, entity, allow_multiple=False):
        """Gets the profile picture name for the given entity.
           If allow_multiple is given, a more unique ID will be given to the files (photo.photo_id)
           If allow_multiple is NOT given, a more generic ID will be given to the files (entity.id)"""
        if isinstance(entity, int):
            # Hope it is an user ID
            return '{}.jpg'.format(entity)

        if isinstance(entity, User):
            if not entity.photo:
                return None

            # TODO Perhaps a better way would be to keep all the versions,
            # and a symlink when downloading a new one
            file_id = str(entity.photo.photo_id if allow_multiple else entity.id)
            return '{}{}'.format(file_id, get_extension(entity.photo))
예제 #13
0
파일: paste.py 프로젝트: TheVaders/InVade
async def _(event):
    if event.fwd_from:
        return
    evnt = await eor(event, "`Pasting ....`")
    input_str = event.pattern_match.group(1)
    reply = await event.get_reply_message()
    ext = re.findall(r"-\w+", input_str)
    try:
        extension = ext[0].replace("-", "")
        input_str = input_str.replace(ext[0], "").strip()
    except IndexError:
        extension = None
    text_to_print = ""
    if input_str:
        text_to_print = input_str
    if text_to_print == "" and reply.media:
        mediatype = media_type(reply)
        if mediatype == "Document":
            d_file_name = await event.client.download_media(reply, Config.TEMP_DIR)
            if extension is None:
                extension = get_extension(reply.document)
            with open(d_file_name, "r") as f:
                text_to_print = f.read()
    if text_to_print == "":
        if reply.text:
            text_to_print = reply.raw_text
        else:
            return await eod(
                evnt,
                "`Reply to a file or msg or give a text to paste...`",
            )
    if extension and extension.startswith("."):
        extension = extension[1:]
    try:
        response = await pasty(text_to_print, extension)
        if "error" in response:
            return await eod(
                evnt,
                f"**Error While Pasting Text !!**",
            )
        result = f"<b>📍 Pasted To <a href={response['url']}>Here</a></b>"
        if response["raw"] != "":
            result += f"\n<b>📃 Raw link: <a href={response['raw']}>Raw</a></b>"
        await evnt.edit(result, link_preview=False, parse_mode="html")
    except Exception as e:
        await eod(evnt, f"**ERROR !!**\n\n`{str(e)}`")
예제 #14
0
파일: telecord.py 프로젝트: batzor/telecord
async def handler(update):
    #prints peer ids of channels
    result = await client.get_dialogs()
    print("\n List of channels:")
    for p in result:
        print(str(utils.resolve_id(p.id)[0]) + ": " + p.name)

    try:
        if str(update.message.to_id.channel_id) in channels:
            m = update.message
            media = m.media
            reply_msg = await m.get_reply_message()
            if reply_msg is None:
                text = "{}\n".format(m.message)
            else:
                text = "```REPLIED_TO:\n{}```{}\n".format(
                    reply_msg.message, m.message)

            if media is not None and utils.get_extension(media) == '.jpg':
                logger.info("Will download image")
                download_res = await client.download_media(
                    media, './downloads/')
                logger.info("Download done: {}".format(download_res))
                url = img_cli.upload_from_path(download_res, anon=True)['link']
                text += url + "\n"
                os.remove(download_res)
                logger.debug("File deleted")

            if not text == '':
                date = m.date.astimezone(timezone('Asia/Ulaanbaatar'))
                text += "`" + date.strftime("%Y-%m-%d %H:%M:%S GMT%Z") + "`\n"
                text += "==================================\n"
                msg = Webhook(channels[str(m.to_id.channel_id)], msg=text)
                msg.post()
                logger.info("Relaying Message from Channel ID: {}".format(
                    update.message.to_id.channel_id))
            else:
                logger.debug('Ignoring empty message: {}'.format(
                    update.message))
        else:
            logger.info("Ignoring Message from Channel: {}: {}".format(
                update.message.to_id.channel_id,
                update.message.to_id.channel_name))
    except:
        logger.debug('Exception occurred while handling:\n')
        logger.debug(update)
예제 #15
0
    def get_propic_name(self, entity, allow_multiple=False):
        """Gets the profile picture name for the given entity.
           If allow_multiple is given, a more unique ID will be given to the files (photo.photo_id)
           If allow_multiple is NOT given, a more generic ID will be given to the files (entity.id)"""
        if isinstance(entity, int):
            # Hope it is an user ID
            return '{}.jpg'.format(entity)

        if isinstance(entity, User):
            if not entity.photo:
                return None

            # TODO Perhaps a better way would be to keep all the versions,
            # and a symlink when downloading a new one
            file_id = str(
                entity.photo.photo_id if allow_multiple else entity.id)
            return '{}{}'.format(file_id, get_extension(entity.photo))
예제 #16
0
    def download_media(self, msg, target_id, entities):
        """
        Save media to disk using the self.media_fmt under OutputDirectory.

        The entities parameter must be a dictionary consisting of {id: entity}
        and it *has* to contain the IDs for sender_id and context_id.
        """
        media = msg.media
        if isinstance(media, types.MessageMediaPhoto):
            if isinstance(media.photo, types.PhotoEmpty):
                return None
        elif isinstance(media, types.MessageMediaDocument):
            if isinstance(media.document, types.DocumentEmpty):
                return None
        else:
            return None

        formatter = defaultdict(
            str,
            id=msg.id,
            context_id=target_id,
            sender_id=msg.from_id or 0,
            ext=utils.get_extension(media) or '.bin',
            type=self._get_media_type(media) or 'unknown',
            name=utils.get_display_name(entities[target_id]) or 'unknown',
            sender_name=utils.get_display_name(entities.get(msg.from_id))
            or 'unknown')
        filename = None
        if isinstance(media, types.MessageMediaDocument):
            for attr in media.document.attributes:
                if isinstance(attr, types.DocumentAttributeFilename):
                    filename = attr.file_name

        formatter['filename'] = filename or msg.date.strftime(
            '{}_%Y-%m-%d_%H-%M-%S'.format(formatter['type']))
        filename = msg.date.strftime(self.media_fmt).format_map(formatter)
        if not filename.endswith(formatter['ext']):
            if filename.endswith('.'):
                filename = filename[:-1]
            filename += formatter['ext']

        os.makedirs(os.path.dirname(filename), exist_ok=True)
        return self.client.download_media(media, file=filename)
예제 #17
0
    def download_document(self,
                          message_media_document,
                          file_path=None,
                          add_extension=True,
                          progress_callback=None):
        """Downloads the given MessageMediaDocument into the desired
           file_path, optionally finding its extension automatically.
           If no file_path is given, it will try to be guessed from the document
           The progress_callback should be a callback function which takes two parameters,
           uploaded size (in bytes) and total file size (in bytes).
           This will be called every time a part is downloaded"""
        document = message_media_document.document
        file_size = document.size

        # If no file path was given, try to guess it from the attributes
        if file_path is None:
            for attr in document.attributes:
                if type(attr) == DocumentAttributeFilename:
                    file_path = attr.file_name
                    break  # This attribute has higher preference

                elif type(attr) == DocumentAttributeAudio:
                    file_path = '{} - {}'.format(attr.performer, attr.title)

            if file_path is None:
                print('Could not determine a filename for the document')

        if add_extension:
            file_path += get_extension(message_media_document)

        self.download_file_loc(
            InputDocumentFileLocation(
                id=document.id,
                access_hash=document.access_hash,
                version=document.version),
            file_path,
            file_size=file_size,
            progress_callback=progress_callback)
        return file_path
예제 #18
0
             Config.TMP_DOWNLOAD_DIRECTORY,
             os.path.splitext(os.path.basename(path))[0],
         )
         with zipfile.ZipFile(path, "r") as zip_ref:
             zip_ref.extractall(destination)
         end = datetime.now()
         ms = (end - start).seconds
         await mone.edit(
             f"unzipped and stored to `{destination}` \n**Time Taken :** `{ms} seconds`"
         )
     else:
         await edit_delete(event, f"I can't find that path `{input_str}`", 10)
 elif event.reply_to_msg_id:
     start = datetime.now()
     reply = await event.get_reply_message()
     ext = get_extension(reply.document)
     if ext != ".zip":
         return await edit_delete(
             event,
             "`The replied file is not a zip file recheck the replied message`",
         )
     mone = await edit_or_reply(event, "`Unpacking....`")
     for attr in getattr(reply.document, "attributes", []):
         if isinstance(attr, types.DocumentAttributeFilename):
             filename = attr.file_name
     filename = os.path.join(Config.TMP_DOWNLOAD_DIRECTORY, filename)
     c_time = time.time()
     try:
         dl = io.FileIO(filename, "a")
         await event.client.fast_download_file(
             location=reply.document,
예제 #19
0
async def reverse(event: NewMessage.Event) -> None:
    """Reverse search supported media types on Google images."""
    reply = await event.get_reply_message()
    if reply and reply.media:
        ffmpeg = await is_ffmpeg_there()
        ext = get_extension(reply.media)
        if reply.gif:
            if not ffmpeg:
                await event.answer("`Install FFMPEG to reverse search GIFs.`")
                return
            ext = ".gif"
        if reply.video:
            if not ffmpeg:
                await event.answer("`Install FFMPEG to reverse search videos.`"
                                   )
                return
        acceptable = [".jpg", ".gif", ".png", ".bmp", ".tif", ".webp", ".mp4"]
        if ext not in acceptable:
            await event.answer("`Nice try, fool!`")
            return

        await event.answer("`Downloading media...`")
        if (reply.video or reply.gif) and ffmpeg:
            message = f"{event.chat_id}:{event.message.id}"
            await client.download_media(reply, "media.mp4")
            filters = "fps=10,scale=320:-1:flags=lanczos,"
            filters += "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"
            process = await asyncio.create_subprocess_shell(
                f'ffmpeg -i media.mp4 -t 25 -vf "{filters}" -loop 0 media.gif',
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE,
            )
            client.running_processes[message] = process
            await event.answer("`Converting the mp4 to a gif...`")
            await process.communicate()
            client.running_processes.pop(message)
            photo = io.BytesIO(io.open("media.gif", mode="rb").read())
            os.remove("media.mp4")
            os.remove("media.gif")
        else:
            photo = io.BytesIO()
            await client.download_media(reply, photo)
    else:
        await event.answer("`Reply to a photo or a non-animated sticker.`")
        return

    await event.answer("`Uploading media...`")
    response = await _run_sync(
        functools.partial(_post, f"media{ext}", photo.getvalue()))

    fetchUrl = response.headers["Location"]
    photo.close()

    if not response.ok:
        await event.answer("`Google said go away for a while.`")
        return

    await event.answer("`Parsing the results...`")
    match = await _scrape_url(fetchUrl + "&hl=en")
    if isinstance(match, urllib.error.HTTPError):
        await event.answer(f"`{match.code}: {match.reason}`")
        return
    guess = match["best_guess"]
    imgspage = match["similar_images"]
    matching_text = match["matching_text"]
    matching = match["matching"]

    if guess:
        text = f"**Possible related search:** [{guess}]({fetchUrl})"
        if imgspage:
            text += f"\n\n[Visually similar images]({imgspage})"
        if matching_text and matching:
            text += "\n\n**" + matching_text + ":**"
            for title, link in matching.items():
                text += f"\n-  [{title}]({link})"
        msg = await get_chat_link(event, event.id)
        extra = f"Successfully reversed media in {msg}: [{guess}]({fetchUrl})"
        await event.answer(text, log=("reverse", extra))
    else:
        await event.answer(f"[Couldn't find anything for you.]({fetchUrl})")
        return

    limit = event.matches[0].group(1)
    lim = int(limit) if limit else 2

    if imgspage:
        images, gifs = await _get_similar_links(imgspage, lim)
        if images:
            await event.answer(file=images, reply_to=event.message.id)
        if gifs:
            for gif in gifs:
                await event.answer(file=gif, reply_to=event.message.id)
예제 #20
0
    def run(self):
        os.makedirs("output/dumps", exist_ok=True)
        os.makedirs("output/usermedia", exist_ok=True)

        # Entities represent the user, chat or channel
        # corresponding to the dialog on the same index
        dialogs, entities = self.get_dialogs(limit=70000)

        dumps = {}
        minId = {}
        maxId = {}

        if os.path.exists("output/offsets.json"):
            with open("output/offsets.json") as outfile:
                maxIdJson = json.load(outfile)
                for key in maxIdJson:
                    maxId[int(key)] = maxIdJson[key]

        for entity in entities:
            minId[entity.id] = 2147483647

            if entity.id not in maxId:
                maxId[entity.id] = 0

            currentMaxId = maxId[entity.id]

            while True:
                try:
                    total_count, messages, senders = self.get_message_history(
                        entity,
                        limit=200,
                        offset_id=minId[entity.id],
                        min_id=maxId[entity.id])

                    if len(messages) == 0:
                        break

                    for msg, sender in zip(messages, senders):
                        if msg.id < minId[entity.id]:
                            minId[entity.id] = msg.id

                        if msg.id > currentMaxId:
                            currentMaxId = msg.id

                        senderName = "???"
                        if sender:
                            senderName = getattr(sender, 'first_name', None)
                            if not senderName:
                                senderName = getattr(sender, 'title', None)
                                if not senderName:
                                    senderName = str(sender.id)

                        content = ""
                        senderName = senderName.replace("/", "-")

                        # Format the message content
                        if getattr(msg, 'media', None):
                            if type(msg.media) == MessageMediaWebPage:
                                caption = getattr(msg.media, 'caption', '')
                                photo = getattr(msg.media.webpage, 'photo',
                                                None)
                                url = getattr(msg.media.webpage, 'url', '')
                                site_name = getattr(msg.media.webpage,
                                                    'site_name', '')
                                title = getattr(msg.media.webpage, 'title', '')
                                description = getattr(msg.media.webpage,
                                                      'description', '')
                                content = '[[web]:{}] {}'.format(
                                    {
                                        "url": url,
                                        "site_name": site_name,
                                        "title": title,
                                        "description": description
                                    }, caption)
                                if photo is not None:
                                    msg_media_id = int(msg.id)
                                    output = str(
                                        'output/usermedia/{}/{}'.format(
                                            senderName, msg_media_id)) + ".jpg"
                                    if not os.path.exists(output):
                                        print(
                                            'Downloading Web picture with name {}...'
                                            .format(output))
                                        output = self.download_photo(
                                            msg.media.webpage, output, False,
                                            self.download_progress_callback)
                                        print('Web picture downloaded to {}!'.
                                              format(output))
                                    else:
                                        print(
                                            'Web picture already downloaded to {}!'
                                            .format(output))
                            else:  #photo, #document, #contact
                                msg_media_id = int(msg.id)
                                # Let the output be the message ID
                                output = str('output/usermedia/{}/{}'.format(
                                    senderName, msg_media_id))
                                ext = get_extension(msg.media)
                                if ext is None:
                                    ext = ""
                                if not os.path.exists(output + ext):
                                    print('Downloading media with name {}...'.
                                          format(output))
                                    output = self.download_msg_media(
                                        msg.media,
                                        file_path=output,
                                        progress_callback=self.
                                        download_progress_callback)
                                    print('Media downloaded to {}!'.format(
                                        output))
                                else:
                                    print('Media already downloaded to {}!'.
                                          format(output))

                                #The media may or may not have a caption
                                caption = getattr(msg.media, 'caption', '')
                                content = '<{}> {}'.format(
                                    type(msg.media).__name__, caption)

                        elif hasattr(msg, 'message'):
                            content = msg.message
                        elif hasattr(msg, 'action'):
                            content = str(msg.action)
                        else:
                            # Unknown message, simply print its class name
                            content = type(msg).__name__

                        if senderName not in dumps:
                            dumps[senderName] = []
                            print("Added sender: " + senderName)

                        dump = json.dumps({
                            "date": str(msg.date),
                            "id": msg.id,
                            "content": content
                        })

                        dumps[senderName].append(dump)

                except ServerError:
                    time.sleep(2)
                except FloodWaitError as e:
                    print("Flood, waiting " + str(e.seconds) + " seconds.")
                    time.sleep(e.seconds)
                    self.reconnect()  # we most likely timedout..
                finally:
                    time.sleep(1)

            maxId[entity.id] = currentMaxId

        suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
        for key in dumps:
            with open("output/dumps/" + key + "_" + suffix + ".json",
                      'w') as outfile:
                json.dump(dumps[key], outfile)

        with open("output/offsets.json", "w") as outfile:
            json.dump(maxId, outfile)
예제 #21
0
async def rmbg(event: NewMessage.Event) -> None:
    """Remove the background from an image or sticker."""
    API_KEY = client.config['api_keys'].get('api_key_removebg', False)
    if not API_KEY:
        await event.answer("`You don't have an API key set for remove.bg!`")
        return

    match = event.matches[0].group(1)
    reply = await event.get_reply_message()

    if match and match != '':
        async with aiohttp.ClientSession() as session:
            try:
                async with session.get(match) as response:
                    if not (response.status == 200
                            and response.content_type.startswith('image/')):
                        await event.answer(
                            "`The provided link seems to be invalid.`")
                        return
            except aiohttp.client_exceptions.InvalidURL:
                await event.answer("`Invalid URL provided!`")
                return
            except Exception as e:
                exc = await client.get_traceback(e)
                await event.answer(f"**Unknown exception:**\n```{exc}```")
                return
        media = match
    elif reply and reply.media:
        ext = utils.get_extension(reply.media)
        acceptable = [".jpg", ".png", ".bmp", ".tif", ".webp"]
        if ext not in acceptable:
            await event.answer("`Nice try, fool!`")
            return

        await event.answer("`Downloading media...`")
        media = io.BytesIO()
        await client.download_media(reply, media)
        if ext in [".bmp", ".tif", ".webp"]:
            new_media = io.BytesIO()
            try:
                pilImg = PIL.Image.open(media)
            except OSError as e:
                await event.answer(f'`OSError: {e}`')
                return
            pilImg.save(new_media, format="PNG")
            pilImg.close()
            media.close()
            media = new_media
    else:
        await event.answer("`Reply to a photo or provide a valid link.`")
        return

    response = await client.loop.run_in_executor(
        None, functools.partial(removebg_post, API_KEY, media.getvalue()))
    if not isinstance(media, str):
        media.close()
    if response.status_code == 200:
        await event.delete()
        image = io.BytesIO(response.content)
        image.name = "image.png"
        await event.answer(file=image, force_document=True, reply=True)
        image.close()
    else:
        error = response.json()['errors'][0]
        code = error.get('code', False)
        title = error.get('title', 'No title?')
        body = code + ': ' + title if code else title
        text = f"`[{response.status_code}] {body}`"
        await event.answer(text)
예제 #22
0
async def handler(update):
    global temp_completed_path
    global FOLDER_GROUP
    try:

        real_id = get_peer_id(update.message.peer_id)
        CID, peer_type = resolve_id(real_id)

        if update.message.from_id is not None:
            logger.info(
                "USER ON GROUP => U:[%s]G:[%s]M:[%s]" %
                (update.message.from_id.user_id, CID, update.message.message))

        if update.message.media is not None and (not AUTHORIZED_USER
                                                 or CID in usuarios):
            if FOLDER_GROUP != update.message.date:
                logger.info(
                    "FOLDER_GROUP => [%s][%s][%s]" %
                    (FOLDER_GROUP, update.message.date, temp_completed_path))
                temp_completed_path = ''

        if update.message.media is not None and (not AUTHORIZED_USER
                                                 or CID in usuarios):
            file_name = 'NONAME'

            if isinstance(update.message.media, types.MessageMediaPhoto):
                file_name = '{}{}'.format(update.message.media.photo.id,
                                          get_extension(update.message.media))
                logger.info("MessageMediaPhoto  [%s]" % file_name)
            elif any(x in update.message.message for x in youtube_list):
                file_name = 'YOUTUBE VIDEO'
            else:
                attributes = update.message.media.document.attributes
                for attr in attributes:
                    if isinstance(attr, types.DocumentAttributeFilename):
                        file_name = attr.file_name
                    elif update.message.message:
                        file_name = re.sub(r'[^A-Za-z0-9 -!\[\]\(\)]+', ' ',
                                           update.message.message)

            mensaje = 'DOWNLOAD IN QUEUE [%s] [%s] => [%s]' % (time.strftime(
                '%d/%m/%Y %H:%M:%S',
                time.localtime()), file_name, temp_completed_path)
            logger.info(mensaje)
            message = await update.reply('Download in queue...')
            await queue.put([update, message, temp_completed_path])
        elif not AUTHORIZED_USER or CID in usuarios:
            if update.message.message == '/help':
                message = await update.reply(HELP)
                await queue.put([update, message])
            elif update.message.message == '/version':
                message = await update.reply(VERSION)
                await queue.put([update, message, temp_completed_path])
            elif update.message.message == '/alive':
                message = await update.reply('Keep-Alive')
                await queue.put([update, message, temp_completed_path])
            elif update.message.message == '/me' or update.message.message == '/id':
                message = await update.reply('id: {}'.format(CID))
                await queue.put([update, message, temp_completed_path])
                logger.info('me :[%s]' % (CID))
            else:
                time.sleep(2)
                if '/folder' in update.message.message:
                    folder = update.message.message
                    FOLDER_GROUP = update.message.date
                    temp_completed_path = os.path.join(
                        TG_DOWNLOAD_PATH, 'completed',
                        folder.replace('/folder ', '')
                    )  # SI VIENE EL TEXTO '/folder NAME_FOLDER' ESTE CREARÁ UNA CARPETA Y METERÁ ADENTRO TODOS LOS ARCHIVOS A CONTINUACION
                    logger.info("DOWNLOAD FILE IN :[%s]", temp_completed_path)
                elif ((update.message.message).startswith('/sendfiles')):
                    msg = await update.reply('Sending files...')
                    create_directory(os.path.join(download_path, 'sendFiles'))
                    ignored = {"*._process"}
                    basepath = os.path.join(download_path, 'sendFiles')
                    sending = 0
                    for root, subFolder, files in os.walk(basepath):
                        subFolder.sort()
                        files.sort()
                        for item in files:
                            if item.endswith('_process'):
                                #skip directories
                                continue
                            sending += 1
                            fileNamePath = str(os.path.join(root, item))
                            logger.info("SEND FILE :[%s]", fileNamePath)
                            await msg.edit('Sending {}...'.format(item))
                            loop = asyncio.get_event_loop()
                            task = loop.create_task(
                                tg_send_file(CID, fileNamePath, item))
                            download_result = await asyncio.wait_for(
                                task, timeout=maximum_seconds_per_download)
                            #message = await tg_send_file(fileNamePath)
                            shutil.move(fileNamePath,
                                        fileNamePath + "_process")
                    await msg.edit('{} files submitted'.format(sending))
                    logger.info("FILES SUBMITTED:[%s]", sending)
                elif ((update.message.message).startswith('#')):
                    folder = update.message.message
                    FOLDER_GROUP = update.message.date
                    temp_completed_path = os.path.join(
                        TG_DOWNLOAD_PATH, 'completed', folder.replace('#', '')
                    )  # SI VIENE EL TEXTO '/folder NAME_FOLDER' ESTE CREARÁ UNA CARPETA Y METERÁ ADENTRO TODOS LOS ARCHIVOS A CONTINUACION
                    logger.info("DOWNLOAD FILE IN :[%s]", temp_completed_path)

        elif update.message.message == '/me' or update.message.message == '/id':
            logger.info('UNAUTHORIZED USER: %s ', CID)
            message = await update.reply(
                'UNAUTHORIZED USER: %s \n add this ID to TG_AUTHORIZED_USER_ID'
                % CID)
    except Exception as e:
        message = await update.reply('ERROR: ' + str(e))
        logger.info('EXCEPTION USER: %s ', str(e))
예제 #23
0
async def worker(name):
    while True:
        # Esperando una unidad de trabajo.

        queue_item = await queue.get()
        update = queue_item[0]
        message = queue_item[1]
        FOLDER_TO_GROUP = queue_item[2] if queue_item[2] else ''

        real_id = get_peer_id(update.message.peer_id)
        CID, peer_type = resolve_id(real_id)
        sender = await update.get_sender()
        username = sender.username

        # Comprobación de usuario
        if AUTHORIZED_USER and CID not in usuarios:
            logger.info('USUARIO: %s NO AUTORIZADO', CID)
            continue
        ###
        file_path = tmp_path
        file_name = 'FILENAME'
        if isinstance(update.message.media, types.MessageMediaPhoto):
            file_name = '{}{}'.format(update.message.media.photo.id,
                                      get_extension(update.message.media))
        elif any(x in update.message.message for x in youtube_list):
            try:
                url = update.message.message

                logger.info(f'INIT DOWNLOADING VIDEO YOUTUBE [{url}] ')
                await youtube_download(url, update, message)
                logger.info(f'FINIT DOWNLOADING VIDEO YOUTUBE [{url}] ')
                queue.task_done()
                continue
            except Exception as e:
                logger.info('ERROR: %s DOWNLOADING YT: %s' %
                            (e.__class__.__name__, str(e)))
                await message.edit('Error!')
                message = await message.edit('ERROR: %s DOWNLOADING YT: %s' %
                                             (e.__class__.__name__, str(e)))
                queue.task_done()
                continue
        else:
            attributes = update.message.media.document.attributes
            for attr in attributes:
                if isinstance(attr, types.DocumentAttributeFilename):
                    file_name = attr.file_name
                elif update.message.message:
                    file_name = re.sub(r'[^A-Za-z0-9 -!\[\]\(\)]+', ' ',
                                       update.message.message)
                else:
                    file_name = time.strftime('%Y%m%d %H%M%S',
                                              time.localtime())
                    file_name = '{}{}'.format(
                        update.message.media.document.id,
                        get_extension(update.message.media))
        file_path = os.path.join(file_path, file_name)
        _download_path, _complete_path = getDownloadPath(file_name, CID)
        logger.info(
            f"getDownloadPath FILE [{file_name}] to [{_download_path}]")
        await message.edit(
            f'Downloading {file_name} \ndownload in:\n{_download_path}')
        #time.sleep(1)
        logger.info('Downloading... ')
        mensaje = 'STARTING DOWNLOADING %s [%s] BY [%s]' % (time.strftime(
            '%d/%m/%Y %H:%M:%S', time.localtime()), file_path, (CID))
        logger.info(mensaje)
        try:
            loop = asyncio.get_event_loop()
            if (TG_PROGRESS_DOWNLOAD == True
                    or TG_PROGRESS_DOWNLOAD == 'True'):
                task = loop.create_task(
                    client.download_media(update.message,
                                          file_path,
                                          progress_callback=lambda x, y:
                                          callback(x, y, file_path, file_name,
                                                   message, _download_path)))
            else:
                task = loop.create_task(
                    client.download_media(update.message, file_path))
            download_result = await asyncio.wait_for(
                task, timeout=maximum_seconds_per_download)
            end_time = time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())
            end_time_short = time.strftime('%H:%M', time.localtime())
            filename = os.path.split(download_result)[1]

            if FOLDER_TO_GROUP:
                final_path = os.path.join(FOLDER_TO_GROUP, filename)
                create_directory(FOLDER_TO_GROUP)
                os.chmod(FOLDER_TO_GROUP, 0o777)
            else:
                _path, final_path = getDownloadPath(filename, CID)
                create_directory(_path)
            ######
            logger.info("RENAME/MOVE [%s] [%s]" %
                        (download_result, final_path))
            #create_directory(completed_path)
            shutil.move(download_result, final_path)
            os.chmod(final_path, 0o666)
            if TG_UNZIP_TORRENTS:
                if zipfile.is_zipfile(final_path):
                    with zipfile.ZipFile(final_path, 'r') as zipObj:
                        for fileName in zipObj.namelist():
                            if fileName.endswith('.torrent'):
                                zipObj.extract(fileName, download_path_torrent)
                                logger.info("UNZIP TORRENTS [%s] to [%s]" %
                                            (fileName, download_path_torrent))

            ######
            mensaje = 'DOWNLOAD FINISHED %s [%s] => [%s]' % (
                end_time, file_name, final_path)
            logger.info(mensaje)
            await message.edit('Downloading finished:\n%s \nIN: %s\nat %s' %
                               (file_name, _path, end_time_short))
        except asyncio.TimeoutError:
            logger.info('[%s] Time exceeded %s' %
                        (file_name,
                         time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())))
            await message.edit('Error!')
            message = await update.reply(
                'ERROR: Time exceeded downloading this file')
        except Exception as e:
            logger.critical(e)
            logger.info('[EXCEPCION]: %s' % (str(e)))
            logger.info('[%s] Excepcion %s' %
                        (file_name,
                         time.strftime('%d/%m/%Y %H:%M:%S', time.localtime())))
            await message.edit('Error!')
            message = await update.reply('ERROR: %s downloading : %s' %
                                         (e.__class__.__name__, str(e)))

        # Unidad de trabajo terminada.
        queue.task_done()
예제 #24
0
client.start()
target_handler = client.get_entity(group)
client = Bot(client)
print("[*] Running...")
oldID = None
client.sendMsg(target_handler, "Hi, Clara Lille here!")
try:
    while 1:
        sleep(
            0.6
        )  # o sleep evita sobrecarga. Evitando Travamentos demorados n lembro porque isso ta aq
        messagesblock, oldID = client.getMsg(target_handler, oldID)
        if messagesblock:
            for msg in messagesblock:
                if msg.media:
                    if utils.get_extension(msg.media) in exts_perm:
                        filename = msg.media.document.attributes[0].file_name
                        if client.searchmedia(acervo, filename):
                            client.forwardfile(msg, acervo)
                            client.msg_reply(
                                msg, "[*] Upload Sucessfull to " + acervo +
                                ", by Clara Lille", target_handler)
                        else:
                            client.msg_reply(msg,
                                             "[*] File Exist, by Clara Lille",
                                             target_handler)
                else:
                    client.check_command(msg, target_handler, acervo)
except KeyboardInterrupt:
    client.sendMsg(target_handler, "i'll AFK, bye")
    client.dead()
예제 #25
0
async def zip_file(event):  # sourcery no-metrics
    "To unpack the zip file"
    input_str = event.pattern_match.group(1)
    if input_str:
        path = Path(input_str)
        if os.path.exists(path):
            start = datetime.now()
            if not zipfile.is_zipfile(path):
                return await edit_delete(
                    event,
                    f"`The Given path {str(path)} is not zip file to unpack`")
            mone = await edit_or_reply(event, "`Unpacking....`")
            destination = os.path.join(
                Config.TMP_DOWNLOAD_DIRECTORY,
                os.path.splitext(os.path.basename(path))[0],
            )
            with zipfile.ZipFile(path, "r") as zip_ref:
                zip_ref.extractall(destination)
            end = datetime.now()
            ms = (end - start).seconds
            await mone.edit(
                f"unzipped and stored to `{destination}` \n**Time Taken :** `{ms} seconds`"
            )
        else:
            await edit_delete(event, f"I can't find that path `{input_str}`",
                              10)
    elif event.reply_to_msg_id:
        start = datetime.now()
        reply = await event.get_reply_message()
        ext = get_extension(reply.document)
        if ext != ".zip":
            return await edit_delete(
                event,
                "`The replied file is not a zip file recheck the replied message`",
            )
        mone = await edit_or_reply(event, "`Unpacking....`")
        for attr in getattr(reply.document, "attributes", []):
            if isinstance(attr, types.DocumentAttributeFilename):
                filename = attr.file_name
        filename = os.path.join(Config.TMP_DOWNLOAD_DIRECTORY, filename)
        c_time = time.time()
        try:
            dl = io.FileIO(filename, "a")
            await event.client.fast_download_file(
                location=reply.document,
                out=dl,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, mone, c_time, "trying to download")),
            )
            dl.close()
        except Exception as e:
            return await edit_delete(mone, f"**Error:**\n__{str(e)}__")
        await mone.edit("`Download finished Unpacking now`")
        destination = os.path.join(
            Config.TMP_DOWNLOAD_DIRECTORY,
            os.path.splitext(os.path.basename(filename))[0],
        )
        with zipfile.ZipFile(filename, "r") as zip_ref:
            zip_ref.extractall(destination)
        end = datetime.now()
        ms = (end - start).seconds
        await mone.edit(
            f"unzipped and stored to `{destination}` \n**Time Taken :** `{ms} seconds`"
        )
        os.remove(filename)
    else:
        await edit_delete(
            mone,
            "`Either reply to the zipfile or provide path of zip file along with command`",
        )
예제 #26
0
    def _get_media_name(self, media, *parts):
        key = '/'.join(map(str, parts))
        name = str(uuid3(NAMESPACE_URL, key))
        ext = str(get_extension(media))

        return name + ext
예제 #27
0
async def _(event):  # sourcery no-metrics
    pwd = os.getcwd()
    input_str = event.pattern_match.group(3)
    if not input_str:
        return await edit_delete(
            event,
            "Where should i save this file. mention folder name",
            parse_mode=_format.parse_pre,
        )

    location = os.path.join(pwd, input_str)
    if not os.path.isdir(location):
        os.makedirs(location)
    reply = await event.get_reply_message()
    if not reply:
        return await edit_delete(
            event,
            "Reply to media file to download it to bot server",
            parse_mode=_format.parse_pre,
        )
    mone = await edit_or_reply(event,
                               "Downloading the file ...",
                               parse_mode=_format.parse_pre)
    start = datetime.now()
    for attr in getattr(reply.document, "attributes", []):
        if isinstance(attr, types.DocumentAttributeFilename):
            name = attr.file_name
    if input_str:
        path = pathlib.Path(os.path.join(location, input_str.strip()))
    else:
        path = pathlib.Path(os.path.join(location, name))
    ext = get_extension(reply.document)
    if path and not path.suffix and ext:
        path = path.with_suffix(ext)
    if name == NAME:
        name += "_" + str(getattr(reply.document, "id", reply.id)) + ext
    if path and path.exists():
        if path.is_file():
            newname = str(path.stem) + "_OLD"
            path.rename(path.with_name(newname).with_suffix(path.suffix))
            file_name = path
        else:
            file_name = path / name
    elif path and not path.suffix and ext:
        file_name = location / path.with_suffix(ext)
    elif path:
        file_name = path
    else:
        file_name = location / name
    file_name.parent.mkdir(parents=True, exist_ok=True)
    c_time = time.time()
    if (not reply.document and reply.photo and file_name and file_name.suffix
            or not reply.document and not reply.photo):
        await reply.download_media(
            file=file_name.absolute(),
            progress_callback=lambda d, t: asyncio.get_event_loop().
            create_task(progress(d, t, mone, c_time, "trying to download")),
        )
    elif not reply.document:
        file_name = await reply.download_media(
            file=location,
            progress_callback=lambda d, t: asyncio.get_event_loop().
            create_task(progress(d, t, mone, c_time, "trying to download")),
        )
    else:
        dl = io.FileIO(file_name.absolute(), "a")
        await event.client.fast_download_file(
            location=reply.document,
            out=dl,
            progress_callback=lambda d, t: asyncio.get_event_loop().
            create_task(progress(d, t, mone, c_time, "trying to download")),
        )
        dl.close()
    end = datetime.now()
    ms = (end - start).seconds
    await mone.edit(
        f"**•  Downloaded in {ms} seconds.**\n**•  Downloaded to :- **  `{os.path.relpath(file_name,os.getcwd())}`\n   "
    )
예제 #28
0
async def _(event):  # sourcery no-metrics
    "To download the replied telegram file"
    mone = await edit_or_reply(event, "`Downloading....`")
    input_str = event.pattern_match.group(3)
    name = NAME
    path = None
    if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
        os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
    reply = await event.get_reply_message()
    if reply:
        start = datetime.now()
        for attr in getattr(reply.document, "attributes", []):
            if isinstance(attr, types.DocumentAttributeFilename):
                name = attr.file_name
        if input_str:
            path = pathlib.Path(os.path.join(downloads, input_str.strip()))
        else:
            path = pathlib.Path(os.path.join(downloads, name))
        ext = get_extension(reply.document)
        if path and not path.suffix and ext:
            path = path.with_suffix(ext)
        if name == NAME:
            name += "_" + str(getattr(reply.document, "id", reply.id)) + ext
        if path and path.exists():
            if path.is_file():
                newname = str(path.stem) + "_OLD"
                path.rename(path.with_name(newname).with_suffix(path.suffix))
                file_name = path
            else:
                file_name = path / name
        elif path and not path.suffix and ext:
            file_name = downloads / path.with_suffix(ext)
        elif path:
            file_name = path
        else:
            file_name = downloads / name
        file_name.parent.mkdir(parents=True, exist_ok=True)
        c_time = time.time()
        if (not reply.document and reply.photo and file_name
                and file_name.suffix
                or not reply.document and not reply.photo):
            await reply.download_media(
                file=file_name.absolute(),
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, mone, c_time, "trying to download")),
            )
        elif not reply.document:
            file_name = await reply.download_media(
                file=downloads,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, mone, c_time, "trying to download")),
            )
        else:
            dl = io.FileIO(file_name.absolute(), "a")
            await event.client.fast_download_file(
                location=reply.document,
                out=dl,
                progress_callback=lambda d, t: asyncio.get_event_loop(
                ).create_task(
                    progress(d, t, mone, c_time, "trying to download")),
            )
            dl.close()
        end = datetime.now()
        ms = (end - start).seconds
        await mone.edit(
            f"**•  Downloaded in {ms} seconds.**\n**•  Downloaded to :- **  `{os.path.relpath(file_name,os.getcwd())}`\n   "
        )
    elif input_str:
        start = datetime.now()
        if "|" in input_str:
            url, file_name = input_str.split("|")
        else:
            url = input_str
            file_name = None
        url = url.strip()
        file_name = os.path.basename(
            url) if file_name is None else file_name.strip()
        downloaded_file_name = pathlib.Path(os.path.join(downloads, file_name))
        if not downloaded_file_name.suffix:
            ext = os.path.splitext(url)[1]
            downloaded_file_name = downloaded_file_name.with_suffix(ext)
        downloader = SmartDL(url,
                             str(downloaded_file_name),
                             progress_bar=False)
        downloader.start(blocking=False)
        c_time = time.time()
        count = 0
        oldmsg = ""
        while not downloader.isFinished():
            total_length = downloader.filesize or None
            downloaded = downloader.get_dl_size()
            now = time.time()
            now - c_time
            percentage = downloader.get_progress() * 100
            downloader.get_speed()
            progress_str = "`{0}{1} {2}`%".format(
                "".join("▰" for i in range(math.floor(percentage / 5))),
                "".join("▱" for i in range(20 - math.floor(percentage / 5))),
                round(percentage, 2),
            )
            estimated_total_time = downloader.get_eta(human=True)
            current_message = f"Downloading the file\
                                \n\n**URL : **`{url}`\
                                \n**File Name :** `{file_name}`\
                                \n{progress_str}\
                                \n`{humanbytes(downloaded)} of {humanbytes(total_length)}`\
                                \n**ETA : **`{estimated_total_time}`"

            count += 1
            if oldmsg != current_message:
                if count >= 0.5:
                    count = 0
                    await mone.edit(current_message)
                oldmsg = current_message
            await asyncio.sleep(1)
        end = datetime.now()
        ms = (end - start).seconds
        if downloader.isSuccessful():
            await mone.edit(
                f"**•  Downloaded in {ms} seconds.**\n**•  Downloaded file location :- ** `{os.path.relpath(downloaded_file_name,os.getcwd())}`"
            )
        else:
            await mone.edit("Incorrect URL\n {}".format(input_str))
    else:
        await mone.edit("`Reply to a message to download to my local server.`")