From 0617fc013fba5054e88665fd346caef3aca58702 Mon Sep 17 00:00:00 2001 From: Baruch Odem Date: Thu, 23 Apr 2020 20:12:34 +0300 Subject: [PATCH] Some fixes and refactor --- telegram-download-daemon.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/telegram-download-daemon.py b/telegram-download-daemon.py index 5cb9fa2..82b35fa 100644 --- a/telegram-download-daemon.py +++ b/telegram-download-daemon.py @@ -18,10 +18,10 @@ TELEGRAM_DEAMON_API_HASH = getenv("TELEGRAM_DEAMON_API_HASH") TELEGRAM_DEAMON_CHANNEL = getenv("TELEGRAM_DEAMON_CHANNEL") parser = argparse.ArgumentParser(description="Script to download files from Telegram Channel.") -parser.add_argument("--api-id", required=TELEGRAM_DEAMON_API_ID == None, type=str, default=TELEGRAM_DEAMON_API_ID, help='api_id from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DEAMON_API_ID env var)') +parser.add_argument("--api-id", required=TELEGRAM_DEAMON_API_ID == None, type=int, default=TELEGRAM_DEAMON_API_ID, help='api_id from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DEAMON_API_ID env var)') parser.add_argument("--api-hash", required=TELEGRAM_DEAMON_API_HASH == None, type=str, default=TELEGRAM_DEAMON_API_HASH, help='api_hash from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DEAMON_API_HASH env var)') parser.add_argument("--dest", type=str, default=getenv("TELEGRAM_DEAMON_DEST", "/telegram-downloads"), help='Destenation path for downloading files (default is /telegram-downloads).') -parser.add_argument("--channel", required=TELEGRAM_DEAMON_CHANNEL == None, type=str, default=TELEGRAM_DEAMON_CHANNEL, help='Channel id to download from it (default is TELEGRAM_DEAMON_CHANNEL env var') +parser.add_argument("--channel", required=TELEGRAM_DEAMON_CHANNEL == None, type=int, default=TELEGRAM_DEAMON_CHANNEL, help='Channel id to download from it (default is TELEGRAM_DEAMON_CHANNEL env var') args = parser.parse_args() api_id = args.api_id @@ -39,17 +39,22 @@ client = TelegramClient(session, api_id, api_hash, proxy=proxy).start() @client.on(events.NewMessage()) async def handler(event): + + async def log_respond(respond): + print(respond) + await event.respond(respond) + if event.to_id != PeerChannel(channel_id): return + print(event) + if event.media: filename=event.media.document.attributes[0].file_name - answer="Downloading file %s (%i bytes)" % (filename,event.media.document.size) - print(answer) - await event.respond(answer) + log_respond(f"Downloading file {filename} ({event.media.document.size} bytes)") - await client.download_media(event.message,downloadFolder) - await event.respond(filename+" ready") + await client.download_media(event.message, downloadFolder) + log_respond(f"{filename} ready") with client: