2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-11-20 22:16:58 +00:00
|
|
|
import os
|
2023-08-27 19:59:53 +00:00
|
|
|
from pathlib import Path
|
2020-05-12 21:15:16 +00:00
|
|
|
from urllib.parse import quote_plus
|
|
|
|
|
2023-11-13 18:47:48 +00:00
|
|
|
from typing import Any, Dict, List, Optional, TypeVar
|
|
|
|
|
2022-02-27 06:04:18 +00:00
|
|
|
from plexapi import media, utils
|
2023-07-28 00:45:51 +00:00
|
|
|
from plexapi.base import Playable, PlexPartialObject, PlexHistory, PlexSession
|
2023-07-28 01:02:23 +00:00
|
|
|
from plexapi.exceptions import BadRequest
|
2022-02-27 05:40:51 +00:00
|
|
|
from plexapi.mixins import (
|
2022-08-26 19:14:24 +00:00
|
|
|
AdvancedSettingsMixin, SplitMergeMixin, UnmatchMatchMixin, ExtrasMixin, HubsMixin, PlayedUnplayedMixin, RatingMixin,
|
2022-02-27 05:40:51 +00:00
|
|
|
ArtUrlMixin, ArtMixin, PosterUrlMixin, PosterMixin, ThemeMixin, ThemeUrlMixin,
|
2023-07-27 21:45:23 +00:00
|
|
|
ArtistEditMixins, AlbumEditMixins, TrackEditMixins
|
2022-02-27 05:40:51 +00:00
|
|
|
)
|
2022-01-24 04:39:35 +00:00
|
|
|
from plexapi.playlist import Playlist
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2023-11-13 18:47:48 +00:00
|
|
|
TAudio = TypeVar("TAudio", bound="Audio")
|
|
|
|
|
|
|
|
|
2023-07-27 21:45:23 +00:00
|
|
|
class Audio(PlexPartialObject, PlayedUnplayedMixin):
|
2020-12-24 06:24:46 +00:00
|
|
|
""" Base class for all audio objects including :class:`~plexapi.audio.Artist`,
|
|
|
|
:class:`~plexapi.audio.Album`, and :class:`~plexapi.audio.Track`.
|
2017-01-22 06:37:39 +00:00
|
|
|
|
|
|
|
Attributes:
|
2020-12-23 23:23:10 +00:00
|
|
|
addedAt (datetime): Datetime the item was added to the library.
|
|
|
|
art (str): URL to artwork image (/library/metadata/<ratingKey>/art/<artid>).
|
2020-11-22 05:25:24 +00:00
|
|
|
artBlurHash (str): BlurHash string for artwork image.
|
2023-11-13 18:47:48 +00:00
|
|
|
distance (float): Sonic Distance of the item from the seed item.
|
2020-12-23 23:23:10 +00:00
|
|
|
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).
|
2017-01-22 06:37:39 +00:00
|
|
|
key (str): API URL (/library/metadata/<ratingkey>).
|
2021-05-30 22:47:53 +00:00
|
|
|
lastRatedAt (datetime): Datetime the item was last rated.
|
2020-12-23 23:23:10 +00:00
|
|
|
lastViewedAt (datetime): Datetime the item was last played.
|
2017-01-22 06:37:39 +00:00
|
|
|
librarySectionID (int): :class:`~plexapi.library.LibrarySection` ID.
|
2020-12-23 23:23:10 +00:00
|
|
|
librarySectionKey (str): :class:`~plexapi.library.LibrarySection` key.
|
|
|
|
librarySectionTitle (str): :class:`~plexapi.library.LibrarySection` title.
|
2017-01-22 06:37:39 +00:00
|
|
|
listType (str): Hardcoded as 'audio' (useful for search filters).
|
2020-12-23 23:23:10 +00:00
|
|
|
moods (List<:class:`~plexapi.media.Mood`>): List of mood objects.
|
2021-09-13 00:53:06 +00:00
|
|
|
musicAnalysisVersion (int): The Plex music analysis version for the item.
|
2020-12-23 23:23:10 +00:00
|
|
|
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>).
|
2020-11-22 05:25:24 +00:00
|
|
|
thumbBlurHash (str): BlurHash string for thumbnail image.
|
2020-12-23 23:23:10 +00:00
|
|
|
title (str): Name of the artist, album, or track (Jason Mraz, We Sing, Lucky, etc.).
|
2017-01-22 06:37:39 +00:00
|
|
|
titleSort (str): Title to use when sorting (defaults to title).
|
|
|
|
type (str): 'artist', 'album', or 'track'.
|
2022-02-27 03:26:08 +00:00
|
|
|
updatedAt (datetime): Datetime the item was updated.
|
2021-05-30 22:47:53 +00:00
|
|
|
userRating (float): Rating of the item (0.0 - 10.0) equaling (0 stars - 5 stars).
|
2020-12-23 23:23:10 +00:00
|
|
|
viewCount (int): Count of times the item was played.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2018-09-08 15:25:16 +00:00
|
|
|
METADATA_TYPE = 'track'
|
|
|
|
|
2016-01-19 10:56:16 +00:00
|
|
|
def _loadData(self, data):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2017-02-06 04:52:10 +00:00
|
|
|
self._data = data
|
2017-02-04 17:43:50 +00:00
|
|
|
self.addedAt = utils.toDatetime(data.attrib.get('addedAt'))
|
2020-11-22 04:02:31 +00:00
|
|
|
self.art = data.attrib.get('art')
|
|
|
|
self.artBlurHash = data.attrib.get('artBlurHash')
|
2023-11-13 18:47:48 +00:00
|
|
|
self.distance = utils.cast(float, data.attrib.get('distance'))
|
2020-12-24 04:39:15 +00:00
|
|
|
self.fields = self.findItems(data, media.Field)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.guid = data.attrib.get('guid')
|
|
|
|
self.index = utils.cast(int, data.attrib.get('index'))
|
2020-12-24 06:17:43 +00:00
|
|
|
self.key = data.attrib.get('key', '')
|
2021-05-16 05:38:35 +00:00
|
|
|
self.lastRatedAt = utils.toDatetime(data.attrib.get('lastRatedAt'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.lastViewedAt = utils.toDatetime(data.attrib.get('lastViewedAt'))
|
2021-03-11 21:27:08 +00:00
|
|
|
self.librarySectionID = utils.cast(int, data.attrib.get('librarySectionID'))
|
2020-12-23 23:23:10 +00:00
|
|
|
self.librarySectionKey = data.attrib.get('librarySectionKey')
|
|
|
|
self.librarySectionTitle = data.attrib.get('librarySectionTitle')
|
|
|
|
self.listType = 'audio'
|
|
|
|
self.moods = self.findItems(data, media.Mood)
|
2021-09-13 00:53:06 +00:00
|
|
|
self.musicAnalysisVersion = utils.cast(int, data.attrib.get('musicAnalysisVersion'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.ratingKey = utils.cast(int, data.attrib.get('ratingKey'))
|
|
|
|
self.summary = data.attrib.get('summary')
|
|
|
|
self.thumb = data.attrib.get('thumb')
|
2020-11-22 04:02:31 +00:00
|
|
|
self.thumbBlurHash = data.attrib.get('thumbBlurHash')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.title = data.attrib.get('title')
|
2016-03-21 04:26:02 +00:00
|
|
|
self.titleSort = data.attrib.get('titleSort', self.title)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.type = data.attrib.get('type')
|
|
|
|
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt'))
|
2021-05-30 22:47:53 +00:00
|
|
|
self.userRating = utils.cast(float, data.attrib.get('userRating'))
|
2016-03-21 04:26:02 +00:00
|
|
|
self.viewCount = utils.cast(int, data.attrib.get('viewCount', 0))
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-11-08 21:01:53 +00:00
|
|
|
def url(self, part):
|
2020-12-23 23:23:10 +00:00
|
|
|
""" Returns the full URL for the audio item. Typically used for getting a specific track. """
|
2018-01-05 02:44:35 +00:00
|
|
|
return self._server.url(part, includeToken=True) if part else None
|
2017-11-08 21:01:53 +00:00
|
|
|
|
2018-09-08 15:25:16 +00:00
|
|
|
def _defaultSyncTitle(self):
|
|
|
|
""" Returns str, default title for a new syncItem. """
|
|
|
|
return self.title
|
|
|
|
|
2021-09-13 00:53:06 +00:00
|
|
|
@property
|
|
|
|
def hasSonicAnalysis(self):
|
|
|
|
""" Returns True if the audio has been sonically analyzed. """
|
|
|
|
return self.musicAnalysisVersion == 1
|
|
|
|
|
2018-09-08 15:25:16 +00:00
|
|
|
def sync(self, bitrate, client=None, clientId=None, limit=None, title=None):
|
|
|
|
""" Add current audio (artist, album or track) as sync item for specified device.
|
2020-11-23 03:06:30 +00:00
|
|
|
See :func:`~plexapi.myplex.MyPlexAccount.sync` for possible exceptions.
|
2018-09-08 15:25:16 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
bitrate (int): maximum bitrate for synchronized music, better use one of MUSIC_BITRATE_* values from the
|
2020-11-23 03:06:30 +00:00
|
|
|
module :mod:`~plexapi.sync`.
|
|
|
|
client (:class:`~plexapi.myplex.MyPlexDevice`): sync destination, see
|
|
|
|
:func:`~plexapi.myplex.MyPlexAccount.sync`.
|
|
|
|
clientId (str): sync destination, see :func:`~plexapi.myplex.MyPlexAccount.sync`.
|
2018-09-08 15:25:16 +00:00
|
|
|
limit (int): maximum count of items to sync, unlimited if `None`.
|
2020-11-23 03:06:30 +00:00
|
|
|
title (str): descriptive title for the new :class:`~plexapi.sync.SyncItem`, if empty the value would be
|
2018-09-08 15:25:16 +00:00
|
|
|
generated from metadata of current media.
|
|
|
|
|
|
|
|
Returns:
|
2020-11-23 03:06:30 +00:00
|
|
|
:class:`~plexapi.sync.SyncItem`: an instance of created syncItem.
|
2018-09-08 15:25:16 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from plexapi.sync import SyncItem, Policy, MediaSettings
|
|
|
|
|
|
|
|
myplex = self._server.myPlexAccount()
|
|
|
|
sync_item = SyncItem(self._server, None)
|
|
|
|
sync_item.title = title if title else self._defaultSyncTitle()
|
|
|
|
sync_item.rootTitle = self.title
|
|
|
|
sync_item.contentType = self.listType
|
|
|
|
sync_item.metadataType = self.METADATA_TYPE
|
|
|
|
sync_item.machineIdentifier = self._server.machineIdentifier
|
|
|
|
|
|
|
|
section = self._server.library.sectionByID(self.librarySectionID)
|
|
|
|
|
2022-08-28 05:56:01 +00:00
|
|
|
sync_item.location = f'library://{section.uuid}/item/{quote_plus(self.key)}'
|
2018-09-08 15:25:16 +00:00
|
|
|
sync_item.policy = Policy.create(limit)
|
|
|
|
sync_item.mediaSettings = MediaSettings.createMusic(bitrate)
|
|
|
|
|
|
|
|
return myplex.sync(sync_item, client=client, clientId=clientId)
|
|
|
|
|
2023-11-13 18:47:48 +00:00
|
|
|
def sonicallySimilar(
|
|
|
|
self: TAudio,
|
|
|
|
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. 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"{self.key}/nearest"
|
|
|
|
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,
|
|
|
|
cls=self.__class__,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
2016-03-15 02:20:02 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2022-02-27 05:40:51 +00:00
|
|
|
class Artist(
|
|
|
|
Audio,
|
2022-02-27 06:04:18 +00:00
|
|
|
AdvancedSettingsMixin, SplitMergeMixin, UnmatchMatchMixin, ExtrasMixin, HubsMixin, RatingMixin,
|
2022-02-27 05:40:51 +00:00
|
|
|
ArtMixin, PosterMixin, ThemeMixin,
|
2023-07-27 21:45:23 +00:00
|
|
|
ArtistEditMixins
|
2022-02-27 05:40:51 +00:00
|
|
|
):
|
2020-12-24 06:24:46 +00:00
|
|
|
""" Represents a single Artist.
|
2017-01-22 06:37:39 +00:00
|
|
|
|
|
|
|
Attributes:
|
2017-02-14 04:32:27 +00:00
|
|
|
TAG (str): 'Directory'
|
|
|
|
TYPE (str): 'artist'
|
2021-03-11 17:20:28 +00:00
|
|
|
albumSort (int): Setting that indicates how albums are sorted for the artist
|
|
|
|
(-1 = Library default, 0 = Newest first, 1 = Oldest first, 2 = By name).
|
2020-12-23 23:23:10 +00:00
|
|
|
collections (List<:class:`~plexapi.media.Collection`>): List of collection objects.
|
|
|
|
countries (List<:class:`~plexapi.media.Country`>): List country objects.
|
|
|
|
genres (List<:class:`~plexapi.media.Genre`>): List of genre objects.
|
2022-07-21 02:35:46 +00:00
|
|
|
guids (List<:class:`~plexapi.media.Guid`>): List of guid objects.
|
2017-01-22 06:37:39 +00:00
|
|
|
key (str): API URL (/library/metadata/<ratingkey>).
|
2022-02-27 01:32:18 +00:00
|
|
|
labels (List<:class:`~plexapi.media.Label`>): List of label objects.
|
2020-12-24 04:40:08 +00:00
|
|
|
locations (List<str>): List of folder paths where the artist is found on disk.
|
2020-12-23 23:23:10 +00:00
|
|
|
similar (List<:class:`~plexapi.media.Similar`>): List of similar objects.
|
|
|
|
styles (List<:class:`~plexapi.media.Style`>): List of style objects.
|
2022-02-27 02:47:54 +00:00
|
|
|
theme (str): URL to theme resource (/library/metadata/<ratingkey>/theme/<themeid>).
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Directory'
|
2016-01-19 09:31:45 +00:00
|
|
|
TYPE = 'artist'
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2016-04-07 05:39:04 +00:00
|
|
|
Audio._loadData(self, data)
|
2021-03-11 17:20:28 +00:00
|
|
|
self.albumSort = utils.cast(int, data.attrib.get('albumSort', '-1'))
|
2020-12-23 23:23:10 +00:00
|
|
|
self.collections = self.findItems(data, media.Collection)
|
2017-02-13 02:55:55 +00:00
|
|
|
self.countries = self.findItems(data, media.Country)
|
|
|
|
self.genres = self.findItems(data, media.Genre)
|
2022-07-21 02:35:46 +00:00
|
|
|
self.guids = self.findItems(data, media.Guid)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.key = self.key.replace('/children', '') # FIX_BUG_50
|
2022-02-27 01:32:18 +00:00
|
|
|
self.labels = self.findItems(data, media.Label)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.locations = self.listAttrs(data, 'path', etag='Location')
|
2017-02-13 02:55:55 +00:00
|
|
|
self.similar = self.findItems(data, media.Similar)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.styles = self.findItems(data, media.Style)
|
2022-02-27 02:47:54 +00:00
|
|
|
self.theme = data.attrib.get('theme')
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2017-10-09 14:07:09 +00:00
|
|
|
def __iter__(self):
|
|
|
|
for album in self.albums():
|
|
|
|
yield album
|
|
|
|
|
2016-01-19 09:31:45 +00:00
|
|
|
def album(self, title):
|
2017-01-31 04:44:03 +00:00
|
|
|
""" Returns the :class:`~plexapi.audio.Album` that matches the specified title.
|
2017-01-22 06:37:39 +00:00
|
|
|
|
|
|
|
Parameters:
|
|
|
|
title (str): Title of the album to return.
|
|
|
|
"""
|
2023-07-28 01:02:23 +00:00
|
|
|
return self.section().get(
|
|
|
|
title=title,
|
|
|
|
libtype='album',
|
|
|
|
filters={'artist.id': self.ratingKey}
|
|
|
|
)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2017-02-09 04:08:25 +00:00
|
|
|
def albums(self, **kwargs):
|
2020-12-23 23:23:10 +00:00
|
|
|
""" Returns a list of :class:`~plexapi.audio.Album` objects by the artist. """
|
2023-07-28 01:02:23 +00:00
|
|
|
return self.section().search(
|
|
|
|
libtype='album',
|
|
|
|
filters={'artist.id': self.ratingKey},
|
|
|
|
**kwargs
|
|
|
|
)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2020-12-24 05:38:48 +00:00
|
|
|
def track(self, title=None, album=None, track=None):
|
2017-01-31 04:44:03 +00:00
|
|
|
""" Returns the :class:`~plexapi.audio.Track` that matches the specified title.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-22 06:37:39 +00:00
|
|
|
Parameters:
|
|
|
|
title (str): Title of the track to return.
|
2020-12-24 05:38:48 +00:00
|
|
|
album (str): Album name (default: None; required if title not specified).
|
|
|
|
track (int): Track number (default: None; required if title not specified).
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:exc:`~plexapi.exceptions.BadRequest`: If title or album and track parameters are missing.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2022-07-21 02:37:48 +00:00
|
|
|
key = f'{self.key}/allLeaves'
|
2020-12-30 23:49:26 +00:00
|
|
|
if title is not None:
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.fetchItem(key, Track, title__iexact=title)
|
|
|
|
elif album is not None and track is not None:
|
|
|
|
return self.fetchItem(key, Track, parentTitle__iexact=album, index=track)
|
|
|
|
raise BadRequest('Missing argument: title or album and track are required')
|
2017-02-06 04:52:10 +00:00
|
|
|
|
2017-02-09 04:08:25 +00:00
|
|
|
def tracks(self, **kwargs):
|
2020-12-23 23:23:10 +00:00
|
|
|
""" Returns a list of :class:`~plexapi.audio.Track` objects by the artist. """
|
2022-07-21 02:37:48 +00:00
|
|
|
key = f'{self.key}/allLeaves'
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.fetchItems(key, Track, **kwargs)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2020-12-24 05:38:48 +00:00
|
|
|
def get(self, title=None, album=None, track=None):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Alias of :func:`~plexapi.audio.Artist.track`. """
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.track(title, album, track)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2021-11-20 22:16:58 +00:00
|
|
|
def download(self, savepath=None, keep_original_name=False, subfolders=False, **kwargs):
|
|
|
|
""" Download all tracks from the artist. See :func:`~plexapi.base.Playable.download` for details.
|
2017-02-20 05:37:00 +00:00
|
|
|
|
2017-02-02 03:53:05 +00:00
|
|
|
Parameters:
|
2021-11-20 22:16:58 +00:00
|
|
|
savepath (str): Defaults to current working dir.
|
|
|
|
keep_original_name (bool): True to keep the original filename otherwise
|
|
|
|
a friendlier filename is generated.
|
|
|
|
subfolders (bool): True to separate tracks in to album folders.
|
|
|
|
**kwargs: Additional options passed into :func:`~plexapi.base.PlexObject.getStreamURL`.
|
2017-02-02 03:53:05 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
filepaths = []
|
2021-11-20 22:16:58 +00:00
|
|
|
for track in self.tracks():
|
|
|
|
_savepath = os.path.join(savepath, track.parentTitle) if subfolders else savepath
|
|
|
|
filepaths += track.download(_savepath, keep_original_name, **kwargs)
|
2017-02-06 04:52:10 +00:00
|
|
|
return filepaths
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2022-01-24 04:39:35 +00:00
|
|
|
def station(self):
|
|
|
|
""" Returns a :class:`~plexapi.playlist.Playlist` artist radio station or `None`. """
|
2022-08-28 05:56:01 +00:00
|
|
|
key = f'{self.key}?includeStations=1'
|
2022-01-24 04:39:35 +00:00
|
|
|
return next(iter(self.fetchItems(key, cls=Playlist, rtag="Stations")), None)
|
|
|
|
|
2023-08-27 19:59:53 +00:00
|
|
|
@property
|
|
|
|
def metadataDirectory(self):
|
|
|
|
""" Returns the Plex Media Server data directory where the metadata is stored. """
|
|
|
|
guid_hash = utils.sha1hash(self.guid)
|
|
|
|
return str(Path('Metadata') / 'Artists' / guid_hash[0] / f'{guid_hash[1:]}.bundle')
|
|
|
|
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2022-02-27 05:40:51 +00:00
|
|
|
class Album(
|
|
|
|
Audio,
|
|
|
|
UnmatchMatchMixin, RatingMixin,
|
|
|
|
ArtMixin, PosterMixin, ThemeUrlMixin,
|
2023-07-27 21:45:23 +00:00
|
|
|
AlbumEditMixins
|
2022-02-27 05:40:51 +00:00
|
|
|
):
|
2020-12-24 06:24:46 +00:00
|
|
|
""" Represents a single Album.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-23 05:15:51 +00:00
|
|
|
Attributes:
|
2017-02-14 04:32:27 +00:00
|
|
|
TAG (str): 'Directory'
|
|
|
|
TYPE (str): 'album'
|
2020-12-23 23:23:10 +00:00
|
|
|
collections (List<:class:`~plexapi.media.Collection`>): List of collection objects.
|
2021-09-16 22:03:46 +00:00
|
|
|
formats (List<:class:`~plexapi.media.Format`>): List of format objects.
|
2020-12-23 23:23:10 +00:00
|
|
|
genres (List<:class:`~plexapi.media.Genre`>): List of genre objects.
|
2022-07-21 02:35:46 +00:00
|
|
|
guids (List<:class:`~plexapi.media.Guid`>): List of guid objects.
|
2017-01-23 05:15:51 +00:00
|
|
|
key (str): API URL (/library/metadata/<ratingkey>).
|
2020-12-23 23:23:10 +00:00
|
|
|
labels (List<:class:`~plexapi.media.Label`>): List of label objects.
|
|
|
|
leafCount (int): Number of items in the album view.
|
|
|
|
loudnessAnalysisVersion (int): The Plex loudness analysis version level.
|
|
|
|
originallyAvailableAt (datetime): Datetime the album was released.
|
|
|
|
parentGuid (str): Plex GUID for the album artist (plex://artist/5d07bcb0403c64029053ac4c).
|
|
|
|
parentKey (str): API URL of the album artist (/library/metadata/<parentRatingKey>).
|
|
|
|
parentRatingKey (int): Unique key identifying the album artist.
|
2022-02-27 02:47:54 +00:00
|
|
|
parentTheme (str): URL to artist theme resource (/library/metadata/<parentRatingkey>/theme/<themeid>).
|
2020-12-23 23:23:10 +00:00
|
|
|
parentThumb (str): URL to album artist thumbnail image (/library/metadata/<parentRatingKey>/thumb/<thumbid>).
|
|
|
|
parentTitle (str): Name of the album artist.
|
|
|
|
rating (float): Album rating (7.9; 9.8; 8.1).
|
|
|
|
studio (str): Studio that released the album.
|
|
|
|
styles (List<:class:`~plexapi.media.Style`>): List of style objects.
|
2021-09-16 22:03:46 +00:00
|
|
|
subformats (List<:class:`~plexapi.media.Subformat`>): List of subformat objects.
|
2020-12-23 23:23:10 +00:00
|
|
|
viewedLeafCount (int): Number of items marked as played in the album view.
|
|
|
|
year (int): Year the album was released.
|
2017-01-23 05:15:51 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Directory'
|
2016-01-19 09:31:45 +00:00
|
|
|
TYPE = 'album'
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2016-04-07 05:39:04 +00:00
|
|
|
Audio._loadData(self, data)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.collections = self.findItems(data, media.Collection)
|
2021-09-16 22:03:46 +00:00
|
|
|
self.formats = self.findItems(data, media.Format)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.genres = self.findItems(data, media.Genre)
|
2022-07-21 02:35:46 +00:00
|
|
|
self.guids = self.findItems(data, media.Guid)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.key = self.key.replace('/children', '') # FIX_BUG_50
|
|
|
|
self.labels = self.findItems(data, media.Label)
|
|
|
|
self.leafCount = utils.cast(int, data.attrib.get('leafCount'))
|
|
|
|
self.loudnessAnalysisVersion = utils.cast(int, data.attrib.get('loudnessAnalysisVersion'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.originallyAvailableAt = utils.toDatetime(data.attrib.get('originallyAvailableAt'), '%Y-%m-%d')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.parentGuid = data.attrib.get('parentGuid')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.parentKey = data.attrib.get('parentKey')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.parentRatingKey = utils.cast(int, data.attrib.get('parentRatingKey'))
|
2022-02-27 02:47:54 +00:00
|
|
|
self.parentTheme = data.attrib.get('parentTheme')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.parentThumb = data.attrib.get('parentThumb')
|
|
|
|
self.parentTitle = data.attrib.get('parentTitle')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.rating = utils.cast(float, data.attrib.get('rating'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.studio = data.attrib.get('studio')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.styles = self.findItems(data, media.Style)
|
2021-09-16 22:03:46 +00:00
|
|
|
self.subformats = self.findItems(data, media.Subformat)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.viewedLeafCount = utils.cast(int, data.attrib.get('viewedLeafCount'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.year = utils.cast(int, data.attrib.get('year'))
|
2020-12-23 23:23:10 +00:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for track in self.tracks():
|
|
|
|
yield track
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2020-12-24 05:38:48 +00:00
|
|
|
def track(self, title=None, track=None):
|
2017-01-31 04:44:03 +00:00
|
|
|
""" Returns the :class:`~plexapi.audio.Track` that matches the specified title.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-22 06:37:39 +00:00
|
|
|
Parameters:
|
|
|
|
title (str): Title of the track to return.
|
2020-12-24 05:38:48 +00:00
|
|
|
track (int): Track number (default: None; required if title not specified).
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
:exc:`~plexapi.exceptions.BadRequest`: If title or track parameter is missing.
|
2017-01-02 21:06:40 +00:00
|
|
|
"""
|
2022-07-21 02:37:48 +00:00
|
|
|
key = f'{self.key}/children'
|
2022-05-17 02:52:16 +00:00
|
|
|
if title is not None and not isinstance(title, int):
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.fetchItem(key, Track, title__iexact=title)
|
2022-05-17 02:52:16 +00:00
|
|
|
elif track is not None or isinstance(title, int):
|
|
|
|
if isinstance(title, int):
|
|
|
|
index = title
|
|
|
|
else:
|
|
|
|
index = track
|
|
|
|
return self.fetchItem(key, Track, parentTitle__iexact=self.title, index=index)
|
2020-12-24 05:38:48 +00:00
|
|
|
raise BadRequest('Missing argument: title or track is required')
|
2017-02-06 04:52:10 +00:00
|
|
|
|
2017-02-09 04:08:25 +00:00
|
|
|
def tracks(self, **kwargs):
|
2020-12-23 23:23:10 +00:00
|
|
|
""" Returns a list of :class:`~plexapi.audio.Track` objects in the album. """
|
2022-07-21 02:37:48 +00:00
|
|
|
key = f'{self.key}/children'
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.fetchItems(key, Track, **kwargs)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2020-12-24 05:38:48 +00:00
|
|
|
def get(self, title=None, track=None):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Alias of :func:`~plexapi.audio.Album.track`. """
|
2020-12-24 05:38:48 +00:00
|
|
|
return self.track(title, track)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
|
|
|
def artist(self):
|
2020-12-23 23:23:10 +00:00
|
|
|
""" Return the album's :class:`~plexapi.audio.Artist`. """
|
2017-02-07 06:20:49 +00:00
|
|
|
return self.fetchItem(self.parentKey)
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2019-01-07 13:04:53 +00:00
|
|
|
def download(self, savepath=None, keep_original_name=False, **kwargs):
|
2021-11-20 22:16:58 +00:00
|
|
|
""" Download all tracks from the album. See :func:`~plexapi.base.Playable.download` for details.
|
2017-02-20 05:37:00 +00:00
|
|
|
|
2017-02-02 03:53:05 +00:00
|
|
|
Parameters:
|
2021-11-20 22:16:58 +00:00
|
|
|
savepath (str): Defaults to current working dir.
|
|
|
|
keep_original_name (bool): True to keep the original filename otherwise
|
|
|
|
a friendlier filename is generated.
|
|
|
|
**kwargs: Additional options passed into :func:`~plexapi.base.PlexObject.getStreamURL`.
|
2017-02-02 03:53:05 +00:00
|
|
|
"""
|
2017-02-06 04:52:10 +00:00
|
|
|
filepaths = []
|
|
|
|
for track in self.tracks():
|
2019-01-07 13:04:53 +00:00
|
|
|
filepaths += track.download(savepath, keep_original_name, **kwargs)
|
2017-02-06 04:52:10 +00:00
|
|
|
return filepaths
|
2017-01-09 14:21:54 +00:00
|
|
|
|
2018-09-08 15:25:16 +00:00
|
|
|
def _defaultSyncTitle(self):
|
|
|
|
""" Returns str, default title for a new syncItem. """
|
2022-08-28 05:56:01 +00:00
|
|
|
return f'{self.parentTitle} - {self.title}'
|
2018-09-08 15:25:16 +00:00
|
|
|
|
2023-08-27 19:59:53 +00:00
|
|
|
@property
|
|
|
|
def metadataDirectory(self):
|
|
|
|
""" Returns the Plex Media Server data directory where the metadata is stored. """
|
|
|
|
guid_hash = utils.sha1hash(self.guid)
|
|
|
|
return str(Path('Metadata') / 'Albums' / guid_hash[0] / f'{guid_hash[1:]}.bundle')
|
|
|
|
|
2016-01-19 09:31:45 +00:00
|
|
|
|
2017-02-13 02:55:55 +00:00
|
|
|
@utils.registerPlexObject
|
2022-02-27 05:40:51 +00:00
|
|
|
class Track(
|
|
|
|
Audio, Playable,
|
2022-02-27 06:04:18 +00:00
|
|
|
ExtrasMixin, RatingMixin,
|
2022-02-27 05:40:51 +00:00
|
|
|
ArtUrlMixin, PosterUrlMixin, ThemeUrlMixin,
|
2023-07-27 21:45:23 +00:00
|
|
|
TrackEditMixins
|
2022-02-27 05:40:51 +00:00
|
|
|
):
|
2020-12-24 06:24:46 +00:00
|
|
|
""" Represents a single Track.
|
2017-01-02 21:06:40 +00:00
|
|
|
|
2017-01-23 05:15:51 +00:00
|
|
|
Attributes:
|
2017-02-14 04:32:27 +00:00
|
|
|
TAG (str): 'Directory'
|
|
|
|
TYPE (str): 'track'
|
2023-05-24 19:04:03 +00:00
|
|
|
chapters (List<:class:`~plexapi.media.Chapter`>): List of Chapter objects.
|
2020-12-23 23:23:10 +00:00
|
|
|
chapterSource (str): Unknown
|
2021-05-09 19:24:55 +00:00
|
|
|
collections (List<:class:`~plexapi.media.Collection`>): List of collection objects.
|
2020-12-23 23:23:10 +00:00
|
|
|
duration (int): Length of the track in milliseconds.
|
|
|
|
grandparentArt (str): URL to album artist artwork (/library/metadata/<grandparentRatingKey>/art/<artid>).
|
|
|
|
grandparentGuid (str): Plex GUID for the album artist (plex://artist/5d07bcb0403c64029053ac4c).
|
|
|
|
grandparentKey (str): API URL of the album artist (/library/metadata/<grandparentRatingKey>).
|
|
|
|
grandparentRatingKey (int): Unique key identifying the album artist.
|
2022-02-27 02:47:54 +00:00
|
|
|
grandparentTheme (str): URL to artist theme resource (/library/metadata/<grandparentRatingkey>/theme/<themeid>).
|
|
|
|
(/library/metadata/<grandparentRatingkey>/theme/<themeid>).
|
2020-12-23 23:23:10 +00:00
|
|
|
grandparentThumb (str): URL to album artist thumbnail image
|
|
|
|
(/library/metadata/<grandparentRatingKey>/thumb/<thumbid>).
|
|
|
|
grandparentTitle (str): Name of the album artist for the track.
|
2022-07-21 02:35:46 +00:00
|
|
|
guids (List<:class:`~plexapi.media.Guid`>): List of guid objects.
|
2022-02-27 01:32:18 +00:00
|
|
|
labels (List<:class:`~plexapi.media.Label`>): List of label objects.
|
2020-12-23 23:23:10 +00:00
|
|
|
media (List<:class:`~plexapi.media.Media`>): List of media objects.
|
2021-05-16 05:39:26 +00:00
|
|
|
originalTitle (str): The artist for the track.
|
2020-12-23 23:23:10 +00:00
|
|
|
parentGuid (str): Plex GUID for the album (plex://album/5d07cd8e403c640290f180f9).
|
2022-02-27 05:40:51 +00:00
|
|
|
parentIndex (int): Disc number of the track.
|
2020-12-23 23:23:10 +00:00
|
|
|
parentKey (str): API URL of the album (/library/metadata/<parentRatingKey>).
|
|
|
|
parentRatingKey (int): Unique key identifying the album.
|
|
|
|
parentThumb (str): URL to album thumbnail image (/library/metadata/<parentRatingKey>/thumb/<thumbid>).
|
|
|
|
parentTitle (str): Name of the album for the track.
|
|
|
|
primaryExtraKey (str) API URL for the primary extra for the track.
|
2022-07-21 02:03:00 +00:00
|
|
|
ratingCount (int): Number of listeners who have scrobbled this track, as reported by Last.fm.
|
2022-02-27 06:31:21 +00:00
|
|
|
skipCount (int): Number of times the track has been skipped.
|
2020-12-23 23:23:10 +00:00
|
|
|
viewOffset (int): View offset in milliseconds.
|
|
|
|
year (int): Year the track was released.
|
2017-01-23 05:15:51 +00:00
|
|
|
"""
|
2017-02-13 02:55:55 +00:00
|
|
|
TAG = 'Track'
|
2016-01-19 09:31:45 +00:00
|
|
|
TYPE = 'track'
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
2017-01-22 06:37:39 +00:00
|
|
|
""" Load attribute values from Plex XML response. """
|
2016-04-07 05:39:04 +00:00
|
|
|
Audio._loadData(self, data)
|
|
|
|
Playable._loadData(self, data)
|
2023-05-24 19:04:03 +00:00
|
|
|
self.chapters = self.findItems(data, media.Chapter)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.chapterSource = data.attrib.get('chapterSource')
|
2021-05-09 19:24:55 +00:00
|
|
|
self.collections = self.findItems(data, media.Collection)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.duration = utils.cast(int, data.attrib.get('duration'))
|
|
|
|
self.grandparentArt = data.attrib.get('grandparentArt')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.grandparentGuid = data.attrib.get('grandparentGuid')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.grandparentKey = data.attrib.get('grandparentKey')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.grandparentRatingKey = utils.cast(int, data.attrib.get('grandparentRatingKey'))
|
2022-02-27 02:47:54 +00:00
|
|
|
self.grandparentTheme = data.attrib.get('grandparentTheme')
|
2017-02-04 17:43:50 +00:00
|
|
|
self.grandparentThumb = data.attrib.get('grandparentThumb')
|
|
|
|
self.grandparentTitle = data.attrib.get('grandparentTitle')
|
2022-07-21 02:35:46 +00:00
|
|
|
self.guids = self.findItems(data, media.Guid)
|
2022-02-27 01:32:18 +00:00
|
|
|
self.labels = self.findItems(data, media.Label)
|
2020-12-23 23:23:10 +00:00
|
|
|
self.media = self.findItems(data, media.Media)
|
2017-02-04 17:43:50 +00:00
|
|
|
self.originalTitle = data.attrib.get('originalTitle')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.parentGuid = data.attrib.get('parentGuid')
|
2022-02-27 05:40:51 +00:00
|
|
|
self.parentIndex = utils.cast(int, data.attrib.get('parentIndex'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.parentKey = data.attrib.get('parentKey')
|
2020-12-23 23:23:10 +00:00
|
|
|
self.parentRatingKey = utils.cast(int, data.attrib.get('parentRatingKey'))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.parentThumb = data.attrib.get('parentThumb')
|
|
|
|
self.parentTitle = data.attrib.get('parentTitle')
|
|
|
|
self.primaryExtraKey = data.attrib.get('primaryExtraKey')
|
|
|
|
self.ratingCount = utils.cast(int, data.attrib.get('ratingCount'))
|
2022-02-27 06:31:21 +00:00
|
|
|
self.skipCount = utils.cast(int, data.attrib.get('skipCount'))
|
2016-03-21 04:26:02 +00:00
|
|
|
self.viewOffset = utils.cast(int, data.attrib.get('viewOffset', 0))
|
2017-02-04 17:43:50 +00:00
|
|
|
self.year = utils.cast(int, data.attrib.get('year'))
|
2017-02-06 04:52:10 +00:00
|
|
|
|
2020-12-23 23:23:10 +00:00
|
|
|
@property
|
|
|
|
def locations(self):
|
|
|
|
""" This does not exist in plex xml response but is added to have a common
|
|
|
|
interface to get the locations of the track.
|
2020-12-24 04:40:08 +00:00
|
|
|
|
2021-06-06 21:54:15 +00:00
|
|
|
Returns:
|
2020-12-24 04:40:08 +00:00
|
|
|
List<str> of file paths where the track is found on disk.
|
2020-12-23 23:23:10 +00:00
|
|
|
"""
|
|
|
|
return [part.file for part in self.iterParts() if part]
|
|
|
|
|
2021-05-11 00:56:51 +00:00
|
|
|
@property
|
|
|
|
def trackNumber(self):
|
|
|
|
""" Returns the track number. """
|
|
|
|
return self.index
|
|
|
|
|
2023-07-28 01:02:23 +00:00
|
|
|
def _prettyfilename(self):
|
|
|
|
""" Returns a filename for use in download. """
|
|
|
|
return f'{self.grandparentTitle} - {self.parentTitle} - {str(self.trackNumber).zfill(2)} - {self.title}'
|
|
|
|
|
|
|
|
def album(self):
|
|
|
|
""" Return the track's :class:`~plexapi.audio.Album`. """
|
|
|
|
return self.fetchItem(self.parentKey)
|
|
|
|
|
|
|
|
def artist(self):
|
|
|
|
""" Return the track's :class:`~plexapi.audio.Artist`. """
|
|
|
|
return self.fetchItem(self.grandparentKey)
|
|
|
|
|
2018-09-08 15:25:16 +00:00
|
|
|
def _defaultSyncTitle(self):
|
|
|
|
""" Returns str, default title for a new syncItem. """
|
2022-08-28 05:56:01 +00:00
|
|
|
return f'{self.grandparentTitle} - {self.parentTitle} - {self.title}'
|
2021-08-03 03:37:29 +00:00
|
|
|
|
2021-09-26 22:23:09 +00:00
|
|
|
def _getWebURL(self, base=None):
|
|
|
|
""" Get the Plex Web URL with the correct parameters. """
|
|
|
|
return self._server._buildWebURL(base=base, endpoint='details', key=self.parentKey)
|
2022-07-21 03:03:20 +00:00
|
|
|
|
2023-08-27 19:59:53 +00:00
|
|
|
@property
|
|
|
|
def metadataDirectory(self):
|
|
|
|
""" Returns the Plex Media Server data directory where the metadata is stored. """
|
|
|
|
guid_hash = utils.sha1hash(self.parentGuid)
|
|
|
|
return str(Path('Metadata') / 'Albums' / guid_hash[0] / f'{guid_hash[1:]}.bundle')
|
|
|
|
|
2022-07-21 03:03:20 +00:00
|
|
|
|
|
|
|
@utils.registerPlexObject
|
|
|
|
class TrackSession(PlexSession, Track):
|
|
|
|
""" Represents a single Track session
|
|
|
|
loaded from :func:`~plexapi.server.PlexServer.sessions`.
|
|
|
|
"""
|
|
|
|
_SESSIONTYPE = True
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
|
|
|
""" Load attribute values from Plex XML response. """
|
|
|
|
Track._loadData(self, data)
|
|
|
|
PlexSession._loadData(self, data)
|
2023-07-28 00:45:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@utils.registerPlexObject
|
|
|
|
class TrackHistory(PlexHistory, Track):
|
|
|
|
""" Represents a single Track history entry
|
|
|
|
loaded from :func:`~plexapi.server.PlexServer.history`.
|
|
|
|
"""
|
|
|
|
_HISTORYTYPE = True
|
|
|
|
|
|
|
|
def _loadData(self, data):
|
|
|
|
""" Load attribute values from Plex XML response. """
|
|
|
|
Track._loadData(self, data)
|
|
|
|
PlexHistory._loadData(self, data)
|