forked from alfem/telegram-downloader-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-download-bot.py
executable file
·117 lines (86 loc) · 2.92 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
#!/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)
import time
import requests
from multiprocessing import Process, Manager
from telegram import Bot, ReplyKeyboardMarkup
from telegram.error import TelegramError
# CONFIGURATION
TELEGRAM_TIMEOUT=50
TELEGRAM_BOT_TOKEN="YOUR TOKEN HERE"
TELEGRAM_CHAT_ID="YOUR CHAT ID HERE"
TELEGRAM_REFRESH_SECONDS=1
DOWNLOADS_FOLDER="/tmp"
# 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(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
print "Download completed"
filename=""
# 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=""
pass
if 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
newfile.file_name
newfile.file_id
newfile.file_size
bot.send_message(chat_id=TELEGRAM_CHAT_ID, text="Downloading %s (%i bytes)" %(newfile.file_name, newfile.file_size))
tfile=bot.getFile(newfile.file_id)
# print "Newfile:", tfile
filenames.append(newfile.file_name)
urls.append(tfile.file_path)
except AttributeError:
pass
time.sleep(TELEGRAM_REFRESH_SECONDS)