Skip to content

Commit

Permalink
update trakt watched_movies list during scan
Browse files Browse the repository at this point in the history
  • Loading branch information
simonc56 committed Sep 7, 2021
1 parent 83ded35 commit 694bd79
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions plex_trakt_sync/trakt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class TraktApi:

def __init__(self, batch_size=None):
self.batch = TraktBatch(self, batch_size=batch_size)
self._watched_movies = {}

This comment has been minimized.

Copy link
@glensc

glensc Sep 7, 2021

Collaborator

_watched_movies is later initialized as set(), so this is the wrong type. to create empty set, you use set():

trakt.core.CONFIG_PATH = pytrakt_file
trakt.core.session = factory.session()
load_config()
Expand Down Expand Up @@ -88,13 +89,14 @@ def liked_lists(self):
return pytrakt_extensions.get_liked_lists()

@property
@memoize
@nocache
@rate_limit()
def watched_movies(self):
return set(
map(lambda m: m.trakt, self.me.watched_movies)
)
if not self._watched_movies:
self._watched_movies = set(
map(lambda m: m.trakt, self.me.watched_movies)
)
return self._watched_movies

@property
@memoize
Expand Down Expand Up @@ -181,6 +183,7 @@ def scrobbler(media: Union[Movie, TVEpisode]) -> ScrobblerProxy:
@time_limit()
def mark_watched(self, m, time):
m.mark_as_seen(time)
self._watched_movies.add(m.trakt)

This comment has been minimized.

Copy link
@glensc

glensc Sep 7, 2021

Collaborator

this is fragile. what if someone calls mark_watched before watched_movies is initialized, the whole movies list be with one entry (the one filled by mark_watched).

but why the _watched_movies wrapper?
isn't the type of watched_movies after lazy init already of proper type?

also, the watched_movies seems to use indexed trakt_id, but m.trakt is an object not a number?

This comment has been minimized.

Copy link
@simonc56

simonc56 Sep 7, 2021

Author Collaborator

this is fragile. what if someone calls mark_watched before watched_movies is initialized

Correct

but why the _watched_movies wrapper?
isn't the type of watched_movies after lazy init already of proper type?

watched_movies is a property so it cannot be updated. You need to use a class variable to store the data. EDIT: I did not perfectly understand the python doc, it seems property CAN be updated just like a variable.

also, the watched_movies seems to use indexed trakt_id, but m.trakt is an object not a number?

yes I may be wrong here, i need to recheck this.

This comment has been minimized.

Copy link
@glensc

glensc Sep 7, 2021

Collaborator
        print(type(trakt.watched_movies))
        print(3 in trakt.watched_movies)
        print(3000 in trakt.watched_movies)
        print(len(trakt.watched_movies))
        trakt.watched_movies.add(3)
        trakt.watched_movies.add(3000)
        print(3 in trakt.watched_movies)
        print(3000 in trakt.watched_movies)
        print(len(trakt.watched_movies))

prints:

<class 'set'>
True
False
859
True
True
860

so, your wrapper code is totally un-needed.

regarding m.trakt seems your code is correct. it's passed as m.trakt from media, so in the end it's m.trakt.trakt which is m.trakt_id (isn't it confusing, yes! :D)

This comment has been minimized.

Copy link
@glensc

glensc Sep 7, 2021

Collaborator

and if it makes you understand things better, then trakt_api.watched_movies property is just a pointer to a set(), you not updating the property but a set() that it points to.

This comment has been minimized.

Copy link
@simonc56

simonc56 Sep 7, 2021

Author Collaborator

I removed the wrapper.
Thank you for your help, it's getting difficult for me. I have the idea but it's not easy to write it correctly.


def add_to_collection(self, m, pm: PlexLibraryItem):
if m.media_type == "movies":
Expand Down

0 comments on commit 694bd79

Please sign in to comment.