Skip to content

Commit

Permalink
Merge pull request #1737 from glensc/merge-section-pager
Browse files Browse the repository at this point in the history
Refactor: Rename PlexShowSectionPager to PlexSectionPager
  • Loading branch information
glensc authored Jan 16, 2024
2 parents 54a03cc + eee2464 commit f361324
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 54 deletions.
8 changes: 2 additions & 6 deletions plextraktsync/plan/Walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,8 @@ def media_from_sections(self, sections: list[PlexLibrarySection]) -> Generator[P
for section in sections:
with measure_time(f"{section.title_link} processed", extra={"markup": True}):
self.set_window_title(f"Processing {section.title}")
total = len(section)
it = self.progressbar(
section.items(total),
total=total,
section.pager(),
desc=f"Processing {section.title_link}",
)
yield from it
Expand All @@ -158,10 +156,8 @@ def episodes_from_sections(self, sections: list[PlexLibrarySection]) -> Generato
for section in sections:
with measure_time(f"{section.title_link} processed", extra={"markup": True}):
self.set_window_title(f"Processing {section.title}")
items = section.search_episodes()
it = self.progressbar(
items,
total=len(items),
section.pager("episode"),
desc=f"Processing {section.title_link}",
)
yield from it
Expand Down
46 changes: 5 additions & 41 deletions plextraktsync/plex/PlexLibrarySection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

from typing import TYPE_CHECKING

from plexapi import X_PLEX_CONTAINER_SIZE
from plexapi.exceptions import NotFound

from plextraktsync.decorators.retry import retry
from plextraktsync.mixin.RichMarkup import RichMarkup
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem

if TYPE_CHECKING:
from typing import Literal

from plexapi.library import MovieSection, ShowSection

from plextraktsync.plex.PlexApi import PlexApi
Expand All @@ -21,11 +20,10 @@ def __init__(self, section: ShowSection | MovieSection, plex: PlexApi = None):
self.section = section
self.plex = plex

def __len__(self):
return self.section.totalSize
def pager(self, libtype: Literal["episode"] = None):
from plextraktsync.plex.PlexSectionPager import PlexSectionPager

def __iter__(self):
return self.items(len(self))
return PlexSectionPager(section=self.section, plex=self.plex, libtype=libtype)

@property
def type(self):
Expand Down Expand Up @@ -61,39 +59,5 @@ def find_by_id(self, id: str | int) -> PlexMedia | None:
except NotFound:
return None

def search_episodes(self):
if self.section.type == "show":
from plextraktsync.plex.PlexShowSectionPager import \
PlexShowSectionPager

return PlexShowSectionPager(section=self.section, plex=self.plex)

return None

def all(self, max_items: int):
libtype = self.section.TYPE
key = self.section._buildSearchKey(libtype=libtype, returnKwargs=False)
start = 0
size = X_PLEX_CONTAINER_SIZE

while True:
items = self.fetch_items(key, size, start)
if not len(items):
break

yield from items

start += size
if start > max_items:
break

@retry()
def fetch_items(self, key: str, size: int, start: int):
return self.section.fetchItems(key, container_start=start, container_size=size, maxresults=size)

def items(self, max_items: int):
for item in self.all(max_items):
yield PlexLibraryItem(item, plex=self.plex)

def __repr__(self):
return f"<{self.__class__.__name__}:{self.type}:{self.title}>"
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
from functools import cached_property
from typing import TYPE_CHECKING

from plextraktsync.decorators.retry import retry
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem

if TYPE_CHECKING:
from plexapi.library import ShowSection
from plexapi.library import MovieSection, ShowSection

from plextraktsync.plex.PlexApi import PlexApi


class PlexShowSectionPager:
def __init__(self, section: ShowSection, plex: PlexApi):
class PlexSectionPager:
def __init__(self, section: ShowSection | MovieSection, plex: PlexApi, libtype: str = None):
self.section = section
self.plex = plex
self.libtype = libtype if libtype is not None else section.TYPE

def __len__(self):
return self.total_size

@cached_property
def total_size(self):
return self.section.totalViewSize(libtype="episode", includeCollections=False)
return self.section.totalViewSize(libtype=self.libtype, includeCollections=False)

@retry()
def fetch_items(self, start: int, size: int):
return self.section.search(libtype=self.libtype, container_start=start, container_size=size, maxresults=size)

def __iter__(self):
from plexapi import X_PLEX_CONTAINER_SIZE
Expand All @@ -31,7 +37,7 @@ def __iter__(self):
size = X_PLEX_CONTAINER_SIZE

while True:
items = self.section.searchEpisodes(container_start=start, container_size=size, maxresults=size)
items = self.fetch_items(start=start, size=size)

if not len(items):
break
Expand Down
8 changes: 6 additions & 2 deletions plextraktsync/rich/RichProgressBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@

class RichProgressBar:
def __init__(self, iterable, total=None, options=None, desc=""):
self.iter = iter(iterable)
self.options = options or {}
self.desc = desc
if total is None:
total = len(iterable)
self.total = total
self.i = 0

if hasattr(iterable, "__next__"):
self.iterable_next = iterable.__next__
else:
self.iterable_next = iter(iterable).__next__

def __iter__(self):
return self

def __next__(self):
res = self.iter.__next__()
res = self.iterable_next()
self.update()
return res

Expand Down

0 comments on commit f361324

Please sign in to comment.