Skip to content

Commit 2dd7a50

Browse files
committed
Merge branch 'remove_extract_ids' into merge-remove_extract_ids
Merged moogar0880/PyTrakt#186
2 parents 3a6ab70 + f8f7a2e commit 2dd7a50

8 files changed

Lines changed: 94 additions & 92 deletions

File tree

docs/movies.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ block the specified movie from being shown in your recommended movies.
1515
::
1616

1717
>>> from trakt.movies import dismiss_recommendation
18-
>>> dismiss_recommendation(imdb_id='tt3139072', title='Son of Batman',
19-
... year=2014)
18+
>>> dismiss_recommendation('Son of Batman')
2019

2120

2221
This code snippet would prevent Son of Batman from appearing in your recommended

trakt/calendar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from trakt.core import get
55
from trakt.movies import Movie
66
from trakt.tv import TVEpisode, TVShow
7-
from trakt.utils import extract_ids, now, airs_date
7+
from trakt.utils import now, airs_date
88

99
__author__ = 'Jon Nappi'
1010
__all__ = ['Calendar', 'PremiereCalendar', 'MyPremiereCalendar',
@@ -72,7 +72,6 @@ def _build(self, data):
7272
first_aired = cal_item.get('first_aired')
7373
season = episode.get('season')
7474
ep_num = episode.get('number')
75-
extract_ids(show_data)
7675
show_data.update(show_data)
7776
e_data = {
7877
'airs_at': airs_date(first_aired),

trakt/mixins.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
"""Contains various MixIns"""
3+
4+
__author__ = 'Jon Nappi, Elan Ruusamäe'
5+
6+
7+
class IdsMixin:
8+
"""
9+
Provides Mixin to translate "ids" array
10+
to appropriate provider ids in base class.
11+
12+
This is replacement for extract_ids() utility method.
13+
"""
14+
15+
__ids = ['imdb', 'slug', 'tmdb', 'trakt']
16+
17+
def __init__(self):
18+
self._ids = {}
19+
20+
@property
21+
def ids(self):
22+
"""
23+
Accessor to the trakt, imdb, and tmdb ids,
24+
as well as the trakt.tv slug
25+
"""
26+
ids = {k: getattr(self, k, None) for k in self.__ids}
27+
return {
28+
'ids': ids
29+
}
30+
31+
@property
32+
def imdb(self):
33+
return self._ids.get('imdb', None)
34+
35+
@property
36+
def tmdb(self):
37+
return self._ids.get('tmdb', None)
38+
39+
@property
40+
def trakt(self):
41+
return self._ids.get('trakt', None)
42+
43+
@property
44+
def tvdb(self):
45+
return self._ids.get('tvdb', None)
46+
47+
@property
48+
def tvrage(self):
49+
return self._ids.get('tvrage', None)

trakt/movies.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"""Interfaces to all of the Movie objects offered by the Trakt.tv API"""
33
from collections import namedtuple
44
from trakt.core import Alias, Comment, Genre, get, delete
5+
from trakt.mixins import IdsMixin
56
from trakt.sync import (Scrobbler, comment, rate, add_to_history,
67
remove_from_history, add_to_watchlist,
78
remove_from_watchlist, add_to_collection,
89
remove_from_collection, search, checkin_media,
910
delete_checkin)
1011
from trakt.people import Person
11-
from trakt.utils import slugify, now, extract_ids
12+
from trakt.utils import slugify, now
1213

1314
__author__ = 'Jon Nappi'
1415
__all__ = ['dismiss_recommendation', 'get_recommended_movies', 'genres',
@@ -36,7 +37,6 @@ def get_recommended_movies():
3637
data = yield 'recommendations/movies'
3738
movies = []
3839
for movie in data:
39-
extract_ids(movie)
4040
movies.append(Movie(**movie))
4141
yield movies
4242

@@ -71,7 +71,6 @@ def updated_movies(timestamp=None):
7171
to_ret = []
7272
for movie in data:
7373
mov = movie.pop('movie')
74-
extract_ids(mov)
7574
mov.update({'updated_at': movie.pop('updated_at')})
7675
to_ret.append(Movie(**mov))
7776
yield to_ret
@@ -81,7 +80,7 @@ def updated_movies(timestamp=None):
8180
'note', 'release_type'])
8281

8382

84-
class Movie:
83+
class Movie(IdsMixin):
8584
"""A Class representing a Movie object"""
8685
def __init__(self, title, year=None, slug=None, **kwargs):
8786
super().__init__()
@@ -93,13 +92,15 @@ def __init__(self, title, year=None, slug=None, **kwargs):
9392
else:
9493
self.slug = slug or slugify(self.title)
9594

96-
self.released = self.tmdb_id = self.imdb_id = self.duration = None
97-
self.trakt_id = self.tagline = self.overview = self.runtime = None
95+
self.released = self.duration = None
96+
self.tagline = self.overview = self.runtime = None
9897
self.updated_at = self.trailer = self.homepage = self.rating = None
9998
self.votes = self.language = self.available_translations = None
10099
self.genres = self.certification = None
101100
self._comments = self._images = self._aliases = self._people = None
102101
self._ratings = self._releases = self._translations = None
102+
self.tmdb_id = self.imdb_id = None # @deprecated: unused
103+
self.trakt_id = None # @deprecated: unused
103104

104105
if len(kwargs) > 0:
105106
self._build(kwargs)
@@ -125,7 +126,6 @@ def _get(self):
125126

126127
def _build(self, data):
127128
"""Build this :class:`Movie` object with the data in *data*"""
128-
extract_ids(data)
129129
for key, val in data.items():
130130
if hasattr(self, '_' + key):
131131
setattr(self, '_' + key, val)
@@ -186,14 +186,6 @@ def crew(self):
186186
"""All of the crew members that worked on this :class:`Movie`"""
187187
return [p for p in self.people if getattr(p, 'job')]
188188

189-
@property
190-
def ids(self):
191-
"""Accessor to the trakt, imdb, and tmdb ids, as well as the trakt.tv
192-
slug
193-
"""
194-
return {'ids': {'trakt': self.trakt, 'slug': self.slug,
195-
'imdb': self.imdb, 'tmdb': self.tmdb}}
196-
197189
@property
198190
@get
199191
def images(self):

trakt/people.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# -*- coding: utf-8 -*-
22
"""Interfaces to all of the People objects offered by the Trakt.tv API"""
33
from trakt.core import get
4+
from trakt.mixins import IdsMixin
45
from trakt.sync import search
5-
from trakt.utils import extract_ids, slugify
6+
from trakt.utils import slugify
67

78
__author__ = 'Jon Nappi'
89
__all__ = ['Person', 'ActingCredit', 'CrewCredit', 'Credits', 'MovieCredits',
910
'TVCredits']
1011

1112

12-
class Person:
13+
class Person(IdsMixin):
1314
"""A Class representing a trakt.tv Person such as an Actor or Director"""
1415
def __init__(self, name, slug=None, **kwargs):
1516
super().__init__()
1617
self.name = name
17-
self.biography = self.birthplace = self.tmdb_id = self.birthday = None
18+
self.biography = self.birthplace = self.birthday = None
19+
self.death = self.homepage = None
1820
self.job = self.character = self._images = self._movie_credits = None
1921
self._tv_credits = None
2022
self.slug = slug or slugify(self.name)
23+
self.tmdb_id = None # @deprecated: unused
2124

2225
if len(kwargs) > 0:
2326
self._build(kwargs)
@@ -59,22 +62,13 @@ def _get(self):
5962
self._build(data)
6063

6164
def _build(self, data):
62-
extract_ids(data)
6365
for key, val in data.items():
6466
try:
6567
setattr(self, key, val)
6668
except AttributeError as ae:
6769
if not hasattr(self, '_' + key):
6870
raise ae
6971

70-
@property
71-
def ids(self):
72-
"""Accessor to the trakt, imdb, and tmdb ids, as well as the trakt.tv
73-
slug
74-
"""
75-
return {'ids': {'trakt': self.trakt, 'slug': self.slug,
76-
'imdb': self.imdb, 'tmdb': self.tmdb}}
77-
7872
@property
7973
@get
8074
def images(self):

trakt/sync.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from deprecated import deprecated
66

77
from trakt.core import get, post, delete
8-
from trakt.utils import slugify, extract_ids, timestamp
8+
from trakt.utils import slugify, timestamp
99

1010

1111
__author__ = 'Jon Nappi'
@@ -211,7 +211,6 @@ def get_search_results(query, search_type=None, slugify_query=False):
211211
# need to import Scrobblers
212212
results = []
213213
for media_item in data:
214-
extract_ids(media_item)
215214
result = SearchResult(media_item['type'], media_item['score'])
216215
if media_item['type'] == 'movie':
217216
from trakt.movies import Movie
@@ -280,15 +279,11 @@ def search_by_id(query, id_type='imdb', media_type=None, slugify_query=False):
280279
query=query, source=source, media_type=media_type)
281280
data = yield uri
282281

