python-plexapi/plexapi/photo.py

128 lines
5.9 KiB
Python
Raw Normal View History

2016-04-10 03:59:47 +00:00
# -*- coding: utf-8 -*-
from plexapi import media, utils
from plexapi.utils import PlexPartialObject
NA = utils.NA
@utils.register_libtype
class Photoalbum(PlexPartialObject):
""" Represents a photoalbum (collection of photos).
Parameters:
server (:class:`~plexapi.server.PlexServer`): PlexServer this client is connected to (optional)
data (ElementTree): Response from PlexServer used to build this object (optional).
initpath (str): Relative path requested when retrieving specified `data` (optional).
Attributes:
addedAt (datetime): Datetime this item was added to the library.
art (str): Photo art (/library/metadata/<ratingkey>/art/<artid>)
composite (str): Unknown
guid (str): Unknown (unique ID)
index (sting): Index number of this album.
key (str): API URL (/library/metadata/<ratingkey>).
librarySectionID (int): :class:`~plexapi.library.LibrarySection` ID.
listType (str): Hardcoded as 'photo' (useful for search filters).
ratingKey (int): Unique key identifying this item.
summary (str): Summary of the photoalbum.
thumb (str): URL to thumbnail image.
title (str): Photoalbum title. (Trip to Disney World)
type (str): Unknown
updatedAt (datatime): Datetime this item was updated.
2017-01-02 21:06:40 +00:00
"""
2016-04-10 03:59:47 +00:00
TYPE = 'photoalbum'
def __init__(self, server, data, initpath):
super(Photoalbum, self).__init__(data, initpath, server)
def _loadData(self, data):
""" Load attribute values from Plex XML response. """
self.listType = 'photo'
2016-04-10 03:59:47 +00:00
self.addedAt = utils.toDatetime(data.attrib.get('addedAt', NA))
self.art = data.attrib.get('art', NA)
self.composite = data.attrib.get('composite', NA)
self.guid = data.attrib.get('guid', NA)
self.index = utils.cast(int, data.attrib.get('index', NA))
self.key = data.attrib.get('key', NA)
self.librarySectionID = data.attrib.get('librarySectionID', NA)
self.ratingKey = data.attrib.get('ratingKey', NA)
self.summary = data.attrib.get('summary', NA)
self.thumb = data.attrib.get('thumb', NA)
self.title = data.attrib.get('title', NA)
self.type = data.attrib.get('type', NA)
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt', NA))
2017-01-02 21:06:40 +00:00
2016-04-10 03:59:47 +00:00
def photos(self):
""" Returns a list of :class:`~plexapi.photo.Photo` objects in this album. """
2016-04-10 03:59:47 +00:00
path = '/library/metadata/%s/children' % self.ratingKey
return utils.listItems(self.server, path, Photo.TYPE)
def photo(self, title):
""" Returns the :class:`~plexapi.photo.Photo` that matches the specified title. """
2016-04-10 03:59:47 +00:00
path = '/library/metadata/%s/children' % self.ratingKey
return utils.findItem(self.server, path, title)
2017-01-02 21:06:40 +00:00
def section(self):
""" Returns the :class:`~plexapi.library.LibrarySection` this item belongs to. """
return self.server.library.sectionByID(self.librarySectionID)
2016-04-10 03:59:47 +00:00
@utils.register_libtype
class Photo(PlexPartialObject):
""" Represents a single photo.
Parameters:
server (:class:`~plexapi.server.PlexServer`): PlexServer this client is connected to (optional)
data (ElementTree): Response from PlexServer used to build this object (optional).
initpath (str): Relative path requested when retrieving specified `data` (optional).
Attributes:
addedAt (datetime): Datetime this item was added to the library.
index (sting): Index number of this photo.
key (str): API URL (/library/metadata/<ratingkey>).
listType (str): Hardcoded as 'photo' (useful for search filters).
media (TYPE): Unknown
originallyAvailableAt (datetime): Datetime this photo was added to Plex.
parentKey (str): Photoalbum API URL.
parentRatingKey (int): Unique key identifying the photoalbum.
ratingKey (int): Unique key identifying this item.
summary (str): Summary of the photo.
thumb (str): URL to thumbnail image.
title (str): Photo title.
type (str): Unknown
updatedAt (datatime): Datetime this item was updated.
year (int): Year this photo was taken.
2017-01-02 21:06:40 +00:00
"""
2016-04-10 03:59:47 +00:00
TYPE = 'photo'
def __init__(self, server, data, initpath):
super(Photo, self).__init__(data, initpath, server)
def _loadData(self, data):
""" Load attribute values from Plex XML response. """
self.listType = 'photo'
2016-04-10 03:59:47 +00:00
self.addedAt = utils.toDatetime(data.attrib.get('addedAt', NA))
self.index = utils.cast(int, data.attrib.get('index', NA))
self.key = data.attrib.get('key', NA)
2017-01-02 21:06:40 +00:00
self.originallyAvailableAt = utils.toDatetime(
data.attrib.get('originallyAvailableAt', NA), '%Y-%m-%d')
2016-04-10 03:59:47 +00:00
self.parentKey = data.attrib.get('parentKey', NA)
self.parentRatingKey = data.attrib.get('parentRatingKey', NA)
self.ratingKey = data.attrib.get('ratingKey', NA)
self.summary = data.attrib.get('summary', NA)
self.thumb = data.attrib.get('thumb', NA)
self.title = data.attrib.get('title', NA)
self.type = data.attrib.get('type', NA)
self.updatedAt = utils.toDatetime(data.attrib.get('updatedAt', NA))
self.year = utils.cast(int, data.attrib.get('year', NA))
if self.isFullObject():
2017-01-02 21:06:40 +00:00
self.media = [media.Media(self.server, e, self.initpath, self)
for e in data if e.tag == media.Media.TYPE]
2017-01-02 21:06:40 +00:00
2016-04-10 03:59:47 +00:00
def photoalbum(self):
""" Return this photo's :class:`~plexapi.photo.Photoalbum`. """
2016-04-10 03:59:47 +00:00
return utils.listItems(self.server, self.parentKey)[0]
def section(self):
""" Returns the :class:`~plexapi.library.LibrarySection` this item belongs to. """
return self.server.library.sectionByID(self.photoalbum().librarySectionID)