Skip to content

Commit ad370dc

Browse files
authored
Feature: Add User.get_watched_movies (without pagination) (#114)
2 parents 8bcaa74 + 6efabb2 commit ad370dc

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

tests/test_users.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def test_watched():
125125
assert all([isinstance(s, TVShow) for s in sean.watched_shows])
126126

127127

128+
def test_get_watched_movies():
129+
sean = User('sean')
130+
watched_movies = sean.get_watched_movies()
131+
assert isinstance(watched_movies, list)
132+
assert all([isinstance(m, Movie) for m in watched_movies])
133+
134+
128135
def test_stats():
129136
sean = User('sean')
130137
assert isinstance(sean.get_stats(), dict)

trakt/users.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -509,22 +509,39 @@ def show_collection(self):
509509
self._show_collection.append(show)
510510
yield self._show_collection
511511

512-
@property
512+
def _build_watched_movies(self, data):
513+
"""Parse raw API response data into a list of :class:`Movie` objects.
514+
515+
:param data: List of raw movie dicts from the Trakt API
516+
:return: List of :class:`Movie` instances
517+
"""
518+
watched_movies = []
519+
for movie in data:
520+
movie_data = movie.pop('movie')
521+
movie_data.update(movie)
522+
watched_movies.append(Movie(**movie_data))
523+
return watched_movies
524+
513525
@get
526+
def get_watched_movies(self):
527+
"""Watched progress for all :class:`Movie` objects for this
528+
:class:`User`.
529+
530+
:return: List of :class:`Movie` instances
531+
"""
532+
data = yield 'users/{user}/watched/movies'.format(
533+
user=slugify(self.username)
534+
)
535+
yield self._build_watched_movies(data)
536+
537+
@property
514538
def watched_movies(self):
515539
"""Watched progress for all :class:`Movie`'s in this :class:`User`'s
516540
collection.
517541
"""
518542
if self._watched_movies is None:
519-
data = yield 'users/{user}/watched/movies'.format(
520-
user=slugify(self.username)
521-
)
522-
self._watched_movies = []
523-
for movie in data:
524-
movie_data = movie.pop('movie')
525-
movie_data.update(movie)
526-
self._watched_movies.append(Movie(**movie_data))
527-
yield self._watched_movies
543+
self._watched_movies = self.get_watched_movies()
544+
return self._watched_movies
528545

529546
@property
530547
@get

0 commit comments

Comments
 (0)