Source code for plexapi.mixins.resources

from urllib.parse import quote_plus

from plexapi import media
from plexapi.utils import openOrRead


[docs] class ArtUrlMixin: """ Mixin for Plex objects that can have a background artwork url. """ @property def artUrl(self): """ Return the art url for the Plex object. """ art = self.firstAttr('art', 'grandparentArt') return self._server.url(art, includeToken=True) if art else None
[docs] class ArtLockMixin: """ Mixin for Plex objects that can have a locked background artwork. """
[docs] def lockArt(self): """ Lock the background artwork for a Plex object. """ return self._edit(**{'art.locked': 1})
[docs] def unlockArt(self): """ Unlock the background artwork for a Plex object. """ return self._edit(**{'art.locked': 0})
[docs] class ArtMixin(ArtUrlMixin, ArtLockMixin): """ Mixin for Plex objects that can have background artwork. """
[docs] def arts(self): """ Returns list of available :class:`~plexapi.media.Art` objects. """ return self.fetchItems(f'/library/metadata/{self.ratingKey}/arts', cls=media.Art)
[docs] def uploadArt(self, url=None, filepath=None): """ Upload a background artwork from a url or filepath. Parameters: url (str): The full URL to the image to upload. filepath (str): The full file path to the image to upload or file-like object. """ if url: key = f'/library/metadata/{self.ratingKey}/arts?url={quote_plus(url)}' self._server.query(key, method=self._server._session.post) elif filepath: key = f'/library/metadata/{self.ratingKey}/arts' data = openOrRead(filepath) self._server.query(key, method=self._server._session.post, data=data) return self
[docs] def setArt(self, art): """ Set the background artwork for a Plex object. Parameters: art (:class:`~plexapi.media.Art`): The art object to select. """ art.select() return self
[docs] def deleteArt(self): """ Delete the art from a Plex object. """ key = f'/library/metadata/{self.ratingKey}/art' self._server.query(key, method=self._server._session.delete) return self
[docs] class LogoUrlMixin: """ Mixin for Plex objects that can have a logo url. """ @property def logo(self): """ Return the API path to the logo image. """ return next((i.url for i in self.images if i.type == 'clearLogo'), None) @property def logoUrl(self): """ Return the logo url for the Plex object. """ return self._server.url(self.logo, includeToken=True) if self.logo else None
[docs] class LogoLockMixin: """ Mixin for Plex objects that can have a locked logo. """
[docs] class LogoMixin(LogoUrlMixin, LogoLockMixin): """ Mixin for Plex objects that can have logos. """
[docs] def logos(self): """ Returns list of available :class:`~plexapi.media.Logo` objects. """ return self.fetchItems(f'/library/metadata/{self.ratingKey}/clearLogos', cls=media.Logo)
[docs] class PosterUrlMixin: """ Mixin for Plex objects that can have a poster url. """ @property def thumbUrl(self): """ Return the thumb url for the Plex object. """ thumb = self.firstAttr('thumb', 'parentThumb', 'grandparentThumb') return self._server.url(thumb, includeToken=True) if thumb else None @property def posterUrl(self): """ Alias to self.thumbUrl. """ return self.thumbUrl
[docs] class PosterLockMixin: """ Mixin for Plex objects that can have a locked poster. """
[docs] def lockPoster(self): """ Lock the poster for a Plex object. """ return self._edit(**{'thumb.locked': 1})
[docs] def unlockPoster(self): """ Unlock the poster for a Plex object. """ return self._edit(**{'thumb.locked': 0})
[docs] class PosterMixin(PosterUrlMixin, PosterLockMixin): """ Mixin for Plex objects that can have posters. """
[docs] def posters(self): """ Returns list of available :class:`~plexapi.media.Poster` objects. """ return self.fetchItems(f'/library/metadata/{self.ratingKey}/posters', cls=media.Poster)
[docs] def uploadPoster(self, url=None, filepath=None): """ Upload a poster from a url or filepath. Parameters: url (str): The full URL to the image to upload. filepath (str): The full file path to the image to upload or file-like object. """ if url: key = f'/library/metadata/{self.ratingKey}/posters?url={quote_plus(url)}' self._server.query(key, method=self._server._session.post) elif filepath: key = f'/library/metadata/{self.ratingKey}/posters' data = openOrRead(filepath) self._server.query(key, method=self._server._session.post, data=data) return self
[docs] def setPoster(self, poster): """ Set the poster for a Plex object. Parameters: poster (:class:`~plexapi.media.Poster`): The poster object to select. """ poster.select() return self
[docs] def deletePoster(self): """ Delete the poster from a Plex object. """ key = f'/library/metadata/{self.ratingKey}/thumb' self._server.query(key, method=self._server._session.delete) return self
[docs] class SquareArtUrlMixin: """ Mixin for Plex objects that can have a square art url. """ @property def squareArt(self): """ Return the API path to the square art image. """ return next((i.url for i in self.images if i.type == 'backgroundSquare'), None) @property def squareArtUrl(self): """ Return the square art url for the Plex object. """ return self._server.url(self.squareArt, includeToken=True) if self.squareArt else None
[docs] class SquareArtLockMixin: """ Mixin for Plex objects that can have a locked square art. """
[docs] def lockSquareArt(self): """ Lock the square art for a Plex object. """ return self._edit(**{'squareArt.locked': 1})
[docs] def unlockSquareArt(self): """ Unlock the square art for a Plex object. """ return self._edit(**{'squareArt.locked': 0})
[docs] class SquareArtMixin(SquareArtUrlMixin, SquareArtLockMixin): """ Mixin for Plex objects that can have square art. """
[docs] def squareArts(self): """ Returns list of available :class:`~plexapi.media.SquareArt` objects. """ return self.fetchItems(f'/library/metadata/{self.ratingKey}/squareArts', cls=media.SquareArt)
[docs] def uploadSquareArt(self, url=None, filepath=None): """ Upload a square art from a url or filepath. Parameters: url (str): The full URL to the image to upload. filepath (str): The full file path to the image to upload or file-like object. """ if url: key = f'/library/metadata/{self.ratingKey}/squareArts?url={quote_plus(url)}' self._server.query(key, method=self._server._session.post) elif filepath: key = f'/library/metadata/{self.ratingKey}/squareArts' data = openOrRead(filepath) self._server.query(key, method=self._server._session.post, data=data) return self
[docs] def setSquareArt(self, squareArt): """ Set the square art for a Plex object. Parameters: squareArt (:class:`~plexapi.media.SquareArt`): The square art object to select. """ squareArt.select() return self
[docs] def deleteSquareArt(self): """ Delete the square art from a Plex object. """ key = f'/library/metadata/{self.ratingKey}/squareArt' self._server.query(key, method=self._server._session.delete) return self
[docs] class ThemeUrlMixin: """ Mixin for Plex objects that can have a theme url. """ @property def themeUrl(self): """ Return the theme url for the Plex object. """ theme = self.firstAttr('theme', 'parentTheme', 'grandparentTheme') return self._server.url(theme, includeToken=True) if theme else None
[docs] class ThemeLockMixin: """ Mixin for Plex objects that can have a locked theme. """
[docs] def lockTheme(self): """ Lock the theme for a Plex object. """ return self._edit(**{'theme.locked': 1})
[docs] def unlockTheme(self): """ Unlock the theme for a Plex object. """ return self._edit(**{'theme.locked': 0})
[docs] class ThemeMixin(ThemeUrlMixin, ThemeLockMixin): """ Mixin for Plex objects that can have themes. """
[docs] def themes(self): """ Returns list of available :class:`~plexapi.media.Theme` objects. """ return self.fetchItems(f'/library/metadata/{self.ratingKey}/themes', cls=media.Theme)
[docs] def uploadTheme(self, url=None, filepath=None, timeout=None): """ Upload a theme from url or filepath. Warning: Themes cannot be deleted using PlexAPI! Parameters: url (str): The full URL to the theme to upload. filepath (str): The full file path to the theme to upload or file-like object. timeout (int, optional): Timeout, in seconds, to use when uploading themes to the server. (default config.TIMEOUT). """ if url: key = f'/library/metadata/{self.ratingKey}/themes?url={quote_plus(url)}' self._server.query(key, method=self._server._session.post, timeout=timeout) elif filepath: key = f'/library/metadata/{self.ratingKey}/themes' data = openOrRead(filepath) self._server.query(key, method=self._server._session.post, data=data, timeout=timeout) return self
[docs] def setTheme(self, theme): """ Set the theme for a Plex object. Raises: :exc:`~plexapi.exceptions.NotImplementedError`: Themes cannot be set through the API. """ raise NotImplementedError( 'Themes cannot be set through the API. ' 'Re-upload the theme using "uploadTheme" to set it.' )
[docs] def deleteTheme(self): """ Delete the theme from a Plex object. """ key = f'/library/metadata/{self.ratingKey}/theme' self._server.query(key, method=self._server._session.delete) return self