-
Notifications
You must be signed in to change notification settings - Fork 51
/
telegram-download-bot.py
executable file
·181 lines (138 loc) · 6 KB
/
telegram-download-bot.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
#!/usr/bin/python
# -*- coding: utf8 -*-
# telegram-downloader-bot: File downloader from Telegram
# Author: Alfonso E.M. <[email protected]>
# Requires:
# telegram_bot https://pypi.python.org/pypi/python-telegram-bot/
# a bot token (ask @BotFather)
#
# Version: 1.8
from __future__ import print_function
import time
from os import path, getenv
import requests
from multiprocessing import Process, Manager
from telegram import Bot, ReplyKeyboardMarkup
from telegram.error import BadRequest
# CONFIGURATION
TELEGRAM_TIMEOUT=getenv("TELEGRAM_TIMEOUT", 50)
TELEGRAM_BOT_TOKEN=getenv("TELEGRAM_BOT_TOKEN", "YOUR TOKEN HERE") # <-- Configure here
TELEGRAM_CHAT_ID=getenv("TELEGRAM_CHAT_ID", "YOUR CHAT ID HERE") # <-- Configure here
TELEGRAM_REFRESH_SECONDS=getenv("TELEGRAM_REFRESH_SECONDS", 1)
DOWNLOADS_FOLDER=getenv("DOWNLOADS_FOLDER", "/downloads")
# END CONFIGURATION
# DOWNLOAD INDEPENDENT PROCESS
def downloader(filenames,urls):
filename=""
while filename != "QUIT":
try:
filename=filenames.pop(0)
url=urls.pop(0)
except IndexError:
time.sleep(5)
if filename and filename != "QUIT":
print("Downloading:"+filename+ ' from '+url)
r = requests.get(url, stream=True)
with open(path.join(DOWNLOADS_FOLDER,filename), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print("Download completed")
filename=""
if __name__ == '__main__':
# START
bot = Bot(TELEGRAM_BOT_TOKEN)
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloader ready!")
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Share your file posts with me and I will download them for you.")
manager = Manager()
filenames = manager.list()
urls = manager.list()
download_process = Process(target=downloader, args=(filenames,urls,))
download_process.daemon = True
download_process.start()
update_id=0
user_quit=False
# MAIN LOOP
while not user_quit:
try:
telegram_updates=bot.get_updates(offset=update_id, timeout=TELEGRAM_TIMEOUT)
except:
telegram_updates=[]
for update in telegram_updates:
print(update)
update_id = update.update_id + 1
# TEXT MESSAGES
try:
user_command=update.message.text
except AttributeError:
user_command=None
pass
if user_command and user_command.lower() == "quit":
filenames.append("QUIT")
download_process.join()
user_quit=True
telegram_updates=bot.get_updates(offset=update_id, timeout=TELEGRAM_TIMEOUT) # mark this as read or Telegram will send it again
break
elif user_command == "?":
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=str(len(filenames)))
# FILE MESSAGES
try:
newfile=update.message.document
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading file %s (%i bytes)" %(newfile.file_name, newfile.file_size))
tfile=bot.getFile(newfile.file_id)
filenames.append(newfile.file_name)
urls.append(tfile.file_path)
except BadRequest:
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Too big file!")
print("Too big file")
except AttributeError:
pass
# PHOTO MESSAGES
try:
photos=update.message.photo # a list of different available sizes
if len(photos) > 0:
photo=photos[-1]
name="{}-{}x{}.jpg".format(update.update_id,photo.width,photo.height)
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading photo %s (%i bytes)" %(name, photo.file_size))
tfile=bot.getFile(photo.file_id)
filenames.append(name)
urls.append(tfile.file_path)
except AttributeError:
pass
# VIDEO MESSAGES
try:
video=update.message.video # a video
name="{}-{}x{}.mp4".format(update.update_id,video.width,video.height)
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading video %s (%i bytes)" %(name, video.file_size))
tfile=bot.getFile(video.file_id)
filenames.append(name)
urls.append(tfile.file_path)
except BadRequest:
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Too big video!")
print("Too big video file")
except AttributeError:
pass
# AUDIO MESSAGES
try:
audio=update.message.audio # audio files
name=u"{}-{}-{}.mp3".format(audio.title,audio.performer,update.update_id)
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading audio %s (%i bytes)" %(name, audio.file_size))
tfile=bot.getFile(audio.file_id)
filenames.append(name)
urls.append(tfile.file_path)
except BadRequest:
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Too big audio!")
print("Too big audio file")
except AttributeError:
pass
# VOICE MESSAGES
try:
voice=update.message.voice # recorded voice
name="{}.ogg".format(update.update_id)
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading voice %s (%i bytes)" %(name, voice.file_size))
tfile=bot.getFile(voice.file_id)
filenames.append(name)
urls.append(tfile.file_path)
except AttributeError:
pass
time.sleep(TELEGRAM_REFRESH_SECONDS)