forked from ch3p4ll3/QBittorrentBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·435 lines (326 loc) · 13.6 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/local/bin/python3.7
import datetime
import os
import tempfile
from math import log, floor
import botogram
import psutil
import qbittorrent_control
from json_validation import get_configs
import db_management
bot = botogram.create(get_configs().token)
bot.about = "with this bot you can control QBittorrent from telegram"
bot.owner = "@ch3p4ll3"
def convert_size(size_bytes) -> str:
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(floor(log(size_bytes, 1024)))
p = pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
def convert_eta(n) -> str:
return str(datetime.timedelta(seconds=n))
def send_menu(message, chat) -> None:
db_management.write_support("None", chat.id)
btn = botogram.Buttons()
btn[0].callback("📝 List", "list")
btn[1].callback("➕ Add Magnet", "category", "add_magnet")
btn[1].callback("➕ Add Torrent", "category", "add_torrent")
btn[2].callback("⏸ Pause", "pause")
btn[2].callback("▶️ Resume", "resume")
btn[3].callback("⏸ Pause All", "pause_all")
btn[3].callback("▶️ Resume All", "resume_all")
btn[4].callback("🗑 Delete", "delete_one")
btn[4].callback("🗑 Delete All", "delete_all")
btn[5].callback("➕ Add Category", "add_category")
btn[5].callback("🗑 Remove Category", "remove_category")
btn[6].callback("📝 Modify Category", "modify_category")
try:
message.edit("Qbitorrent Control", attach=btn)
except botogram.api.APIError:
chat.send("Qbitorrent Control", attach=btn)
def list_active_torrents(n, chat, message, callback) -> None:
torrents = qbittorrent_control.get_torrent_info()
if not torrents:
btn = botogram.Buttons()
btn[0].callback("🔙 Menu", "menu")
try:
message.edit("There are no torrents", attach=btn)
except botogram.api.APIError:
chat.send("There are no torrents", attach=btn)
return
btn = botogram.Buttons()
if n == 1:
for key, i in enumerate(torrents):
btn[key].callback(i.name, callback, str(key))
btn[key + 1].callback("🔙 Menu", "menu")
try:
message.edit_attach(btn)
except botogram.api.APIError:
chat.send("Qbitorrent Control", attach=btn)
else:
for key, i in enumerate(torrents):
btn[key].callback(i.name, "torrentInfo", str(key))
btn[key + 1].callback("🔙 Menu", "menu")
try:
message.edit_attach(btn)
except botogram.api.APIError:
chat.send("Qbitorrent Control", attach=btn)
@bot.command("start")
def start_command(message, chat) -> None:
"""Start the bot."""
if chat.id in get_configs().id:
send_menu(message, chat)
else:
btn = botogram.Buttons()
btn[0].url("GitHub", "https://github.com/ch3p4ll3/QBittorrentBot/")
chat.send("You are not authorized to use this bot.", attach=btn)
@bot.command("stats")
def stats_command(chat) -> None:
if chat.id in get_configs().id:
txt = f"""*============SYSTEM============*
*CPU Usage: *{psutil.cpu_percent(interval=None)}%
*CPU Temp: *{psutil.sensors_temperatures()['cpu-thermal'][0][1]}°C
*Free Memory: *{convert_size(psutil.virtual_memory().available)} \
of {convert_size(psutil.virtual_memory().total)} \
({psutil.virtual_memory().percent}%)
*Disks usage: *{convert_size(psutil.disk_usage('/mnt/usb').used)} \
of {convert_size(psutil.disk_usage('/mnt/usb').total)} \
({psutil.disk_usage('/mnt/usb').percent}%)"""
chat.send(txt, syntax="markdown")
else:
btn = botogram.Buttons()
btn[0].url("GitHub", "https://github.com/ch3p4ll3/QBittorrentBot/")
chat.send("You are not authorized to use this bot.", attach=btn)
@bot.callback("add_category")
def add_category_callback(chat, message) -> None:
db_management.write_support("category_name", chat.id)
btn = botogram.Buttons()
btn[2].callback("🔙 Menu", "menu")
try:
message.edit("Send the category name")
except botogram.api.APIError:
chat.send("Send the category name")
@bot.callback("remove_category")
def remove_category_callback(chat, message, data):
btn = botogram.Buttons()
if data is not None:
qbittorrent_control.remove_category(data=data)
btn[0].callback("🔙 Menu", "menu")
message.edit(f"The category {data} has been removed", attach=btn)
return
categories = qbittorrent_control.get_categories()
if categories is None:
btn[0].callback("🔙 Menu", "menu")
message.edit("There are no categories", attach=btn)
return
for key, i in enumerate(categories):
btn[key].callback(i, "remove_category", i)
btn[key + 1].callback("🔙 Menu", "menu")
try:
message.edit("Choose a category:", attach=btn)
except botogram.api.APIError:
chat.send("Choose a category:", attach=btn)
@bot.callback("modify_category")
def modify_category_callback(chat, message, data):
btn = botogram.Buttons()
if data is not None:
btn[2].callback("🔙 Menu", "menu")
db_management.write_support(f"category_dir_modify#{data}", chat.id)
message.edit(f"Send new path for category {data}", attach=btn)
return
categories = qbittorrent_control.get_categories()
if categories is None:
btn[0].callback("🔙 Menu", "menu")
message.edit("There are no categories", attach=btn)
return
for key, i in enumerate(categories):
btn[key].callback(i, "modify_category", i)
btn[key + 1].callback("🔙 Menu", "menu")
try:
message.edit("Choose a category:", attach=btn)
except botogram.api.APIError:
chat.send("Choose a category:", attach=btn)
@bot.callback("category")
def category(chat, message, data, query) -> None:
btn = botogram.Buttons()
categories = qbittorrent_control.get_categories()
if categories is None:
if "magnet" in data:
addmagnet_callback(query, "#None")
else:
addtorrent_callback(query, "#None")
return
for key, i in enumerate(categories):
btn[key].callback(i, data, i)
btn[key + 1].callback("None", data, "None")
btn[key + 2].callback("🔙 Menu", "menu")
try:
message.edit("Choose a category:", attach=btn)
except botogram.api.APIError:
chat.send("Choose a category:", attach=btn)
@bot.callback("menu")
def menu(chat, message) -> None:
send_menu(message, chat)
@bot.callback("list")
def list_callback(chat, message) -> None:
btn = botogram.Buttons()
btn[0].callback("🔙 Menu", "menu")
list_active_torrents(0, chat, message,
db_management.read_support(chat.id))
@bot.callback("add_magnet")
def addmagnet_callback(query, data) -> None:
db_management.write_support(f"magnet#{data}", query.sender.id)
query.notify("Send a magnet link")
@bot.callback("add_torrent")
def addtorrent_callback(query, data) -> None:
db_management.write_support(f"torrent#{data}", query.sender.id)
query.notify("Send a torrent file")
@bot.callback("pause_all")
def pauseall_callback(query) -> None:
qbittorrent_control.pause_all()
query.notify("Paused All")
@bot.callback("resume_all")
def resumeall_callback(query) -> None:
qbittorrent_control.resume_all()
query.notify("Resumed All")
@bot.callback("pause")
def pause_callback(chat, message, data) -> None:
if data is None:
list_active_torrents(1, chat, message, "pause")
else:
qbittorrent_control.pause(id_torrent=int(data))
send_menu(message, chat)
@bot.callback("resume")
def resume_callback(chat, message, data) -> None:
if data is None:
list_active_torrents(1, chat, message, "resume")
else:
qbittorrent_control.resume(id_torrent=int(data))
send_menu(message, chat)
@bot.callback("delete_one")
def delete_callback(message, data) -> None:
btn = botogram.Buttons()
btn[0].callback("🗑 Delete torrent", "delete_one_no_data", data)
btn[1].callback("🗑 Delete torrent and data", "delete_one_data", data)
btn[2].callback("🔙 Menu", "menu")
message.edit("Qbitorrent Control", attach=btn)
@bot.callback("delete_one_no_data")
def delete_no_data_callback(chat, message, data) -> None:
if data is None:
list_active_torrents(1, chat, message, "delete_one_no_data")
else:
qbittorrent_control.delete_one_no_data(id_torrent=int(data))
send_menu(message, chat)
@bot.callback("delete_one_data")
def delete_with_data_callback(chat, message, data) -> None:
if data is None:
list_active_torrents(1, chat, message, "delete_one_data")
else:
qbittorrent_control.delete_one_data(id_torrent=int(data))
send_menu(message, chat)
@bot.callback("delete_all")
def delete_all_callback(message) -> None:
btn = botogram.Buttons()
btn[0].callback("🗑 Delete all torrents", "delete_all_no_data")
btn[1].callback("🗑 Delete all torrents and data", "delete_all_data")
btn[2].callback("🔙 Menu", "menu")
message.edit("Qbitorrent Control", attach=btn)
@bot.callback("delete_all_no_data")
def delete__all_with_no_data_callback(message, chat, query) -> None:
qbittorrent_control.delall_no_data()
query.notify("Deleted only torrents")
send_menu(message, chat)
@bot.callback("delete_all_data")
def delete_all_with_data_callback(message, chat, query) -> None:
qbittorrent_control.delall_data()
query.notify("Deleted All+Torrents")
send_menu(message, chat)
@bot.callback("torrentInfo")
def torrent_info_callback(message, data) -> None:
torrent = qbittorrent_control.get_torrent_info(data=int(data))
progress = torrent.progress * 100
text = ""
if progress == 0:
text += f"{torrent.name}\n[ ] " \
f"{round(progress, 2)}% completed\n" \
f"State: {torrent.state.capitalize()}\n" \
f"Download Speed: {convert_size(torrent.dlspeed)}/s\n" \
f"Size: {convert_size(torrent.size)}\nETA: " \
f"{convert_eta(int(torrent.eta))}\n\n"
elif progress == 100:
text += f"{torrent.name}\n[completed] " \
f"{round(progress, 2)}% completed\n" \
f"State: {torrent.state.capitalize()}\n" \
f"Upload Speed: {convert_size(torrent.upspeed)}/s\n\n"
else:
text += f"{torrent.name}\n[{'=' * int(progress / 10)}" \
f"{' ' * int(12 - (progress / 10))}]" \
f" {round(progress, 2)}% completed\n" \
f"State: {torrent.state.capitalize()} \n" \
f"Download Speed: {convert_size(torrent.dlspeed)}/s\n" \
f"Size: {convert_size(torrent.size)}\nETA: " \
f"{convert_eta(int(torrent.eta))}\n\n"
btn = botogram.Buttons()
btn[0].callback("⏸ Pause", "pause", data)
btn[0].callback("▶️ Resume", "resume", data)
btn[1].callback("🗑 Delete", "delete_one", data)
btn[2].callback("🔙 Menu", "menu")
message.edit(text, attach=btn)
@bot.process_message
def process_message(chat, message) -> None:
action = db_management.read_support(chat.id)
if action == "magnet":
if message.text.startswith("magnet:?xt"):
magnet_link = message.text.split(" , ")
category = db_management.read_support(chat.id).split("#")[1]
qbittorrent_control.add_magnet(magnet_link=magnet_link,
category=category)
send_menu(message, chat)
db_management.write_support("None", chat.id)
else:
chat.send("This magnet link is invalid! Retry")
elif action == "torrent" and message.document:
if ".torrent" in message.document.file_name:
with tempfile.TemporaryDirectory() as tempdir:
name = f"{tempdir}/{message.document.file_name}"
category = db_management.read_support(chat.id).split("#")[1]
message.document.save(name)
qbittorrent_control.add_torrent(file_name=name,
category=category)
send_menu(message, chat)
db_management.write_support("None", chat.id)
else:
chat.send("This is not a torrent file! Retry")
elif action == "category_name":
db_management.write_support(f"category_dir#{message.text}", chat.id)
chat.send(f"now send me the path for the category {message.text}")
elif "category_dir" in action:
if os.path.exists(message.text):
name = db_management.read_support(chat.id).split("#")[1]
if "modify" in action:
qbittorrent_control.edit_category(name=name,
save_path=message.text)
send_menu(message, chat)
return
qbittorrent_control.create_category(name=name,
save_path=message.text)
send_menu(message, chat)
else:
chat.send("The path entered does not exist! Retry")
@bot.timer(60)
def torrent_finished():
for i in qbittorrent_control.get_torrent_info():
if i.progress == 1 and \
db_management.read_completed_torrents(i.hash) is None \
and get_configs().notify:
for j in get_configs().id:
try:
bot.chat(j).send(f"torrent {i.name} has "
f"finished downloading!")
except botogram.api.ChatUnavailableError:
pass
db_management.write_completed_torrents(i.hash)
if __name__ == "__main__":
bot.run()