Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error where we can't get the imdb details request. #9797

Merged
merged 7 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
## Unreleased

#### New Features

#### Improvements

#### Fixes
- Fix history page (compact layout) fails to load. ([9794](https://github.com/pymedusa/Medusa/pull/9794))
- Prevent recommended shows (imdb) to cache empty responses to the api. ([9797](https://github.com/pymedusa/Medusa/pull/9797))

## 0.5.16 (13-08-2021)

Expand Down
12 changes: 1 addition & 11 deletions medusa/helpers/anidb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from medusa.cache import anidb_cache
from medusa.helper.exceptions import AnidbAdbaConnectionException
from medusa.logger.adapters.style import BraceAdapter

import six
from medusa.show.recommendations.recommended import create_key

log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())
Expand Down Expand Up @@ -48,15 +47,6 @@ def set_up_anidb_connection():
return app.ADBA_CONNECTION.authed()


def create_key(namespace, fn, **kw):
def generate_key(*args, **kw):
show_key = namespace + '|' + args[0]
if six.PY2:
return show_key.encode('utf-8')
return show_key
return generate_key


@anidb_cache.cache_on_arguments(namespace='anidb', function_key_generator=create_key)
def get_release_groups_for_anime(series_name):
"""Get release groups for an anidb anime."""
Expand Down
86 changes: 48 additions & 38 deletions medusa/show/recommendations/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
BasePopular,
RecommendedShow,
cached_get_imdb_series_details,
cached_get_imdb_series_genres,
create_key_from_series,
)

Expand Down Expand Up @@ -77,44 +78,53 @@ def fetch_popular_shows(self):
show_details = None
imdb_id = series['imdb_tt'] = imdb_show['id'].strip('/').split('/')[-1]

if imdb_id:
try:
show_details = cached_get_imdb_series_details(imdb_id)
except RequestException as error:
log.warning('Could not get show details for {imdb_id} with error: {error!r}',
{'imdb_id': imdb_id, 'error': error})

if show_details:
try:
series['year'] = imdb_show.get('year')
series['name'] = imdb_show['title']
series['image_url_large'] = imdb_show['image']['url']
series['image_path'] = posixpath.join('images', 'imdb_popular',
os.path.basename(series['image_url_large']))
series['image_url'] = '{0}{1}'.format(imdb_show['image']['url'].split('V1')[0], '_SY600_AL_.jpg')
series['imdb_url'] = 'http://www.imdb.com{imdb_id}'.format(imdb_id=imdb_show['id'])
series['votes'] = show_details['ratings'].get('ratingCount', 0)
series['outline'] = show_details['plot'].get('outline', {}).get('text')
series['rating'] = show_details['ratings'].get('rating', 0)
series['genres'] = show_details['genres'].get('genres')
except Exception as error:
log.warning('Could not parse show {imdb_id} with error: {error!r}',
{'imdb_id': imdb_id, 'error': error})
else:
continue

if all([series['year'], series['name'], series['imdb_tt']]):
try:
recommended_show = self._create_recommended_show(series)
if recommended_show:
recommended_show.save_to_db()
result.append(recommended_show)
except RequestException:
log.warning(
u'Could not connect to indexers to check if you already have'
u' this show in your library: {show} ({year})',
{'show': series['name'], 'year': series['name']}
)
if not imdb_id:
continue

series['year'] = imdb_show.get('year')
series['name'] = imdb_show['title']
series['image_url_large'] = imdb_show['image']['url']
series['image_path'] = posixpath.join(
'images', 'imdb_popular', os.path.basename(series['image_url_large'])
)
series['image_url'] = '{0}{1}'.format(imdb_show['image']['url'].split('V1')[0], '_SY600_AL_.jpg')
series['imdb_url'] = 'http://www.imdb.com{imdb_id}'.format(imdb_id=imdb_show['id'])

show_details = {}
show_genres = {}
try:
show_details = cached_get_imdb_series_details(imdb_id)
show_genres = cached_get_imdb_series_genres(imdb_id)
except Exception as error:
log.warning('Could not get show details for {imdb_id} with error: {error}',
{'imdb_id': imdb_id, 'error': error})

if not show_details:
continue

# Get details.
try:
series['votes'] = show_details.get('ratings', {}).get('ratingCount', 0)
series['outline'] = show_details.get('plot', {}).get('outline', {}).get('text')
series['rating'] = show_details.get('ratings', {}).get('rating', 0)
except Exception as error:
log.warning('Could not parse show {imdb_id} with error: {error!r}',
{'imdb_id': imdb_id, 'error': error})

series['genres'] = show_genres.get('genres', [])

if all([series['year'], series['name'], series['imdb_tt']]):
try:
recommended_show = self._create_recommended_show(series)
if recommended_show:
recommended_show.save_to_db()
result.append(recommended_show)
except RequestException:
log.warning(
u'Could not connect to indexers to check if you already have'
u' this show in your library: {show} ({year})',
{'show': series['name'], 'year': series['name']}
)

return result

Expand Down
35 changes: 29 additions & 6 deletions medusa/show/recommendations/recommended.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,16 @@ def get_categories():
return categories


def create_key(namespace, fn, **kw):
"""Create key out of namespace + firs arg."""
def generate_key(*args, **kw):
show_key = f'{namespace}|{args[0]}'
return show_key
return generate_key


@LazyApi.load_anidb_api
@recommended_series_cache.cache_on_arguments()
@recommended_series_cache.cache_on_arguments(namespace='tvdb_to_anidb', function_key_generator=create_key)
def cached_tvdb_to_aid(tvdb_id):
"""
Try to match an anidb id with a tvdb id.
Expand All @@ -359,7 +367,7 @@ def cached_tvdb_to_aid(tvdb_id):


@LazyApi.load_anidb_api
@recommended_series_cache.cache_on_arguments()
@recommended_series_cache.cache_on_arguments(namespace='anidb_to_tvdb', function_key_generator=create_key)
def cached_aid_to_tvdb(aid):
"""
Try to match a tvdb id with an anidb id.
Expand All @@ -370,16 +378,31 @@ def cached_aid_to_tvdb(aid):


@LazyApi.load_imdb_api
@recommended_series_cache.cache_on_arguments()
@recommended_series_cache.cache_on_arguments(namespace='series_details', function_key_generator=create_key)
def cached_get_imdb_series_details(imdb_id):
"""
Request the series details from the imdbpie api.

Use dogpile cache to return a cached id if available.
"""
title_detailed = LazyApi.imdb_api.get_title(imdb_id)
title_detailed['genres'] = LazyApi.imdb_api.get_title_genres(imdb_id)
return title_detailed
details = LazyApi.imdb_api.get_title(imdb_id)
if not details:
raise Exception(f'Could not get imdb details from {imdb_id}')
return details


@LazyApi.load_imdb_api
@recommended_series_cache.cache_on_arguments(namespace='series_genres', function_key_generator=create_key)
def cached_get_imdb_series_genres(imdb_id):
"""
Request the series genres from the imdbpie api.

Use dogpile cache to return a cached id if available.
"""
genres = LazyApi.imdb_api.get_title_genres(imdb_id)
if not genres:
raise Exception(f'Could not get imdb genres from {imdb_id}')
return genres


def create_key_from_series(namespace, fn, **kw):
Expand Down