From cd58670514861432f4fc4a2e9562bf8579e44240 Mon Sep 17 00:00:00 2001 From: Rato Date: Wed, 8 Feb 2023 19:20:51 +0100 Subject: [PATCH] fix: audio and subtitles codec were not matching correctly --- plexy/api.py | 43 ++++++++++++++++++++++++++++++------------- plexy/cli.py | 4 ++-- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/plexy/api.py b/plexy/api.py index 98e6468..ed1241b 100644 --- a/plexy/api.py +++ b/plexy/api.py @@ -130,13 +130,13 @@ def __repr__(self): class Criteria: def __init__(self, - libraries: typing.List[str], - titles: typing.List[Title], - newer_than: typing.Optional[str], - older_than: typing.Optional[str], - skip_watching: bool): - self.libraries = libraries - self.titles = titles + libraries: typing.Optional[typing.Iterable[str]] = None, + titles: typing.Optional[typing.Iterable[Title]] = None, + newer_than: typing.Optional[str] = None, + older_than: typing.Optional[str] = None, + skip_watching=False): + self.libraries = list(libraries or []) + self.titles = list(titles or []) self.newer_than = newer_than self.older_than = older_than self.skip_watching = skip_watching @@ -264,6 +264,14 @@ def save_preferences(self, preferences: Preferences): return changes + def __eq__(self, other): + if isinstance(other, Video): + return self.video == other.video + return NotImplemented + + def __hash__(self): + return hash(tuple(sorted(self.__dict__.items()))) + def __str__(self): return f'{self.title}' @@ -319,11 +327,13 @@ def default(self): @property def codec(self): - return self.stream.codec + if self.stream.codec: + return AudioCodec(self.stream.codec) @property def format(self): - return self.subtitle_stream.format if self.subtitle_stream else None + if self.subtitle_stream and self.subtitle_stream.format: + return SubtitleCodec(self.subtitle_stream.format) @property def audio_stream(self): @@ -514,16 +524,23 @@ def __repr__(self): class Plex: def __init__(self, settings: Settings): - self.plex = plexapi.server.PlexServer(baseurl=settings.url, token=settings.token) - logger.debug('Connected to %s', settings.url) + self.settings = settings + self._plex: typing.Optional[plexapi.server.PlexServer] = None + + @property + def server(self): + if self._plex is None: + logger.debug('Connected to %s', self.settings.url) + self._plex = plexapi.server.PlexServer(baseurl=self.settings.url, token=self.settings.token) + return self._plex def __find_sections(self, criteria: Criteria): if not criteria.libraries: - return self.plex.library.sections() + return self.server.library.sections() sections: typing.List[plexapi.library.LibrarySection] = [] for library in criteria.libraries: - sections.append(self.plex.library.section(library)) + sections.append(self.server.library.section(library)) return sections diff --git a/plexy/cli.py b/plexy/cli.py index ebf1592..a3562d4 100644 --- a/plexy/cli.py +++ b/plexy/cli.py @@ -55,10 +55,10 @@ def convert(self, value: typing.Any, param: typing.Optional[click.Parameter], ct class AgeParamType(click.ParamType): name = 'age' - language_re = re.compile(r'^(?:(?P\d+?)w)?(?:(?P\d+?)d)?(?:(?P\d+?)h)?$') + age_re = re.compile(r'^(?:(?P\d+?)w)?(?:(?P\d+?)d)?(?:(?P\d+?)h)?$') def convert(self, value: typing.Any, param: typing.Optional[click.Parameter], ctx: typing.Optional[click.Context]): - match = self.language_re.match(value) + match = self.age_re.match(value) if not match: self.fail(f"{click.style(f'{value}', bold=True)} is not a valid age")