Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Plex collected_at for Movies #211

Merged
merged 8 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plex_trakt_sync/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def sync_collection(pm, tm, trakt: TraktApi, trakt_movie_collection):
return

logger.info(f"Add to Trakt Collection: {pm}")
trakt.add_to_collection(tm)
trakt.add_to_collection(tm, pm)


def sync_show_collection(pm, tm, pe, te, trakt: TraktApi):
Expand All @@ -31,7 +31,7 @@ def sync_show_collection(pm, tm, pe, te, trakt: TraktApi):
return

logger.info(f"Add to Trakt Collection: {pm} S{pe.seasonNumber:02}E{pe.index:02}")
trakt.add_to_collection(te.instance)
trakt.add_to_collection(te.instance, pe)


def sync_ratings(pm, tm, plex: PlexApi, trakt: TraktApi):
Expand Down
29 changes: 20 additions & 9 deletions plex_trakt_sync/plex_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from plexapi.video import Movie, Show
from plex_trakt_sync.decorators import memoize, nocache
from plex_trakt_sync.config import CONFIG
from trakt.utils import timestamp


class PlexLibraryItem:
Expand Down Expand Up @@ -66,16 +67,12 @@ def rating(self):
@property
@memoize
def seen_date(self):
media = self.item
if not media.lastViewedAt:
raise ValueError('lastViewedAt is not set')
return self.date_value(self.item.lastViewedAt)

date = media.lastViewedAt

try:
return date.astimezone(datetime.timezone.utc)
except ValueError: # for py<3.6
return date
@property
@memoize
def collected_at(self):
return self.date_value(self.item.addedAt)

def watch_progress(self, view_offset):
percent = view_offset / self.item.duration * 100
Expand All @@ -89,9 +86,23 @@ def guid_is_imdb_legacy(self):
# old item, like imdb 'tt0112253'
return guid[0:2] == "tt" and guid[2:].isnumeric()

def date_value(self, date):
if not date:
raise ValueError("Value can't be None")

try:
return date.astimezone(datetime.timezone.utc)
except ValueError: # for py<3.6
return date

def __repr__(self):
return "<%s:%s:%s>" % (self.provider, self.id, self.item)

def to_json(self):
return {
"collected_at": timestamp(self.collected_at),
}


class PlexLibrarySection:
def __init__(self, section: LibrarySection):
Expand Down
17 changes: 15 additions & 2 deletions plex_trakt_sync/trakt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,21 @@ def mark_watched(self, m, time):

@nocache
@rate_limit(delay=TRAKT_POST_DELAY)
def add_to_collection(self, m):
m.add_to_library()
def add_to_collection(self, m, pm: PlexLibraryItem):
# support is missing, compose custom json ourselves
# https://github.com/moogar0880/PyTrakt/issues/143
if m.media_type == "movies":
json = {
m.media_type: [dict(
title=m.title,
year=m.year,
**m.ids,
**pm.to_json(),
)],
}
return trakt.sync.add_to_collection(json)
else:
return m.add_to_library()

@memoize
@nocache
Expand Down