-
Notifications
You must be signed in to change notification settings - Fork 139
/
handlers.py
88 lines (75 loc) · 2.87 KB
/
handlers.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
from telegram.ext import updater
import subprocess
import re
import aria
import os
import sys
from threading import Thread
# Import BOT_LOG_CHAT from environment variable
BOT_LOG_CHAT = os.environ.get("BOT_LOG_CHAT")
def button(update, context):
query = update.callback_query
query.answer()
data = query.data
if data.startswith("pause:"):
gid = data.replace('pause:', '')
download = aria.aria2.get_download(gid)
try:
aria.aria2.pause([download])
print("Download Paused(btn)")
except Exception as e:
print(e)
if data.startswith("resume:"):
gid = data.replace('resume:', '')
download = aria.aria2.get_download(gid)
try:
download.update()
download.resume()
print("Download Resumed(btn)")
except Exception as e:
print(e)
if data.startswith("cancel:"):
gid = data.replace('cancel:', '')
download = aria.aria2.get_download(gid)
try:
download.update()
download.remove(force=True, files=True)
print("Download Cancelled(btn)")
except Exception as e:
print(e)
def uploader(updater, update, message, download, ftype):
updateText = f"Download Completed \n'{download.name}'\nSize : {(float(download.total_length)/ 1024 ** 2):.2f} MBs"
updater.bot.edit_message_text(
chat_id=message.chat.id, message_id=message.message_id, text=updateText)
current = update.message.reply_text(
f"{download.name} is complete, \nUploading now")
op = ""
if ftype == 'folder':
op = subprocess.run(
['bash', 'folder.sh', f"{download.name}"], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8')
if ftype == 'file':
op = subprocess.run(
['bash', 'folder.sh', f"downloads/{download.name}"], check=True, stdout=subprocess.PIPE).stdout.decode('utf-8')
print(op)
link = re.findall(
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', op)
print(link)
print("Download instance finished")
updater.bot.send_message(chat_id=BOT_LOG_CHAT, text=f'Upload complete')
updater.bot.edit_message_text(
chat_id=message.chat.id, message_id=current.message_id, text=str(link[0]))
def stop_and_restart(updater):
"""Stop and restart the bot"""
updater.stop()
os.execl(sys.executable, sys.executable, *sys.argv)
def restart(updater, update, context):
update.message.reply_text('Me die! Halp plOx!!...')
Thread(target=stop_and_restart, args=[updater]).start()
def update_and_restart(updater, update, context):
try:
update.message.reply_text(
'I just got an update...\nUnlike your Android phone')
subprocess.run(['git', 'pull'])
Thread(target=stop_and_restart, args=[updater]).start()
except:
print("ERROR")