diff --git a/media/sonarr.py b/media/sonarr.py index b032172..7030f2f 100644 --- a/media/sonarr.py +++ b/media/sonarr.py @@ -99,12 +99,35 @@ class Sonarr: return None @backoff.on_predicate(backoff.expo, lambda x: x is None, max_tries=4, on_backoff=backoff_handler) - def add_series(self, series_tvdbid, series_title, series_title_slug, profile_id, root_folder, search_missing=False): + def get_tags(self): + tags = {} + try: + # make request + req = requests.get(urljoin(self.server_url, 'api/tag'), headers=self.headers, timeout=30) + log.debug("Request URL: %s", req.url) + log.debug("Request Response: %d", req.status_code) + + if req.status_code == 200: + resp_json = req.json() + log.debug("Found %d tags", len(resp_json)) + for tag in resp_json: + tags[tag['label']] = tag['id'] + return tags + else: + log.error("Failed to retrieve all tags, request response: %d", req.status_code) + except Exception: + log.exception("Exception retrieving tags: ") + return None + + @backoff.on_predicate(backoff.expo, lambda x: x is None, max_tries=4, on_backoff=backoff_handler) + def add_series(self, series_tvdbid, series_title, series_title_slug, profile_id, root_folder, tag_ids=None, + search_missing=False): try: # generate payload payload = { 'tvdbId': series_tvdbid, 'title': series_title, 'titleSlug': series_title_slug, - 'qualityProfileId': profile_id, 'images': [], + 'qualityProfileId': profile_id, 'tags': [] if not tag_ids or not isinstance(tag_ids, list) else tag_ids, + 'images': [], 'seasons': [], 'seasonFolder': True, 'monitored': True, 'rootFolderPath': root_folder, 'addOptions': {'ignoreEpisodesWithFiles': False, diff --git a/misc/config.py b/misc/config.py index 7a59244..00f6fab 100644 --- a/misc/config.py +++ b/misc/config.py @@ -16,7 +16,11 @@ base_config = { 'url': 'http://localhost:8989', 'api_key': '', 'profile': 'HD-1080p', - 'root_folder': '/tv/' + 'root_folder': '/tv/', + 'tags': { + 'amzn': ['hbo', 'amc', 'usa network', 'tnt', 'starz', 'the cw', 'fxx', 'fox', 'abc', 'nbc', 'cbs', 'tbs', + 'amazon', 'syfy', 'cinemax'] + } }, 'radarr': { 'url': 'http://localhost:7878', diff --git a/misc/helpers.py b/misc/helpers.py index 5773415..c02bf97 100644 --- a/misc/helpers.py +++ b/misc/helpers.py @@ -8,6 +8,18 @@ log = logger.get_logger(__name__) # SONARR ############################################################ +def sonarr_series_tag_id_from_network(profile_tags, network_tags, network): + try: + for tag_name, tag_networks in network_tags.items(): + for tag_network in tag_networks: + if tag_network.lower() in network.lower() and tag_name.lower() in profile_tags: + log.debug("Using %s tag for network: %s", tag_name, network) + return [profile_tags[tag_name.lower()]] + except Exception: + log.exception("Exception determining tag to use for network %s: ", network) + return None + + def sonarr_series_to_tvdb_dict(sonarr_series): series = {} try: diff --git a/traktarr.py b/traktarr.py index 2b97e30..92afffd 100644 --- a/traktarr.py +++ b/traktarr.py @@ -61,6 +61,14 @@ def shows(list_type, add_limit=0, add_delay=2.5, no_search=False): else: log.info("Retrieved Profile ID for %s: %d", cfg.sonarr.profile, profile_id) + # retrieve profile tags + profile_tags = sonarr.get_tags() + if profile_tags is None: + log.error("Aborting due to failure to retrieve Tag ID's") + return + else: + log.info("Retrieved %d Tag ID's", len(profile_tags)) + # get sonarr series list sonarr_series_list = sonarr.get_series() if not sonarr_series_list: @@ -109,13 +117,18 @@ def shows(list_type, add_limit=0, add_delay=2.5, no_search=False): ', '.join(series['show']['genres']), series['show']['network'], series['show']['country'].upper()) + # determine which tags to use when adding this series + use_tags = helpers.sonarr_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, not no_search): - log.info("ADDED %s (%d)", series['show']['title'], series['show']['year']) + 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'], use_tags) added_shows += 1 else: - log.error("FAILED adding %s (%d)", series['show']['title'], series['show']['year']) + log.error("FAILED adding %s (%d) with tags: %s", series['show']['title'], series['show']['year'], + use_tags) # stop adding shows, if added_shows >= add_limit if add_limit and added_shows >= add_limit: