forked from Da7oomHTPC/traktarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraktarr.py
executable file
·905 lines (739 loc) · 40.5 KB
/
traktarr.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
#!/usr/bin/env python3
import os.path
import signal
import sys
import time
import click
import schedule
from pyfiglet import Figlet
############################################################
# INIT
############################################################
cfg = None
log = None
notify = None
# Click
@click.group(help='Add new shows & movies to Sonarr/Radarr from Trakt.')
@click.version_option('1.2.3', prog_name='traktarr')
@click.option(
'--config',
envvar='TRAKTARR_CONFIG',
type=click.Path(file_okay=True, dir_okay=False),
help='Configuration file',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "config.json")
)
@click.option(
'--logfile',
envvar='TRAKTARR_LOGFILE',
type=click.Path(file_okay=True, dir_okay=False),
help='Log file',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "activity.log")
)
def app(config, logfile):
# Setup global variables
global cfg, log, notify
# Load config
from misc.config import Config
cfg = Config(config_path=config, logfile=logfile).cfg
# Load logger
from misc.log import logger
log = logger.get_logger('traktarr')
# Load notifications
from notifications import Notifications
notify = Notifications()
# Notifications
init_notifications()
############################################################
# Trakt OAuth
############################################################
@app.command(help='Authenticate traktarr.')
def trakt_authentication():
from media.trakt import Trakt
trakt = Trakt(cfg)
if trakt.oauth_authentication():
log.info("Authentication information saved; please restart the application")
exit()
def validate_trakt(trakt, notifications):
if not trakt.validate_client_id():
log.error("Aborting due to failure to validate Trakt API Key")
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to validate Trakt API Key'})
exit()
else:
log.info("Validated Trakt API Key")
def validate_pvr(pvr, type, notifications):
if not pvr.validate_api_key():
log.error("Aborting due to failure to validate %s URL / API Key", type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to validate %s URL / API Key' % type})
return None
else:
log.info("Validated %s URL & API Key", type)
def get_profile_id(pvr, profile):
# retrieve profile id for requested profile
profile_id = pvr.get_profile_id(profile)
if not profile_id or not profile_id > 0:
log.error("Aborting due to failure to retrieve Profile ID for: %s", profile)
exit()
log.info("Retrieved Profile ID for %s: %d", profile, profile_id)
return profile_id
def get_profile_tags(pvr):
profile_tags = pvr.get_tags()
if profile_tags is None:
log.error("Aborting due to failure to retrieve Tag ID's")
exit()
log.info("Retrieved %d Tag ID's", len(profile_tags))
return profile_tags
def get_objects(pvr, type, notifications):
objects_list = pvr.get_objects()
if not objects_list:
log.error("Aborting due to failure to retrieve %s shows list", type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to retrieve %s shows list' % type})
exit()
objects_type = 'movies' if type.lower() == 'radarr' else 'shows'
log.info("Retrieved %s %s list, %s found: %d", type, objects_type, objects_type, len(objects_list))
return objects_list
############################################################
# SHOWS
############################################################
@app.command(help='Add a single show to Sonarr.')
@click.option('--show_id', '-id', help='Trakt show_id.', required=True)
@click.option('--folder', '-f', default=None, help='Add show with this root folder to Sonarr.')
@click.option('--no-search', is_flag=True, help='Disable search when adding show to Sonarr.')
def show(show_id, folder=None, no_search=False):
from media.sonarr import Sonarr
from media.trakt import Trakt
from helpers import sonarr as sonarr_helper
# replace sonarr root_folder if folder is supplied
if folder:
cfg['sonarr']['root_folder'] = folder
trakt = Trakt(cfg)
sonarr = Sonarr(cfg.sonarr.url, cfg.sonarr.api_key)
validate_trakt(trakt, False)
validate_pvr(sonarr, 'Sonarr', False)
profile_id = get_profile_id(sonarr, cfg.sonarr.profile)
profile_tags = get_profile_tags(sonarr)
# get trakt show
trakt_show = trakt.get_show(show_id)
if not trakt_show:
log.error("Aborting due to failure to retrieve Trakt show")
return None
else:
log.info("Retrieved Trakt show information for %s: %s (%d)", show_id, trakt_show['title'],
trakt_show['year'])
# determine which tags to use when adding this series
use_tags = sonarr_helper.series_tag_id_from_network(profile_tags, cfg.sonarr.tags, trakt_show['network'])
# add show to sonarr
if sonarr.add_series(trakt_show['ids']['tvdb'], trakt_show['title'], trakt_show['ids']['slug'], profile_id,
cfg.sonarr.root_folder, use_tags, not no_search):
log.info("ADDED %s (%d) with tags: %s", trakt_show['title'], trakt_show['year'],
sonarr_helper.readable_tag_from_ids(profile_tags, use_tags))
else:
log.error("FAILED adding %s (%d) with tags: %s", trakt_show['title'], trakt_show['year'],
sonarr_helper.readable_tag_from_ids(profile_tags, use_tags))
return
@app.command(help='Add multiple shows to Sonarr.')
@click.option('--list-type', '-t',
help='Trakt list to process. For example, anticipated, trending, popular, person, watched, played, '
'recommended, watchlist or any URL to a list', required=True)
@click.option('--add-limit', '-l', default=0, help='Limit number of shows added to Sonarr.', show_default=True)
@click.option('--add-delay', '-d', default=2.5, help='Seconds between each add request to Sonarr.', show_default=True)
@click.option('--sort', '-s', default='votes', type=click.Choice(['votes', 'rating', 'release']),
help='Sort list to process.')
@click.option('--genre', '-g', default=None, help='Only add shows from this genre to Sonarr.')
@click.option('--folder', '-f', default=None, help='Add shows with this root folder to Sonarr.')
@click.option('--actor', '-a', default=None, help='Only add movies from this actor to Radarr.')
@click.option('--no-search', is_flag=True, help='Disable search when adding shows to Sonarr.')
@click.option('--notifications', is_flag=True, help='Send notifications.')
@click.option('--authenticate-user',
help='Specify which user to authenticate with to retrieve Trakt lists. Default: first user in the config')
@click.option('--ignore-blacklist', is_flag=True, help='Ignores the blacklist when running the command.')
@click.option('--remove-rejected-from-recommended', is_flag=True,
help='Removes rejected/existing shows from recommended.')
def shows(list_type, add_limit=0, add_delay=2.5, sort='votes', genre=None, folder=None, actor=None, no_search=False,
notifications=False, authenticate_user=None, ignore_blacklist=False, remove_rejected_from_recommended=False):
from media.sonarr import Sonarr
from media.trakt import Trakt
from helpers import misc as misc_helper
from helpers import sonarr as sonarr_helper
from helpers import trakt as trakt_helper
added_shows = 0
# remove genre from shows blacklisted_genres if supplied
if genre:
misc_helper.unblacklist_genres(genre, cfg['filters']['shows']['blacklisted_genres'])
# replace sonarr root_folder if folder is supplied
if folder:
cfg['sonarr']['root_folder'] = folder
# validate trakt client_id
trakt = Trakt(cfg)
sonarr = Sonarr(cfg.sonarr.url, cfg.sonarr.api_key)
validate_trakt(trakt, notifications)
validate_pvr(sonarr, 'Sonarr', notifications)
profile_id = get_profile_id(sonarr, cfg.sonarr.profile)
profile_tags = get_profile_tags(sonarr)
pvr_objects_list = get_objects(sonarr, 'Sonarr', notifications)
# get trakt series list
if list_type.lower() == 'anticipated':
trakt_objects_list = trakt.get_anticipated_shows(genres=genre, languages=cfg.filters.shows.allowed_languages)
elif list_type.lower() == 'trending':
trakt_objects_list = trakt.get_trending_shows(genres=genre, languages=cfg.filters.shows.allowed_languages)
elif list_type.lower() == 'popular':
trakt_objects_list = trakt.get_popular_shows(genres=genre, languages=cfg.filters.shows.allowed_languages)
elif list_type.lower() == 'person':
if not actor:
log.error("You must specify an actor with the --actor / -a parameter when using the person list type!")
return None
trakt_objects_list = trakt.get_person_shows(person=actor, genres=genre,
languages=cfg.filters.shows.allowed_languages)
elif list_type.lower() == 'recommended':
trakt_objects_list = trakt.get_recommended_shows(authenticate_user, genres=genre,
languages=cfg.filters.shows.allowed_languages)
elif list_type.lower().startswith('played'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_played_shows(genres=genre, languages=cfg.filters.shows.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower().startswith('watched'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_watched_shows(genres=genre, languages=cfg.filters.shows.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower() == 'watchlist':
trakt_objects_list = trakt.get_watchlist_shows(authenticate_user)
else:
trakt_objects_list = trakt.get_user_list_shows(list_type, authenticate_user)
if not trakt_objects_list:
log.error("Aborting due to failure to retrieve Trakt %s shows list", list_type)
if notifications:
callback_notify(
{'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to retrieve Trakt %s shows list' % list_type})
return None
else:
log.info("Retrieved Trakt %s shows list, shows found: %d", list_type, len(trakt_objects_list))
# set remove_rejected_recommended to False if this is not the recommended list
if list_type.lower() != 'recommended':
remove_rejected_from_recommended = False
# build filtered series list without series that exist in sonarr
processed_series_list = sonarr_helper.remove_existing_series(pvr_objects_list, trakt_objects_list,
callback_remove_recommended
if remove_rejected_from_recommended else None)
if processed_series_list is None:
log.error("Aborting due to failure to remove existing Sonarr shows from retrieved Trakt shows list")
if notifications:
callback_notify({'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to remove existing Sonarr shows from retrieved Trakt %s shows list'
% list_type})
return None
else:
log.info("Removed existing Sonarr shows from Trakt shows list, shows left to process: %d",
len(processed_series_list))
# sort filtered series list
if sort == 'release':
sorted_series_list = misc_helper.sorted_list(processed_series_list, 'show', 'first_aired')
log.info("Sorted shows list to process by release date")
elif sort == 'rating':
sorted_series_list = misc_helper.sorted_list(processed_series_list, 'show', 'rating')
log.info("Sorted shows list to process by highest rating")
else:
sorted_series_list = misc_helper.sorted_list(processed_series_list, 'show', 'votes')
log.info("Sorted shows list to process by highest votes")
# loop series_list
log.info("Processing list now...")
for series in sorted_series_list:
try:
# check if genre matches genre supplied via argument
if genre and not misc_helper.allowed_genres(genre, 'show', series):
log.debug("Skipping: %s because it was not from %s genre(s)", series['show']['title'], genre.lower())
continue
# check if series passes out blacklist criteria inspection
if not trakt_helper.is_show_blacklisted(series, cfg.filters.shows, ignore_blacklist,
callback_remove_recommended
if remove_rejected_from_recommended else None):
log.info("Adding: %s | Genres: %s | Network: %s | Country: %s", series['show']['title'],
', '.join(series['show']['genres']), series['show']['network'],
series['show']['country'].upper())
# determine which tags to use when adding this series
use_tags = sonarr_helper.series_tag_id_from_network(profile_tags, cfg.sonarr.tags,
series['show']['network'])
# add show to sonarr
if sonarr.add_series(series['show']['ids']['tvdb'], series['show']['title'],
series['show']['ids']['slug'], profile_id, cfg.sonarr.root_folder, use_tags,
not no_search):
log.info("ADDED %s (%d) with tags: %s", series['show']['title'], series['show']['year'],
sonarr_helper.readable_tag_from_ids(profile_tags, use_tags))
if notifications:
callback_notify({'event': 'add_show', 'list_type': list_type, 'show': series['show']})
added_shows += 1
else:
log.error("FAILED adding %s (%d) with tags: %s", series['show']['title'], series['show']['year'],
sonarr_helper.readable_tag_from_ids(profile_tags, use_tags))
# stop adding shows, if added_shows >= add_limit
if add_limit and added_shows >= add_limit:
break
# sleep before adding any more
time.sleep(add_delay)
except Exception:
log.exception("Exception while processing show %s: ", series['show']['title'])
log.info("Added %d new show(s) to Sonarr", added_shows)
# send notification
if notifications and (cfg.notifications.verbose or added_shows > 0):
notify.send(message="Added %d shows from Trakt's %s list" % (added_shows, list_type))
return added_shows
############################################################
# MOVIES
############################################################
@app.command(help='Add a single movie to Radarr.')
@click.option('--movie_id', '-id', help='Trakt movie_id.', required=True)
@click.option('--folder', '-f', default=None, help='Add movie with this root folder to Radarr.')
@click.option('--no-search', is_flag=True, help='Disable search when adding movie to Radarr.')
def movie(movie_id, folder=None, no_search=False):
from media.radarr import Radarr
from media.trakt import Trakt
# replace radarr root_folder if folder is supplied
if folder:
cfg['radarr']['root_folder'] = folder
# validate trakt api_key
trakt = Trakt(cfg)
radarr = Radarr(cfg.radarr.url, cfg.radarr.api_key)
validate_trakt(trakt, False)
validate_pvr(radarr, 'Radarr', False)
profile_id = get_profile_id(radarr, cfg.radarr.profile)
# get trakt movie
trakt_movie = trakt.get_movie(movie_id)
if not trakt_movie:
log.error("Aborting due to failure to retrieve Trakt movie")
return None
else:
log.info("Retrieved Trakt movie information for %s: %s (%d)", movie_id, trakt_movie['title'],
trakt_movie['year'])
# add movie to radarr
if radarr.add_movie(trakt_movie['ids']['tmdb'], trakt_movie['title'], trakt_movie['year'],
trakt_movie['ids']['slug'], profile_id, cfg.radarr.root_folder, not no_search):
log.info("ADDED %s (%d)", trakt_movie['title'], trakt_movie['year'])
else:
log.error("FAILED adding %s (%d)", trakt_movie['title'], trakt_movie['year'])
return
@app.command(help='Add multiple movies to Radarr.')
@click.option('--list-type', '-t',
help='Trakt list to process. For example, anticipated, trending, popular, boxoffice, person, watched, '
'recommended, played, watchlist or any URL to a list', required=True)
@click.option('--add-limit', '-l', default=0, help='Limit number of movies added to Radarr.', show_default=True)
@click.option('--add-delay', '-d', default=2.5, help='Seconds between each add request to Radarr.', show_default=True)
@click.option('--sort', '-s', default='votes', type=click.Choice(['votes', 'rating', 'release']),
help='Sort list to process.')
@click.option('--rating','-r',default=None,type=(int),help='Set a minimum rating threshold (according to Rotten Tomatoes)')
@click.option('--genre', '-g', default=None, help='Only add movies from this genre to Radarr.')
@click.option('--folder', '-f', default=None, help='Add movies with this root folder to Radarr.')
@click.option('--actor', '-a', default=None, help='Only add movies from this actor to Radarr.')
@click.option('--no-search', is_flag=True, help='Disable search when adding movies to Radarr.')
@click.option('--notifications', is_flag=True, help='Send notifications.')
@click.option('--authenticate-user',
help='Specify which user to authenticate with to retrieve Trakt lists. Default: first user in the config.')
@click.option('--ignore-blacklist', is_flag=True, help='Ignores the blacklist when running the command.')
@click.option('--remove-rejected-from-recommended', is_flag=True,
help='Removes rejected/existing movies from recommended.')
def movies(list_type, add_limit=0, add_delay=2.5, sort='votes', rating=None, genre=None, folder=None, actor=None, no_search=False,
notifications=False, authenticate_user=None, ignore_blacklist=False, remove_rejected_from_recommended=False):
from media.radarr import Radarr
from media.trakt import Trakt
from helpers import misc as misc_helper
from helpers import radarr as radarr_helper
from helpers import trakt as trakt_helper
from helpers import rating as rating_helper
added_movies = 0
# remove genre from movies blacklisted_genres if supplied
if genre:
misc_helper.unblacklist_genres(genre, cfg['filters']['movies']['blacklisted_genres'])
# replace radarr root_folder if folder is supplied
if folder:
cfg['radarr']['root_folder'] = folder
# validate trakt api_key
trakt = Trakt(cfg)
radarr = Radarr(cfg.radarr.url, cfg.radarr.api_key)
validate_trakt(trakt, notifications)
validate_pvr(radarr, 'Radarr', notifications)
profile_id = get_profile_id(radarr, cfg.radarr.profile)
pvr_objects_list = get_objects(radarr, 'Radarr', notifications)
# get trakt movies list
if list_type.lower() == 'anticipated':
trakt_objects_list = trakt.get_anticipated_movies(genres=genre, languages=cfg.filters.movies.allowed_languages)
elif list_type.lower() == 'trending':
trakt_objects_list = trakt.get_trending_movies(genres=genre, languages=cfg.filters.movies.allowed_languages)
elif list_type.lower() == 'popular':
trakt_objects_list = trakt.get_popular_movies(genres=genre, languages=cfg.filters.movies.allowed_languages)
elif list_type.lower() == 'boxoffice':
trakt_objects_list = trakt.get_boxoffice_movies()
elif list_type.lower() == 'person':
if not actor:
log.error("You must specify an actor with the --actor / -a parameter when using the person list type!")
return None
trakt_objects_list = trakt.get_person_movies(person=actor, genres=genre,
languages=cfg.filters.movies.allowed_languages)
elif list_type.lower() == 'recommended':
trakt_objects_list = trakt.get_recommended_movies(authenticate_user, genres=genre,
languages=cfg.filters.movies.allowed_languages)
elif list_type.lower().startswith('played'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_played_movies(genres=genre, languages=cfg.filters.movies.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower().startswith('watched'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_watched_movies(genres=genre, languages=cfg.filters.movies.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower() == 'watchlist':
trakt_objects_list = trakt.get_watchlist_movies(authenticate_user)
else:
trakt_objects_list = trakt.get_user_list_movies(list_type, authenticate_user)
if not trakt_objects_list:
log.error("Aborting due to failure to retrieve Trakt %s movies list", list_type)
if notifications:
callback_notify(
{'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to retrieve Trakt %s movies list' % list_type})
return None
else:
log.info("Retrieved Trakt %s movies list, movies found: %d", list_type, len(trakt_objects_list))
# set remove_rejected_recommended to False if this is not the recommended list
if list_type.lower() != 'recommended':
remove_rejected_from_recommended = False
# build filtered movie list without movies that exist in radarr
processed_movies_list = radarr_helper.remove_existing_movies(pvr_objects_list, trakt_objects_list,
callback_remove_recommended
if remove_rejected_from_recommended else None)
if processed_movies_list is None:
log.error("Aborting due to failure to remove existing Radarr movies from retrieved Trakt movies list")
if notifications:
callback_notify({'event': 'abort', 'type': 'movies', 'list_type': list_type,
'reason': 'Failure to remove existing Radarr movies from retrieved '
'Trakt %s movies list' % list_type})
return None
else:
log.info("Removed existing Radarr movies from Trakt movies list, movies left to process: %d",
len(processed_movies_list))
# sort filtered movie list
if sort == 'release':
sorted_movies_list = misc_helper.sorted_list(processed_movies_list, 'movie', 'released')
log.info("Sorted movies list to process by release date")
elif sort == 'rating':
sorted_movies_list = misc_helper.sorted_list(processed_movies_list, 'movie', 'rating')
log.info("Sorted movies list to process by highest rating")
else:
sorted_movies_list = misc_helper.sorted_list(processed_movies_list, 'movie', 'votes')
log.info("Sorted movies list to process by highest votes")
# loop movies
log.info("Processing list now...")
for movie in sorted_movies_list:
try:
# check if genre matches genre supplied via argument
if genre and not misc_helper.allowed_genres(genre, 'movie', movie):
log.debug("Skipping: %s because it was not from %s genre(s)", movie['movie']['title'], genre.lower())
continue
# check if movie passes out blacklist criteria inspection
if not trakt_helper.is_movie_blacklisted(movie, cfg.filters.movies, ignore_blacklist,
callback_remove_recommended if remove_rejected_from_recommended
else None):
# Assuming the movie is not blacklisted, proceed to pull RT score if the user wishes to restrict
movieRating = None
if (rating != None and cfg['omdb']['api_key'] != ''):
movieRating = rating_helper.get_rating(cfg['omdb']['api_key'],movie)
if (movieRating == -1):
log.debug("Skipping: %s because it did not have a rating/lacked imdbID",
movie['movie']['title'])
continue
if (rating == None or movieRating >= rating):
log.info("Adding: %s (%d) | Genres: %s | Country: %s", movie['movie']['title'], movie['movie']['year'],
', '.join(movie['movie']['genres']), movie['movie']['country'].upper())
# add movie to radarr
if radarr.add_movie(movie['movie']['ids']['tmdb'], movie['movie']['title'], movie['movie']['year'],
movie['movie']['ids']['slug'], profile_id, cfg.radarr.root_folder, not no_search):
log.info("ADDED %s (%d)", movie['movie']['title'], movie['movie']['year'])
if notifications:
callback_notify({'event': 'add_movie', 'list_type': list_type, 'movie': movie['movie']})
added_movies += 1
else:
log.error("FAILED adding %s (%d)", movie['movie']['title'], movie['movie']['year'])
else:
log.info("SKIPPING: %s (%d) | Genres: %s | Country: %s", movie['movie']['title'],
movie['movie']['year'],
', '.join(movie['movie']['genres']), movie['movie']['country'].upper())
# stop adding movies, if added_movies >= add_limit
if add_limit and added_movies >= add_limit:
break
# sleep before adding any more
time.sleep(add_delay)
except Exception:
log.exception("Exception while processing movie %s: ", movie['movie']['title'])
log.info("Added %d new movie(s) to Radarr", added_movies)
# send notification
if notifications and (cfg.notifications.verbose or added_movies > 0):
notify.send(message="Added %d movies from Trakt's %s list" % (added_movies, list_type))
return added_movies
############################################################
# CALLBACKS
############################################################
def callback_remove_recommended(media_type, media_info):
from media.trakt import Trakt
trakt = Trakt(cfg)
if not media_info[media_type]['title'] or not media_info[media_type]['year']:
log.debug("Skipping removing %s item from recommended list as no title/year was available:\n%s", media_type,
media_info)
return
media_name = '%s (%d)' % (media_info[media_type]['title'], media_info[media_type]['year'])
if trakt.remove_recommended_item(media_type, media_info[media_type]['ids']['trakt']):
log.info("Removed rejected recommended %s: %s", media_type, media_name)
else:
log.info("FAILED removing rejected recommended %s: %s", media_type, media_name)
def callback_notify(data):
log.debug("Received callback data: %s", data)
# handle event
if data['event'] == 'add_movie':
if cfg.notifications.verbose:
notify.send(
message="Added %s movie: %s (%d)" % (data['list_type'], data['movie']['title'], data['movie']['year']))
return
elif data['event'] == 'add_show':
if cfg.notifications.verbose:
notify.send(
message="Added %s show: %s (%d)" % (data['list_type'], data['show']['title'], data['show']['year']))
return
elif data['event'] == 'abort':
notify.send(message="Aborted adding Trakt %s %s due to: %s" % (data['list_type'], data['type'], data['reason']))
return
elif data['event'] == 'error':
notify.send(message="Error: %s" % data['reason'])
return
else:
log.error("Unexpected callback: %s", data)
return
############################################################
# AUTOMATIC
############################################################
def automatic_shows(add_delay=2.5, sort='votes', no_search=False, notifications=False, ignore_blacklist=False):
from media.trakt import Trakt
total_shows_added = 0
try:
log.info("Started")
for list_type, value in cfg.automatic.shows.items():
added_shows = None
if list_type.lower() == 'interval':
continue
if list_type.lower() in Trakt.non_user_lists or (
'_' in list_type and list_type.lower().partition("_")[0] in Trakt.non_user_lists):
limit = value
if limit <= 0:
log.info("Skipped Trakt's %s shows list", list_type)
continue
else:
log.info("Adding %d shows from Trakt's %s list", limit, list_type)
local_ignore_blacklist = ignore_blacklist
if list_type.lower() in cfg.filters.shows.disabled_for:
local_ignore_blacklist = True
# run shows
added_shows = shows.callback(list_type=list_type, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications, ignore_blacklist=local_ignore_blacklist)
elif list_type.lower() == 'watchlist':
for authenticate_user, limit in value.items():
if limit <= 0:
log.info("Skipped Trakt's %s for %s", list_type, authenticate_user)
continue
else:
log.info("Adding %d shows from the %s from %s", limit, list_type, authenticate_user)
local_ignore_blacklist = ignore_blacklist
if "watchlist:%s".format(authenticate_user) in cfg.filters.shows.disabled_for:
local_ignore_blacklist = True
# run shows
added_shows = shows.callback(list_type=list_type, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications, authenticate_user=authenticate_user,
ignore_blacklist=local_ignore_blacklist)
elif list_type.lower() == 'lists':
for list, v in value.items():
if isinstance(v, dict):
authenticate_user = v['authenticate_user']
limit = v['limit']
else:
authenticate_user = None
limit = v
local_ignore_blacklist = ignore_blacklist
if "list:%s".format(list) in cfg.filters.shows.disabled_for:
local_ignore_blacklist = True
# run shows
added_shows = shows.callback(list_type=list, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications, authenticate_user=authenticate_user,
ignore_blacklist=local_ignore_blacklist)
if added_shows is None:
log.error("Failed adding shows from Trakt's %s list", list_type)
time.sleep(10)
continue
total_shows_added += added_shows
# sleep
time.sleep(10)
log.info("Finished, added %d shows total to Sonarr!", total_shows_added)
# send notification
if notifications and (cfg.notifications.verbose or total_shows_added > 0):
notify.send(message="Added %d shows total to Sonarr!" % total_shows_added)
except Exception:
log.exception("Exception while automatically adding shows: ")
return
def automatic_movies(add_delay=2.5, sort='votes', no_search=False, notifications=False, ignore_blacklist=False,rating_limit=None):
from media.trakt import Trakt
total_movies_added = 0
try:
log.info("Started")
for list_type, value in cfg.automatic.movies.items():
added_movies = None
if list_type.lower() == 'interval':
continue
if list_type.lower() in Trakt.non_user_lists or (
'_' in list_type and list_type.lower().partition("_")[0] in Trakt.non_user_lists):
limit = value
if limit <= 0:
log.info("Skipped Trakt's %s movies list", list_type)
continue
else:
log.info("Adding %d movies from Trakt's %s list", limit, list_type)
local_ignore_blacklist = ignore_blacklist
if list_type.lower() in cfg.filters.movies.disabled_for:
local_ignore_blacklist = True
# run movies
added_movies = movies.callback(list_type=list_type, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications,rating=rating_limit)
elif list_type.lower() == 'watchlist':
for authenticate_user, limit in value.items():
if limit <= 0:
log.info("Skipped Trakt's %s for %s", list_type, authenticate_user)
continue
else:
log.info("Adding %d movies from the %s from %s", limit, list_type, authenticate_user)
local_ignore_blacklist = ignore_blacklist
if "watchlist:%s".format(authenticate_user) in cfg.filters.movies.disabled_for:
local_ignore_blacklist = True
# run movies
added_movies = movies.callback(list_type=list_type, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications, authenticate_user=authenticate_user,
ignore_blacklist=local_ignore_blacklist,rating=rating_limit)
elif list_type.lower() == 'lists':
for list, v in value.items():
if isinstance(v, dict):
authenticate_user = v['authenticate_user']
limit = v['limit']
else:
authenticate_user = None
limit = v
local_ignore_blacklist = ignore_blacklist
if "list:%s".format(list) in cfg.filters.movies.disabled_for:
local_ignore_blacklist = True
# run shows
added_movies = movies.callback(list_type=list, add_limit=limit,
add_delay=add_delay, sort=sort, no_search=no_search,
notifications=notifications, authenticate_user=authenticate_user,
ignore_blacklist=local_ignore_blacklist,rating=rating_limit)
if added_movies is None:
log.error("Failed adding movies from Trakt's %s list", list_type)
time.sleep(10)
continue
total_movies_added += added_movies
# sleep
time.sleep(10)
log.info("Finished, added %d movies total to Radarr!", total_movies_added)
# send notification
if notifications and (cfg.notifications.verbose or total_movies_added > 0):
notify.send(message="Added %d movies total to Radarr!" % total_movies_added)
except Exception:
log.exception("Exception while automatically adding movies: ")
return
@app.command(help='Run in automatic mode.')
@click.option('--add-delay', '-d', default=2.5, help='Seconds between each add request to Sonarr / Radarr.',
show_default=True)
@click.option('--sort', '-s', default='votes', type=click.Choice(['votes', 'rating', 'release']),
help='Sort list to process.')
@click.option('--no-search', is_flag=True, help='Disable search when adding to Sonarr / Radarr.')
@click.option('--run-now', is_flag=True, help="Do a first run immediately without waiting.")
@click.option('--no-notifications', is_flag=True, help="Disable notifications.")
@click.option('--ignore-blacklist', is_flag=True, help='Ignores the blacklist when running the command.')
def run(add_delay=2.5, sort='votes', no_search=False, run_now=False, no_notifications=False, ignore_blacklist=False):
log.info("Automatic mode is now running...")
# Add tasks to schedule and do first run if enabled
if cfg.automatic.movies.interval:
movie_schedule = schedule.every(cfg.automatic.movies.interval).hours.do(
automatic_movies,
add_delay,
sort,
no_search,
not no_notifications,
ignore_blacklist,
int(cfg.filters.movies.rating_limit) if cfg.filters.movies.rating_limit != "" else None
)
if run_now:
movie_schedule.run()
# Sleep between tasks
time.sleep(add_delay)
if cfg.automatic.shows.interval:
shows_schedule = schedule.every(cfg.automatic.shows.interval).hours.do(
automatic_shows,
add_delay,
sort,
no_search,
not no_notifications,
ignore_blacklist
)
if run_now:
shows_schedule.run()
# Sleep between tasks
time.sleep(add_delay)
# Enter running schedule
while True:
try:
# Sleep until next run
log.info("Next job at %s", schedule.next_run())
time.sleep(max(schedule.idle_seconds(), 0))
# Check jobs to run
schedule.run_pending()
except Exception as e:
log.exception("Unhandled exception occurred while processing scheduled tasks: %s", e)
time.sleep(1)
############################################################
# MISC
############################################################
def init_notifications():
try:
for notification_name, notification_config in cfg.notifications.items():
if notification_name.lower() == 'verbose':
continue
notify.load(**notification_config)
except Exception:
log.exception("Exception initializing notification agents: ")
return
# Handles exit signals, cancels jobs and exits cleanly
def exit_handler(signum, frame):
log.info("Received %s, canceling jobs and exiting.", signal.Signals(signum).name)
schedule.clear()
exit()
############################################################
# MAIN
############################################################
if __name__ == "__main__":
print("")
f = Figlet(font='graffiti')
print(f.renderText('traktarr'))
print("""
#########################################################################
# Author: l3uddz #
# URL: https://github.com/l3uddz/traktarr #
# -- #
# Part of the Cloudbox project: https://cloudbox.works #
#########################################################################
# GNU General Public License v3.0 #
#########################################################################
""")
# Register the signal handlers
signal.signal(signal.SIGTERM, exit_handler)
signal.signal(signal.SIGINT, exit_handler)
# Start application
app()