from datetime import datetime
from urllib.parse import quote
[docs]
class EditFieldMixin:
""" Mixin for editing Plex object fields. """
[docs]
def editField(self, field, value, locked=True, **kwargs):
""" Edit the field of a Plex object. All field editing methods can be chained together.
Also see :func:`~plexapi.base.PlexPartialObject.batchEdits` for batch editing fields.
Parameters:
field (str): The name of the field to edit.
value (str): The value to edit the field to.
locked (bool): True (default) to lock the field, False to unlock the field.
Example:
.. code-block:: python
# Chaining multiple field edits with reloading
Movie.editTitle('A New Title').editSummary('A new summary').editTagline('A new tagline').reload()
"""
edits = {
f'{field}.value': value or '',
f'{field}.locked': 1 if locked else 0
}
edits.update(kwargs)
return self._edit(**edits)
[docs]
class AddedAtMixin(EditFieldMixin):
""" Mixin for Plex objects that can have an added at date. """
[docs]
def editAddedAt(self, addedAt, locked=True):
""" Edit the added at date.
Parameters:
addedAt (int or str or datetime): The new value as a unix timestamp (int),
"YYYY-MM-DD" (str), or datetime object.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
if isinstance(addedAt, str):
addedAt = int(round(datetime.strptime(addedAt, '%Y-%m-%d').timestamp()))
elif isinstance(addedAt, datetime):
addedAt = int(round(addedAt.timestamp()))
return self.editField('addedAt', addedAt, locked=locked)
[docs]
class AudienceRatingMixin(EditFieldMixin):
""" Mixin for Plex objects that can have an audience rating. """
[docs]
def editAudienceRating(self, audienceRating, locked=True):
""" Edit the audience rating.
Parameters:
audienceRating (float): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('audienceRating', audienceRating, locked=locked)
[docs]
class ContentRatingMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a content rating. """
[docs]
def editContentRating(self, contentRating, locked=True):
""" Edit the content rating.
Parameters:
contentRating (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('contentRating', contentRating, locked=locked)
[docs]
class CriticRatingMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a critic rating. """
[docs]
def editCriticRating(self, criticRating, locked=True):
""" Edit the critic rating.
Parameters:
criticRating (float): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('rating', criticRating, locked=locked)
[docs]
class EditionTitleMixin(EditFieldMixin):
""" Mixin for Plex objects that can have an edition title. """
[docs]
def editEditionTitle(self, editionTitle, locked=True):
""" Edit the edition title. Plex Pass is required to edit this field.
Parameters:
editionTitle (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('editionTitle', editionTitle, locked=locked)
[docs]
class OriginallyAvailableMixin(EditFieldMixin):
""" Mixin for Plex objects that can have an originally available date. """
[docs]
def editOriginallyAvailable(self, originallyAvailable, locked=True):
""" Edit the originally available date.
Parameters:
originallyAvailable (str or datetime): The new value "YYYY-MM-DD (str) or datetime object.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
if isinstance(originallyAvailable, datetime):
originallyAvailable = originallyAvailable.strftime('%Y-%m-%d')
return self.editField('originallyAvailableAt', originallyAvailable, locked=locked)
[docs]
class OriginalTitleMixin(EditFieldMixin):
""" Mixin for Plex objects that can have an original title. """
[docs]
def editOriginalTitle(self, originalTitle, locked=True):
""" Edit the original title.
Parameters:
originalTitle (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('originalTitle', originalTitle, locked=locked)
[docs]
class SortTitleMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a sort title. """
[docs]
def editSortTitle(self, sortTitle, locked=True):
""" Edit the sort title.
Parameters:
sortTitle (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('titleSort', sortTitle, locked=locked)
[docs]
class StudioMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a studio. """
[docs]
def editStudio(self, studio, locked=True):
""" Edit the studio.
Parameters:
studio (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('studio', studio, locked=locked)
[docs]
class SummaryMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a summary. """
[docs]
def editSummary(self, summary, locked=True):
""" Edit the summary.
Parameters:
summary (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('summary', summary, locked=locked)
[docs]
class TaglineMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a tagline. """
[docs]
def editTagline(self, tagline, locked=True):
""" Edit the tagline.
Parameters:
tagline (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('tagline', tagline, locked=locked)
[docs]
class TitleMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a title. """
[docs]
def editTitle(self, title, locked=True):
""" Edit the title.
Parameters:
title (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
kwargs = {}
if self.TYPE == 'album':
# Editing album title also requires the artist ratingKey
kwargs['artist.id.value'] = self.parentRatingKey
return self.editField('title', title, locked=locked, **kwargs)
[docs]
class TrackArtistMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a track artist. """
[docs]
def editTrackArtist(self, trackArtist, locked=True):
""" Edit the track artist.
Parameters:
trackArtist (str): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('originalTitle', trackArtist, locked=locked)
[docs]
class TrackNumberMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a track number. """
[docs]
def editTrackNumber(self, trackNumber, locked=True):
""" Edit the track number.
Parameters:
trackNumber (int): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('index', trackNumber, locked=locked)
[docs]
class TrackDiscNumberMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a track disc number. """
[docs]
def editDiscNumber(self, discNumber, locked=True):
""" Edit the track disc number.
Parameters:
discNumber (int): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('parentIndex', discNumber, locked=locked)
[docs]
class PhotoCapturedTimeMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a captured time. """
[docs]
def editCapturedTime(self, capturedTime, locked=True):
""" Edit the photo captured time.
Parameters:
capturedTime (str or datetime): The new value "YYYY-MM-DD hh:mm:ss" (str) or datetime object.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
if isinstance(capturedTime, datetime):
capturedTime = capturedTime.strftime('%Y-%m-%d %H:%M:%S')
return self.editField('originallyAvailableAt', capturedTime, locked=locked)
[docs]
class UserRatingMixin(EditFieldMixin):
""" Mixin for Plex objects that can have a user rating. """
[docs]
def editUserRating(self, userRating, locked=True):
""" Edit the user rating.
Parameters:
userRating (float): The new value.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editField('userRating', userRating, locked=locked)
[docs]
class CollectionMixin(EditTagsMixin):
""" Mixin for Plex objects that can have collections. """
[docs]
def addCollection(self, collections, locked=True):
""" Add a collection tag(s).
Parameters:
collections (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('collection', collections, locked=locked)
[docs]
def removeCollection(self, collections, locked=True):
""" Remove a collection tag(s).
Parameters:
collections (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('collection', collections, locked=locked, remove=True)
[docs]
class CountryMixin(EditTagsMixin):
""" Mixin for Plex objects that can have countries. """
[docs]
def addCountry(self, countries, locked=True):
""" Add a country tag(s).
Parameters:
countries (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('country', countries, locked=locked)
[docs]
def removeCountry(self, countries, locked=True):
""" Remove a country tag(s).
Parameters:
countries (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('country', countries, locked=locked, remove=True)
[docs]
class DirectorMixin(EditTagsMixin):
""" Mixin for Plex objects that can have directors. """
[docs]
def addDirector(self, directors, locked=True):
""" Add a director tag(s).
Parameters:
directors (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('director', directors, locked=locked)
[docs]
def removeDirector(self, directors, locked=True):
""" Remove a director tag(s).
Parameters:
directors (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('director', directors, locked=locked, remove=True)
[docs]
class GenreMixin(EditTagsMixin):
""" Mixin for Plex objects that can have genres. """
[docs]
def addGenre(self, genres, locked=True):
""" Add a genre tag(s).
Parameters:
genres (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('genre', genres, locked=locked)
[docs]
def removeGenre(self, genres, locked=True):
""" Remove a genre tag(s).
Parameters:
genres (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('genre', genres, locked=locked, remove=True)
[docs]
class LabelMixin(EditTagsMixin):
""" Mixin for Plex objects that can have labels. """
[docs]
def addLabel(self, labels, locked=True):
""" Add a label tag(s).
Parameters:
labels (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('label', labels, locked=locked)
[docs]
def removeLabel(self, labels, locked=True):
""" Remove a label tag(s).
Parameters:
labels (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('label', labels, locked=locked, remove=True)
[docs]
class MoodMixin(EditTagsMixin):
""" Mixin for Plex objects that can have moods. """
[docs]
def addMood(self, moods, locked=True):
""" Add a mood tag(s).
Parameters:
moods (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('mood', moods, locked=locked)
[docs]
def removeMood(self, moods, locked=True):
""" Remove a mood tag(s).
Parameters:
moods (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('mood', moods, locked=locked, remove=True)
[docs]
class ProducerMixin(EditTagsMixin):
""" Mixin for Plex objects that can have producers. """
[docs]
def addProducer(self, producers, locked=True):
""" Add a producer tag(s).
Parameters:
producers (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('producer', producers, locked=locked)
[docs]
def removeProducer(self, producers, locked=True):
""" Remove a producer tag(s).
Parameters:
producers (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('producer', producers, locked=locked, remove=True)
[docs]
class SimilarArtistMixin(EditTagsMixin):
""" Mixin for Plex objects that can have similar artists. """
[docs]
def addSimilarArtist(self, artists, locked=True):
""" Add a similar artist tag(s).
Parameters:
artists (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('similar', artists, locked=locked)
[docs]
def removeSimilarArtist(self, artists, locked=True):
""" Remove a similar artist tag(s).
Parameters:
artists (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('similar', artists, locked=locked, remove=True)
[docs]
class StyleMixin(EditTagsMixin):
""" Mixin for Plex objects that can have styles. """
[docs]
def addStyle(self, styles, locked=True):
""" Add a style tag(s).
Parameters:
styles (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('style', styles, locked=locked)
[docs]
def removeStyle(self, styles, locked=True):
""" Remove a style tag(s).
Parameters:
styles (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('style', styles, locked=locked, remove=True)
[docs]
class TagMixin(EditTagsMixin):
""" Mixin for Plex objects that can have tags. """
[docs]
def addTag(self, tags, locked=True):
""" Add a tag(s).
Parameters:
tags (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('tag', tags, locked=locked)
[docs]
def removeTag(self, tags, locked=True):
""" Remove a tag(s).
Parameters:
tags (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('tag', tags, locked=locked, remove=True)
[docs]
class WriterMixin(EditTagsMixin):
""" Mixin for Plex objects that can have writers. """
[docs]
def addWriter(self, writers, locked=True):
""" Add a writer tag(s).
Parameters:
writers (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('writer', writers, locked=locked)
[docs]
def removeWriter(self, writers, locked=True):
""" Remove a writer tag(s).
Parameters:
writers (List<str> or List<:class:`~plexapi.media.MediaTag`>): List of tags.
locked (bool): True (default) to lock the field, False to unlock the field.
"""
return self.editTags('writer', writers, locked=locked, remove=True)