def download(from_, config, delete_on_success): """Download all the latest messages that are files in a chatt, 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()) client.start() messages = client.find_files(from_) client.download_files(from_, messages, delete_on_success)
class TestClient(unittest.TestCase): @patch('builtins.open', mock_open(read_data=json.dumps(CONFIG_DATA))) @patch('telegram_upload.client.TelegramClient.__init__') def setUp(self, m1) -> None: self.upload_file_path = os.path.abspath( os.path.join(directory, 'logo.png')) self.client = Client('foo.json') self.client.send_file = Mock() self.client.send_file.return_value.media.document.size = os.path.getsize( self.upload_file_path) def test_send_files(self): entity = 'foo' self.client.send_files(entity, [self.upload_file_path]) self.client.send_file.assert_called_once_with( entity, self.upload_file_path, thumb=None, file_size=None, caption=os.path.basename(self.upload_file_path).split('.')[0], force_document=False, progress_callback=AnyArg(), attributes=[], ) def test_send_files_data_loss(self): self.client.send_file.return_value.media.document.size = 200 with self.assertRaises(TelegramUploadDataLoss): self.client.send_files('foo', [self.upload_file_path]) def test_download_files(self): m = Mock() m.document.attributes = [DocumentAttributeFilename('download.png')] m.document.size = 0 self.client.download_files('foo', [m]) def test_no_space_error(self): m = Mock() m.document.attributes = [DocumentAttributeFilename('download.png')] m.document.size = 1000 with patch('telegram_upload.client.free_disk_usage', return_value=0), \ self.assertRaises(TelegramUploadNoSpaceError): self.client.download_files('foo', [m])
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)
def download(from_, config, delete_on_success): client = Client(config or default_config()) client.start() messages = client.find_files(from_) client.download_files(from_, messages, delete_on_success)