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

Fix IMDB cache location #4745

Merged
merged 6 commits into from
Jul 24, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Fixed being unable to change scene numbering for first 2 episodes of each season on displayShow ([#4656](https://github.com/pymedusa/Medusa/pull/4656))
- Fixed YggTorrents provider downloads by updating the provider's URL ([#4725](https://github.com/pymedusa/Medusa/pull/4725))
- Fixed Abnormal provider login check ([#4727](https://github.com/pymedusa/Medusa/pull/4727))
- Fixed IMDB cache location ([#4745](https://github.com/pymedusa/Medusa/pull/4745))
- _Simple message describing the fix, and a link to the pull request._

### [**Previous versions**](https://github.com/pymedusa/medusa.github.io/blob/master/news/CHANGES.md)
5 changes: 2 additions & 3 deletions medusa/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@

import guessit

from imdbpie import imdbpie

from medusa import app, db
from medusa.common import DOWNLOADED, USER_AGENT
from medusa.helper.common import (episode_num, http_code_description, media_extensions,
pretty_file_size, subtitle_extensions)
from medusa.helpers.utils import generate
from medusa.imdb import Imdb
from medusa.indexers.indexer_exceptions import IndexerException
from medusa.logger.adapters.style import BraceAdapter, BraceMessage
from medusa.session.core import MedusaSafeSession
Expand Down Expand Up @@ -1727,7 +1726,7 @@ def is_info_hash_processed(info_hash):
def title_to_imdb(title, start_year, imdb_api=None):
"""Get the IMDb ID from a show title and its start year."""
if imdb_api is None:
imdb_api = imdbpie.Imdb()
imdb_api = Imdb()

titles = imdb_api.search_for_title(title)

Expand Down
21 changes: 21 additions & 0 deletions medusa/imdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding=utf-8
"""IMDbPie shim."""

from __future__ import unicode_literals

import os.path

from imdbpie import imdbpie

from medusa import app


class Imdb(imdbpie.Imdb):
"""Subclass imdbpie.Imdb to override the cache folder location."""

def __init__(self, *args, **kwargs):
super(Imdb, self).__init__(*args, **kwargs)
# Just to make sure new versions of `imdbpie` won't break this fix
assert hasattr(self, '_cachedir')
# Override the cache location
self._cachedir = os.path.join(app.CACHE_DIR, 'diskcache')
8 changes: 3 additions & 5 deletions medusa/show/recommendations/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import re
from builtins import object

from imdbpie import imdbpie

from medusa import helpers
from medusa.cache import recommended_series_cache
from medusa.imdb import Imdb
from medusa.indexers.indexer_config import INDEXER_TVDBV2
from medusa.logger.adapters.style import BraceAdapter
from medusa.session.core import MedusaSession
Expand All @@ -27,8 +26,6 @@
log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())

imdb_api = imdbpie.Imdb()


class ImdbPopular(object):
"""Gets a list of most popular TV series from imdb."""
Expand All @@ -37,6 +34,7 @@ def __init__(self):
"""Initialize class."""
self.cache_subfolder = __name__.split('.')[-1] if '.' in __name__ else __name__
self.session = MedusaSession()
self.imdb_api = Imdb(session=self.session)
self.recommender = 'IMDB Popular'
self.default_img_src = 'poster.png'

Expand Down Expand Up @@ -68,7 +66,7 @@ def fetch_popular_shows(self):
"""Get popular show information from IMDB."""
popular_shows = []

imdb_result = imdb_api.get_popular_shows()
imdb_result = self.imdb_api.get_popular_shows()

for imdb_show in imdb_result['ranks']:
series = {}
Expand Down
59 changes: 38 additions & 21 deletions medusa/show/recommendations/recommended.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import posixpath
from builtins import object

from imdbpie import imdbpie

from medusa import (
app,
helpers,
)
from medusa.cache import recommended_series_cache
from medusa.helpers import ensure_list
from medusa.imdb import Imdb
from medusa.indexers.utils import indexer_id_to_name
from medusa.logger.adapters.style import BraceAdapter
from medusa.session.core import MedusaSession
Expand All @@ -43,24 +42,41 @@
log.logger.addHandler(logging.NullHandler())

session = MedusaSession()
imdb_api = imdbpie.Imdb(session=session)

anidb_api = None

class LazyApi(object):
"""Decorators to lazily construct API classes."""

def load_anidb_api(func):
"""
Decorate a function to lazy load the anidb_api.
imdb_api = None
anidb_api = None

We need to do this, because we're passing the Medusa cache location to the lib. As the module is imported before
the app.CACHE_DIR location has been read, we can't initialize it at module level.
"""
def func_wrapper(aid):
global anidb_api
if anidb_api is None:
anidb_api = Anidb(cache_dir=app.CACHE_DIR)
return func(aid)
return func_wrapper
@classmethod
def load_anidb_api(cls, func):
"""
Decorate a function to lazy load the anidb_api.

We need to do this, because we're passing the Medusa cache location to the lib. As the module is imported before
the app.CACHE_DIR location has been read, we can't initialize it at module level.
"""
def func_wrapper(*args, **kwargs):
if cls.anidb_api is None:
cls.anidb_api = Anidb(cache_dir=app.CACHE_DIR)
return func(*args, **kwargs)
return func_wrapper

@classmethod
def load_imdb_api(cls, func):
"""
Decorate a function to lazy load the imdb_api.

We need to do this, because we're overriding the cache location of the library.
As the module is imported before the app.CACHE_DIR location has been read, we can't initialize it at module level.
"""
def func_wrapper(*args, **kwargs):
if cls.imdb_api is None:
cls.imdb_api = Imdb(session=session)
return func(*args, **kwargs)
return func_wrapper


class MissingTvdbMapping(Exception):
Expand Down Expand Up @@ -167,36 +183,37 @@ def __str__(self):
return 'Recommended show {0} from recommended list: {1}'.format(self.title, self.recommender)


@load_anidb_api
@LazyApi.load_anidb_api
@recommended_series_cache.cache_on_arguments()
def cached_tvdb_to_aid(tvdb_id):
"""
Try to match an anidb id with a tvdb id.

Use dogpile cache to return a cached id if available.
"""
return anidb_api.tvdb_id_to_aid(tvdbid=tvdb_id)
return LazyApi.anidb_api.tvdb_id_to_aid(tvdbid=tvdb_id)


@load_anidb_api
@LazyApi.load_anidb_api
@recommended_series_cache.cache_on_arguments()
def cached_aid_to_tvdb(aid):
"""
Try to match a tvdb id with an anidb id.

Use dogpile cache to return a cached id if available.
"""
return anidb_api.aid_to_tvdb_id(aid=aid)
return LazyApi.anidb_api.aid_to_tvdb_id(aid=aid)


@LazyApi.load_imdb_api
@recommended_series_cache.cache_on_arguments()
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.
"""
return imdb_api.get_title(imdb_id)
return LazyApi.imdb_api.get_title(imdb_id)


def create_key_from_series(namespace, fn, **kw):
Expand Down
5 changes: 2 additions & 3 deletions medusa/tv/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
)
from itertools import groupby

from imdbpie import imdbpie

from medusa import (
app,
db,
Expand Down Expand Up @@ -67,6 +65,7 @@
from medusa.helpers.anidb import get_release_groups_for_anime, short_group_names
from medusa.helpers.externals import get_externals, load_externals_from_db
from medusa.helpers.utils import safe_get
from medusa.imdb import Imdb
from medusa.indexers.indexer_api import indexerApi
from medusa.indexers.indexer_config import (
INDEXER_TVRAGE,
Expand Down Expand Up @@ -1501,7 +1500,7 @@ def load_from_indexer(self, tvapi=None):

def load_imdb_info(self):
"""Load all required show information from IMDb with ImdbPie."""
imdb_api = imdbpie.Imdb()
imdb_api = Imdb()

if not self.imdb_id:
self.imdb_id = helpers.title_to_imdb(self.name, self.start_year, imdb_api)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_imdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# coding=utf-8
"""Tests for medusa/imdb.py."""

import os.path

from medusa import imdb


def test_imdb_cache_dir(app_config):
# Given
cache_dir = app_config('CACHE_DIR', '/opt/medusa/data/cache')

# When
instance = imdb.Imdb()
expected = os.path.join(cache_dir, 'diskcache')

# Then
assert instance._cachedir, expected