-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
317 lines (260 loc) · 12.5 KB
/
Copy pathcommand.py
File metadata and controls
317 lines (260 loc) · 12.5 KB
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
import re
import __builtin__ as shared
from neomodel import DoesNotExist
from model import Chat, Show, Season, Episode, Video
from telegram import Emoji, ReplyKeyboardMarkup, ReplyKeyboardHide
from worker import ShowFinder, VideoFinder
def start(bot, update):
''' Returns the standard greeting with a list of commands. '''
chat = Chat.get_or_create(id=update.message.chat_id)
chat.referer = ''
chat.save()
response = (
_('You can control me by sending these commands:') + '\n',
'/showlist - %s' % _('show list of available TV shows'),
'/subscriptions - %s' % _('show active subscriptions'),
'/subscribe - %s' % _('subscribe to a TV show'),
'/unsubscribe - %s' % _('unsubscribe from a TV show'),
'/setlanguage - %s' % _('change language'),
'/watch - %s' % _('find any available episode')
)
bot.sendMessage(chat_id=update.message.chat_id,
text='\n'.join(response),
reply_markup=ReplyKeyboardHide())
def showlist(bot, update):
''' Returns list of available TV shows. '''
chat = Chat.get_or_create(id=update.message.chat_id)
chat.referer = ''
chat.save()
response_lines = [ unicode(_('List of available TV shows:') + '\n', 'utf-8')
] + [' '.join([unicode(Emoji.BLACK_SMALL_SQUARE if chat in s.subscribers.all()
else Emoji.WHITE_SMALL_SQUARE, 'utf-8'),
s.title]) for s in sorted(Show.nodes.all(),
key=lambda x: x.title)]
bot.sendMessage(chat_id=update.message.chat_id,
text='\n'.join(response_lines),
reply_markup=ReplyKeyboardHide())
def subscriptions(bot, update):
''' Returns active subscriptions of the chat. '''
chat = Chat.get_or_create(id=update.message.chat_id)
chat.referer = ''
chat.save()
if chat.subscriptions.all():
response_lines = [ unicode(_('Active subscriptions:') + '\n', 'utf-8')
] + [' '.join([unicode(Emoji.BLACK_SMALL_SQUARE, 'utf-8'),
s.title]) for s in sorted(chat.subscriptions.all(),
key=lambda x: x.title)]
response = '\n'.join(response_lines)
else:
response = _('You are not subscribed to any of the series.')
bot.sendMessage(chat_id=update.message.chat_id,
text=response,
reply_markup=ReplyKeyboardHide())
def subscribe(bot, update):
''' Provides user subscription. '''
chat = Chat.get_or_create(id=update.message.chat_id)
show_title = update.message.text[11:].strip().lower()
if show_title:
try:
show = Show.nodes.get(title_lower=show_title)
if chat in show.subscribers.all():
response = _('You are already subscribed to this series.')
else:
show.subscribers.connect(chat)
response = _('You have subscribed to the show.')
except DoesNotExist:
response = _('Specified series is not on my list, '
'but we will try to find this series.')
ShowFinder(bot=bot,
chat_id=update.message.chat_id,
show_title=show_title).start()
chat.referer = ''
else:
response = _('Which series would you like to subscribe?')
chat.referer = update.message.text
bot.sendMessage(chat_id=update.message.chat_id,
text=response,
reply_markup=ReplyKeyboardHide())
chat.save()
def unsubscribe(bot, update):
''' Provides user unsubscription. '''
chat = Chat.get_or_create(id=update.message.chat_id)
show_title = update.message.text[13:].strip().lower()
if show_title:
try:
show = Show.nodes.get(title_lower=show_title)
if chat in show.subscribers.all():
show.subscribers.disconnect(chat)
response = _('You have unsubscribed from the show.')
else:
response = _('You are not subscribed to the show.')
except DoesNotExist:
response = _('Specified series is not on my list.')
chat.referer = ''
else:
response = _('Which series would you like to unsubscribe?')
chat.referer = update.message.text
bot.sendMessage(chat_id=update.message.chat_id,
text=response,
reply_markup=ReplyKeyboardHide())
chat.save()
def setlanguage(bot, update):
''' Provides the setting of language preferences. '''
chat = Chat.get_or_create(id=update.message.chat_id)
language = update.message.text[13:].strip().lower()
reply_markup=ReplyKeyboardHide()
if language:
if language in shared.translations:
if chat.language != language:
chat.language = language
chat.save()
response = _('The language has changed!')
else:
response = _('You are already using this language!')
else:
response = _('Unknown language!')
chat.referer = ''
else:
response = _('Which language do you prefer?')
reply_markup = ReplyKeyboardMarkup([shared.config['telegram']['locales']])
chat.referer = update.message.text
bot.sendMessage(chat_id=update.message.chat_id,
text=response,
reply_markup=reply_markup)
chat.save()
def watch(bot, update):
''' Returns a link to the video of episode. '''
def parse(text):
matches = [re.search(r'/watch (.+) s(\d+) e(\d+)', text),
re.search(r'/watch (.+) s(\d+)', text),
re.search(r'/watch (.+)', text)]
if not matches[0] is None:
show_title, season_number, episode_number = matches[0].group(1,2,3)
elif not matches[1] is None:
show_title, season_number, episode_number = matches[1].group(1,2) + (None,)
elif not matches[2] is None:
show_title, season_number, episode_number = (matches[2].group(1), None, None)
else:
show_title, season_number, episode_number = ('', None, None)
return show_title.strip().lower(), season_number, episode_number
chat = Chat.get_or_create(id=update.message.chat_id)
response, reply_markup = None, ReplyKeyboardHide()
if not chat.referer:
show_title, season_number, episode_number = parse(update.message.text)
else:
show_title, season_number, episode_number = parse(chat.referer)
if not show_title:
show_title = update.message.text[7:].strip().lower()
elif not season_number:
try:
season_number = int(update.message.text.split(chat.referer + ' s')[1])
except ValueError:
response = _('Invalide season number.')
else:
try:
episode_number = int(update.message.text.split(chat.referer + ' e')[1])
except ValueError:
response = _('Invalid episode number.')
if not response:
if show_title:
try:
show = Show.nodes.get(title_lower=show_title)
if show.is_available:
if season_number:
try:
season = show.seasons.get(number=season_number)
if season.is_available:
if episode_number:
try:
episode = season.episodes.get(number=episode_number)
if episode.is_available:
response = _('We will send you video soon.')
chat.referer = update.message.text
VideoFinder(bot=bot,
chat_id=update.message.chat_id,
episode=episode).start()
else:
raise DoesNotExist({'episode_number': episode_number})
except DoesNotExist:
response = _('This episode is not available.')
chat.referer = ''
else:
response = _('Which episode would you like to see?')
chat.referer, l = update.message.text, sorted(
[str(ep.number) for ep in season.available_episodes],
key=lambda x: int(x))
reply_markup = ReplyKeyboardMarkup(
[l[i:i+3] for i in range(0,len(l),3)])
else:
raise DoesNotExist({'season_number': season_number})
except DoesNotExist:
response = _('This season is not available.')
chat.referer = ''
else:
response = _('Which season would you like to see?')
chat.referer, l = update.message.text, sorted(
[str(s.number) for s in show.available_seasons],
key=lambda x: int(x))
reply_markup = ReplyKeyboardMarkup(
[l[i:i+3] for i in range(0,len(l),3)])
else:
raise DoesNotExist({'show_title': show_title})
except DoesNotExist:
response = _('Specified series is not on my list, '
'but we will try to find this series.')
ShowFinder(bot=bot,
chat_id=update.message.chat_id,
show_title=show_title).start()
else:
response = _('Which TV show would you like to see?')
chat.referer = update.message.text
bot.sendMessage(chat_id=update.message.chat_id,
text=response,
reply_markup=reply_markup)
chat.save()
def rate(bot, update):
''' Provides the ability to rate videos. '''
chat = Chat.get_or_create(id=update.message.chat_id)
video = Video.nodes.get(url=' '.join(update.message.text[6:].split()[:-1]))
chat.rated_videos.disconnect(video)
ref, chat.referer = chat.referer, ''
chat.save()
if update.message.text.split()[-1] == unicode(Emoji.THUMBS_UP_SIGN, 'utf-8'):
chat.rated_videos.connect(video, {'value': 1})
bot.sendMessage(chat_id=update.message.chat_id,
text= _('Thanks for the feedback!'),
reply_markup=ReplyKeyboardHide())
elif update.message.text.split()[-1] == unicode(Emoji.THUMBS_DOWN_SIGN, 'utf-8'):
chat.rated_videos.connect(video, {'value': -1})
update.message.text = ' '.join(['/watch', video.episode.get().id])
watch(bot, update)
else:
chat.referer = ref
chat.save()
bot.sendMessage(chat_id=update.message.chat_id,
text= _('Please rate the video.'),
reply_markup = ReplyKeyboardMarkup([
[Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN]]))
def default(bot, update):
''' Handles messages that are not commands. '''
chat = Chat.get_or_create(id=update.message.chat_id)
if chat.referer.startswith('/subscribe'):
update.message.text = ' '.join([chat.referer, update.message.text])
subscribe(bot, update)
elif chat.referer.startswith('/unsubscribe'):
update.message.text = ' '.join([chat.referer, update.message.text])
unsubscribe(bot, update)
elif chat.referer.startswith('/setlanguage'):
update.message.text = ' '.join([chat.referer, update.message.text])
setlanguage(bot, update)
elif chat.referer.startswith('/rate'):
update.message.text = ' '.join([chat.referer, update.message.text])
rate(bot, update)
elif chat.referer.startswith('/watch'):
pairs = re.findall(r'([a-z]+)([0-9]+)', chat.referer)
update.message.text = (' %s' % (
'' if chat.referer == '/watch' else 'e' if pairs else 's')).join([
chat.referer, update.message.text])
watch(bot, update)
else:
start(bot, update)