-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrentservice.py
195 lines (176 loc) · 7.57 KB
/
torrentservice.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
from transmission_rpc import Client
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ConversationHandler
import re
TORRENTLIST, TORRENTINFO, REMOVETORRENT = range(3)
class TorrentService:
def __init__(self, address, port, username, password):
self.client = Client(address=address, port=port, username=username, password=password)
def torrent_icon(self, status):
if status == 'stopped':
return '⏹'
else:
return '▶️'
def torrent_description(self, torrent):
return '{} {} {:01.0f}%, {}'.format(self.torrent_icon(torrent.status), torrent.status, torrent.progress, torrent.name)
def add_torrent(self, url, path):
tor = self.client.add_torrent(url, download_dir=path)
return tor.name
def torrent_list(self, update, context):
torrents = self.client.get_torrents()
torrents = sorted(torrents, key = lambda i: i.queue_position)
keyboard = []
for torrent in torrents:
tname = self.torrent_description(torrent)
keyboard.append([InlineKeyboardButton(tname, callback_data='t_info_{}'.format(torrent.id))])
keyboard.append([InlineKeyboardButton('✕ Cancel', callback_data='cancel')])
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text(text='Torrents', reply_markup=reply_markup)
return TORRENTLIST
def torrent_info(self, update, context):
query = update.callback_query
tid = re.search('^t_info_(\d+?)$', query.data).group(1)
tor = self.client.get_torrent(tid)
stop_resume = []
if tor.status == 'stopped':
stop_resume = [InlineKeyboardButton('▷ Resume', callback_data='t_resume_{}'.format(tid)), InlineKeyboardButton('▶︎ Resume now', callback_data='t_resume_now_{}'.format(tid))]
elif tor.status == 'download pending':
stop_resume = [InlineKeyboardButton('▶︎ Resume now', callback_data='t_resume_now_{}'.format(tid)), InlineKeyboardButton('◼︎ Stop', callback_data='t_stop_{}'.format(tid))]
else:
stop_resume = [InlineKeyboardButton('◼︎ Stop', callback_data='t_stop_{}'.format(tid))]
keyboard = [
[InlineKeyboardButton('▲ Top', callback_data='t_top_{}'.format(tid)), InlineKeyboardButton('△ Up', callback_data='t_up_{}'.format(tid)), InlineKeyboardButton('▽ Down', callback_data='t_down_{}'.format(tid)), InlineKeyboardButton('▼ Bottom', callback_data='t_bottom_{}'.format(tid))],
stop_resume,
[InlineKeyboardButton('◉ Verify', callback_data='t_verify_{}'.format(tid))],
[InlineKeyboardButton('☒ Remove', callback_data='t_remove_confirm_{}'.format(tid))],
[InlineKeyboardButton('✕ Cancel', callback_data='cancel')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
bot = context.bot
bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
reply_markup=reply_markup,
text=self.torrent_description(tor)
)
return TORRENTINFO
def move_up(self, update, context):
query = update.callback_query
tid = re.search('^t_up_(\d+?)$', query.data).group(1)
self.client.queue_up(tid)
tor = self.client.get_torrent(tid)
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='{} has been moved up'.format(tor.name)
)
return ConversationHandler.END
def move_down(self, update, context):
query = update.callback_query
tid = re.search('^t_down_(\d+?)$', query.data).group(1)
self.client.queue_down(tid)
tor = self.client.get_torrent(tid)
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='{} has been moved down'.format(tor.name)
)
return ConversationHandler.END
def move_top(self, update, context):
query = update.callback_query
tid = re.search('^t_top_(\d+?)$', query.data).group(1)
self.client.queue_top(tid)
tor = self.client.get_torrent(tid)
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='{} has been moved to top'.format(tor.name)
)
return ConversationHandler.END
def move_bottom(self, update, context):
query = update.callback_query
tid = re.search('^t_bottom_(\d+?)$', query.data).group(1)
self.client.queue_bottom(tid)
tor = self.client.get_torrent(tid)
context.bot.edit_message_text(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
text='{} has been moved to bottom'.format(tor.name)
)
return ConversationHandler.END
def stop_torrent(self, update, context):
tid = re.search('^t_stop_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.stop_torrent(tid)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} has been stopped'.format(tor.name)
)
return ConversationHandler.END
def resume_torrent(self, update, context):
tid = re.search('^t_resume_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.start_torrent(tid)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} has been resumed'.format(tor.name)
)
return ConversationHandler.END
def resume_torrent_now(self, update, context):
tid = re.search('^t_resume_now_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.start_torrent(tid, bypass_queue=True)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} has been resumed'.format(tor.name)
)
return ConversationHandler.END
def verify_torrent(self, update, context):
tid = re.search('^t_verify_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.verify_torrent(tid)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} verification has been started'.format(tor.name)
)
return ConversationHandler.END
def remove_torrent_confirm(self, update, context):
tid = re.search('^t_remove_confirm_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
keyboard = [
[InlineKeyboardButton('☒ Remove torrent', callback_data='t_remove_{}'.format(tid))],
[InlineKeyboardButton('⌧ Remove torrent and delete files', callback_data='t_remove_complete_{}'.format(tid))],
[InlineKeyboardButton('✕ Cancel', callback_data='cancel')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
reply_markup=reply_markup,
text='Are you sure you want to remove {}?'.format(tor.name)
)
return REMOVETORRENT
def remove_torrent(self, update, context):
tid = re.search('^t_remove_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.remove_torrent(tid)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} has been removed'.format(tor.name)
)
return ConversationHandler.END
def remove_torrent_complete(self, update, context):
tid = re.search('^t_remove_complete_(\d+?)$', update.callback_query.data).group(1)
tor = self.client.get_torrent(tid)
self.client.remove_torrent(tid, delete_data=True)
context.bot.edit_message_text(
chat_id=update.callback_query.message.chat_id,
message_id=update.callback_query.message.message_id,
text='{} has been removed'.format(tor.name)
)
return ConversationHandler.END