telegram-download-daemon/telegram-download-daemon.py

207 lines
6.6 KiB
Python
Raw Normal View History

2020-04-23 02:35:13 +08:00
#!/usr/bin/env python3
# Telegram Download Daemon
# Author: Alfonso E.M. <alfonso@el-magnifico.org>
# You need to install telethon (and cryptg to speed up downloads)
from os import getenv, rename
import subprocess
import math
2020-04-23 02:35:13 +08:00
from sessionManager import getSession, saveSession
2020-04-23 02:35:13 +08:00
from telethon import TelegramClient, events
2020-11-07 23:04:50 +08:00
from telethon.tl.types import PeerChannel, DocumentAttributeFilename, DocumentAttributeVideo
2020-04-23 02:35:13 +08:00
import logging
2020-05-24 23:32:29 +08:00
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s]%(name)s:%(message)s',
level=logging.WARNING)
2020-04-23 02:35:13 +08:00
import multiprocessing
2020-04-24 00:32:42 +08:00
import argparse
import asyncio
2020-04-24 00:32:42 +08:00
2021-02-21 20:50:44 +08:00
TDD_VERSION="1.1"
2020-05-10 12:43:13 +08:00
TELEGRAM_DAEMON_API_ID = getenv("TELEGRAM_DAEMON_API_ID")
TELEGRAM_DAEMON_API_HASH = getenv("TELEGRAM_DAEMON_API_HASH")
TELEGRAM_DAEMON_CHANNEL = getenv("TELEGRAM_DAEMON_CHANNEL")
2020-04-24 00:32:42 +08:00
2020-05-10 12:43:13 +08:00
TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH")
2020-04-24 00:32:42 +08:00
2021-01-28 00:35:18 +08:00
TELEGRAM_DAEMON_DEST=getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads")
TELEGRAM_DAEMON_TEMP=getenv("TELEGRAM_DAEMON_TEMP", "")
2021-01-28 00:52:39 +08:00
TELEGRAM_DAEMON_TEMP_SUFFIX="tdd"
2021-01-28 00:35:18 +08:00
2020-05-24 23:32:29 +08:00
parser = argparse.ArgumentParser(
description="Script to download files from Telegram Channel.")
parser.add_argument(
"--api-id",
required=TELEGRAM_DAEMON_API_ID == None,
type=int,
default=TELEGRAM_DAEMON_API_ID,
help=
'api_id from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_ID env var)'
)
parser.add_argument(
"--api-hash",
required=TELEGRAM_DAEMON_API_HASH == None,
type=str,
default=TELEGRAM_DAEMON_API_HASH,
help=
'api_hash from https://core.telegram.org/api/obtaining_api_id (default is TELEGRAM_DAEMON_API_HASH env var)'
)
parser.add_argument(
"--dest",
type=str,
2021-01-28 00:35:18 +08:00
default=TELEGRAM_DAEMON_DEST,
help=
'Destination path for downloaded files (default is /telegram-downloads).')
parser.add_argument(
"--temp",
type=str,
2021-02-11 21:06:08 +08:00
default=TELEGRAM_DAEMON_TEMP,
2020-05-24 23:32:29 +08:00
help=
2021-01-28 00:35:18 +08:00
'Destination path for temporary files (default is using the same downloaded files directory).')
2020-05-24 23:32:29 +08:00
parser.add_argument(
"--channel",
required=TELEGRAM_DAEMON_CHANNEL == None,
type=int,
default=TELEGRAM_DAEMON_CHANNEL,
help=
'Channel id to download from it (default is TELEGRAM_DAEMON_CHANNEL env var'
)
2020-04-24 00:32:42 +08:00
args = parser.parse_args()
api_id = args.api_id
api_hash = args.api_hash
channel_id = args.channel
downloadFolder = args.dest
2021-01-28 00:52:39 +08:00
tempFolder = args.temp
worker_count = multiprocessing.cpu_count()
2020-04-24 00:32:42 +08:00
2021-02-11 21:06:08 +08:00
if not tempFolder:
tempFolder = downloadFolder
2020-04-23 02:35:13 +08:00
# Edit these lines:
2020-04-24 00:32:42 +08:00
proxy = None
2020-04-23 02:35:13 +08:00
2020-05-24 23:32:29 +08:00
# End of interesting parameters
async def sendHelloMessage(client, peerChannel):
entity = await client.get_entity(peerChannel)
2020-05-26 03:06:10 +08:00
print("Hi! Ready for your files!")
2021-02-21 20:50:44 +08:00
await client.send_message(entity, "Telegram Download Daemon "+TDD_VERSION)
2020-05-25 00:25:39 +08:00
await client.send_message(entity, "Hi! Ready for your files!")
2020-05-24 23:32:29 +08:00
2021-02-16 03:11:01 +08:00
async def log_reply(message, reply):
print(reply)
2021-02-16 03:11:01 +08:00
await message.edit(reply)
2021-02-09 02:28:54 +08:00
def getFilename(event: events.NewMessage.Event):
mediaFileName = "unknown"
2020-11-07 23:04:50 +08:00
for attribute in event.media.document.attributes:
if isinstance(attribute, DocumentAttributeFilename): return attribute.file_name
if isinstance(attribute, DocumentAttributeVideo): mediaFileName = event.original_update.message.message
return mediaFileName
2020-04-23 02:35:13 +08:00
in_progress={}
2021-02-16 03:46:21 +08:00
async def set_progress(filename, message, received, total):
if received >= total:
try: in_progress.pop(filename)
except: pass
return
percentage = math.trunc(received / total * 10000) / 100;
in_progress[filename] = f"{percentage} % ({received} / {total})"
2021-02-16 03:46:21 +08:00
if (int(percentage) % 5) == 0:
await log_reply(message, f"{percentage} % ({received} / {total})")
2020-05-24 23:32:29 +08:00
with TelegramClient(getSession(), api_id, api_hash,
proxy=proxy).start() as client:
2020-04-23 02:35:13 +08:00
saveSession(client.session)
2020-04-23 02:35:13 +08:00
queue = asyncio.Queue()
peerChannel = PeerChannel(channel_id)
2020-04-23 02:35:13 +08:00
@client.on(events.NewMessage())
async def handler(event):
2020-04-23 02:35:13 +08:00
if event.to_id != peerChannel:
return
2020-05-24 23:32:29 +08:00
print(event)
2020-05-24 23:32:29 +08:00
if not event.media and event.message:
command = event.message.message
command = command.lower()
output = "Unknown command"
if command == "list":
output = subprocess.run(["ls -l "+downloadFolder], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
2021-02-21 20:50:44 +08:00
elif command == "status":
try:
output = "".join([ f"{key}: {value}\n" for (key, value) in in_progress.items()])
2021-02-21 20:50:44 +08:00
if output:
output = "Active downloads:\n\n" + output
else:
output = "No active downloads"
except:
output = "Some error occured while checking the status. Retry."
2021-02-21 20:50:44 +08:00
elif command == "clean":
output = "Cleaning "+tempFolder+"\n"
output+=subprocess.run(["rm "+tempFolder+"/*."+TELEGRAM_DAEMON_TEMP_SUFFIX], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT,encoding="utf-8").stdout
2021-02-21 20:50:44 +08:00
else:
output = "Available commands: list, status, clean"
2021-02-16 03:46:21 +08:00
await log_reply(event, output)
if event.media:
filename=getFilename(event)
2021-02-16 03:11:01 +08:00
message=await event.reply(f"{filename} added to queue")
await queue.put([event, message])
async def worker():
while True:
2021-02-16 03:11:01 +08:00
element = await queue.get()
event=element[0]
message=element[1]
filename=getFilename(event)
2020-05-24 23:40:48 +08:00
await log_reply(
2021-02-16 03:11:01 +08:00
message,
2020-05-24 23:32:29 +08:00
f"Downloading file {filename} ({event.media.document.size} bytes)"
)
2020-04-23 02:35:13 +08:00
2021-02-16 03:46:21 +08:00
download_callback = lambda received, total: set_progress(filename, message, received, total)
2021-01-28 00:52:39 +08:00
await client.download_media(event.message, f"{tempFolder}/{filename}.{TELEGRAM_DAEMON_TEMP_SUFFIX}", progress_callback = download_callback)
2021-02-16 03:46:21 +08:00
set_progress(filename, message, 100, 100)
2021-01-28 00:52:39 +08:00
rename(f"{tempFolder}/{filename}.{TELEGRAM_DAEMON_TEMP_SUFFIX}", f"{downloadFolder}/{filename}")
2021-02-16 03:11:01 +08:00
await log_reply(message, f"{filename} ready")
queue.task_done()
async def start():
tasks = []
2020-11-07 22:12:17 +08:00
loop = asyncio.get_event_loop()
for i in range(worker_count):
2020-11-07 22:12:17 +08:00
task = loop.create_task(worker())
tasks.append(task)
await sendHelloMessage(client, peerChannel)
await client.run_until_disconnected()
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
2020-05-24 23:32:29 +08:00
client.loop.run_until_complete(start())