Plex-Meta-Manager/modules/sonarr.py

74 lines
3 KiB
Python
Raw Normal View History

2021-06-14 01:51:30 +00:00
import logging
2021-01-20 21:37:59 +00:00
from modules import util
from modules.util import Failed
2021-06-14 01:51:30 +00:00
from arrapi import SonarrAPI
from arrapi.exceptions import ArrException, Invalid
2021-01-20 21:37:59 +00:00
logger = logging.getLogger("Plex Meta Manager")
series_type = ["standard", "daily", "anime"]
monitor_translation = {
"all": "all",
"future": "future",
"missing": "missing",
"existing": "existing",
"pilot": "pilot",
"first": "firstSeason",
"latest": "latestSeason",
"none": "none"
}
2021-06-14 01:51:30 +00:00
class Sonarr:
def __init__(self, params):
self.url = params["url"]
2021-04-02 20:15:59 +00:00
self.token = params["token"]
2021-01-20 21:37:59 +00:00
try:
2021-06-14 01:51:30 +00:00
self.api = SonarrAPI(self.url, self.token)
except ArrException as e:
raise Failed(e)
self.add = params["add"]
self.root_folder_path = params["root_folder_path"]
self.monitor = params["monitor"]
2021-06-14 01:51:30 +00:00
self.quality_profile = params["quality_profile"]
2021-04-02 15:45:29 +00:00
self.language_profile_id = None
2021-06-14 01:51:30 +00:00
self.language_profile = params["language_profile"]
self.series_type = params["series_type"]
2021-03-01 22:25:38 +00:00
self.season_folder = params["season_folder"]
2021-02-16 14:31:15 +00:00
self.tag = params["tag"]
self.search = params["search"]
self.cutoff_search = params["cutoff_search"]
2021-04-02 20:15:59 +00:00
def add_tvdb(self, tvdb_ids, **options):
2021-05-24 03:38:46 +00:00
logger.info("")
util.separator(f"Adding to Sonarr", space=False, border=False)
2021-01-20 21:37:59 +00:00
logger.info("")
2021-02-24 06:44:06 +00:00
logger.debug(f"TVDb IDs: {tvdb_ids}")
2021-06-14 01:51:30 +00:00
logger.debug("")
folder = options["folder"] if "folder" in options else self.root_folder_path
2021-06-14 01:51:30 +00:00
monitor = monitor_translation[options["monitor"] if "monitor" in options else self.monitor]
quality_profile = options["quality"] if "quality" in options else self.quality_profile
language_profile = options["language"] if "language" in options else self.language_profile
language_profile = language_profile if self.api.v3 else 1
series = options["series"] if "series" in options else self.series_type
season = options["season"] if "season" in options else self.season_folder
tags = options["tag"] if "tag" in options else self.tag
search = options["search"] if "search" in options else self.search
cutoff_search = options["cutoff_search"] if "cutoff_search" in options else self.cutoff_search
2021-06-14 01:51:30 +00:00
try:
added, exists, invalid = self.api.add_multiple_series(tvdb_ids, folder, quality_profile, language_profile, monitor, season, search, cutoff_search, series, tags)
except Invalid as e:
raise Failed(f"Sonarr Error: {e}")
2021-04-05 04:09:26 +00:00
2021-06-14 01:51:30 +00:00
if len(added) > 0:
for series in added:
logger.info(f"Added to Sonarr | {series.tvdbId:<6} | {series.title}")
logger.info(f"{len(added)} Series added to Sonarr")
2021-01-20 21:37:59 +00:00
2021-06-14 01:51:30 +00:00
if len(exists) > 0:
for series in exists:
logger.info(f"Already in Sonarr | {series.tvdbId:<6} | {series.title}")
logger.info(f"{len(exists)} Series already existing in Sonarr")
2021-02-16 14:31:15 +00:00
2021-06-14 01:51:30 +00:00
for series in invalid:
logger.info(f"Invalid TVDb ID | {series}")