-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
164 lines (143 loc) · 5.88 KB
/
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
import base64
import os
import telebot
from telebot import types
from consts import TOKKEN, ACCESS, LIST_BUTTON_TEXT, list_keyboard
from TransmissionConnection import (
TorrentStatus,
TransmissionConnection,
)
bot = telebot.TeleBot(TOKKEN)
tc = TransmissionConnection(
address=os.environ.get("TRANSMISSION_URL", "localhost"),
login=os.environ.get("TRANSMISSION_LOGIN", None),
password=os.environ.get("TRANSMISSION_PASSWORD", None),
port=int(os.environ.get("TRANSMISSION_PORT", 9091)),
)
class AccessDeniedException(Exception):
pass
def access(message):
if message.from_user.id not in ACCESS:
raise AccessDeniedException
def torrent_info(hashStr: str) -> tuple[str, types.InlineKeyboardMarkup]:
torrent = tc[hashStr]
keyboard = types.InlineKeyboardMarkup()
if torrent.status == TorrentStatus.STOPPED:
key_start = types.InlineKeyboardButton(
text="Раздавать", callback_data=f"start_{torrent.hashStr}"
)
keyboard.add(key_start)
if torrent.status == TorrentStatus.SEEDING:
key_pause = types.InlineKeyboardButton(
text="Поставить на паузу", callback_data=f"pause_{torrent.hashStr}"
)
keyboard.add(key_pause)
key_del = types.InlineKeyboardButton(
text=f"Удалить {torrent.name}", callback_data=f"del_{torrent.hashStr}"
)
keyboard.add(key_del)
text = f"{torrent.name}\nstatus: {torrent.status.to_emoji()}"
return text, keyboard
def list_torrents(chat_id):
for torrent in tc:
text, keyboard = torrent_info(torrent.hashStr)
bot.send_message(chat_id, text=text, reply_markup=keyboard)
@bot.message_handler(content_types=["text"])
def get_text_message(message):
try:
access(message)
if message.text == "/help" or message.text == "/start":
bot.send_message(
message.from_user.id,
"Перешли мне торрент файл или магнет ссылку и я начну скачивание. Введи /list – для просмотра списка торрентов. Этим ботом могут пользоваться только доверенные лица",
reply_markup=list_keyboard,
)
elif message.text == "/list" or message.text == LIST_BUTTON_TEXT:
list_torrents(message.from_user.id)
elif message.text.startswith("magnet:"):
tc.add_torrent(message.text)
bot.send_message(
message.from_user.id,
f"{tc[-1].name} добавлен",
reply_markup=list_keyboard,
)
except AccessDeniedException:
bot.send_message(message.from_user.id, "Этот бот не для тебя")
@bot.message_handler(content_types=["document"])
def get_document_message(message):
try:
access(message)
if (
len(message.document.file_name) < 8
or message.document.file_name[-7:] != "torrent"
):
bot.send_message(
message.from_user.id, "Можно мне отправить только torrent файл"
)
file_info = bot.get_file(message.document.file_id)
new_torrent = bot.download_file(file_info.file_path)
new_torrent = base64.b64encode(bytes(new_torrent)).decode("utf-8")
tc.add_torrent(new_torrent)
bot.send_message(
message.from_user.id, f"{tc[-1].name} добавлен", reply_markup=list_keyboard
)
except AccessDeniedException:
bot.send_message(message.from_user.id, "Этот бот не для тебя")
@bot.callback_query_handler(func=lambda _: True)
def callback_worker(call):
for torrent in tc:
if call.data == f"del_{torrent.hashStr}":
keyboard = types.InlineKeyboardMarkup()
keyboard.add(
types.InlineKeyboardButton(
text="Да", callback_data=f"del_agree_{torrent.hashStr}"
)
)
keyboard.add(
types.InlineKeyboardButton(
text="Нет", callback_data=f"del_disagree_{torrent.hashStr}"
)
)
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text="ТЫ УВЕРЕН?",
reply_markup=keyboard,
)
elif call.data == f"pause_{torrent.hashStr}":
tc.stop_torrent(torrent)
text, keyboard = torrent_info(torrent.hashStr)
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text=text,
reply_markup=keyboard,
)
elif call.data == f"del_disagree_{torrent.hashStr}":
info, keyboard = torrent_info(torrent.hashStr)
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text=info,
reply_markup=keyboard,
)
elif call.data == f"del_agree_{torrent.hashStr}":
tc.del_torrent(torrent, delete_data=True)
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text=f"{torrent.name} удален",
)
elif call.data == f"start_{torrent.hashStr}":
tc.start_torrent(torrent)
text, keyboard = torrent_info(torrent.hashStr)
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text=text,
reply_markup=keyboard,
)
elif call.data == "list":
list_torrents(call.message.chat.id)
if __name__ == "__main__":
bot.polling(none_stop=True)