From 88b0b40052e0cca5dd66f45a369fd203dfe3daae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 9 Apr 2021 23:17:13 +0300 Subject: [PATCH 1/8] Add date_value helper to format dates from plex --- plex_trakt_sync/plex_api.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plex_trakt_sync/plex_api.py b/plex_trakt_sync/plex_api.py index c11c42bd016..172a0b38db1 100644 --- a/plex_trakt_sync/plex_api.py +++ b/plex_trakt_sync/plex_api.py @@ -66,16 +66,7 @@ def rating(self): @property @memoize def seen_date(self): - media = self.item - if not media.lastViewedAt: - raise ValueError('lastViewedAt is not set') - - date = media.lastViewedAt - - try: - return date.astimezone(datetime.timezone.utc) - except ValueError: # for py<3.6 - return date + return self.date_value(self.item.lastViewedAt) def watch_progress(self, view_offset): percent = view_offset / self.item.duration * 100 @@ -89,6 +80,15 @@ 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) From 9f4b7ece54c0e29a79742e88e101eef87372eb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 9 Apr 2021 23:18:40 +0300 Subject: [PATCH 2/8] Add collected_at property --- plex_trakt_sync/plex_api.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plex_trakt_sync/plex_api.py b/plex_trakt_sync/plex_api.py index 172a0b38db1..b0ef5dcc008 100644 --- a/plex_trakt_sync/plex_api.py +++ b/plex_trakt_sync/plex_api.py @@ -68,6 +68,11 @@ def rating(self): def seen_date(self): return self.date_value(self.item.lastViewedAt) + @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 return percent From c3fe8f651c925a5c9e0069832a28d507e3c261f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Fri, 9 Apr 2021 23:19:21 +0300 Subject: [PATCH 3/8] Add logic to fill collected_at for library items --- plex_trakt_sync/commands/sync.py | 4 ++-- plex_trakt_sync/trakt_api.py | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plex_trakt_sync/commands/sync.py b/plex_trakt_sync/commands/sync.py index 54c7523dc2a..5d7397b2557 100644 --- a/plex_trakt_sync/commands/sync.py +++ b/plex_trakt_sync/commands/sync.py @@ -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): @@ -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): diff --git a/plex_trakt_sync/trakt_api.py b/plex_trakt_sync/trakt_api.py index 84f820fc2cb..be7ca26ec82 100644 --- a/plex_trakt_sync/trakt_api.py +++ b/plex_trakt_sync/trakt_api.py @@ -165,8 +165,12 @@ 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 + json = m.to_json() + json.update(pm.to_json()) + trakt.sync.add_to_collection(json) @memoize @nocache From 7ff33b8eb928fbb81de096ffb8deb432e10ed1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 10 Apr 2021 00:56:07 +0300 Subject: [PATCH 4/8] Add to_json method to plex media item --- plex_trakt_sync/plex_api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plex_trakt_sync/plex_api.py b/plex_trakt_sync/plex_api.py index b0ef5dcc008..1c233edbae9 100644 --- a/plex_trakt_sync/plex_api.py +++ b/plex_trakt_sync/plex_api.py @@ -97,6 +97,10 @@ def date_value(self, date): def __repr__(self): return "<%s:%s:%s>" % (self.provider, self.id, self.item) + def to_json(self): + return { + "collected_at": self.collected_at, + } class PlexLibrarySection: def __init__(self, section: LibrarySection): From 9afcc6c344df37c513fa6f550f1b07d2620a0c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 10 Apr 2021 00:56:41 +0300 Subject: [PATCH 5/8] Implement collected_at for movies --- plex_trakt_sync/trakt_api.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/plex_trakt_sync/trakt_api.py b/plex_trakt_sync/trakt_api.py index be7ca26ec82..9af178078e0 100644 --- a/plex_trakt_sync/trakt_api.py +++ b/plex_trakt_sync/trakt_api.py @@ -168,9 +168,16 @@ def mark_watched(self, m, time): def add_to_collection(self, m, pm: PlexLibraryItem): # support is missing, compose custom json ourselves # https://github.com/moogar0880/PyTrakt/issues/143 - json = m.to_json() - json.update(pm.to_json()) - trakt.sync.add_to_collection(json) + if m.media_type == "movies": + json = { + m.media_type: dict( + **m.ids, + **pm.to_json(), + ), + } + trakt.sync.add_to_collection(json) + else: + m.add_to_library() @memoize @nocache From a6cdbaccbf5b2b315b15fdd1a627c7cbeff7129c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 10 Apr 2021 00:59:52 +0300 Subject: [PATCH 6/8] The collected_at value must be iso8601-formatted --- plex_trakt_sync/plex_api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plex_trakt_sync/plex_api.py b/plex_trakt_sync/plex_api.py index 1c233edbae9..27fc83c95e2 100644 --- a/plex_trakt_sync/plex_api.py +++ b/plex_trakt_sync/plex_api.py @@ -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: @@ -99,9 +100,10 @@ def __repr__(self): def to_json(self): return { - "collected_at": self.collected_at, + "collected_at": timestamp(self.collected_at), } + class PlexLibrarySection: def __init__(self, section: LibrarySection): self.section = section From aa45f23e0fb70383ef20c602eacf6300990fdbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 10 Apr 2021 01:22:41 +0300 Subject: [PATCH 7/8] Movies is a list --- plex_trakt_sync/trakt_api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plex_trakt_sync/trakt_api.py b/plex_trakt_sync/trakt_api.py index 9af178078e0..a687a63a45b 100644 --- a/plex_trakt_sync/trakt_api.py +++ b/plex_trakt_sync/trakt_api.py @@ -170,14 +170,14 @@ def add_to_collection(self, m, pm: PlexLibraryItem): # https://github.com/moogar0880/PyTrakt/issues/143 if m.media_type == "movies": json = { - m.media_type: dict( + m.media_type: [dict( **m.ids, **pm.to_json(), - ), + )], } - trakt.sync.add_to_collection(json) + return trakt.sync.add_to_collection(json) else: - m.add_to_library() + return m.add_to_library() @memoize @nocache From 5eecd1635163b198c85355da76a201b818d40d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 10 Apr 2021 01:27:59 +0300 Subject: [PATCH 8/8] Add title and year --- plex_trakt_sync/trakt_api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plex_trakt_sync/trakt_api.py b/plex_trakt_sync/trakt_api.py index a687a63a45b..06361a7cc53 100644 --- a/plex_trakt_sync/trakt_api.py +++ b/plex_trakt_sync/trakt_api.py @@ -171,6 +171,8 @@ def add_to_collection(self, m, pm: PlexLibraryItem): if m.media_type == "movies": json = { m.media_type: [dict( + title=m.title, + year=m.year, **m.ids, **pm.to_json(), )],