def download_files(self, entity, messages: Iterable[Message], delete_on_success: bool = False): messages = reversed(list(messages)) for message in messages: filename_attr = get_message_file_attribute(message) filename = filename_attr.file_name if filename_attr else 'Unknown' if message.document.size > free_disk_usage(): raise TelegramUploadNoSpaceError( 'There is no disk space to download "{}". Space required: {}'.format( filename, sizeof_fmt(message.document.size - free_disk_usage()) ) ) progress, bar = get_progress_bar('Downloading', filename, message.document.size) try: self.download_media(message, progress_callback=progress) finally: bar.render_finish() if delete_on_success: self.delete_messages(entity, [message])
def download_files(self, entity, messages: Iterable[Message], delete_on_success: bool = False): messages = reversed(list(messages)) for message in messages: filename_attr = next( filter(lambda x: isinstance(x, DocumentAttributeFilename), message.document.attributes), None) filename = filename_attr.file_name if filename_attr else 'Unknown' if message.document.size > free_disk_usage(): raise TelegramUploadNoSpaceError( 'There is no disk space to download "{}". Space required: {}' .format( filename, sizeof_fmt(message.document.size - free_disk_usage()))) progress = get_progress_bar('Downloading', filename, message.document.size) self.download_media(message, progress_callback=progress) if delete_on_success: self.delete_messages(entity, [message]) print()
def test_bytes(self): self.assertEqual(sizeof_fmt(1023), '1023.0B')
def test_exact_mebibytes(self): self.assertEqual(sizeof_fmt((1024**2) * 3), '3.0MiB')
def test_kibibytes(self): self.assertEqual(sizeof_fmt(2400), '2.3KiB')