Skip to content

Commit

Permalink
Add sonicallySimilar method to Audio class
Browse files Browse the repository at this point in the history
closes #1183
  • Loading branch information
Dr-Blank committed Nov 6, 2023
1 parent 6ce6dfd commit 4afa80c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions plexapi/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<ratingKey>/thumb/<thumbid>).
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 4afa80c

Please sign in to comment.