python-plexapi/plexapi/mixins.py

490 lines
17 KiB
Python
Raw Normal View History

2021-01-24 22:35:13 +00:00
# -*- coding: utf-8 -*-
from urllib.parse import quote_plus, urlencode
2021-01-24 23:29:20 +00:00
from plexapi import media, utils
2021-01-24 23:18:28 +00:00
from plexapi.exceptions import NotFound
2021-01-24 23:29:20 +00:00
2021-02-15 03:38:09 +00:00
class ArtUrlMixin(object):
""" 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
2021-02-15 03:38:09 +00:00
class ArtMixin(ArtUrlMixin):
""" Mixin for Plex objects that can have background artwork."""
2021-02-07 04:19:11 +00:00
def arts(self):
""" Returns list of available :class:`~plexapi.media.Art` objects. """
return self.fetchItems('/library/metadata/%s/arts' % self.ratingKey, cls=media.Art)
2021-01-24 23:29:20 +00:00
2021-02-07 04:19:11 +00:00
def uploadArt(self, url=None, filepath=None):
2021-02-15 00:11:50 +00:00
""" Upload a background artwork from a url or filepath.
2021-02-07 04:19:11 +00:00
2021-01-24 23:29:20 +00:00
Parameters:
url (str): The full URL to the image to upload.
filepath (str): The full file path the the image to upload.
"""
if url:
2021-02-07 04:19:11 +00:00
key = '/library/metadata/%s/arts?url=%s' % (self.ratingKey, quote_plus(url))
2021-01-24 23:29:20 +00:00
self._server.query(key, method=self._server._session.post)
elif filepath:
2021-02-07 04:19:11 +00:00
key = '/library/metadata/%s/arts?' % self.ratingKey
2021-01-24 23:29:20 +00:00
data = open(filepath, 'rb').read()
self._server.query(key, method=self._server._session.post, data=data)
2021-02-07 04:19:11 +00:00
def setArt(self, art):
2021-02-15 00:11:50 +00:00
""" Set the background artwork for a Plex object.
2021-01-24 23:29:20 +00:00
Parameters:
2021-02-07 04:19:11 +00:00
art (:class:`~plexapi.media.Art`): The art object to select.
2021-01-24 23:29:20 +00:00
"""
2021-02-07 04:19:11 +00:00
art.select()
2021-01-24 23:29:20 +00:00
2021-02-15 03:38:09 +00:00
class BannerUrlMixin(object):
""" Mixin for Plex objects that can have a banner url."""
2021-02-15 00:11:50 +00:00
@property
def bannerUrl(self):
""" Return the banner url for the Plex object."""
banner = self.firstAttr('banner')
return self._server.url(banner, includeToken=True) if banner else None
2021-02-15 03:38:09 +00:00
class BannerMixin(BannerUrlMixin):
""" Mixin for Plex objects that can have banners."""
2021-02-15 00:11:50 +00:00
def banners(self):
""" Returns list of available :class:`~plexapi.media.Banner` objects. """
return self.fetchItems('/library/metadata/%s/banners' % self.ratingKey, cls=media.Banner)
def uploadBanner(self, url=None, filepath=None):
""" Upload a banner from a url or filepath.
Parameters:
url (str): The full URL to the image to upload.
filepath (str): The full file path the the image to upload.
"""
if url:
key = '/library/metadata/%s/banners?url=%s' % (self.ratingKey, quote_plus(url))
self._server.query(key, method=self._server._session.post)
elif filepath:
key = '/library/metadata/%s/banners?' % self.ratingKey
data = open(filepath, 'rb').read()
self._server.query(key, method=self._server._session.post, data=data)
def setBanner(self, banner):
""" Set the banner for a Plex object.
Parameters:
banner (:class:`~plexapi.media.Banner`): The banner object to select.
"""
banner.select()
2021-02-15 03:38:09 +00:00
class PosterUrlMixin(object):
""" Mixin for Plex objects that can have a poster url."""
2021-02-07 04:19:11 +00:00
@property
def thumbUrl(self):
""" Return the thumb url for the Plex object."""
thumb = self.firstAttr('thumb', 'parentThumb', 'granparentThumb')
return self._server.url(thumb, includeToken=True) if thumb else None
2021-02-15 03:58:18 +00:00
@property
def posterUrl(self):
""" Alias to self.thumbUrl."""
return self.thumbUrl
2021-02-15 03:38:09 +00:00
class PosterMixin(PosterUrlMixin):
""" Mixin for Plex objects that can have posters."""
2021-02-07 04:19:11 +00:00
def posters(self):
""" Returns list of available :class:`~plexapi.media.Poster` objects. """
return self.fetchItems('/library/metadata/%s/posters' % self.ratingKey, cls=media.Poster)
def uploadPoster(self, url=None, filepath=None):
2021-02-15 00:11:50 +00:00
""" Upload a poster from a url or filepath.
2021-02-07 04:19:11 +00:00
2021-01-24 23:29:20 +00:00
Parameters:
url (str): The full URL to the image to upload.
filepath (str): The full file path the the image to upload.
"""
if url:
2021-02-07 04:19:11 +00:00
key = '/library/metadata/%s/posters?url=%s' % (self.ratingKey, quote_plus(url))
2021-01-24 23:29:20 +00:00
self._server.query(key, method=self._server._session.post)
elif filepath:
2021-02-07 04:19:11 +00:00
key = '/library/metadata/%s/posters?' % self.ratingKey
2021-01-24 23:29:20 +00:00
data = open(filepath, 'rb').read()
self._server.query(key, method=self._server._session.post, data=data)
2021-02-07 04:19:11 +00:00
def setPoster(self, poster):
""" Set the poster for a Plex object.
2021-01-24 23:29:20 +00:00
Parameters:
2021-02-07 04:19:11 +00:00
poster (:class:`~plexapi.media.Poster`): The poster object to select.
2021-01-24 23:29:20 +00:00
"""
2021-02-07 04:19:11 +00:00
poster.select()
2021-01-24 22:35:13 +00:00
2021-02-07 04:23:05 +00:00
class SplitMergeMixin(object):
2021-01-24 23:13:22 +00:00
""" Mixin for Plex objects that can be split and merged."""
2021-01-04 20:30:42 +00:00
def split(self):
2021-01-24 23:13:22 +00:00
""" Split duplicated Plex object into separate objects. """
2021-01-04 20:30:42 +00:00
key = '/library/metadata/%s/split' % self.ratingKey
return self._server.query(key, method=self._server._session.put)
def merge(self, ratingKeys):
2021-01-24 23:13:22 +00:00
""" Merge other Plex objects into the current object.
Parameters:
ratingKeys (list): A list of rating keys to merge.
"""
2021-01-04 20:30:42 +00:00
if not isinstance(ratingKeys, list):
2021-01-24 23:13:22 +00:00
ratingKeys = str(ratingKeys).split(',')
2021-01-04 20:30:42 +00:00
2021-01-24 23:13:22 +00:00
key = '%s/merge?ids=%s' % (self.key, ','.join([str(r) for r in ratingKeys]))
2021-01-04 20:30:42 +00:00
return self._server.query(key, method=self._server._session.put)
2021-01-24 23:18:28 +00:00
2021-02-07 04:23:05 +00:00
class UnmatchMatchMixin(object):
2021-01-24 23:18:28 +00:00
""" Mixin for Plex objects that can be unmatched and matched."""
def unmatch(self):
""" Unmatches metadata match from object. """
key = '/library/metadata/%s/unmatch' % self.ratingKey
self._server.query(key, method=self._server._session.put)
def matches(self, agent=None, title=None, year=None, language=None):
""" Return list of (:class:`~plexapi.media.SearchResult`) metadata matches.
Parameters:
agent (str): Agent name to be used (imdb, thetvdb, themoviedb, etc.)
title (str): Title of item to search for
year (str): Year of item to search in
language (str) : Language of item to search in
Examples:
1. video.matches()
2. video.matches(title="something", year=2020)
3. video.matches(title="something")
4. video.matches(year=2020)
5. video.matches(title="something", year="")
6. video.matches(title="", year=2020)
7. video.matches(title="", year="")
1. The default behaviour in Plex Web = no params in plexapi
2. Both title and year specified by user
3. Year automatically filled in
4. Title automatically filled in
5. Explicitly searches for title with blank year
6. Explicitly searches for blank title with year
7. I don't know what the user is thinking... return the same result as 1
For 2 to 7, the agent and language is automatically filled in
"""
key = '/library/metadata/%s/matches' % self.ratingKey
params = {'manual': 1}
if agent and not any([title, year, language]):
params['language'] = self.section().language
params['agent'] = utils.getAgentIdentifier(self.section(), agent)
else:
if any(x is not None for x in [agent, title, year, language]):
if title is None:
params['title'] = self.title
else:
params['title'] = title
if year is None:
params['year'] = self.year
else:
params['year'] = year
params['language'] = language or self.section().language
if agent is None:
params['agent'] = self.section().agent
else:
params['agent'] = utils.getAgentIdentifier(self.section(), agent)
key = key + '?' + urlencode(params)
data = self._server.query(key, method=self._server._session.get)
return self.findItems(data, initpath=key)
def fixMatch(self, searchResult=None, auto=False, agent=None):
""" Use match result to update show metadata.
Parameters:
auto (bool): True uses first match from matches
False allows user to provide the match
searchResult (:class:`~plexapi.media.SearchResult`): Search result from
~plexapi.base.matches()
agent (str): Agent name to be used (imdb, thetvdb, themoviedb, etc.)
"""
key = '/library/metadata/%s/match' % self.ratingKey
if auto:
autoMatch = self.matches(agent=agent)
if autoMatch:
searchResult = autoMatch[0]
else:
raise NotFound('No matches found using this agent: (%s:%s)' % (agent, autoMatch))
elif not searchResult:
raise NotFound('fixMatch() requires either auto=True or '
'searchResult=:class:`~plexapi.media.SearchResult`.')
params = {'guid': searchResult.guid,
'name': searchResult.name}
data = key + '?' + urlencode(params)
self._server.query(data, method=self._server._session.put)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class CollectionMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have collections. """
def addCollection(self, collections, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a collection tag(s).
Parameters:
collections (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('collection', collections, locked=locked)
2021-01-24 22:35:13 +00:00
def removeCollection(self, collections, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a collection tag(s).
Parameters:
collections (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('collection', collections, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class CountryMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have countries. """
def addCountry(self, countries, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a country tag(s).
Parameters:
countries (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('country', countries, locked=locked)
2021-01-24 22:35:13 +00:00
def removeCountry(self, countries, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a country tag(s).
Parameters:
countries (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('country', countries, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class DirectorMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have directors. """
def addDirector(self, directors, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a director tag(s).
Parameters:
directors (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('director', directors, locked=locked)
2021-01-24 22:35:13 +00:00
def removeDirector(self, directors, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a director tag(s).
Parameters:
directors (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('director', directors, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class GenreMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have genres. """
def addGenre(self, genres, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a genre tag(s).
Parameters:
genres (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('genre', genres, locked=locked)
2021-01-24 22:35:13 +00:00
def removeGenre(self, genres, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a genre tag(s).
Parameters:
genres (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('genre', genres, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class LabelMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have labels. """
def addLabel(self, labels, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a label tag(s).
Parameters:
labels (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('label', labels, locked=locked)
2021-01-24 22:35:13 +00:00
def removeLabel(self, labels, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a label tag(s).
Parameters:
labels (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('label', labels, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class MoodMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have moods. """
def addMood(self, moods, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a mood tag(s).
Parameters:
moods (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('mood', moods, locked=locked)
2021-01-24 22:35:13 +00:00
def removeMood(self, moods, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a mood tag(s).
Parameters:
moods (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('mood', moods, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class ProducerMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have producers. """
def addProducer(self, producers, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a producer tag(s).
Parameters:
producers (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('producer', producers, locked=locked)
2021-01-24 22:35:13 +00:00
def removeProducer(self, producers, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a producer tag(s).
Parameters:
producers (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('producer', producers, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class SimilarArtistMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have similar artists. """
def addSimilarArtist(self, artists, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a similar artist tag(s).
Parameters:
artists (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('similar', artists, locked=locked)
2021-01-24 22:35:13 +00:00
def removeSimilarArtist(self, artists, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a similar artist tag(s).
Parameters:
artists (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('similar', artists, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class StyleMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have styles. """
def addStyle(self, styles, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a style tag(s).
Parameters:
styles (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('style', styles, locked=locked)
2021-01-24 22:35:13 +00:00
def removeStyle(self, styles, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a style tag(s).
Parameters:
styles (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('style', styles, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 04:21:49 +00:00
class TagMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have tags. """
def addTag(self, tags, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a tag(s).
Parameters:
tags (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('tag', tags, locked=locked)
2021-01-24 22:35:13 +00:00
def removeTag(self, tags, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a tag(s).
Parameters:
tags (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('tag', tags, locked=locked, remove=True)
2021-01-24 22:35:13 +00:00
2021-02-07 18:54:03 +00:00
class WriterMixin(object):
2021-01-24 22:35:13 +00:00
""" Mixin for Plex objects that can have writers. """
def addWriter(self, writers, locked=True):
2021-01-24 22:35:13 +00:00
""" Add a writer tag(s).
Parameters:
writers (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('writer', writers, locked=locked)
2021-01-24 22:35:13 +00:00
def removeWriter(self, writers, locked=True):
2021-01-24 22:35:13 +00:00
""" Remove a writer tag(s).
Parameters:
writers (list): List of strings.
locked (bool): True (default) to lock the field, False to unlock the field.
2021-01-24 22:35:13 +00:00
"""
self._edit_tags('writer', writers, locked=locked, remove=True)