Add ratings property being TraktRatingCollection to trakt_api
MODIFY
plextraktsync/trakt_api.py
plextraktsync/trakt_api.py
+from __future__ import annotations
from typing import Union
import trakt
from plextraktsync import pytrakt_extensions
from plextraktsync.decorators.cached_property import cached_property
+from plextraktsync.decorators.flatten import flatten_dict
from plextraktsync.decorators.memoize import memoize
from plextraktsync.decorators.nocache import nocache
from plextraktsync.decorators.rate_limit import rate_limit
@@ -52,6 +54,24 @@ def stop(self, progress: float):
self.scrobbler.pause()
+class TraktRatingCollection(dict):
+ def __init__(self, trakt: TraktApi):
+ super(dict, self).__init__()
+ self.trakt = trakt
+
+ def __missing__(self, media_type: str):
+ ratings = self.ratings(media_type)
+ self[media_type] = ratings
+
+ return ratings
+
+ @flatten_dict
+ def ratings(self, media_type: str):
+ index = media_type.rstrip('s')
+ for r in self.trakt.me.get_ratings(media_type):
+ yield r[index]['ids']['trakt'], r['rating']
+
+
class TraktApi:
"""
Trakt API class abstracting common data access and dealing with requests cache.
@@ -130,6 +150,10 @@ def watched_shows(self):
def watchlist_movies(self):
return self.me.watchlist_movies
+ @cached_property
+ def ratings(self):
+ return TraktRatingCollection(self)
+
@cached_property
@nocache
@rate_limit()

Add ratings property being TraktRatingCollection to trakt_api