283-
for media_item in data:
284-
extract_ids(media_item)
285-
286282
results = []
287283
for d in data:
288284
if 'episode' in d:
289285
from trakt.tv import TVEpisode
290286
show = d.pop('show')
291-
extract_ids(d['episode'])
292287
results.append(TVEpisode(show.get('title', None),
293288
show_id=show['ids'].get('trakt'),
294289
**d.pop('episode')))
@@ -342,7 +337,6 @@ def get_watchlist(list_type=None, sort=None):
342337
if 'episode' in d:
343338
from trakt.tv import TVEpisode
344339
show = d.pop('show')
345-
extract_ids(d['episode'])
346340
results.append(TVEpisode(show.get('title', None),
347341
show_id=show.get('trakt', None),
348342
**d['episode']))

trakt/tv.py

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
from trakt.core import Airs, Alias, Comment, Genre, delete, get
88
from trakt.errors import NotFoundException
9+
from trakt.mixins import IdsMixin
910
from trakt.sync import (Scrobbler, rate, comment, add_to_collection,
1011
add_to_watchlist, add_to_history, remove_from_history,
1112
remove_from_collection, remove_from_watchlist, search,
1213
checkin_media, delete_checkin)
13-
from trakt.utils import slugify, extract_ids, airs_date
14+
from trakt.utils import slugify, airs_date
1415
from trakt.people import Person
1516

