示例#1
0
    def test_show_checkbox_list(self, m):
        async def aiterator():
            iterator = iter([(x, x) for x in map(str, range(10))])
            for item in iterator:
                yield item

        async_to_sync(show_checkboxlist(aiterator()))
示例#2
0
    def test_empty(self, m):
        async def aiterator():
            for item in []:
                yield item

        async_to_sync(show_radiolist(aiterator()))
        m.assert_called_once()
示例#3
0
 def send_files_as_album(self,
                         entity,
                         files,
                         delete_on_success=False,
                         print_file_id=False,
                         forward=()):
     for files_group in grouper(ALBUM_FILES, files):
         media = self.send_files(entity,
                                 files_group,
                                 delete_on_success,
                                 print_file_id,
                                 forward,
                                 send_as_media=True)
         async_to_sync(self._send_album_media(entity, media))
def upload(files, to, config, delete_on_success, print_file_id, force_file,
           forward, directories, large_files, caption, no_thumbnail,
           thumbnail_file, proxy, album, interactive):
    """Upload one or more files to Telegram using your personal account.
    The maximum file size is 2 GiB and by default they will be saved in
    your saved messages.
    """
    client = Client(config or default_config(), proxy=proxy)
    client.start()
    if interactive and not files:
        click.echo('Select the local files to upload:')
        click.echo('[SPACE] Select file [ENTER] Next step')
        files = async_to_sync(interactive_select_local_files())
    if interactive and not files:
        # No files selected. Exiting.
        return
    if interactive and to is None:
        click.echo('Select the recipient dialog of the files:')
        click.echo('[SPACE] Select dialog [ENTER] Next step')
        to = async_to_sync(interactive_select_dialog(client))
    elif to is None:
        to = 'me'
    files = filter(
        lambda file: is_valid_file(
            file, lambda message: click.echo(message, err=True)), files)
    files = DIRECTORY_MODES[directories](files)
    if directories == 'fail':
        # Validate now
        files = list(files)
    if no_thumbnail:
        thumbnail = False
    elif thumbnail_file:
        thumbnail = thumbnail_file
    else:
        thumbnail = None
    files_cls = LARGE_FILE_MODES[large_files]
    files = files_cls(files,
                      caption=caption,
                      thumbnail=thumbnail,
                      force_file=force_file)
    if large_files == 'fail':
        # Validate now
        files = list(files)
    if album:
        client.send_files_as_album(to, files, delete_on_success, print_file_id,
                                   forward)
    else:
        client.send_files(to, files, delete_on_success, print_file_id, forward)
示例#5
0
    def send_files(self, entity, files: Iterable[File], delete_on_success=False, print_file_id=False,
                   forward=(), send_as_media: bool = False):
        has_files = False
        messages = []
        for file in files:
            has_files = True
            progress, bar = get_progress_bar('Uploading', file.file_name, file.file_size)

            thumb = file.get_thumbnail()
            try:
                try:
                    if send_as_media:
                        message = async_to_sync(self._send_media(entity, file, progress))
                    else:
                        message = self._send_file_message(entity, file, thumb, progress)
                    messages.append(message)
                finally:
                    bar.render_finish()
            finally:
                if thumb and not file.is_custom_thumbnail:
                    os.remove(thumb)
            if print_file_id:
                click.echo('Uploaded successfully "{}" (file_id {})'.format(file.file_name,
                                                                            pack_bot_file_id(message.media)))
            if delete_on_success:
                click.echo('Deleting "{}"'.format(file))
                os.remove(file.path)
            self.forward_to(message, forward)
        if not has_files:
            raise MissingFileError('Files do not exist.')
        return messages
def download(from_, config, delete_on_success, proxy, interactive):
    """Download all the latest messages that are files in a chat, by default download
    from "saved messages". It is recommended to forward the files to download to
    "saved messages" and use parameter ``--delete-on-success``. Forwarded messages will
    be removed from the chat after downloading, such as a download queue.
    """
    client = Client(config or default_config(), proxy=proxy)
    client.start()
    if not interactive and not from_:
        from_ = 'me'
    elif interactive and not from_:
        click.echo('Select the dialog of the files to download:')
        click.echo('[SPACE] Select dialog [ENTER] Next step')
        from_ = async_to_sync(interactive_select_dialog(client))
    if interactive:
        click.echo('Select all files to download:')
        click.echo('[SPACE] Select files [ENTER] Download selected files')
        messages = async_to_sync(interactive_select_files(client, from_))
    else:
        messages = client.find_files(from_)
    client.download_files(from_, messages, delete_on_success)