2021-01-20 21:37:59 +00:00
|
|
|
import logging, re, requests
|
|
|
|
from modules import util
|
|
|
|
from modules.util import Failed
|
|
|
|
from retrying import retry
|
|
|
|
|
|
|
|
logger = logging.getLogger("Plex Meta Manager")
|
|
|
|
|
|
|
|
class SonarrAPI:
|
2021-04-02 20:15:59 +00:00
|
|
|
def __init__(self, params, language):
|
2021-02-24 06:44:06 +00:00
|
|
|
self.base_url = f"{params['url']}/api{'/v3/' if params['version'] == 'v3' else '/'}"
|
2021-04-02 20:15:59 +00:00
|
|
|
self.token = params["token"]
|
2021-01-20 21:37:59 +00:00
|
|
|
try:
|
2021-04-02 20:15:59 +00:00
|
|
|
result = requests.get(f"{self.base_url}system/status", params={"apikey": f"{self.token}"}).json()
|
2021-02-24 06:42:58 +00:00
|
|
|
except Exception:
|
2021-01-20 21:37:59 +00:00
|
|
|
util.print_stacktrace()
|
2021-02-24 06:44:06 +00:00
|
|
|
raise Failed(f"Sonarr Error: Could not connect to Sonarr at {params['url']}")
|
2021-01-20 21:37:59 +00:00
|
|
|
if "error" in result and result["error"] == "Unauthorized":
|
|
|
|
raise Failed("Sonarr Error: Invalid API Key")
|
|
|
|
if "version" not in result:
|
|
|
|
raise Failed("Sonarr Error: Unexpected Response Check URL")
|
|
|
|
self.quality_profile_id = None
|
|
|
|
profiles = ""
|
2021-04-02 15:45:29 +00:00
|
|
|
for profile in self.send_get("qualityProfile" if params["version"] == "v3" else "profile"):
|
2021-01-20 21:37:59 +00:00
|
|
|
if len(profiles) > 0:
|
|
|
|
profiles += ", "
|
|
|
|
profiles += profile["name"]
|
|
|
|
if profile["name"] == params["quality_profile"]:
|
|
|
|
self.quality_profile_id = profile["id"]
|
|
|
|
if not self.quality_profile_id:
|
2021-02-24 06:44:06 +00:00
|
|
|
raise Failed(f"Sonarr Error: quality_profile: {params['quality_profile']} does not exist in sonarr. Profiles available: {profiles}")
|
2021-04-02 15:45:29 +00:00
|
|
|
|
|
|
|
self.language_profile_id = None
|
|
|
|
if params["version"] == "v3" and params["language_profile"] is not None:
|
|
|
|
profiles = ""
|
|
|
|
for profile in self.send_get("languageProfile"):
|
|
|
|
if len(profiles) > 0:
|
|
|
|
profiles += ", "
|
|
|
|
profiles += profile["name"]
|
|
|
|
if profile["name"] == params["language_profile"]:
|
|
|
|
self.language_profile_id = profile["id"]
|
|
|
|
if not self.quality_profile_id:
|
|
|
|
raise Failed(f"Sonarr Error: language_profile: {params['language_profile']} does not exist in sonarr. Profiles available: {profiles}")
|
|
|
|
|
|
|
|
if self.language_profile_id is None:
|
|
|
|
self.language_profile_id = 1
|
|
|
|
|
2021-04-02 18:15:43 +00:00
|
|
|
self.tags = self.get_tags()
|
2021-01-20 21:37:59 +00:00
|
|
|
self.language = language
|
|
|
|
self.version = params["version"]
|
|
|
|
self.root_folder_path = params["root_folder_path"]
|
|
|
|
self.add = params["add"]
|
|
|
|
self.search = params["search"]
|
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"]
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-04-02 18:15:43 +00:00
|
|
|
def get_tags(self):
|
|
|
|
return {tag["label"]: tag["id"] for tag in self.send_get("tag")}
|
|
|
|
|
|
|
|
def add_tags(self, tags):
|
|
|
|
added = False
|
|
|
|
for label in tags:
|
|
|
|
if label not in self.tags:
|
|
|
|
added = True
|
|
|
|
self.send_post("tag", {"label": str(label)})
|
|
|
|
if added:
|
|
|
|
self.tags = self.get_tags()
|
|
|
|
|
2021-04-02 20:15:59 +00:00
|
|
|
def lookup(self, tvdb_id):
|
|
|
|
results = self.send_get("series/lookup", params={"term": f"tvdb:{tvdb_id}"})
|
|
|
|
if results:
|
|
|
|
return results[0]
|
|
|
|
else:
|
|
|
|
raise Failed(f"Sonarr Error: TVDb ID: {tvdb_id} not found")
|
|
|
|
|
2021-04-02 18:15:43 +00:00
|
|
|
def add_tvdb(self, tvdb_ids, tags=None, folder=None):
|
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-02-16 14:31:15 +00:00
|
|
|
tag_nums = []
|
2021-01-20 21:37:59 +00:00
|
|
|
add_count = 0
|
2021-04-02 18:15:43 +00:00
|
|
|
if tags is None:
|
|
|
|
tags = self.tag
|
|
|
|
if tags:
|
|
|
|
self.add_tags(tags)
|
|
|
|
tag_nums = [self.tags[label] for label in tags if label in self.tags]
|
2021-01-20 21:37:59 +00:00
|
|
|
for tvdb_id in tvdb_ids:
|
|
|
|
try:
|
2021-04-02 20:15:59 +00:00
|
|
|
show_info = self.lookup(tvdb_id)
|
2021-01-20 21:37:59 +00:00
|
|
|
except Failed as e:
|
|
|
|
logger.error(e)
|
|
|
|
continue
|
|
|
|
|
2021-04-02 20:15:59 +00:00
|
|
|
poster_url = None
|
|
|
|
for image in show_info["images"]:
|
|
|
|
if "coverType" in image and image["coverType"] == "poster" and "remoteUrl" in image:
|
|
|
|
poster_url = image["remoteUrl"]
|
2021-01-20 21:37:59 +00:00
|
|
|
|
|
|
|
url_json = {
|
2021-04-02 20:15:59 +00:00
|
|
|
"title": show_info["title"],
|
2021-02-24 06:44:06 +00:00
|
|
|
f"{'qualityProfileId' if self.version == 'v3' else 'profileId'}": self.quality_profile_id,
|
2021-04-02 09:33:19 +00:00
|
|
|
"languageProfileId": self.language_profile_id,
|
2021-01-20 21:37:59 +00:00
|
|
|
"tvdbId": int(tvdb_id),
|
2021-04-02 20:15:59 +00:00
|
|
|
"titleslug": show_info["titleSlug"],
|
2021-01-20 21:37:59 +00:00
|
|
|
"language": self.language,
|
|
|
|
"monitored": True,
|
2021-03-01 22:25:38 +00:00
|
|
|
"seasonFolder": self.season_folder,
|
2021-04-01 20:31:16 +00:00
|
|
|
"rootFolderPath": self.root_folder_path if folder is None else folder,
|
2021-02-24 06:42:58 +00:00
|
|
|
"seasons": [],
|
2021-04-02 20:15:59 +00:00
|
|
|
"images": [{"covertype": "poster", "url": poster_url}],
|
2021-01-20 21:37:59 +00:00
|
|
|
"addOptions": {"searchForMissingEpisodes": self.search}
|
|
|
|
}
|
2021-02-16 14:31:15 +00:00
|
|
|
if tag_nums:
|
|
|
|
url_json["tags"] = tag_nums
|
2021-04-02 15:45:29 +00:00
|
|
|
response = self.send_post("series", url_json)
|
2021-01-20 21:37:59 +00:00
|
|
|
if response.status_code < 400:
|
2021-04-02 20:15:59 +00:00
|
|
|
logger.info(f"Added to Sonarr | {tvdb_id:<6} | {show_info['title']}")
|
2021-01-20 21:37:59 +00:00
|
|
|
add_count += 1
|
|
|
|
else:
|
2021-02-11 06:11:48 +00:00
|
|
|
try:
|
2021-04-02 20:15:59 +00:00
|
|
|
logger.error(f"Sonarr Error: ({tvdb_id}) {show_info['title']}: ({response.status_code}) {response.json()[0]['errorMessage']}")
|
2021-02-24 06:42:58 +00:00
|
|
|
except KeyError:
|
2021-02-11 06:11:48 +00:00
|
|
|
logger.debug(url_json)
|
2021-02-24 06:44:06 +00:00
|
|
|
logger.error(f"Sonarr Error: {response.json()}")
|
|
|
|
logger.info(f"{add_count} Show{'s' if add_count > 1 else ''} added to Sonarr")
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-02-16 14:31:15 +00:00
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=10000)
|
2021-04-02 20:15:59 +00:00
|
|
|
def send_get(self, url, params=None):
|
|
|
|
url_params = {"apikey": f"{self.token}"}
|
|
|
|
if params:
|
|
|
|
for param in params:
|
|
|
|
url_params[param] = params[param]
|
|
|
|
return requests.get(f"{self.base_url}{url}", params=url_params).json()
|
2021-02-16 14:31:15 +00:00
|
|
|
|
2021-01-20 21:37:59 +00:00
|
|
|
@retry(stop_max_attempt_number=6, wait_fixed=10000)
|
|
|
|
def send_post(self, url, url_json):
|
2021-04-02 20:15:59 +00:00
|
|
|
return requests.post(f"{self.base_url}{url}", json=url_json, params={"apikey": f"{self.token}"})
|