From eb9a99130327c06aa838c6fb442f4eb9e1f77d8a Mon Sep 17 00:00:00 2001 From: todd Date: Mon, 26 Oct 2020 19:57:31 -0400 Subject: [PATCH] FIX: Existing series detection does not work with Sonarr v2 VER: 1.3.2 --- searcharr.py | 2 +- sonarr.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/searcharr.py b/searcharr.py index 12f5048..d10eefa 100644 --- a/searcharr.py +++ b/searcharr.py @@ -19,7 +19,7 @@ import sonarr import settings -__version__ = "1.3.1" +__version__ = "1.3.2" DBPATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") DBFILE = "searcharr.db" diff --git a/sonarr.py b/sonarr.py index 28ada05..2b76648 100644 --- a/sonarr.py +++ b/sonarr.py @@ -6,6 +6,7 @@ https://github.com/toddrob99/searcharr """ import requests +import time from urllib.parse import quote from log import set_up_logger @@ -18,6 +19,8 @@ def __init__(self, api_url, api_key, verbose=False): if api_url[-1] == "/": api_url = api_url[:-1] self.api_url = api_url + "/api/{endpoint}?apikey=" + api_key + self._all_series = {} + self.get_all_series() def lookup_series(self, title=None, tvdb_id=None): r = self._api_get( @@ -42,7 +45,7 @@ def lookup_series(self, title=None, tvdb_id=None): "seriesType": x.get("seriesType"), "imdbId": x.get("imdbId"), "certification": x.get("certification"), - "id": x.get("id"), + "id": x.get("id", self._series_internal_id(x.get("tvdbId"))), "titleSlug": x.get("titleSlug"), "cleanTitle": x.get("cleanTitle"), "tvRageId": x.get("tvRageId"), @@ -52,6 +55,20 @@ def lookup_series(self, title=None, tvdb_id=None): for x in r ] + def _series_internal_id(self, tvdb_id): + return next( + (x["id"] for x in self.get_all_series() if x.get("tvdbId", 0) == tvdb_id), + None, + ) + + def get_all_series(self): + if int(round(self._all_series.get("ts", 0))) < int(round(time.time())) - 30: + self.logger.debug("Refreshing all series cache...") + r = self._api_get("series", {}) + self._all_series.update({"series": r, "ts": time.time()}) + + return self._all_series["series"] + def add_series( self, series_info=None,