Skip to content

Commit

Permalink
Improvements to progress construction
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzeman committed Oct 5, 2018
1 parent 772ada5 commit a8e751b
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions trakt/objects/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from trakt.core.helpers import from_iso8601_datetime, popitems, to_iso8601_datetime
from trakt.objects.core.helpers import update_attributes
from trakt.objects.episode import Episode
from trakt.objects.season import Season

LABELS = {
'last_progress_change': {
Expand Down Expand Up @@ -53,17 +51,17 @@ def __repr__(self):


class Progress(BaseProgress):
def __init__(self, client, progress_type, aired=None, completed=None):
super(Progress, self).__init__(aired, completed)
progress_type = None
"""
:type: :class:`~python:str`
self._client = client
Progress Type (:code:`watched` or :code:`collection`)
"""

self.progress_type = progress_type
"""
:type: :class:`~python:str`
def __init__(self, client, aired=None, completed=None):
super(Progress, self).__init__(aired, completed)

Progress Type (:code:`watched` or :code:`collection`)
"""
self._client = client

self.last_progress_change = None
"""
Expand Down Expand Up @@ -150,6 +148,7 @@ def _update(self, info=None, **kwargs):
super(Progress, self)._update(info, **kwargs)

label = LABELS['last_progress_change'][self.progress_type]

if label in info:
self.last_progress_change = from_iso8601_datetime(info.get(label))

Expand All @@ -159,28 +158,28 @@ def _update(self, info=None, **kwargs):
if 'seasons' in info:
for season in info['seasons']:
season_progress = SeasonProgress._construct(season, progress_type=self.progress_type)

if season_progress:
self.seasons[season_progress.pk] = season_progress

if 'hidden_seasons' in info:
self.hidden_seasons = {}
from trakt.mapper import ProgressMapper

for season in info['hidden_seasons']:
_, keys = ProgressMapper.get_ids('season', season)
hidden_season = Season._construct(self._client, keys, season)
hidden_season = self._client.construct('season', season)

if hidden_season:
self.hidden_seasons[hidden_season.pk] = hidden_season

if 'next_episode' in info:
from trakt.mapper import ProgressMapper
_, keys = ProgressMapper.get_ids('episode', info['next_episode'])
episode = Episode._construct(self._client, keys, info['next_episode'])
episode = self._client.construct('episode', info['next_episode'])

if episode:
self.next_episode = episode

if 'last_episode' in info:
_, keys = ProgressMapper.get_ids('episode', info['last_episode'])
episode = Episode._construct(self._client, keys, info['last_episode'])
episode = self._client.construct('episode', info['last_episode'])

if episode:
self.last_episode = episode

Expand All @@ -196,13 +195,11 @@ def _construct(cls, client, info=None, **kwargs):


class WatchedProgress(Progress):
def __init__(self, client):
super(WatchedProgress, self).__init__(client, 'watched')
progress_type = 'watched'


class CollectionProgress(Progress):
def __init__(self, client):
super(CollectionProgress, self).__init__(client, 'collection')
progress_type = 'collection'


class SeasonProgress(BaseProgress):
Expand Down Expand Up @@ -240,9 +237,11 @@ def _update(self, info=None, **kwargs):
super(SeasonProgress, self)._update(info, **kwargs)

self.pk = info['number']

if 'episodes' in info:
for episode in info['episodes']:
episode_progress = EpisodeProgress._construct(episode, **kwargs)

if episode_progress:
self.episodes[episode_progress.pk] = episode_progress

Expand Down Expand Up @@ -292,6 +291,7 @@ def to_dict(self):
label = LABELS['episode_progress_change'][self.progress_type]
else:
label = 'progress_timestamp'

result[label] = to_iso8601_datetime(self.progress_timestamp)

return result
Expand All @@ -301,13 +301,15 @@ def _update(self, info=None, **kwargs):
return

self.pk = info['number']

if 'progress_type' in kwargs:
self.progress_type = kwargs['progress_type']

self.completed = info['completed']

if 'last_watched_at' in info:
self.progress_timestamp = from_iso8601_datetime(info.get('last_watched_at'))

elif 'collected_at' in info:
self.progress_timestamp = from_iso8601_datetime(info.get('collected_at'))

Expand Down

0 comments on commit a8e751b

Please sign in to comment.