Skip to content

Commit

Permalink
Merge pull request #81 from milokmet/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzeman committed Jun 8, 2020
2 parents 4808727 + b7e7edf commit dd539a4
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 6 deletions.
32 changes: 32 additions & 0 deletions tests/fixtures/api.trakt.tv/users/me/watched/movies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"plays": 4,
"last_watched_at": "2014-10-11T17:00:54.000Z",
"last_updated_at": "2014-10-11T17:00:54.000Z",
"movie": {
"title": "Batman Begins",
"year": 2005,
"ids": {
"trakt": 6,
"slug": "batman-begins-2005",
"imdb": "tt0372784",
"tmdb": 272
}
}
},
{
"plays": 2,
"last_watched_at": "2014-10-12T17:00:54.000Z",
"last_updated_at": "2014-10-12T17:00:54.000Z",
"movie": {
"title": "The Dark Knight",
"year": 2008,
"ids": {
"trakt": 4,
"slug": "the-dark-knight-2008",
"imdb": "tt0468569",
"tmdb": 155
}
}
}
]
99 changes: 99 additions & 0 deletions tests/fixtures/api.trakt.tv/users/me/watched/shows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[
{
"plays": 56,
"last_watched_at": "2014-10-11T17:00:54.000Z",
"last_updated_at": "2014-10-11T17:00:54.000Z",
"reset_at": null,
"show": {
"title": "Breaking Bad",
"year": 2008,
"ids": {
"trakt": 1,
"slug": "breaking-bad",
"tvdb": 81189,
"imdb": "tt0903747",
"tmdb": 1396
}
},
"seasons": [
{
"number": 1,
"episodes": [
{
"number": 1,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
},
{
"number": 2,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
}
]
},
{
"number": 2,
"episodes": [
{
"number": 1,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
},
{
"number": 2,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
}
]
}
]
},
{
"plays": 23,
"last_watched_at": "2014-10-12T17:00:54.000Z",
"last_updated_at": "2014-10-12T17:00:54.000Z",
"show": {
"title": "Parks and Recreation",
"year": 2009,
"ids": {
"trakt": 4,
"slug": "parks-and-recreation",
"tvdb": 84912,
"imdb": "tt1266020",
"tmdb": 8592
}
},
"seasons": [
{
"number": 1,
"episodes": [
{
"number": 1,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
},
{
"number": 2,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
}
]
},
{
"number": 2,
"episodes": [
{
"number": 1,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
},
{
"number": 2,
"plays": 1,
"last_watched_at": "2014-10-11T17:00:54.000Z"
}
]
}
]
}
]
65 changes: 65 additions & 0 deletions tests/users/test_watched.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from __future__ import absolute_import, division, print_function

from tests.core import mock
from trakt import Trakt

from httmock import HTTMock
import pytest


def test_get():
with HTTMock(mock.fixtures, mock.unknown):
with Trakt.configuration.auth('mock', 'mock'):

with pytest.raises(ValueError):
items = Trakt['users/*/watched'].get('me')

with pytest.raises(ValueError):
items = Trakt['users/*/watched'].get('me', 'episodes')

items = Trakt['users/*/watched'].get('me', 'shows')

assert items is not None

items = list(items)

assert len(items) == 2

assert items[0] is not None

assert items[1] is not None

assert items[0].title == 'Breaking Bad'

assert items[0].keys == [
('tvdb', '81189'),
('tmdb', '1396'),
('imdb', 'tt0903747'),
('slug', 'breaking-bad'),
('trakt', '1')
]

assert items[1].title == 'Parks and Recreation'

items = Trakt['users/*/watched'].get('me', 'movies')

assert items is not None

items = list(items)

assert len(items) == 2

assert items[0] is not None

assert items[1] is not None

assert items[0].title == 'Batman Begins'

assert items[1].title == 'The Dark Knight'

assert items[1].keys == [
('imdb', 'tt0468569'),
('tmdb', '155'),
('slug', 'the-dark-knight-2008'),
('trakt', '4')
]
3 changes: 3 additions & 0 deletions trakt/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
# /users/ratings
users.UsersRatingsInterface,

# /users/watched
users.UsersWatchedInterface,