1617
__author__ = 'Jon Nappi'
@@ -197,15 +198,15 @@ def anticipated_shows(page=1, limit=10, extended=None):
197198
yield [TVShow(**show['show']) for show in data]
198199

199200

200-
class TVShow:
201+
class TVShow(IdsMixin):
201202
"""A Class representing a TV Show object."""
202203

203204
def __init__(self, title='', slug=None, **kwargs):
204205
super().__init__()
205206
self.media_type = 'shows'
206-
self.top_watchers = self.top_episodes = self.year = self.tvdb = None
207-
self.imdb = self.genres = self.certification = self.network = None
208-
self.trakt = self.tmdb = self._aliases = self._comments = None
207+
self.top_watchers = self.top_episodes = self.year = None
208+
self.genres = self.certification = self.network = None
209+
self._aliases = self._comments = None
209210
self._images = self._people = self._ratings = self._translations = None
210211
self._seasons = None
211212
self._last_episode = self._next_episode = None
@@ -219,6 +220,9 @@ def __init__(self, title='', slug=None, **kwargs):
219220

220221
@property
221222
def slug(self):
223+
if self._ids.get('slug', None) is not None:
224+
return self._ids['slug']
225+
222226
if self._slug is not None:
223227
return self._slug
224228

@@ -244,7 +248,6 @@ def _get(self):
244248
self._build(data)
245249

246250
def _build(self, data):
247-
extract_ids(data)
248251
for key, val in data.items():
249252
if hasattr(self, '_' + key):
250253
setattr(self, '_' + key, val)
@@ -363,16 +366,6 @@ def crew(self):
363366
"""All of the crew members that worked on this :class:`TVShow`"""
364367
return [p for p in self.people if getattr(p, 'job')]
365368

366-
@property
367-
def ids(self):
368-
"""Accessor to the trakt, imdb, and tmdb ids, as well as the trakt.tv
369-
slug
370-
"""
371-
return {'ids': {
372-
'trakt': self.trakt, 'slug': self.slug, 'imdb': self.imdb,
373-
'tmdb': self.tmdb, 'tvdb': self.tvdb
374-
}}
375-
376369
@property
377370
@get
378371
def images(self):
@@ -434,8 +427,6 @@ def seasons(self):
434427
data = yield self.ext + '/seasons?extended=episodes'
435428
self._seasons = []
436429
for season in data:
437-
extract_ids(season)
438-
439430
# Prepare episodes
440431
episodes = []
441432
for ep in season.pop('episodes', []):
@@ -447,6 +438,7 @@ def seasons(self):
447438
number = season.pop('number')
448439
season = TVSeason(self.title, number, self.slug, **season)
449440
self._seasons.append(season)
441+
450442
yield self._seasons
451443

452444
@property
@@ -562,7 +554,7 @@ def __str__(self):
562554
__repr__ = __str__
563555

564556

565-
class TVSeason:
557+
class TVSeason(IdsMixin):
566558
"""Container for TV Seasons"""
567559

568560
def __init__(self, show, season=1, slug=None, **kwargs):
@@ -702,7 +694,7 @@ def __len__(self):
702694
__repr__ = __str__
703695

704696

705-
class TVEpisode:
697+
class TVEpisode(IdsMixin):
706698
"""Container for TV Episodes"""
707699

708700
def __init__(self, show, season, number=-1, **kwargs):
@@ -713,8 +705,8 @@ def __init__(self, show, season, number=-1, **kwargs):
713705
self.number = number
714706
self.overview = self.title = self.year = self.number_abs = None
715707
self.first_aired = self.last_updated = None
716-
self.trakt = self.tmdb = self.tvdb = self.imdb = None
717-
self.tvrage = self._stats = self._images = self._comments = None
708+
self.runtime = None
709+
self._stats = self._images = self._comments = None
718710
self._translations = self._ratings = None
719711
if len(kwargs) > 0:
720712
self._build(kwargs)
@@ -732,7 +724,6 @@ def _get(self):
732724

733725
def _build(self, data):
734726
"""Build this :class:`TVEpisode` object with the data in *data*"""
735-
extract_ids(data)
736727
for key, val in data.items():
737728
if hasattr(self, '_' + key):
738729
setattr(self, '_' + key, val)
@@ -782,15 +773,6 @@ def search(title, year=None):
782773
"""
783774
return search(title, search_type='episode', year=year)
784775

785-
@property
786-
def ids(self):
787-
"""Accessor to the trakt, imdb, and tmdb ids, as well as the trakt.tv
788-
slug
789-
"""
790-
return {'ids': {
791-
'trakt': self.trakt, 'imdb': self.imdb, 'tmdb': self.tmdb
792-
}}
793-
794776
@property
795777
@get
796778
def images(self):

0 commit comments

Comments
 (0)