Some fixes and refactor

This commit is contained in:
Baruch Odem 2020-04-23 20:12:34 +03:00
parent 6a1924776e
commit 0617fc013f

View file

@ -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: