Commit 462160c5 authored by richardARPANET's avatar richardARPANET

WIP Imdb Facade

parent 15ae885d
Pipeline #33 failed with stage
in 1 minute and 7 seconds
......@@ -106,7 +106,7 @@ raise ``LookupError``, for any other API status codes > 399 the client will rais
| ``get_title_top_crew('tt0303461')`` | Returns a dict containing |
| | detailed information about |
| | title鈥檚 top crew (ie: directors, |
| | writters, etc.) |
| | writers, etc.) |
+----------------------------------------------------------------+-----------------------------------+
| ``get_title_plot('tt0111161')`` | Returns a dict containing title |
| | plot information |
......
......@@ -5,3 +5,4 @@ boto==2.48.0
python-dateutil==2.6.1
diskcache==2.9.0
setuptools>=39.0.1
dataclasses==0.5
# -*- coding: utf-8 -*-
from .imdbpie import Imdb # noqa
from .exceptions import ImdbAPIError # noqa
from .facade import ImdbFacade # noqa
import re
from datetime import date
from dateutil.parser import parse
from dataclasses import dataclass
REGEX_IMDB_ID = re.compile(r'([a-zA-Z]{2}[0-9]{7})')
@dataclass
class Title:
title: str
type: str
year: int
genres: tuple
writers: tuple
creators: tuple
credits: tuple
directors: tuple
rating_count: int = 0
rating: float = None
plot_outline: str = None
release_date: date = None
releases: tuple = ()
@dataclass
class TitleRelease:
date: date
region: str
@dataclass
class TitleName:
name: str
job: str
category: str
imdb_id: str
class ImdbFacade(object):
def __init__(self, client):
self._client = client
def get_title(self, imdb_id):
base_title_data = self._client.get_title(imdb_id=imdb_id)
title = base_title_data['base']['title']
year = base_title_data['base']['year']
rating = float(base_title_data['ratings']['rating'])
type_ = base_title_data['base']['titleType']
releases_data = self._client.get_title_releases(imdb_id=imdb_id)
release_date = parse(releases_data['releases'][0]['date']).date()
releases = tuple(
TitleRelease(date=parse(r['date']).date(), region=r['region'])
for r in releases_data['releases']
)
rating_count = base_title_data['ratings']['ratingCount']
plot_outline = base_title_data['plot']['outline']['text']
top_crew_data = self._client.get_title_top_crew(imdb_id=imdb_id)
writers = self._get_writers(top_crew_data)
directors = self._get_directors(top_crew_data)
creators = self._get_creators(top_crew_data)
genres = tuple(
g.lower() for g in
self._client.get_title_genres(imdb_id=imdb_id)['genres']
)
credits_data = self._client.get_title_credits(imdb_id=imdb_id)
credits = self._get_credits(credits_data)
return Title(
title=title,
year=year,
rating=rating,
type=type_,
release_date=release_date,
releases=releases,
plot_outline=plot_outline,
rating_count=rating_count,
writers=writers,
directors=directors,
creators=creators,
genres=genres,
credits=credits,
)
def _get_writers(self, top_crew_data):
return tuple(
TitleName(
name=i['name'],
job=i.get('job'),
category=i.get('category'),
imdb_id=REGEX_IMDB_ID.findall(i['id'])[0]
) for i in top_crew_data['writers']
)
def _get_creators(self, top_crew_data):
return tuple(
TitleName(
name=i['name'],
job=i.get('job'),
category=i.get('category'),
imdb_id=REGEX_IMDB_ID.findall(i['id'])[0]
) for i in top_crew_data['writers']
if i.get('job') == 'creator'
)
def _get_directors(self, top_crew_data):
return tuple(
TitleName(
name=i['name'],
job=i.get('job'),
category=i.get('category'),
imdb_id=REGEX_IMDB_ID.findall(i['id'])[0]
) for i in top_crew_data['directors']
)
def _get_credits(self, credits_data):
credits = []
for category in credits_data['credits']:
for item in credits_data['credits'][category]:
credits.append(TitleName(
name=item['name'],
category=item.get('category'),
job=item.get('job'),
imdb_id=REGEX_IMDB_ID.findall(item['id'])[0]
))
return tuple(credits)
......@@ -46,6 +46,7 @@ _SIMPLE_GET_ENDPOINTS = {
'get_title_awards': '/title/{imdb_id}/awards',
'get_title_ratings': '/title/{imdb_id}/ratings',
'get_title_credits': '/title/{imdb_id}/fullcredits',
'get_title_certification': '/title/{imdb_id}/certification',
'get_name': '/name/{imdb_id}/fulldetails',
'get_name_filmography': '/name/{imdb_id}/filmography',
}
......@@ -92,6 +93,22 @@ class Imdb(Auth):
)
return resource
def get_title_auxiliary(self, imdb_id):
logger.info('called get_title_auxiliary %s', imdb_id)
url = (
'/template/imdb-ios-writable/title-auxiliary-v31.jstl'
'/render?inlineBannerAdWeblabOn=false&minwidth=320'
f'&osVersion=11.3.0&region=GB&tconst={imdb_id}&today=2018-05-06'
)
self.validate_imdb_id(imdb_id)
self._redirection_title_check(imdb_id)
try:
resource = self._get_resource(url)
except LookupError:
self._title_not_found()
# TODO: exclude eps check
return resource
def _simple_get_method(self, method, path):
"""Return client method generated from ``_SIMPLE_GET_ENDPOINTS``."""
def get(imdb_id):
......
from datetime import date
import pytest
from imdbpie import Imdb, ImdbFacade
@pytest.fixture(scope='module')
def client():
client = Imdb(locale='en_US')
yield client
client.clear_cached_credentials()
@pytest.fixture(scope='module')
def facade(client):
return ImdbFacade(client=client)
def test_init(facade):
assert isinstance(facade, ImdbFacade)
def test_get_title(facade):
title = facade.get_title(imdb_id='tt0096697')
assert isinstance(title.title, str)
assert isinstance(title.type, str)
assert isinstance(title.year, int)
assert isinstance(title.rating_count, int)
assert isinstance(title.rating, float)
assert isinstance(title.release_date, date)
assert isinstance(title.plot_outline, str)
assert title.releases
for release in title.releases:
assert isinstance(release.date, date)
assert isinstance(release.region, str)
assert title.writers
for name in title.writers:
assert isinstance(name.name, str)
if name.job is not None:
assert isinstance(name.job, str)
assert isinstance(name.imdb_id, str)
facade._client.validate_imdb_id(name.imdb_id)
assert title.creators
for name in title.creators:
assert isinstance(name.name, str)
assert name.job == 'creator'
assert isinstance(name.imdb_id, str)
facade._client.validate_imdb_id(name.imdb_id)
assert title.directors
for name in title.directors:
assert isinstance(name.name, str)
if name.job is not None:
assert isinstance(name.job, str)
assert isinstance(name.imdb_id, str)
facade._client.validate_imdb_id(name.imdb_id)
assert title.credits
for name in title.credits:
assert isinstance(name.name, str)
if name.job is not None:
assert isinstance(name.job, str)
assert isinstance(name.imdb_id, str)
facade._client.validate_imdb_id(name.imdb_id)
assert title.genres
for genre in title.genres:
assert isinstance(genre, str)
# assert isinstance(title.certification, str)
......@@ -215,6 +215,14 @@ def test_get_title_releases(client):
assert sorted(resource.keys()) == sorted(expected_keys)
def test_get_title_auxiliary(client):
imdb_id = 'tt0111161'
resource = client.get_title_auxiliary(imdb_id)
assert resource
def test_get_title_versions(client):
imdb_id = 'tt0111161'
expected_keys = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment