Skip to content

Commit

Permalink
Force refresh (#385)
Browse files Browse the repository at this point in the history
* feat: add force refresh to update plex from sym library

* fix: iteration mutating bugfix

* fix: add validation for plex updating on update_folder

* fix: improve symlink type validation

* fix: attempt at fixing dequeue issue

* fix: update plex watchlists with tweaks

* chore: update version

---------

Co-authored-by: Spoked <Spoked@localhost>
  • Loading branch information
dreulavelle and Spoked authored Jun 11, 2024
1 parent 73f437b commit d814c03
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.4
0.6.5
65 changes: 38 additions & 27 deletions backend/program/content/plex_watchlist.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Plex Watchlist Module"""

from typing import Generator
from typing import Generator, Union
from program.indexers.trakt import create_item_from_imdb_id

from program.media.item import MediaItem
from program.media.item import Episode, MediaItem, Movie, Season, Show
from program.settings.manager import settings_manager
from requests import HTTPError
from utils.logger import logger
Expand Down Expand Up @@ -46,19 +47,30 @@ def validate(self):
return True
return True

def run(self):
"""Fetch new media from `Plex Watchlist`"""
watchlist_items = set(self._get_items_from_watchlist())
rss_items = set()

if self.rss_enabled:
rss_items = set(self._get_items_from_rss())
def run(self) -> Generator[Union[Movie, Show, Season, Episode], None, None]:
"""Fetch new media from `Plex Watchlist` and RSS feed if enabled."""
try:
watchlist_items = set(self._get_items_from_watchlist())
rss_items = set(self._get_items_from_rss()) if self.rss_enabled else set()
except Exception as e:
logger.error(f"Error fetching items: {e}")
return

yield from (
MediaItem({"imdb_id": imdb_id, "requested_by": self.key})
for imdb_id in watchlist_items.union(rss_items)
)
new_items = watchlist_items | rss_items

for imdb_id in new_items:
if imdb_id in self.recurring_items:
continue
self.recurring_items.add(imdb_id)
try:
media_item: MediaItem = create_item_from_imdb_id(imdb_id)
if not media_item:
logger.error(f"Failed to create media item from IMDb ID: {imdb_id}")
continue
yield media_item
except Exception as e:
logger.error(f"Error processing IMDb ID {imdb_id}: {e}")
continue

def _get_items_from_rss(self) -> Generator[MediaItem, None, None]:
"""Fetch media from Plex RSS Feed."""
Expand All @@ -68,15 +80,9 @@ def _get_items_from_rss(self) -> Generator[MediaItem, None, None]:
logger.error(f"Failed to fetch Plex RSS feed: HTTP {response.status_code}")
return
for item in response.data.items:
for guid in item.guids:
if guid.startswith("imdb://"):
imdb_id = guid.split("//")[-1]
if imdb_id and imdb_id not in self.recurring_items:
self.recurring_items.add(imdb_id)
yield imdb_id
yield from self._extract_imdb_ids(item.guids)
except Exception as e:
logger.error(f"An unexpected error occurred while fetching Plex RSS feed: {e}")
return

def _get_items_from_watchlist(self) -> Generator[MediaItem, None, None]:
"""Fetch media from Plex watchlist"""
Expand All @@ -93,8 +99,7 @@ def _get_items_from_watchlist(self) -> Generator[MediaItem, None, None]:
for item in media_container.Metadata:
if hasattr(item, "ratingKey") and item.ratingKey:
imdb_id = self._ratingkey_to_imdbid(item.ratingKey)
if imdb_id and imdb_id not in self.recurring_items:
self.recurring_items.add(imdb_id)
if imdb_id:
yield imdb_id

@staticmethod
Expand All @@ -104,10 +109,16 @@ def _ratingkey_to_imdbid(ratingKey: str) -> str:
filter_params = "includeGuids=1&includeFields=guid,title,year&includeElements=Guid"
url = f"https://metadata.provider.plex.tv/library/metadata/{ratingKey}?X-Plex-Token={token}&{filter_params}"
response = get(url)
if response.is_ok and hasattr(response.data, "MediaContainer"): # noqa: SIM102
if hasattr(response.data.MediaContainer.Metadata[0], "Guid"):
for guid in response.data.MediaContainer.Metadata[0].Guid:
if "imdb://" in guid.id:
return guid.id.split("//")[-1]
if response.is_ok and hasattr(response.data, "MediaContainer"):
metadata = response.data.MediaContainer.Metadata[0]
return next((guid.id.split("//")[-1] for guid in metadata.Guid if "imdb://" in guid.id), None)
logger.debug(f"Failed to fetch IMDb ID for ratingKey: {ratingKey}")
return None

def _extract_imdb_ids(self, guids):
"""Helper method to extract IMDb IDs from guids"""
for guid in guids:
if guid.startswith("imdb://"):
imdb_id = guid.split("//")[-1]
if imdb_id:
yield imdb_id

0 comments on commit d814c03

Please sign in to comment.