forked from toddrob99/searcharr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcharr.py
1115 lines (1053 loc) · 44.4 KB
/
searcharr.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Searcharr
Sonarr & Radarr Telegram Bot
By Todd Roberts
https://github.com/toddrob99/searcharr
"""
import argparse
import json
import os
import sqlite3
from threading import Lock
import uuid
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, InputMediaPhoto
from telegram.error import BadRequest
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
from log import set_up_logger
import radarr
import sonarr
import settings
__version__ = "1.5"
DBPATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
DBFILE = "searcharr.db"
DBLOCK = Lock()
def parse_args():
parser = argparse.ArgumentParser(
prog="Searcharr", description="Start the Searcharr Bot."
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
dest="verbose",
help="Enable debug logging.",
)
parser.add_argument(
"--console-logging",
"-c",
action="store_true",
dest="console_logging",
help="Enable console logging.",
)
parser.add_argument(
"--dev",
"-d",
action="store_true",
dest="dev_mode",
help="Enable developer mode, which will result in more exceptions being raised instead of handled.",
)
return parser.parse_args()
class Searcharr(object):
def __init__(self, token):
self.DEV_MODE = True if args.dev_mode else False
self.token = token
logger.info(f"Searcharr v{__version__} - Logging started!")
self.sonarr = (
sonarr.Sonarr(settings.sonarr_url, settings.sonarr_api_key, args.verbose)
if settings.sonarr_enabled
else None
)
if self.sonarr:
logger.debug(
f"Looking up Sonarr quality profile id for [{settings.sonarr_quality_profile_id}]..."
)
foundId = self.sonarr.lookup_quality_profile_id(
settings.sonarr_quality_profile_id
)
if foundId == 0:
logger.error(
f"Sonarr quality profile provided in settings [{settings.sonarr_quality_profile_id}] is invalid!"
)
else:
settings.sonarr_quality_profile_id = foundId
logger.debug(
f"Found Sonarr quality profile id: [{settings.sonarr_quality_profile_id}]"
)
self.radarr = (
radarr.Radarr(settings.radarr_url, settings.radarr_api_key, args.verbose)
if settings.radarr_enabled
else None
)
if self.radarr:
logger.debug(
f"Looking up Radarr quality profile id for [{settings.radarr_quality_profile_id}]..."
)
foundId = self.radarr.lookup_quality_profile_id(
settings.radarr_quality_profile_id
)
if foundId == 0:
logger.error(
f"Radarr quality profile provided in settings [{settings.radarr_quality_profile_id}] is invalid!"
)
else:
settings.radarr_quality_profile_id = foundId
logger.debug(
f"Found Radarr quality profile id: [{settings.radarr_quality_profile_id}]"
)
self.conversations = {}
try:
settings.searcharr_admin_password
except AttributeError:
settings.searcharr_admin_password = uuid.uuid4().hex
logger.warning(
f'No admin password detected. Please set one in settings.py (searcharr_admin_password="your admin password"). Using {settings.searcharr_admin_password} as the admin password for this session.'
)
if settings.searcharr_password == "":
logger.warning(
'Password is blank. This will allow anyone to add series/movies using your bot. If this is unexpected, set a password in settings.py (searcharr_password="your password").'
)
try: # Handle missing sonarr_tag_with_username setting - added v1.4
settings.sonarr_tag_with_username
except AttributeError:
settings.sonarr_tag_with_username = True
logger.warning(
"No sonarr_tag_with_username setting found. Please add sonarr_tag_with_username to settings.py (sonarr_tag_with_username=True or sonarr_tag_with_username=False). Defaulting to True."
)
try: # Handle missing radarr_tag_with_username setting - added v1.4
settings.radarr_tag_with_username
except AttributeError:
settings.radarr_tag_with_username = True
logger.warning(
"No radarr_tag_with_username setting found. Please add radarr_tag_with_username to settings.py (radarr_tag_with_username=True or radarr_tag_with_username=False). Defaulting to True."
)
def cmd_start(self, update, context):
logger.debug(f"Received start cmd from [{update.message.from_user.username}]")
password = self._strip_entities(update.message)
if password and password == settings.searcharr_admin_password:
self._add_user(
id=update.message.from_user.id,
username=str(update.message.from_user.username),
admin=1,
)
update.message.reply_text(
"Admin authentication successful. Use /help for commands."
)
elif self._authenticated(update.message.from_user.id):
update.message.reply_text(
"You are already authenticated. Try /help for usage info."
)
elif password == settings.searcharr_password:
self._add_user(
id=update.message.from_user.id,
username=str(update.message.from_user.username),
)
update.message.reply_text(
"Authentication successful. Use /help for commands."
)
else:
update.message.reply_text("Incorrect password.")
def cmd_movie(self, update, context):
logger.debug(f"Received movie cmd from [{update.message.from_user.username}]")
if not self._authenticated(update.message.from_user.id):
update.message.reply_text(
"I don't seem to know you... Please authenticate with `/start <password>` and then try again."
)
return
if not settings.radarr_enabled:
update.message.reply_text("Sorry, but movie support is disabled.")
return
title = self._strip_entities(update.message)
if not len(title):
update.message.reply_text(
"Please include the movie title in the command, e.g. `/movie Title Here`"
)
return
results = self.radarr.lookup_movie(title)
cid = self._generate_cid()
# self.conversations.update({cid: {"cid": cid, "type": "movie", "results": results}})
self._create_conversation(
id=cid,
username=str(update.message.from_user.username),
kind="movie",
results=results,
)
if not len(results):
update.message.reply_text("Sorry, but I didn't find any matching movies.")
else:
r = results[0]
reply_message, reply_markup = self._prepare_response(
"movie", r, cid, 0, len(results)
)
try:
context.bot.sendPhoto(
chat_id=update.message.chat.id,
photo=r["remotePoster"],
caption=reply_message,
reply_markup=reply_markup,
)
except BadRequest as e:
if str(e) == "Wrong type of the web page content":
logger.error(
f"Error sending photo [{r['remotePoster']}]: BadRequest: {e}. Attempting to send with default poster..."
)
context.bot.sendPhoto(
chat_id=update.message.chat.id,
photo="https://artworks.thetvdb.com/banners/images/missing/movie.jpg",
caption=reply_message,
reply_markup=reply_markup,
)
else:
raise
def cmd_series(self, update, context):
logger.debug(f"Received series cmd from [{update.message.from_user.username}]")
if not self._authenticated(update.message.from_user.id):
update.message.reply_text(
"I don't seem to know you... Please authenticate with `/start <password>` and then try again."
)
return
if not settings.sonarr_enabled:
update.message.reply_text("Sorry, but series support is disabled.")
return
title = self._strip_entities(update.message)
if not len(title):
update.message.reply_text(
"Please include the series title in the command, e.g. `/series Title Here`"
)
return
results = self.sonarr.lookup_series(title)
cid = self._generate_cid()
# self.conversations.update({cid: {"cid": cid, "type": "series", "results": results}})
self._create_conversation(
id=cid,
username=str(update.message.from_user.username),
kind="series",
results=results,
)
if not len(results):
update.message.reply_text("Sorry, but I didn't find any matching series.")
else:
r = results[0]
reply_message, reply_markup = self._prepare_response(
"series", r, cid, 0, len(results)
)
try:
context.bot.sendPhoto(
chat_id=update.message.chat.id,
photo=r["remotePoster"],
caption=reply_message,
reply_markup=reply_markup,
)
except BadRequest as e:
if str(e) == "Wrong type of the web page content":
logger.error(
f"Error sending photo [{r['remotePoster']}]: BadRequest: {e}. Attempting to send with default poster..."
)
context.bot.sendPhoto(
chat_id=update.message.chat.id,
photo="https://artworks.thetvdb.com/banners/images/missing/movie.jpg",
caption=reply_message,
reply_markup=reply_markup,
)
else:
raise
def cmd_users(self, update, context):
logger.debug(f"Received users cmd from [{update.message.from_user.username}]")
auth_level = self._authenticated(update.message.from_user.id)
if not auth_level:
update.message.reply_text(
"I don't seem to know you... Please authenticate with `/start <password>` and then try again."
)
return
elif auth_level != 2:
update.message.reply_text(
"You do not have admin permissions... Please authenticate with `/start <admin password>` and then try again."
)
return
results = self._get_users()
cid = self._generate_cid()
# self.conversations.update({cid: {"cid": cid, "type": "users", "results": results}})
self._create_conversation(
id=cid,
username=str(update.message.from_user.username),
kind="users",
results=results,
)
if not len(results):
update.message.reply_text(
"Sorry, but I didn't find any users. That seems wrong..."
)
else:
reply_message, reply_markup = self._prepare_response_users(
cid, results, 0, 5, len(results),
)
context.bot.sendMessage(
chat_id=update.message.chat.id,
text=reply_message,
reply_markup=reply_markup,
)
def callback(self, update, context):
query = update.callback_query
logger.debug(
f"Received callback from [{query.from_user.username}]: [{query.data}]"
)
auth_level = self._authenticated(query.from_user.id)
if not auth_level:
query.message.reply_text(
"I don't seem to know you... Please authenticate with `/start <password>` and then try again."
)
query.message.delete()
query.answer()
return
if not query.data or not len(query.data):
query.answer()
return
convo = self._get_conversation(query.data.split("^^^")[0])
# convo = self.conversations.get(query.data.split("^^^")[0])
if not convo:
query.message.reply_text(
"I received your command, but I don't recognize the conversation. Please start again."
)
query.message.delete()
query.answer()
return
cid, i, op = query.data.split("^^^")
i = int(i)
if op == "noop":
pass
elif op == "cancel":
self._delete_conversation(cid)
# self.conversations.pop(cid)
query.message.reply_text("Search canceled!")
query.message.delete()
elif op == "done":
self._delete_conversation(cid)
# self.conversations.pop(cid)
query.message.delete()
elif op == "prev":
if convo["type"] in ["series", "movie"]:
if i <= 0:
query.answer()
return
r = convo["results"][i - 1]
reply_message, reply_markup = self._prepare_response(
convo["type"], r, cid, i - 1, len(convo["results"])
)
try:
query.message.edit_media(
media=InputMediaPhoto(r["remotePoster"]),
reply_markup=reply_markup,
)
except BadRequest as e:
if str(e) == "Wrong type of the web page content":
logger.error(
f"Error sending photo [{r['remotePoster']}]: BadRequest: {e}. Attempting to send with default poster..."
)
query.message.edit_media(
media=InputMediaPhoto(
"https://artworks.thetvdb.com/banners/images/missing/movie.jpg"
),
reply_markup=reply_markup,
)
else:
raise
query.bot.edit_message_caption(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
caption=reply_message,
reply_markup=reply_markup,
)
elif convo["type"] == "users":
if i <= 0:
i = 0
reply_message, reply_markup = self._prepare_response_users(
cid, convo["results"], i, 5, len(convo["results"]),
)
context.bot.edit_message_text(
chat_id=query.message.chat.id,
message_id=query.message.message_id,
text=reply_message,
reply_markup=reply_markup,
)
elif op == "next":
if convo["type"] in ["series", "movie"]:
if i >= len(convo["results"]):
query.answer()
return
r = convo["results"][i + 1]
logger.debug(f"{r=}")
reply_message, reply_markup = self._prepare_response(
convo["type"], r, cid, i + 1, len(convo["results"])
)
try:
query.message.edit_media(
media=InputMediaPhoto(r["remotePoster"]),
reply_markup=reply_markup,
)
except BadRequest as e:
if str(e) == "Wrong type of the web page content":
logger.error(
f"Error sending photo [{r['remotePoster']}]: BadRequest: {e}. Attempting to send with default poster..."
)
query.message.edit_media(
media=InputMediaPhoto(
"https://artworks.thetvdb.com/banners/images/missing/movie.jpg"
),
reply_markup=reply_markup,
)
else:
raise
query.bot.edit_message_caption(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
caption=reply_message,
reply_markup=reply_markup,
)
elif convo["type"] == "users":
if i > len(convo["results"]):
query.answer()
return
reply_message, reply_markup = self._prepare_response_users(
cid, convo["results"], i, 5, len(convo["results"]),
)
context.bot.edit_message_text(
chat_id=query.message.chat.id,
message_id=query.message.message_id,
text=reply_message,
reply_markup=reply_markup,
)
elif op == "add":
paths = (
self.sonarr.get_root_folders()
if convo["type"] == "series"
else self.radarr.get_root_folders()
if convo["type"] == "movie"
else []
)
r = convo["results"][i]
if len(paths) > 1:
reply_message, reply_markup = self._prepare_response(
convo["type"],
r,
cid,
i,
len(convo["results"]),
add=True,
paths=paths,
)
try:
query.message.edit_media(
media=InputMediaPhoto(r["remotePoster"]),
reply_markup=reply_markup,
)
except BadRequest as e:
if str(e) == "Wrong type of the web page content":
logger.error(
f"Error sending photo [{r['remotePoster']}]: BadRequest: {e}. Attempting to send with default poster..."
)
query.message.edit_media(
media=InputMediaPhoto(
"https://artworks.thetvdb.com/banners/images/missing/movie.jpg"
),
reply_markup=reply_markup,
)
else:
raise
query.bot.edit_message_caption(
chat_id=query.message.chat_id,
message_id=query.message.message_id,
caption=reply_message,
reply_markup=reply_markup,
)
elif len(paths) == 1:
try:
if convo["type"] == "series":
added = self.sonarr.add_series(
series_info=r,
path=paths[0]["path"],
quality=settings.sonarr_quality_profile_id,
monitored=settings.sonarr_add_monitored,
search=settings.sonarr_search_on_add,
tag=f"searcharr-{query.from_user.username if query.from_user.username else query.from_user.id}"
if settings.sonarr_tag_with_username
else None,
)
elif convo["type"] == "movie":
added = self.radarr.add_movie(
movie_info=r,
path=paths[0]["path"],
quality=settings.radarr_quality_profile_id,
monitored=settings.radarr_add_monitored,
search=settings.radarr_search_on_add,
tag=f"searcharr-{query.from_user.username if query.from_user.username else query.from_user.id}"
if settings.radarr_tag_with_username
else None,
)
else:
added = False
except Exception as e:
logger.error(f"Error adding {convo['type']}: {e}")
added = False
if added:
self._delete_conversation(cid)
query.message.reply_text(f"Successfully added {r['title']}!")
query.message.delete()
else:
query.message.reply_text(
f"Unspecified error encountered while adding {convo['type']}!"
)
else:
self._delete_conversation(cid)
query.message.reply_text(
f"Error adding {convo['type']}: no root folders found in {'Sonarr' if convo['type'] == 'series' else 'Radarr'}! Please check your configuration in {'Sonarr' if convo['type'] == 'series' else 'Radarr'} and try again."
)
query.message.delete()
elif op.startswith("addto:"):
r = convo["results"][i]
paths = (
self.sonarr.get_root_folders()
if convo["type"] == "series"
else self.radarr.get_root_folders()
if convo["type"] == "movie"
else []
)
pathId = op.split(":")[1]
try:
int(pathId) # Check if pathId is an int - was full path in v1.3.2
except ValueError:
path = pathId
logger.debug(
f"Detected non-integer path id: [{path}], using that as the full path..."
)
else:
path = next((p["path"] for p in paths if p["id"] == int(pathId)), None)
logger.debug(f"Path id [{pathId}] lookup result: [{path}]")
try:
if convo["type"] == "series":
added = self.sonarr.add_series(
series_info=r,
path=path,
quality=settings.sonarr_quality_profile_id,
monitored=settings.sonarr_add_monitored,
search=settings.sonarr_search_on_add,
tag=f"searcharr-{query.from_user.username if query.from_user.username else query.from_user.id}"
if settings.sonarr_tag_with_username
else None,
)
elif convo["type"] == "movie":
added = self.radarr.add_movie(
movie_info=r,
path=path,
quality=settings.radarr_quality_profile_id,
monitored=settings.radarr_add_monitored,
search=settings.radarr_search_on_add,
tag=f"searcharr-{query.from_user.username if query.from_user.username else query.from_user.id}"
if settings.radarr_tag_with_username
else None,
)
else:
added = False
except Exception as e:
logger.error(f"Error adding {convo['type']}: {e}")
added = False
logger.debug(f"Result of attempt to add {convo['type']}: {added}")
if added:
self._delete_conversation(cid)
query.message.reply_text(f"Successfully added {r['title']}!")
query.message.delete()
else:
query.message.reply_text(
f"Unspecified error encountered while adding {convo['type']}!"
)
elif op == "remove_user":
if auth_level != 2:
query.message.reply_text(
"You do not have admin permissions... Please authenticate with `/start <admin password>` and then try again."
)
query.message.delete()
query.answer()
return
try:
self._remove_user(i)
# query.message.reply_text(
# f"Successfully removed all access for user id [{i}]!"
# )
# self._delete_conversation(cid)
# query.message.delete()
convo.update({"results": self._get_users()})
self._create_conversation(
id=cid,
username=str(query.message.from_user.username),
kind="users",
results=convo["results"],
)
reply_message, reply_markup = self._prepare_response_users(
cid, convo["results"], 0, 5, len(convo["results"]),
)
context.bot.edit_message_text(
chat_id=query.message.chat.id,
message_id=query.message.message_id,
text=f"Successfully removed all access for user id [{i}]! "
+ reply_message,
reply_markup=reply_markup,
)
except Exception as e:
logger.error(f"Error removing all access for user id [{i}]: {e}")
query.message.reply_text(
f"Unspecified error encountered while removing user id [{i}]!"
)
elif op == "make_admin":
if auth_level != 2:
query.message.reply_text(
"You do not have admin permissions... Please authenticate with `/start <admin password>` and then try again."
)
query.message.delete()
query.answer()
return
try:
self._update_admin_access(i, 1)
# query.message.reply_text(f"Added admin access for user id [{i}]!")
# self._delete_conversation(cid)
# query.message.delete()
convo.update({"results": self._get_users()})
self._create_conversation(
id=cid,
username=str(query.message.from_user.username),
kind="users",
results=convo["results"],
)
reply_message, reply_markup = self._prepare_response_users(
cid, convo["results"], 0, 5, len(convo["results"]),
)
context.bot.edit_message_text(
chat_id=query.message.chat.id,
message_id=query.message.message_id,
text=f"Added admin access for user id [{i}]! " + reply_message,
reply_markup=reply_markup,
)
except Exception as e:
logger.error(f"Error adding admin access for user id [{i}]: {e}")
query.message.reply_text(
f"Unspecified error encountered while adding admin access for user id [{i}]!"
)
elif op == "remove_admin":
if auth_level != 2:
query.message.reply_text(
"You do not have admin permissions... Please authenticate with `/start <admin password>` and then try again."
)
query.message.delete()
query.answer()
return
try:
self._update_admin_access(i, "")
# query.message.reply_text(f"Removed admin access for user id [{i}]!")
# self._delete_conversation(cid)
# query.message.delete()
convo.update({"results": self._get_users()})
self._create_conversation(
id=cid,
username=str(query.message.from_user.username),
kind="users",
results=convo["results"],
)
reply_message, reply_markup = self._prepare_response_users(
cid, convo["results"], 0, 5, len(convo["results"]),
)
context.bot.edit_message_text(
chat_id=query.message.chat.id,
message_id=query.message.message_id,
text=f"Removed admin access for user id [{i}]! " + reply_message,
reply_markup=reply_markup,
)
except Exception as e:
logger.error(f"Error removing admin access for user id [{i}]: {e}")
query.message.reply_text(
f"Unspecified error encountered while removing admin access for user id [{i}]!"
)
query.answer()
def _prepare_response(self, kind, r, cid, i, total_results, add=False, paths=None):
keyboard = []
keyboardNavRow = []
if i > 0:
keyboardNavRow.append(
InlineKeyboardButton("< Prev", callback_data=f"{cid}^^^{i}^^^prev")
)
if kind == "series" and r["tvdbId"]:
keyboardNavRow.append(
InlineKeyboardButton(
"tvdb", url=f"https://thetvdb.com/series/{r['titleSlug']}"
)
)
elif kind == "movie" and r["tmdbId"]:
keyboardNavRow.append(
InlineKeyboardButton(
"TMDB", url=f"https://www.themoviedb.org/movie/{r['tmdbId']}"
)
)
if r["imdbId"]:
keyboardNavRow.append(
InlineKeyboardButton(
"IMDb", url=f"https://imdb.com/title/{r['imdbId']}"
)
)
if total_results > 1 and i < total_results - 1:
keyboardNavRow.append(
InlineKeyboardButton("Next >", callback_data=f"{cid}^^^{i}^^^next")
)
keyboard.append(keyboardNavRow)
if add:
if not paths and kind == "series":
paths = self.sonarr.get_root_folders()
elif not paths and kind == "movie":
paths = self.radarr.get_root_folders()
for p in paths:
keyboard.append(
[
InlineKeyboardButton(
f"Add to {p['path']}",
callback_data=f"{cid}^^^{i}^^^addto:{p['id']}",
)
],
)
keyboardActRow = []
if not add:
if not r["id"]:
keyboardActRow.append(
InlineKeyboardButton(
f"Add {kind.title()}!", callback_data=f"{cid}^^^{i}^^^add"
),
)
else:
keyboardActRow.append(
InlineKeyboardButton(
"Already Added!", callback_data=f"{cid}^^^{i}^^^noop"
),
)
keyboardActRow.append(
InlineKeyboardButton(
"Cancel Search", callback_data=f"{cid}^^^{i}^^^cancel"
),
)
if len(keyboardActRow):
keyboard.append(keyboardActRow)
reply_markup = InlineKeyboardMarkup(keyboard)
if kind == "series":
reply_message = f"{r['title']}{' (' + str(r['year']) + ')' if r['year'] and str(r['year']) not in r['title'] else ''} - {r['seasonCount']} Season{'s' if r['seasonCount'] != 1 else ''}{' - ' + r['network'] if r['network'] else ''} - {r['status'].title()}\n\n{r['overview']}"[
0:1024
]
elif kind == "movie":
reply_message = f"{r['title']}{' (' + str(r['year']) + ')' if r['year'] and str(r['year']) not in r['title'] else ''}{' - ' + str(r['runtime']) + ' min' if r['runtime'] else ''} - {r['status'].title()}\n\n{r['overview']}"[
0:1024
]
else:
reply_message = "Something went wrong!"
return (reply_message, reply_markup)
def _prepare_response_users(self, cid, users, offset, num, total_results):
keyboard = []
for u in users[offset : offset + num]:
keyboard.append(
[
InlineKeyboardButton(
"Remove", callback_data=f"{cid}^^^{u['id']}^^^remove_user"
),
InlineKeyboardButton(
f"{u['username'] if u['username'] != 'None' else u['id']}",
callback_data=f"{cid}^^^{u['id']}^^^noop",
),
InlineKeyboardButton(
"Remove Admin" if u["admin"] else "Make Admin",
callback_data=f"{cid}^^^{u['id']}^^^{'remove_admin' if u['admin'] else 'make_admin'}",
),
]
)
keyboardNavRow = []
if offset > 0:
keyboardNavRow.append(
InlineKeyboardButton(
"< Prev", callback_data=f"{cid}^^^{offset - num}^^^prev"
),
)
keyboardNavRow.append(
InlineKeyboardButton("Done", callback_data=f"{cid}^^^{offset}^^^done"),
)
if total_results > 1 and offset + num < total_results:
keyboardNavRow.append(
InlineKeyboardButton(
"Next >", callback_data=f"{cid}^^^{offset + num}^^^next"
),
)
keyboard.append(keyboardNavRow)
reply_markup = InlineKeyboardMarkup(keyboard)
reply_message = f"Listing Searcharr users {offset + 1}-{min(offset + num, total_results)} of {total_results}."
return (reply_message, reply_markup)
def handle_error(self, update, context):
logger.error(f"Caught error: {context.error}")
try:
update.callback_query.answer()
except Exception:
pass
def cmd_help(self, update, context):
logger.debug(f"Received help cmd from [{update.message.from_user.username}]")
auth_level = self._authenticated(update.message.from_user.id)
if not auth_level:
update.message.reply_text(
"Please authenticate with `/start <password>` and then try again."
)
return
if settings.sonarr_enabled and settings.radarr_enabled:
resp = "Use /movie <title> to add a movie to Radarr, and /series <title> to add a series to Sonarr."
elif settings.sonarr_enabled:
resp = "Use /series <title> to add a series to Sonarr."
elif settings.radarr_enabled:
resp = "Use /movie <title> to add a movie to Radarr."
else:
resp = "Sorry, but all of my features are currently disabled."
if auth_level == 2:
resp += " Since you are an admin, you can also use /users to manage users."
update.message.reply_text(resp)
def _strip_entities(self, message):
text = message.text
entities = message.parse_entities()
logger.debug(f"{entities=}")
for v in entities.values():
text = text.replace(v, "")
text = text.replace(" ", "").strip()
logger.debug(f"Stripped entities from message [{message.text}]: [{text}]")
return text
def run(self):
self._init_db()
updater = Updater(self.token, use_context=True)
updater.dispatcher.add_handler(CommandHandler("help", self.cmd_help))
updater.dispatcher.add_handler(CommandHandler("start", self.cmd_start))
updater.dispatcher.add_handler(CommandHandler("movie", self.cmd_movie))
updater.dispatcher.add_handler(CommandHandler("series", self.cmd_series))
updater.dispatcher.add_handler(CommandHandler("users", self.cmd_users))
updater.dispatcher.add_handler(CallbackQueryHandler(self.callback))
if not self.DEV_MODE:
updater.dispatcher.add_error_handler(self.handle_error)
else:
logger.info(
"Developer mode is enabled; skipping registration of error handler--exceptions will be raised."
)
updater.start_polling()
updater.idle()
def _create_conversation(self, id, username, kind, results):
con, cur = self._get_con_cur()
q = "INSERT OR REPLACE INTO conversations (id, username, type, results) VALUES (?, ?, ?, ?)"
qa = (id, username, kind, json.dumps(results))
logger.debug(f"Executing query: [{q}] with args: [{qa}]")
try:
with DBLOCK:
cur.execute(q, qa)
con.commit()
con.close()
return True
except sqlite3.Error as e:
logger.error(f"Error executing database query [{q}]: {e}")
raise
def _generate_cid(self):
q = "SELECT * FROM conversations WHERE id=?"
con, cur = self._get_con_cur()
while True:
u = uuid.uuid4().hex[:8]
try:
r = cur.execute(q, (u,))
except sqlite3.Error as e:
r = None
logger.error(
f"Error executing database query to check conversation id uniqueness [{q}]: {e}"
)
if not r:
return None
elif not len(r.fetchall()):
con.close()
return u
else:
logger.warning("Detected conversation id collision. Interesting.")
def _get_conversation(self, id):
q = "SELECT * FROM conversations WHERE id=?;"
qa = (id,)
logger.debug(f"Executing query: [{q}] with args: [{qa}]...")
try:
con, cur = self._get_con_cur()
r = cur.execute(q, qa)
except sqlite3.Error as e:
r = None
logger.error(
f"Error executing database query to look up conversation from the database [{q}]: {e}"
)
if r:
record = r.fetchone()
if record:
logger.debug(f"Found conversation {record['id']} in the database")
record.update({"results": json.loads(record["results"])})
con.close()
return record
logger.debug(f"Found no conversation for id [{id}]")
return None
def _delete_conversation(self, id):
q = "DELETE FROM conversations WHERE id=?;"
qa = (id,)
logger.debug(f"Executing query: [{q}] with args: [{qa}]")
try:
con, cur = self._get_con_cur()
with DBLOCK:
cur.execute(q, qa)
con.commit()
con.close()
return True
except sqlite3.Error as e:
logger.error(
f"Error executing database query to look up conversation from the database [{q}]: {e}"
)
return False
def _add_user(self, id, username, admin=""):
con, cur = self._get_con_cur()
q = "INSERT OR REPLACE INTO users (id, username, admin) VALUES (?, ?, ?);"
qa = (id, username, admin)
logger.debug(f"Executing query: [{q}] with args: [{qa}]")
try:
with DBLOCK:
cur.execute(q, qa)
con.commit()
con.close()
return True
except sqlite3.Error as e:
logger.error(f"Error executing database query [{q}]: {e}")
raise
def _remove_user(self, id):
con, cur = self._get_con_cur()
q = "DELETE FROM users where id=?;"
qa = (id,)
logger.debug(f"Executing query: [{q}] with args: [{qa}]")
try:
with DBLOCK:
cur.execute(q, qa)
con.commit()
con.close()
return True
except sqlite3.Error as e:
logger.error(f"Error executing database query [{q}]: {e}")
raise
def _get_users(self, admin=False):
adminQ = " where IFNULL(admin, '') != ''" if admin else ""
q = f"SELECT * FROM users{adminQ};"
logger.debug(f"Executing query: [{q}] with no args...")
try:
con, cur = self._get_con_cur()
r = cur.execute(q)
except sqlite3.Error as e:
r = None
logger.error(
f"Error executing database query to look up users from the database [{q}]: {e}"
)
if r:
records = r.fetchall()
con.close()
return records
logger.debug(
f"Found no {'admin ' if admin else ''}users in the database (this seems wrong)."
)
return []