Skip to content

Commit

Permalink
Improve content fetching logic to limit series episodes in recent ite…
Browse files Browse the repository at this point in the history
…ms. (#56)


* Improve content fetching logic to limit series episodes in recent items.

* Fixed misconfigured library id for Plex libraries
  • Loading branch information
giuseppe99barchetta authored Oct 25, 2024
1 parent 789a5e0 commit 42e6c2f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
44 changes: 42 additions & 2 deletions api_service/services/plex/plex_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,61 @@ async def get_recent_items(self):
"""
url = f"{self.api_url}/status/sessions/history/all"
params = {
"sort": "viewedAt:desc"
"sort": "viewedAt:desc",
}

try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.headers, params=params, timeout=REQUEST_TIMEOUT) as response:
if response.status == 200:
data = await response.json()
return data.get('MediaContainer', {}).get('Metadata', [])
metadata = data.get('MediaContainer', {}).get('Metadata', [])
# Filter the items while respecting the max content fetch limit
filtered_items = await self.filter_recent_items(metadata)
self.logger.info(f"Returning {len(filtered_items)} filtered recent items.")
return filtered_items
self.logger.error("Failed to retrieve recent items: %d", response.status)
except aiohttp.ClientError as e:
self.logger.error("An error occurred while retrieving recent items: %s", str(e))

return []

async def filter_recent_items(self, metadata):
"""
Filters recent items to avoid duplicates and respects the max content fetch limit.
:param metadata: List of items to filter.
:return: Filtered list of items.
"""
seen_series = set()
filtered_items = []
total_items = 0 # Counter for total filtered items

for item in metadata:
# Check if the item's librarySectionID is in the selected libraries
if self.library_ids and item.get('librarySectionID') not in self.library_ids:
continue # Skip items not in the selected libraries

# Check if we've reached the max content fetch limit
if total_items >= int(self.max_content_fetch):
break

if item['type'] == 'episode':
# Check if the series has already been counted
series_title = item['grandparentTitle']
if series_title not in seen_series:
seen_series.add(series_title)
filtered_items.append(item)
total_items += 1 # Increment total_items for series
else:
filtered_items.append(item)
total_items += 1 # Increment total_items for movies

# Allow fetching more content if we've only seen episodes from one series
if total_items < int(self.max_content_fetch) and len(seen_series) == 1:
continue # Keep looking for more items

return filtered_items

async def get_libraries(self):
"""
Retrieves a list of libraries (sections) from the Plex server asynchronously.
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/configWizard/PlexConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
<div v-if="libraries.length > 0">
<p class="text-sm text-gray-300 mt-4">Select the Plex libraries you want to include:</p>
<div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mt-4">
<div v-for="library in libraries" :key="library.uuid" @click="toggleLibrarySelection(library)" :class="{
'bg-indigo-600 border-indigo-600': isSelected(library.uuid),
'bg-gray-700 border-gray-600': !isSelected(library.uuid)
<div v-for="library in libraries" :key="library.key" @click="toggleLibrarySelection(library)" :class="{
'bg-indigo-600 border-indigo-600': isSelected(library.key),
'bg-gray-700 border-gray-600': !isSelected(library.key)
}" class="cursor-pointer p-4 border rounded-lg text-center text-white hover:bg-indigo-500">
{{ library.title }}
</div>
Expand Down

0 comments on commit 42e6c2f

Please sign in to comment.