From: E.Smith <31170571+azlm8t@users.noreply.github.com> Date: Wed, 3 Oct 2018 23:17:53 +0000 (+0100) Subject: fanart: Update tmdb to support tv lookups. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e324addf495dbcc56f41185adf63e001b76446b9;p=thirdparty%2Ftvheadend.git fanart: Update tmdb to support tv lookups. Previously we only support movie lookups on tmdb since the module we used did not support tv lookups. Now we no longer use an external module we can perform tv lookups on tmdb too. --- diff --git a/lib/py/tvh/tv_meta_tmdb.py b/lib/py/tvh/tv_meta_tmdb.py index 39d5858f0..f6597ffcd 100755 --- a/lib/py/tvh/tv_meta_tmdb.py +++ b/lib/py/tvh/tv_meta_tmdb.py @@ -26,7 +26,7 @@ def get_capabilities(): "name": "tv_meta_tmdb", "version": "0.1", "description": "Grab movie details from TMDB.", - "supports_tv": False, + "supports_tv": True, "supports_movie": True, "required_config" : { "tmdb-key" : "string" @@ -57,30 +57,39 @@ class Tv_meta_tmdb(object): return None return self.image_base_url + size + "/" + img - - def _search_movie(self, title, year, lang = "en"): + def _search_common(self, title, year, lang, query_path, year_query_field): params = { 'api_key' : self.tmdb_key, 'language' : lang, 'query' : title, }; + # movie and tv call year field different things... if year: - params['primary_release_year'] = year + params[year_query_field] = year - logging.debug(params) + url = self.base_url + query_path + logging.debug("Search using %s with %s" % ( url, params)) r = requests.get( - self.base_url + "search/movie", + url, params = params, headers = {'Content-Type': 'application/json', # Does not support this: 'Accept-Language': self.languages }) # List of matches + r.raise_for_status(); + logging.debug(r.json()) return r.json()['results'] - def _search_movie_all_languages(self, title, year): + def _search_movie(self, title, year, lang = "en"): + return self._search_common(title, year, lang, "search/movie", "primary_release_year") + + def _search_tv(self, title, year, lang = "en"): + return self._search_common(title, year, lang, "search/tv", "first_air_date_year") + + def _search_all_languages_common(self, title, year, cb): for lang in self.languages.split(","): try: - res = self._search_movie(title, year, lang) + res = cb(title, year, lang) if res: return res except: @@ -88,19 +97,41 @@ class Tv_meta_tmdb(object): pass return None + def _search_movie_all_languages(self, title, year): + return self._search_all_languages_common(title, year, self._search_movie) + + def _search_tv_all_languages(self, title, year): + return self._search_all_languages_common(title, year, self._search_tv) + + def _search_all_languages(self, title, year, cb1, cb2): + res = cb1(title, year) + if res is None and cb2 is not None: + logging.debug("Trying again with other type") + res = cb2(title, year) + return res + + def fetch_details(self, args): logging.debug("Fetching with details %s " % args); title = args["title"] year = args["year"] + type = "movie" if 'type' not in args else args["type"] if title is None: logging.critical("Need a title"); raise RuntimeError("Need a title"); - res = self._search_movie_all_languages(title, year=year) + # Sometimes movies are actually tv shows, so if we have something + # that says it is a movie and the lookup fails, then search as a + # tv show instead. We don't do the opposite for tv shows since + # they never get identified as a movie due to season and episode. + if type is None or type == 'movie': + res = self._search_all_languages(title, year, self._search_movie_all_languages, self._search_tv_all_languages) + else: + res = self._search_all_languages(title, year, self._search_tv_all_languages, None) logging.debug(res) if res is None or len(res) == 0: - logging.error("Could not find any matching movie"); + logging.info("Could not find any match for %s" % title); raise LookupError("Could not find match for " + title); # Assume first match is the best @@ -130,6 +161,8 @@ if __name__ == '__main__': help='Title to search for.') optp.add_option('--year', default=None, type="int", help='Year to search for.') + optp.add_option('--type', default="movie", + help='Type to search for, either tv or movie.') optp.add_option('--tmdb-languages', default=None, help='Specify tmdb language codes separated by commas, such as "en,sv".') optp.add_option('--capabilities', default=None, action="store_true", @@ -155,11 +188,13 @@ if __name__ == '__main__': print(json.dumps(grabber.fetch_details({ "title": opts.title, "year" : opts.year, + "type" : opts.type, }))) try: logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s:%(module)s:%(message)s') sys.exit(process(sys.argv)) except KeyboardInterrupt: pass - except (RuntimeError,LookupError): + except (RuntimeError,LookupError) as err: + logging.info("Got exception: " + str(err)) sys.exit(1) diff --git a/lib/py/tvh/tv_meta_tvdb.py b/lib/py/tvh/tv_meta_tvdb.py index 2b7e5d941..2643e98f0 100755 --- a/lib/py/tvh/tv_meta_tvdb.py +++ b/lib/py/tvh/tv_meta_tvdb.py @@ -153,7 +153,7 @@ class Tv_meta_tvdb(object): try: tvdbid = self.tvdb.get_tvdbid(title) except: - logging.error("Could not find any matching episode"); + logging.info("Could not find any matching episode for " + title); raise LookupError("Could not find match for " + title); poster = fanart = None diff --git a/support/tvhmeta b/support/tvhmeta index 792a2d882..106a6e633 100755 --- a/support/tvhmeta +++ b/support/tvhmeta @@ -192,7 +192,7 @@ class TvhMeta(object): clients = {} client_modules = {} client_objects = {} - if episode_disp is not None and episode_disp <> "": + if episode_disp is not None and episode_disp != "": # TV episode, so load the tv modules # Have to do len before split since split of an empty string # returns one element... @@ -262,9 +262,14 @@ class TvhMeta(object): else: obj = client_objects[module] logging.debug("Got object %s" % obj) + if episode_disp is not None and len(episode_disp) > 0: + type = "tv" + else: + type = "movie" art = obj.fetch_details({ "title": title, "year": year, + "type": type, "episode_disp": episode_disp, # @todo Need to break this out in to season/episode, maybe subtitle too. "programid": uri})