forked from l3uddz/traktarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traktarr.py
executable file
·1698 lines (1431 loc) · 60.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
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
#!/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.5', 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(
'--cachefile',
envvar='TRAKTARR_CACHEFILE',
type=click.Path(file_okay=True, dir_okay=False),
help='Cache file',
show_default=True,
default=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "cache.db")
)
@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, cachefile, logfile):
# Setup global variables
global cfg, log, notify
# Load config
from misc.config import Config
cfg = Config(configfile=config, cachefile=cachefile, logfile=logfile).cfg
# Legacy Support
if cfg.filters.movies.blacklist_title_keywords:
cfg['filters']['movies']['blacklisted_title_keywords'] = cfg['filters']['movies']['blacklist_title_keywords']
if cfg.filters.movies.rating_limit:
cfg['filters']['movies']['rotten_tomatoes'] = cfg['filters']['movies']['rating_limit']
if cfg.radarr.profile:
cfg['radarr']['quality'] = cfg['radarr']['profile']
if cfg.sonarr.profile:
cfg['sonarr']['quality'] = cfg['sonarr']['profile']
# 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):
log.info("Validating Trakt API Key...")
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, pvr_type, notifications):
if not pvr.validate_api_key():
log.error("Aborting due to failure to validate %s URL / API Key", pvr_type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to validate %s URL / API Key' % pvr_type})
return None
else:
log.info("Validated %s URL & API Key.", pvr_type)
def get_quality_profile_id(pvr, quality_profile):
# retrieve profile id for requested quality profile
quality_profile_id = pvr.get_quality_profile_id(quality_profile)
if not quality_profile_id or quality_profile_id <= 0:
log.error("Aborting due to failure to retrieve Quality Profile ID for: %s", quality_profile)
exit()
log.info("Retrieved Quality Profile ID for \'%s\': %d", quality_profile, quality_profile_id)
return quality_profile_id
def get_language_profile_id(pvr, language_profile):
# retrieve profile id for requested language profile
language_profile_id = pvr.get_language_profile_id(language_profile)
if not language_profile_id or language_profile_id <= 0:
log.error("No Language Profile ID for: %s", language_profile)
else:
log.info("Retrieved Language Profile ID for \'%s\': %d", language_profile, language_profile_id)
return language_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 IDs")
exit()
log.info("Retrieved Sonarr Tag IDs: %d", len(profile_tags))
return profile_tags
def get_objects(pvr, pvr_type, notifications):
objects_list = pvr.get_objects()
objects_type = 'movies' if pvr_type.lower() == 'radarr' else 'shows'
if not objects_list:
log.error("Aborting due to failure to retrieve %s list from %s", objects_type, pvr_type)
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to retrieve \'%s\' list from %s' % (objects_type,
pvr_type)})
exit()
log.info("Retrieved %s %s list, %s found: %d", pvr_type, objects_type, objects_type, len(objects_list))
return objects_list
def get_exclusions(pvr, pvr_type):
objects_list = pvr.get_exclusions()
objects_type = 'movie' if pvr_type.lower() == 'radarr' else 'show'
if not objects_list:
log.info("No %s exclusions list found from %s", objects_type, pvr_type)
log.info("Retrieved %s %s list, %s found: %d", pvr_type, objects_type, objects_type, len(objects_list))
return objects_list
############################################################
# SHOWS
############################################################
@app.command(help='Add a single show to Sonarr.', context_settings=dict(max_content_width=100))
@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
from helpers import str as misc_str
# 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)
# 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
# set common series variables
series_title = trakt_show['title']
# convert series year to string
if trakt_show['year']:
series_year = str(trakt_show['year'])
elif trakt_show['first_aired']:
series_year = misc_str.get_year_from_timestamp(trakt_show['first_aired'])
else:
series_year = '????'
log.info("Retrieved Trakt show information for \'%s\': \'%s (%s)\'", show_id, series_title, series_year)
# quality profile id
quality_profile_id = get_quality_profile_id(sonarr, cfg.sonarr.quality)
# language profile id
language_profile_id = get_language_profile_id(sonarr, cfg.sonarr.language)
# profile tags
profile_tags = None
tag_ids = None
tag_names = None
if cfg.sonarr.tags is not None:
profile_tags = get_profile_tags(sonarr)
if profile_tags is not None:
# determine which tags to use when adding this series
tag_ids = sonarr_helper.series_tag_ids_list_builder(
profile_tags,
cfg.sonarr.tags,
)
tag_names = sonarr_helper.series_tag_names_list_builder(
profile_tags,
tag_ids,
)
# series type
if any('anime' in s.lower() for s in trakt_show['genres']):
series_type = 'anime'
else:
series_type = 'standard'
log.debug("Set series type for \'%s (%s)\' to: %s", series_title, series_year, series_type.title())
# add show to sonarr
if sonarr.add_series(
trakt_show['ids']['tvdb'],
series_title,
trakt_show['ids']['slug'],
quality_profile_id,
language_profile_id,
cfg.sonarr.root_folder,
cfg.sonarr.season_folder,
tag_ids,
not no_search,
series_type,
):
if profile_tags is not None and tag_names is not None:
log.info("ADDED: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year,
tag_names)
else:
log.info("ADDED: \'%s (%s)\'", series_title, series_year)
elif profile_tags is not None:
log.error("FAILED ADDING: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year,
tag_names)
else:
log.info("FAILED ADDING: \'%s (%s)\'", series_title, series_year)
return
@app.command(help='Add multiple shows to Sonarr.', context_settings=dict(max_content_width=100))
@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.')
@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(['rating', 'release', 'votes']),
help='Sort list to process.',
show_default=True)
@click.option(
'--year', '--years', '-y',
default=None,
help='Can be a specific year or a range of years to search. For example, \'2000\' or \'2000-2010\'.')
@click.option(
'--genres', '-g',
default=None,
help='Only add shows from this genre to Sonarr. '
'Multiple genres are specified as a comma-separated list. '
'Use \'ignore\' to add shows from any genre, including ones with no genre specified.')
@click.option(
'--folder', '-f',
default=None,
help='Add shows with this root folder to Sonarr.')
@click.option(
'--person', '-p',
default=None,
help='Only add shows from this person (e.g. actor) to Sonarr. '
'Only one person can be specified. '
'Requires the \'person\' list type.')
@click.option(
'--include-non-acting-roles',
is_flag=True,
help='Include non-acting roles such as \'Director\', \'As Himself\', \'Narrator\', etc. '
'Requires the \'person\' list type with the \'person\' argument.')
@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. '
'Defaults to 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.')
@click.option(
'--dry-run',
is_flag=True,
help='Shows the list of shows remaining after processing, takes no action on them.')
def shows(
list_type,
add_limit=0,
add_delay=2.5,
sort='votes',
years=None,
genres=None,
folder=None,
person=None,
no_search=False,
include_non_acting_roles=False,
notifications=False,
authenticate_user=None,
ignore_blacklist=False,
remove_rejected_from_recommended=False,
dry_run=False,
):
from media.sonarr import Sonarr
from media.trakt import Trakt
from helpers import str as misc_str
from helpers import misc as misc_helper
from helpers import sonarr as sonarr_helper
from helpers import trakt as trakt_helper
from helpers import tvdb as tvdb_helper
from helpers import parameter as parameter_helper
added_shows = 0
# process countries
if not cfg.filters.shows.allowed_countries or 'ignore' in cfg.filters.shows.allowed_countries:
countries = None
else:
countries = cfg.filters.shows.allowed_countries
# process languages
if not cfg.filters.shows.allowed_languages or 'ignore' in cfg.filters.shows.allowed_languages:
languages = None
else:
languages = cfg.filters.shows.allowed_languages
# process genres
if genres:
# split comma separated list
genres = sorted(genres.split(','), key=str.lower)
# look for special keyword 'ignore'
if 'ignore' in genres:
# set special genre keyword to show's blacklisted_genres list
cfg['filters']['shows']['blacklisted_genres'] = ['ignore']
genres = None
else:
# remove genres from show's blacklisted_genres list
misc_helper.unblacklist_genres(genres, cfg['filters']['shows']['blacklisted_genres'])
log.debug("Filter Trakt results with genre(s): %s", ', '.join(map(lambda x: x.title(), genres)))
# process years parameter
years, new_min_year, new_max_year = parameter_helper.years(
years,
cfg.filters.shows.blacklisted_min_year,
cfg.filters.shows.blacklisted_max_year,
)
cfg['filters']['shows']['blacklisted_min_year'] = new_min_year
cfg['filters']['shows']['blacklisted_max_year'] = new_max_year
# runtimes range
if cfg.filters.shows.blacklisted_min_runtime:
min_runtime = cfg.filters.shows.blacklisted_min_runtime
else:
min_runtime = 0
if cfg.filters.shows.blacklisted_max_runtime and cfg.filters.shows.blacklisted_max_runtime >= min_runtime:
max_runtime = cfg.filters.shows.blacklisted_max_runtime
else:
max_runtime = 9999
if min_runtime == 0 and max_runtime == 9999:
runtimes = None
else:
runtimes = str(min_runtime) + '-' + str(max_runtime)
# 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)
# quality profile id
quality_profile_id = get_quality_profile_id(sonarr, cfg.sonarr.quality)
# language profile id
language_profile_id = get_language_profile_id(sonarr, cfg.sonarr.language)
# profile tags
profile_tags = None
tag_ids = None
tag_names = None
if cfg.sonarr.tags is not None:
profile_tags = get_profile_tags(sonarr)
if profile_tags is not None:
# determine which tags to use when adding this series
tag_ids = sonarr_helper.series_tag_ids_list_builder(
profile_tags,
cfg.sonarr.tags,
)
tag_names = sonarr_helper.series_tag_names_list_builder(
profile_tags,
tag_ids,
)
pvr_objects_list = get_objects(sonarr, 'Sonarr', notifications)
# get trakt series list
if list_type.lower() == 'anticipated':
trakt_objects_list = trakt.get_anticipated_shows(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
)
elif list_type.lower() == 'trending':
trakt_objects_list = trakt.get_trending_shows(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
)
elif list_type.lower() == 'popular':
trakt_objects_list = trakt.get_popular_shows(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
)
elif list_type.lower() == 'person':
if not person:
log.error("You must specify an person with the \'--person\' / \'-p\' parameter when using the \'person\'" +
" list type!")
return None
trakt_objects_list = trakt.get_person_shows(
years=years,
person=person,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
include_non_acting_roles=include_non_acting_roles,
)
elif list_type.lower() == 'recommended':
trakt_objects_list = trakt.get_recommended_shows(
authenticate_user,
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
)
elif list_type.lower().startswith('played'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_played_shows(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
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(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
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.capitalize())
if notifications:
callback_notify(
{'event': 'abort', 'type': 'shows', 'list_type': list_type,
'reason': 'Failure to retrieve Trakt \'%s\' shows list.' % list_type.capitalize()})
return None
else:
log.info("Retrieved Trakt \'%s\' shows list, shows found: %d", list_type.capitalize(), 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_from_trakt_list(
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.capitalize()})
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 recent '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:
# noinspection PyBroadException
# set common series variables
series_tvdb_id = series['show']['ids']['tvdb']
series_title = series['show']['title']
# convert series year to string
if series['show']['year']:
series_year = str(series['show']['year'])
elif series['show']['first_aired']:
series_year = misc_str.get_year_from_timestamp(series['show']['first_aired'])
else:
series_year = '????'
# series type
if any('anime' in s.lower() for s in series['show']['genres']):
series_type = 'anime'
else:
series_type = 'standard'
log.debug("Set series type for \'%s (%s)\' to: %s", series_title, series_year, series_type.title())
# build list of genres
series_genres = (', '.join(series['show']['genres'])).title() if series['show']['genres'] else 'N/A'
try:
# check if movie has a valid TVDB ID and that it exists on TVDB
if not tvdb_helper.check_series_tvdb_id(series_title, series_year, series_tvdb_id):
continue
# check if genres matches genre(s) supplied via argument
if genres and not misc_helper.allowed_genres(genres, 'show', series):
log.debug("SKIPPING: \'%s (%s)\' because it was not from the genre(s): %s", series_title,
series_year, ', '.join(map(lambda x: x.title(), genres)))
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 (%s) | Country: %s | Language: %s | Genre(s): %s | Network: %s",
series_title,
series_year,
(series['show']['country'] or 'N/A').upper(),
(series['show']['language'] or 'N/A').upper(),
series_genres,
(series['show']['network'] or 'N/A').upper(),
)
if dry_run:
log.info("dry-run: SKIPPING")
else:
# add show to sonarr
if sonarr.add_series(
series['show']['ids']['tvdb'],
series_title,
series['show']['ids']['slug'],
quality_profile_id,
language_profile_id,
cfg.sonarr.root_folder,
cfg.sonarr.season_folder,
tag_ids,
not no_search,
series_type,
):
if profile_tags is not None and tag_names is not None:
log.info("ADDED: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year,
tag_names)
else:
log.info("ADDED: \'%s (%s)\'", series_title, series_year)
if notifications:
callback_notify({'event': 'add_show', 'list_type': list_type, 'show': series['show']})
added_shows += 1
else:
if profile_tags is not None:
log.error("FAILED ADDING: \'%s (%s)\' with Sonarr Tags: %s", series_title, series_year,
tag_names)
else:
log.info("FAILED ADDING: \'%s (%s)\'", series_title, series_year)
continue
else:
log.info("SKIPPED: \'%s (%s)\'", series_title, series_year)
continue
# 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_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.', context_settings=dict(max_content_width=100))
@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(
'--minimum-availability', '-ma',
type=click.Choice(['announced', 'in_cinemas', 'released']),
help='Add movies with this minimum availability to Radarr. Default is \'released\'.')
@click.option(
'--no-search',
is_flag=True,
help='Disable search when adding movie to Radarr.')
def movie(
movie_id,
folder=None,
minimum_availability=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
log.debug('Set root folder to: \'%s\'', cfg['radarr']['root_folder'])
# replace radarr.minimum_availability if minimum_availability is supplied
valid_min_avail = ['announced', 'in_cinemas', 'released']
if minimum_availability:
cfg['radarr']['minimum_availability'] = minimum_availability
elif cfg['radarr']['minimum_availability'] not in valid_min_avail:
cfg['radarr']['minimum_availability'] = 'released'
log.debug('Set minimum availability to: \'%s\'', cfg['radarr']['minimum_availability'])
# 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)
# quality profile id
quality_profile_id = get_quality_profile_id(radarr, cfg.radarr.quality)
# 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
# convert movie year to string
movie_year = str(trakt_movie['year']) if trakt_movie['year'] else '????'
log.info("Retrieved Trakt movie information for \'%s\': \'%s (%s)\'", movie_id, trakt_movie['title'], movie_year)
# add movie to radarr
if radarr.add_movie(
trakt_movie['ids']['tmdb'],
trakt_movie['title'],
trakt_movie['year'],
trakt_movie['ids']['slug'],
quality_profile_id,
cfg.radarr.root_folder,
cfg.radarr.minimum_availability,
not no_search,
):
log.info("ADDED \'%s (%s)\'", trakt_movie['title'], movie_year)
else:
log.error("FAILED ADDING \'%s (%s)\'", trakt_movie['title'], movie_year)
return
@app.command(help='Add multiple movies to Radarr.', context_settings=dict(max_content_width=100))
@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 movies added to Radarr.')
@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(['rating', 'release', 'votes']),
help='Sort list to process.', show_default=True)
@click.option(
'--rotten_tomatoes', '-rt',
default=None,
type=int,
help='Set a minimum Rotten Tomatoes score.')
@click.option(
'--year', '--years', '-y',
default=None,
help='Can be a specific year or a range of years to search. For example, \'2000\' or \'2000-2010\'.')
@click.option(
'--genres', '-g',
default=None,
help='Only add movies from this genre to Radarr. '
'Multiple genres are specified as a comma-separated list. '
'Use \'ignore\' to add movies from any genre, including ones with no genre specified.')
@click.option(
'--folder', '-f',
default=None,
help='Add movies with this root folder to Radarr.')
@click.option(
'--minimum-availability', '-ma',
type=click.Choice(['announced', 'in_cinemas', 'released']),
help='Add movies with this minimum availability to Radarr. Default is \'released\'.')
@click.option(
'--person', '-p',
default=None,
help='Only add movies from this person (e.g. actor) to Radarr. '
'Only one person can be specified. '
'Requires the \'person\' list type.')
@click.option(
'--include-non-acting-roles',
is_flag=True,
help='Include non-acting roles such as \'Director\', \'As Himself\', \'Narrator\', etc. '
'Requires the \'person\' list type with the \'person\' argument.')
@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. '
'Defaults to 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.')
@click.option(
'--dry-run',
is_flag=True,
help='Shows the list of movies remaining after processing, takes no action on them.')
def movies(
list_type,
add_limit=0,
add_delay=2.5,
sort='votes',
rotten_tomatoes=None,
years=None,
genres=None,
folder=None,
minimum_availability=None,
person=None,
include_non_acting_roles=False,
no_search=False,
notifications=False,
authenticate_user=None,
ignore_blacklist=False,
remove_rejected_from_recommended=False,
dry_run=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 omdb as omdb_helper
from helpers import tmdb as tmdb_helper
from helpers import parameter as parameter_helper
added_movies = 0
# process countries
if not cfg.filters.movies.allowed_countries or 'ignore' in cfg.filters.movies.allowed_countries:
countries = None
else:
countries = cfg.filters.movies.allowed_countries
# process languages
if not cfg.filters.movies.allowed_languages or 'ignore' in cfg.filters.movies.allowed_languages:
languages = None
else:
languages = cfg.filters.movies.allowed_languages
# process genres
if genres:
# split comma separated list
genres = sorted(genres.split(','), key=str.lower)
# look for special keyword 'ignore'
if 'ignore' in genres:
# set special keyword 'ignore' to movies's blacklisted_genres list
cfg['filters']['movies']['blacklisted_genres'] = ['ignore']
# set genre search parameter to None
genres = None
else:
# remove genre from movies's blacklisted_genres list, if it's there
misc_helper.unblacklist_genres(genres, cfg['filters']['movies']['blacklisted_genres'])
log.debug("Filter Trakt results with genre(s): %s", ', '.join(map(lambda x: x.title(), genres)))
# process years parameter
years, new_min_year, new_max_year = parameter_helper.years(
years,
cfg.filters.movies.blacklisted_min_year,
cfg.filters.movies.blacklisted_max_year,
)
cfg['filters']['movies']['blacklisted_min_year'] = new_min_year
cfg['filters']['movies']['blacklisted_max_year'] = new_max_year
# runtimes range
if cfg.filters.movies.blacklisted_min_runtime:
min_runtime = cfg.filters.movies.blacklisted_min_runtime
else:
min_runtime = 0
if cfg.filters.movies.blacklisted_max_runtime and cfg.filters.movies.blacklisted_max_runtime >= min_runtime:
max_runtime = cfg.filters.movies.blacklisted_max_runtime
else:
max_runtime = 9999
if min_runtime == 0 and max_runtime == 9999:
runtimes = None
else:
runtimes = str(min_runtime) + '-' + str(max_runtime)
# replace radarr root_folder if folder is supplied
if folder:
cfg['radarr']['root_folder'] = folder
log.debug('Set root folder to: \'%s\'', cfg['radarr']['root_folder'])
# replace radarr.minimum_availability if minimum_availability is supplied
valid_min_avail = ['announced', 'in_cinemas', 'released']
if minimum_availability:
cfg['radarr']['minimum_availability'] = minimum_availability
elif cfg['radarr']['minimum_availability'] not in valid_min_avail:
cfg['radarr']['minimum_availability'] = 'released'
log.debug('Set minimum availability to: \'%s\'', cfg['radarr']['minimum_availability'])
# 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)
# quality profile id
quality_profile_id = get_quality_profile_id(radarr, cfg.radarr.quality)
pvr_objects_list = get_objects(radarr, 'Radarr', notifications)
pvr_exclusions_list = get_exclusions(radarr, 'Radarr')
# get trakt movies list
if list_type.lower() == 'anticipated':
trakt_objects_list = trakt.get_anticipated_movies(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,
)
elif list_type.lower() == 'trending':
trakt_objects_list = trakt.get_trending_movies(
years=years,
countries=countries,
languages=languages,
genres=genres,
runtimes=runtimes,