forked from alfem/telegram-download-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-download-daemon.py
277 lines (222 loc) · 9.68 KB
/
telegram-download-daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
# Telegram Download Daemon
# Author: Alfonso E.M. <[email protected]>
# You need to install telethon (and cryptg to speed up downloads)
from os import getenv, path
from shutil import move
import subprocess
import math
import time
import random
import string
import os.path
from mimetypes import guess_extension
from sessionManager import getSession, saveSession
from telethon import TelegramClient, events, __version__
from telethon.tl.types import PeerChannel, DocumentAttributeFilename, DocumentAttributeVideo
import logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s]%(name)s:%(message)s',
level=logging.WARNING)
import multiprocessing
import argparse
import asyncio
TDD_VERSION="1.13"
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")
TELEGRAM_DAEMON_SESSION_PATH = getenv("TELEGRAM_DAEMON_SESSION_PATH")
TELEGRAM_DAEMON_DEST=getenv("TELEGRAM_DAEMON_DEST", "/telegram-downloads")
TELEGRAM_DAEMON_TEMP=getenv("TELEGRAM_DAEMON_TEMP", "")
TELEGRAM_DAEMON_DUPLICATES=getenv("TELEGRAM_DAEMON_DUPLICATES", "rename")
TELEGRAM_DAEMON_TEMP_SUFFIX="tdd"
parser = argparse.ArgumentParser(
description="Script to download files from a 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,
default=TELEGRAM_DAEMON_DEST,
help=
'Destination path for downloaded files (default is /telegram-downloads).')
parser.add_argument(
"--temp",
type=str,
default=TELEGRAM_DAEMON_TEMP,
help=
'Destination path for temporary files (default is using the same downloaded files directory).')
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'
)
parser.add_argument(
"--duplicates",
choices=["ignore", "rename", "overwrite"],
type=str,
default=TELEGRAM_DAEMON_DUPLICATES,
help=
'"ignore"=do not download duplicated files, "rename"=add a random suffix, "overwrite"=redownload and overwrite.'
)
args = parser.parse_args()
api_id = args.api_id
api_hash = args.api_hash
channel_id = args.channel
downloadFolder = args.dest
tempFolder = args.temp
duplicates=args.duplicates
worker_count = multiprocessing.cpu_count()
updateFrequency = 10
lastUpdate = 0
#multiprocessing.Value('f', 0)
if not tempFolder:
tempFolder = downloadFolder
# Edit these lines:
proxy = None
# End of interesting parameters
async def sendHelloMessage(client, peerChannel):
entity = await client.get_entity(peerChannel)
print("Telegram Download Daemon "+TDD_VERSION+" using Telethon "+__version__)
await client.send_message(entity, "Telegram Download Daemon "+TDD_VERSION+" using Telethon "+__version__)
await client.send_message(entity, "Hi! Ready for your files!")
async def log_reply(message, reply):
print(reply)
await message.edit(reply)
def getRandomId(len):
chars=string.ascii_lowercase + string.digits
return ''.join(random.choice(chars) for x in range(len))
def getFilename(event: events.NewMessage.Event):
mediaFileName = "unknown"
if hasattr(event.media, 'photo'):
mediaFileName = str(event.media.photo.id)+".jpeg"
elif hasattr(event.media, 'document'):
for attribute in event.media.document.attributes:
if isinstance(attribute, DocumentAttributeFilename):
mediaFileName=attribute.file_name
break
if isinstance(attribute, DocumentAttributeVideo):
if event.original_update.message.message != '':
mediaFileName = event.original_update.message.message
else:
mediaFileName = str(event.message.media.document.id)
mediaFileName+=guess_extension(event.message.media.document.mime_type)
mediaFileName="".join(c for c in mediaFileName if c.isalnum() or c in "()._- ")
return mediaFileName
in_progress={}
async def set_progress(filename, message, received, total):
global lastUpdate
global updateFrequency
if received >= total:
try: in_progress.pop(filename)
except: pass
return
percentage = math.trunc(received / total * 10000) / 100
progress_message= "{0} % ({1} / {2})".format(percentage, received, total)
in_progress[filename] = progress_message
currentTime=time.time()
if (currentTime - lastUpdate) > updateFrequency:
await log_reply(message, progress_message)
lastUpdate=currentTime
with TelegramClient(getSession(), api_id, api_hash,
proxy=proxy).start() as client:
saveSession(client.session)
queue = asyncio.Queue()
peerChannel = PeerChannel(channel_id)
@client.on(events.NewMessage())
async def handler(event):
if event.to_id != peerChannel:
return
print(event)
try:
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).stdout.decode('utf-8')
elif command == "status":
try:
output = "".join([ "{0}: {1}\n".format(key,value) for (key, value) in in_progress.items()])
if output:
output = "Active downloads:\n\n" + output
else:
output = "No active downloads"
except:
output = "Some error occured while checking the status. Retry."
elif command == "clean":
output = "Cleaning "+tempFolder+"\n"
output+=subprocess.run(["rm "+tempFolder+"/*."+TELEGRAM_DAEMON_TEMP_SUFFIX], shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout
else:
output = "Available commands: list, status, clean"
await log_reply(event, output)
if event.media:
if hasattr(event.media, 'document') or hasattr(event.media,'photo'):
filename=getFilename(event)
if ( path.exists("{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX)) or path.exists("{0}/{1}".format(downloadFolder,filename)) ) and duplicates == "ignore":
message=await event.reply("{0} already exists. Ignoring it.".format(filename))
else:
message=await event.reply("{0} added to queue".format(filename))
await queue.put([event, message])
else:
message=await event.reply("That is not downloadable. Try to send it as a file.")
except Exception as e:
print('Events handler error: ', e)
async def worker():
while True:
try:
element = await queue.get()
event=element[0]
message=element[1]
filename=getFilename(event)
fileName, fileExtension = os.path.splitext(filename)
tempfilename=fileName+"-"+getRandomId(8)+fileExtension
if path.exists("{0}/{1}.{2}".format(tempFolder,tempfilename,TELEGRAM_DAEMON_TEMP_SUFFIX)) or path.exists("{0}/{1}".format(downloadFolder,filename)):
if duplicates == "rename":
filename=tempfilename
if hasattr(event.media, 'photo'):
size = 0
else:
size=event.media.document.size
await log_reply(
message,
"Downloading file {0} ({1} bytes)".format(filename,size)
)
download_callback = lambda received, total: set_progress(filename, message, received, total)
await client.download_media(event.message, "{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), progress_callback = download_callback)
set_progress(filename, message, 100, 100)
move("{0}/{1}.{2}".format(tempFolder,filename,TELEGRAM_DAEMON_TEMP_SUFFIX), "{0}/{1}".format(downloadFolder,filename))
await log_reply(message, "{0} ready".format(filename))
queue.task_done()
except Exception as e:
try: await log_reply(message, "Error: {}".format(str(e))) # If it failed, inform the user about it.
except: pass
print('Queue worker error: ', e)
async def start():
tasks = []
loop = asyncio.get_event_loop()
for i in range(worker_count):
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)
client.loop.run_until_complete(start())