From 4afa80ca65b561d8d48477599f6056fb8ea52774 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Mon, 6 Nov 2023 13:45:57 -0500 Subject: [PATCH 1/9] Add sonicallySimilar method to Audio class closes #1183 --- plexapi/audio.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/plexapi/audio.py b/plexapi/audio.py index 2a1698776..7c3327824 100644 --- a/plexapi/audio.py +++ b/plexapi/audio.py @@ -3,6 +3,8 @@ from pathlib import Path from urllib.parse import quote_plus +from typing_extensions import Self + from plexapi import media, utils from plexapi.base import Playable, PlexPartialObject, PlexHistory, PlexSession from plexapi.exceptions import BadRequest @@ -34,6 +36,7 @@ class Audio(PlexPartialObject, PlayedUnplayedMixin): listType (str): Hardcoded as 'audio' (useful for search filters). moods (List<:class:`~plexapi.media.Mood`>): List of mood objects. musicAnalysisVersion (int): The Plex music analysis version for the item. + distance (float): Sonic Distance of the item from the seed item. ratingKey (int): Unique key identifying the item. summary (str): Summary of the artist, album, or track. thumb (str): URL to thumbnail image (/library/metadata//thumb/). @@ -65,6 +68,7 @@ def _loadData(self, data): self.listType = 'audio' self.moods = self.findItems(data, media.Mood) self.musicAnalysisVersion = utils.cast(int, data.attrib.get('musicAnalysisVersion')) + self.distance = utils.cast(float, data.attrib.get('distance')) self.ratingKey = utils.cast(int, data.attrib.get('ratingKey')) self.summary = data.attrib.get('summary') self.thumb = data.attrib.get('thumb') @@ -125,6 +129,31 @@ def sync(self, bitrate, client=None, clientId=None, limit=None, title=None): return myplex.sync(sync_item, client=client, clientId=clientId) + def sonicallySimilar( + self, + limit: int = 30, + maxDistance: float = 0.25, + **kwargs, + ) -> "list[Self]": + """ Find sonically similar audio items. + + Parameters: + limit (int): maximum count of items to return, unlimited if `None`. + maxDistance (float): maximum distance between tracks, 0.0 - 1.0. + **kwargs: Additional options passed into :func:`~plexapi.base.PlexObject.fetchItems`. + + + Returns: + List[:class:`~plexapi.audio.Audio`]: list of sonically similar audio items. + """ + + key = f"/library/metadata/{self.ratingKey}/nearest?limit={limit}&maxDistance={maxDistance}" + return self.fetchItems( + key, + cls=self.__class__, + **kwargs, + ) + @utils.registerPlexObject class Artist( From 3871110d22f3e96de1dc6bd1c880e8865eb6cf1e Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:34:54 -0500 Subject: [PATCH 2/9] Add type hinting for method - fixes import error --- plexapi/audio.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plexapi/audio.py b/plexapi/audio.py index 7c3327824..8a0e62b97 100644 --- a/plexapi/audio.py +++ b/plexapi/audio.py @@ -3,7 +3,7 @@ from pathlib import Path from urllib.parse import quote_plus -from typing_extensions import Self +from typing import TypeVar from plexapi import media, utils from plexapi.base import Playable, PlexPartialObject, PlexHistory, PlexSession @@ -16,6 +16,9 @@ from plexapi.playlist import Playlist +Self = TypeVar("Self", bound="Audio") + + class Audio(PlexPartialObject, PlayedUnplayedMixin): """ Base class for all audio objects including :class:`~plexapi.audio.Artist`, :class:`~plexapi.audio.Album`, and :class:`~plexapi.audio.Track`. @@ -130,7 +133,7 @@ def sync(self, bitrate, client=None, clientId=None, limit=None, title=None): return myplex.sync(sync_item, client=client, clientId=clientId) def sonicallySimilar( - self, + self: Self, limit: int = 30, maxDistance: float = 0.25, **kwargs, @@ -142,7 +145,6 @@ def sonicallySimilar( maxDistance (float): maximum distance between tracks, 0.0 - 1.0. **kwargs: Additional options passed into :func:`~plexapi.base.PlexObject.fetchItems`. - Returns: List[:class:`~plexapi.audio.Audio`]: list of sonically similar audio items. """ From d38bfba67bd6274ffc4cc3f69784930ee684570a Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:24:40 -0500 Subject: [PATCH 3/9] Apply review suggestions Co-Authors @JonnyWong16 --- plexapi/audio.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plexapi/audio.py b/plexapi/audio.py index 8a0e62b97..3c802b8cb 100644 --- a/plexapi/audio.py +++ b/plexapi/audio.py @@ -16,7 +16,7 @@ from plexapi.playlist import Playlist -Self = TypeVar("Self", bound="Audio") +TAudio = TypeVar("TAudio", bound="Audio") class Audio(PlexPartialObject, PlayedUnplayedMixin): @@ -27,6 +27,7 @@ class Audio(PlexPartialObject, PlayedUnplayedMixin): addedAt (datetime): Datetime the item was added to the library. art (str): URL to artwork image (/library/metadata//art/). artBlurHash (str): BlurHash string for artwork image. + distance (float): Sonic Distance of the item from the seed item. fields (List<:class:`~plexapi.media.Field`>): List of field objects. guid (str): Plex GUID for the artist, album, or track (plex://artist/5d07bcb0403c64029053ac4c). index (int): Plex index number (often the track number). @@ -39,7 +40,6 @@ class Audio(PlexPartialObject, PlayedUnplayedMixin): listType (str): Hardcoded as 'audio' (useful for search filters). moods (List<:class:`~plexapi.media.Mood`>): List of mood objects. musicAnalysisVersion (int): The Plex music analysis version for the item. - distance (float): Sonic Distance of the item from the seed item. ratingKey (int): Unique key identifying the item. summary (str): Summary of the artist, album, or track. thumb (str): URL to thumbnail image (/library/metadata//thumb/). @@ -133,12 +133,12 @@ def sync(self, bitrate, client=None, clientId=None, limit=None, title=None): return myplex.sync(sync_item, client=client, clientId=clientId) def sonicallySimilar( - self: Self, + self: TAudio, limit: int = 30, maxDistance: float = 0.25, **kwargs, - ) -> "list[Self]": - """ Find sonically similar audio items. + ) -> List[TAudio]: + """Returns a list of sonically similar audio items. Parameters: limit (int): maximum count of items to return, unlimited if `None`. From a2ac2f34af7c8ab9a8242b89d8e51477dc962971 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Tue, 7 Nov 2023 15:26:17 -0500 Subject: [PATCH 4/9] Add optional parameters to sonicallySimilar method - makes it so that params can be None and use the server default --- plexapi/audio.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/plexapi/audio.py b/plexapi/audio.py index 3c802b8cb..b66e9db46 100644 --- a/plexapi/audio.py +++ b/plexapi/audio.py @@ -3,7 +3,7 @@ from pathlib import Path from urllib.parse import quote_plus -from typing import TypeVar +from typing import List, Optional, TypeVar from plexapi import media, utils from plexapi.base import Playable, PlexPartialObject, PlexHistory, PlexSession @@ -134,22 +134,27 @@ def sync(self, bitrate, client=None, clientId=None, limit=None, title=None): def sonicallySimilar( self: TAudio, - limit: int = 30, - maxDistance: float = 0.25, + limit: Optional[int] = None, + maxDistance: Optional[float] = None, **kwargs, ) -> List[TAudio]: """Returns a list of sonically similar audio items. Parameters: - limit (int): maximum count of items to return, unlimited if `None`. - maxDistance (float): maximum distance between tracks, 0.0 - 1.0. + limit (int): Maximum count of items to return. Default 50 (server default) + maxDistance (float): Maximum distance between tracks, 0.0 - 1.0. Default 0.25 (server default). **kwargs: Additional options passed into :func:`~plexapi.base.PlexObject.fetchItems`. Returns: List[:class:`~plexapi.audio.Audio`]: list of sonically similar audio items. """ - key = f"/library/metadata/{self.ratingKey}/nearest?limit={limit}&maxDistance={maxDistance}" + key = f"{self.key}/nearest" + params = {"maxDistance": maxDistance, "limit": limit} + key += utils.joinArgs( + {k: v for k, v in params.items() if v is not None} + ) + return self.fetchItems( key, cls=self.__class__, From 6b93f93f17f7591e2553e1fd720ca02f5ef9e26f Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:05:10 -0500 Subject: [PATCH 5/9] add test for `sonicallySimilar` --- tests/test_audio.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_audio.py b/tests/test_audio.py index cb26e29b8..be84b5e0a 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -439,6 +439,13 @@ def test_audio_Audio_section(artist, album, track): assert track.section().key == album.section().key == artist.section().key +def test_audio_Audio_sonicallySimilar(artist, album, track): + # TODO: assert list of tracks/albums/artists + assert isinstance(artist.sonicallySimilar(limit=15), list) + assert isinstance(album.sonicallySimilar(maxDistance=0.1), list) + assert isinstance(track.sonicallySimilar(), list) + + def test_audio_Artist_download(monkeydownload, tmpdir, artist): total = len(artist.tracks()) filepaths = artist.download(savepath=str(tmpdir)) From ea5ba9a063047db96c79d94d39e5dce09d59c792 Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:42:32 -0500 Subject: [PATCH 6/9] Refactor test to check type of elements --- tests/test_audio.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_audio.py b/tests/test_audio.py index be84b5e0a..5b71081e9 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -439,11 +439,10 @@ def test_audio_Audio_section(artist, album, track): assert track.section().key == album.section().key == artist.section().key -def test_audio_Audio_sonicallySimilar(artist, album, track): - # TODO: assert list of tracks/albums/artists - assert isinstance(artist.sonicallySimilar(limit=15), list) - assert isinstance(album.sonicallySimilar(maxDistance=0.1), list) - assert isinstance(track.sonicallySimilar(), list) +def test_audio_Audio_sonicallySimilar(artist): + similar_audio = artist.sonicallySimilar() + assert isinstance(similar_audio, list) + assert all(isinstance(i, type(artist)) for i in similar_audio) def test_audio_Artist_download(monkeydownload, tmpdir, artist): From 38da020d4e8735b269dfb9cb9304fabea0baa75d Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Sat, 11 Nov 2023 04:23:50 -0500 Subject: [PATCH 7/9] Apply suggestions Co-authored-by: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> --- plexapi/audio.py | 14 ++++++++------ tests/test_audio.py | 6 ++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plexapi/audio.py b/plexapi/audio.py index b66e9db46..10ba97689 100644 --- a/plexapi/audio.py +++ b/plexapi/audio.py @@ -3,7 +3,7 @@ from pathlib import Path from urllib.parse import quote_plus -from typing import List, Optional, TypeVar +from typing import Any, Dict, List, Optional, TypeVar from plexapi import media, utils from plexapi.base import Playable, PlexPartialObject, PlexHistory, PlexSession @@ -59,6 +59,7 @@ def _loadData(self, data): self.addedAt = utils.toDatetime(data.attrib.get('addedAt')) self.art = data.attrib.get('art') self.artBlurHash = data.attrib.get('artBlurHash') + self.distance = utils.cast(float, data.attrib.get('distance')) self.fields = self.findItems(data, media.Field) self.guid = data.attrib.get('guid') self.index = utils.cast(int, data.attrib.get('index')) @@ -71,7 +72,6 @@ def _loadData(self, data): self.listType = 'audio' self.moods = self.findItems(data, media.Mood) self.musicAnalysisVersion = utils.cast(int, data.attrib.get('musicAnalysisVersion')) - self.distance = utils.cast(float, data.attrib.get('distance')) self.ratingKey = utils.cast(int, data.attrib.get('ratingKey')) self.summary = data.attrib.get('summary') self.thumb = data.attrib.get('thumb') @@ -150,10 +150,12 @@ def sonicallySimilar( """ key = f"{self.key}/nearest" - params = {"maxDistance": maxDistance, "limit": limit} - key += utils.joinArgs( - {k: v for k, v in params.items() if v is not None} - ) + params: Dict[str, Any] = {} + if limit is not None: + params['limit'] = limit + if maxDistance is not None: + params['maxDistance'] = maxDistance + key += utils.joinArgs(params) return self.fetchItems( key, diff --git a/tests/test_audio.py b/tests/test_audio.py index 5b71081e9..185d249d4 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -444,6 +444,12 @@ def test_audio_Audio_sonicallySimilar(artist): assert isinstance(similar_audio, list) assert all(isinstance(i, type(artist)) for i in similar_audio) + similar_audio = artist.sonicallySimilar(limit=1) + assert len(similar_audio) <= 1 + + similar_audio = artist.sonicallySimilar(maxDistance=0.1) + assert all(i.distance <= 0.1 for i in similar_audio) + def test_audio_Artist_download(monkeydownload, tmpdir, artist): total = len(artist.tracks()) From 559532a70893ee97d319bc7e8158d2904e7262aa Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:25:29 -0500 Subject: [PATCH 8/9] Add authentication to sonicallySimilar test in test_audio.py --- tests/test_audio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_audio.py b/tests/test_audio.py index 185d249d4..ef16b2b25 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -438,7 +438,7 @@ def test_audio_Audio_section(artist, album, track): assert track.section() assert track.section().key == album.section().key == artist.section().key - +@pytest.mark.authenticated def test_audio_Audio_sonicallySimilar(artist): similar_audio = artist.sonicallySimilar() assert isinstance(similar_audio, list) From 58960c4884b39c7aa8a0397e0dc54a8c4900692d Mon Sep 17 00:00:00 2001 From: Dr-Blank <64108942+Dr-Blank@users.noreply.github.com> Date: Sat, 11 Nov 2023 14:29:45 -0500 Subject: [PATCH 9/9] fix flake8 --- tests/test_audio.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_audio.py b/tests/test_audio.py index ef16b2b25..2024c39bb 100644 --- a/tests/test_audio.py +++ b/tests/test_audio.py @@ -438,6 +438,7 @@ def test_audio_Audio_section(artist, album, track): assert track.section() assert track.section().key == album.section().key == artist.section().key + @pytest.mark.authenticated def test_audio_Audio_sonicallySimilar(artist): similar_audio = artist.sonicallySimilar()