# /users/watchlist
users.UsersWatchlistInterface

Expand Down
2 changes: 2 additions & 0 deletions trakt/interfaces/users/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from trakt.interfaces.users.lists import UsersListInterface, UsersListsInterface
from trakt.interfaces.users.ratings import UsersRatingsInterface
from trakt.interfaces.users.settings import UsersSettingsInterface
from trakt.interfaces.users.watched import UsersWatchedInterface
from trakt.interfaces.users.watchlist import UsersWatchlistInterface
from trakt.mapper import CommentMapper, ListMapper

Expand All @@ -26,6 +27,7 @@
'UsersListInterface',
'UsersRatingsInterface',
'UsersSettingsInterface',
'UsersWatchedInterface',
'UsersWatchlistInterface'
)

Expand Down
88 changes: 88 additions & 0 deletions trakt/interfaces/users/watched.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import absolute_import, division, print_function

from trakt.core.helpers import clean_username, dictfilter
from trakt.core.pagination import PaginationIterator
from trakt.interfaces.base import Interface, authenticated
from trakt.mapper import SyncMapper

import requests


class UsersWatchedInterface(Interface):
path = 'users/*/watched'
flags = {'is_watched': True}

def get(self, username, media=None, store=None, extended=None, page=None, per_page=None, **kwargs):

if not media or media not in ['shows', 'movies']:
raise ValueError('The "media" have to be one of ["shows", "media"]')

# Build parameters
params = []

if media:
params.append(media)

# Build query
query = {
'extended': extended,
'page': page,
'limit': per_page
}

# Send request
response = self.http.get(
'/users/%s/watched' % (clean_username(username)),
params=params,
query=query,
**dictfilter(kwargs, get=[
'exceptions'
], pop=[
'authenticated',
'pagination',
'validate_token'
])
)

# Parse response
items = self.get_data(response, **kwargs)

if isinstance(items, PaginationIterator):
return items.with_mapper(lambda items: SyncMapper.process(
self.client, store, items,
media=media,
flat=True,
**self.flags
))

if isinstance(items, requests.Response):
return items

if type(items) is not list:
return None

return SyncMapper.process(
self.client, store, items,
media=media,
flat=True,
**self.flags
)

#
# Shortcut methods
#

def movies(self, username, store=None, **kwargs):
return self.get(
username, 'movies',
store=store,
**kwargs
)

@authenticated
def shows(self, username, store=None, **kwargs):
return self.get(
username, 'shows',
store=store,
**kwargs
)
19 changes: 13 additions & 6 deletions trakt/mapper/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def process(cls, client, store, items, media=None, flat=False, **kwargs):
def item(cls, client, store, item, media=None, **kwargs):
i_type = item.get('type') or media

if not i_type:
raise ValueError('Unknown item type')

# Find item type function
if i_type.startswith('movie'):
func = cls.movie
Expand Down Expand Up @@ -205,7 +208,7 @@ def map_items(cls, client, store, items, func, **kwargs):
return store

@classmethod
def iterate_items(cls, client, store, items, func, **kwargs):
def iterate_items(cls, client, store, items, func, media=None, **kwargs):
if store is None:
store = {}

Expand All @@ -222,15 +225,18 @@ def iterate_items(cls, client, store, items, func, **kwargs):
store['episodes'] = {}

for item in items:
i_type = item.get('type')
i_type = item.get('type') or media

if not i_type:
raise ValueError('Unknown item type')

if i_type == 'movie':
if i_type.startswith('movie'):
i_store = store['movies']
elif i_type == 'show':
elif i_type.startswith('show'):
i_store = store['shows']
elif i_type == 'season':
elif i_type.startswith('season'):
i_store = store['seasons']
elif i_type == 'episode':
elif i_type.startswith('episode'):
i_store = store['episodes']
else:
raise ValueError('Unknown item type: %r' % i_type)
Expand All @@ -239,6 +245,7 @@ def iterate_items(cls, client, store, items, func, **kwargs):
result = func(
client, i_store, item,
append=True,
media=media,
**kwargs
)

Expand Down

0 comments on commit dd539a4

Please sign in to comment.