From 83575a7d9badea9097d4b14c55f1163f73805e51 Mon Sep 17 00:00:00 2001 From: p0ps Date: Thu, 6 Jan 2022 16:52:30 +0100 Subject: [PATCH] Add support for banner and background images to tvmaze (#10234) * Add images endpoint. * Add banner, background support to tvmaze * Update CHANGELOG.md --- CHANGELOG.md | 1 + lib/pytvmaze/endpoints.py | 1 + lib/pytvmaze/exceptions.py | 3 ++ lib/pytvmaze/tvmaze.py | 23 +++++++++++++++ medusa/indexers/tvmaze/api.py | 54 +++++++++++++++++++++++++++++------ 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b55e1935e..53b396b0e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Unreleased #### New Features +- Add support for banner and background images to indexer tvmaze ([10234](https://github.com/pymedusa/Medusa/pull/10234)) - Add option for using ffprobe to validate postprocessed media ([10132](https://github.com/pymedusa/Medusa/pull/10132)) #### Improvements diff --git a/lib/pytvmaze/endpoints.py b/lib/pytvmaze/endpoints.py index 1c24079d5e..ceb589ece7 100644 --- a/lib/pytvmaze/endpoints.py +++ b/lib/pytvmaze/endpoints.py @@ -24,6 +24,7 @@ show_seasons = 'http://api.tvmaze.com/shows/{0}/seasons' season_by_id = 'http://api.tvmaze.com/seasons/{0}' episode_by_id = 'http://api.tvmaze.com/episodes/{0}' +show_images = 'http://api.tvmaze.com/shows/{0}/images' # TVMaze Premium endpoints followed_shows = 'http://api.tvmaze.com/v1/user/follows/shows{0}' diff --git a/lib/pytvmaze/exceptions.py b/lib/pytvmaze/exceptions.py index 36fcb38a69..15256233d9 100644 --- a/lib/pytvmaze/exceptions.py +++ b/lib/pytvmaze/exceptions.py @@ -71,6 +71,9 @@ class SeasonNotFound(BaseError): pass +class ImagesNotFound(BaseError): + pass + class IllegalAirDate(BaseError): pass diff --git a/lib/pytvmaze/tvmaze.py b/lib/pytvmaze/tvmaze.py index ec786d26d9..5c3875bf73 100644 --- a/lib/pytvmaze/tvmaze.py +++ b/lib/pytvmaze/tvmaze.py @@ -342,6 +342,21 @@ def __repr__(self): )) +class Image(object): + def __init__(self, data): + self.id = data.get('id') + self.type = data.get('type') + self.main = data.get('main') + self.resolutions = data.get('resolutions') + + def __repr__(self): + return ''.format( + id=self.id, + type=self.type, + main=self.main + ) + + class Updates(object): def __init__(self, data): self.updates = dict() @@ -1199,6 +1214,14 @@ def show_seasons(self, maze_id): else: raise SeasonNotFound('Couldn\'t find Season\'s for TVMaze ID {0}'.format(maze_id)) + def show_images(self, maze_id): + url = endpoints.show_images.format(maze_id) + q = self._endpoint_standard_get(url) + if q: + return [Image(image) for image in q] + else: + raise ImagesNotFound('Couldn\'t find Images\'s for TVMaze ID {0}'.format(maze_id)) + def season_by_id(self, season_id): url = endpoints.season_by_id.format(season_id) q = self._endpoint_standard_get(url) diff --git a/medusa/indexers/tvmaze/api.py b/medusa/indexers/tvmaze/api.py index b9e5ffed75..86e65437b8 100644 --- a/medusa/indexers/tvmaze/api.py +++ b/medusa/indexers/tvmaze/api.py @@ -316,20 +316,56 @@ def _parse_images(self, tvmaze_id): log.debug('Getting show banners for {0}', tvmaze_id) try: - image_medium = self.shows[tvmaze_id]['image_medium'] + images = self.tvmaze_api.show_images(tvmaze_id) + # image_medium = self.shows[tvmaze_id]['image_medium'] except Exception: log.debug('Could not parse Poster for showid: {0}', tvmaze_id) return False # Set the poster (using the original uploaded poster for now, as the medium formated is 210x195 - _images = {u'poster': {u'1014x1500': {u'1': {u'rating': 1, - u'language': u'en', - u'ratingcount': 1, - u'bannerpath': image_medium.split('/')[-1], - u'bannertype': u'poster', - u'bannertype2': u'210x195', - u'_bannerpath': image_medium, - u'id': u'1035106'}}}} + _images = {} + for image in images: + if image.type not in _images: + _images[image.type] = {} + + if not image.resolutions: + continue + + if image.type == 'poster' and not image.main: + continue + + # For banner, poster and fanart use the origin size. + _images[image.type] = { + f"{image.resolutions['original']['width']}x{image.resolutions['original']['height']}": { + image.id: { + 'rating': 1, + 'language': u'en', + 'ratingcount': 1, + 'bannerpath': image.resolutions['original']['url'].split('/')[-1], + 'bannertype': image.type, + 'bannertype2': f"{image.resolutions['original']['width']}x{image.resolutions['original']['height']}", + '_bannerpath': image.resolutions['original']['url'], + 'id': image.id + } + } + } + + if image.type == 'poster': + # Save the main poster as a poster thumb. + _images['poster_thumb'] = { + f"{image.resolutions['medium']['width']}x{image.resolutions['medium']['height']}": { + image.id: { + 'rating': 1, + 'language': u'en', + 'ratingcount': 1, + 'bannerpath': image.resolutions['medium']['url'].split('/')[-1], + 'bannertype': 'poster_thumb', + 'bannertype2': f"{image.resolutions['medium']['width']}x{image.resolutions['medium']['height']}", + '_bannerpath': image.resolutions['medium']['url'], + 'id': image.id + } + } + } season_images = self._parse_season_images(tvmaze_id) if season_images: