Skip to content

Commit

Permalink
Merge pull request #1900 from glensc/rating-episode-no-reload
Browse files Browse the repository at this point in the history
Performance: Remove reloads for episode ratings sync
  • Loading branch information
glensc authored Apr 11, 2024
2 parents 42c8476 + 7984ed3 commit 9ba89f6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
2 changes: 1 addition & 1 deletion plextraktsync/config/HttpCacheConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class HttpCacheConfig:
"plex.tv/api/v2/user": "1m",
# Plex patterns
# Ratings search
"*/library/sections/*/all?*userRating%3E%3E=-1*": DO_NOT_CACHE,
"*/library/sections/*/all?*userRating%3E%3E=-1*": "1m",
# len(PlexLibrarySection)
"*/library/sections/*/all?includeCollections=0&X-Plex-Container-Size=0&X-Plex-Container-Start=0": DO_NOT_CACHE,
# __iter__(PlexLibrarySection)
Expand Down
6 changes: 1 addition & 5 deletions plextraktsync/media/Media.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,7 @@ def trakt_rating(self):

@cached_property
def plex_rating(self):
if self.media_type == "episodes" and not self.plex.is_discover and self.show:
show_id = self.show.plex.item.ratingKey
else:
show_id = None
return self.plex.rating(show_id)
return self.plex.rating()

def trakt_rate(self):
rating = self.plex_rating
Expand Down
4 changes: 2 additions & 2 deletions plextraktsync/plex/PlexLibraryItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def title(self):

return value

def rating(self, show_id: int = None):
def rating(self):
if not self.is_discover and self.plex is not None:
return self.plex.ratings.get(self, show_id)
return self.plex.ratings.get(self)

return Rating.create(self.item.userRating, self.item.lastRatedAt)

Expand Down
27 changes: 10 additions & 17 deletions plextraktsync/plex/PlexRatings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ def get(self, m: PlexLibraryItem, show_id: int = None):
# this can happen when doing "inspect"
if section_id not in self.plex.library_sections:
return None
if m.media_type not in ["movies", "shows", "episodes"]:
raise RuntimeError(f"Unsupported media type: {m.media_type}")

media_type = m.media_type
section = self.plex.library_sections[section_id]
ratings: dict[int, Rating] = self.ratings(section, media_type)
ratings: dict[int, Rating] = self.ratings(section, m.media_type)

if media_type in ["movies", "shows"]:
# For movies and shows, just return from the dict
if m.item.ratingKey in ratings:
return ratings[m.item.ratingKey]
elif media_type == "episodes":
# For episodes the ratings is just (show_id, show_rating) tuples
# if show id is not listed, return none, otherwise fetch from item itself
if show_id not in ratings:
return None
return Rating.create(m.item.userRating, m.item.lastRatedAt)
else:
raise RuntimeError(f"Unsupported media type: {media_type}")

return None
return ratings.get(m.item.ratingKey, None)

@staticmethod
@memoize
Expand All @@ -54,12 +42,17 @@ def ratings(section: PlexLibrarySection, media_type: str):
"episodes": "episode.userRating",
"shows": "show.userRating",
}[media_type]
libtype = {
"movies": "movie",
"episodes": "episode",
"shows": "show",
}[media_type]

filters = {
"and": [
{f"{key}>>": -1},
]
}

for item in section.search(filters=filters, includeGuids=False):
for item in section.search(filters=filters, libtype=libtype, includeGuids=False):
yield item.ratingKey, Rating.create(item.userRating, item.lastRatedAt)

0 comments on commit 9ba89f6

Please sign in to comment.