2014-12-29 03:21:58 +00:00
|
|
|
"""
|
|
|
|
PlexLibrary
|
|
|
|
"""
|
|
|
|
from plexapi import video, utils
|
|
|
|
from plexapi.exceptions import NotFound
|
|
|
|
|
|
|
|
|
|
|
|
class Library(object):
|
2015-06-08 16:41:47 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def __init__(self, server, data):
|
|
|
|
self.server = server
|
|
|
|
self.identifier = data.attrib.get('identifier')
|
|
|
|
self.mediaTagVersion = data.attrib.get('mediaTagVersion')
|
|
|
|
self.title1 = data.attrib.get('title1')
|
|
|
|
self.title2 = data.attrib.get('title2')
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Library:%s>' % self.title1.encode('utf8')
|
|
|
|
|
|
|
|
def sections(self):
|
|
|
|
items = []
|
|
|
|
SECTION_TYPES = {MovieSection.TYPE:MovieSection, ShowSection.TYPE:ShowSection}
|
|
|
|
path = '/library/sections'
|
|
|
|
for elem in self.server.query(path):
|
|
|
|
stype = elem.attrib['type']
|
|
|
|
if stype in SECTION_TYPES:
|
|
|
|
cls = SECTION_TYPES[stype]
|
|
|
|
items.append(cls(self.server, elem, path))
|
|
|
|
return items
|
|
|
|
|
|
|
|
def section(self, title=None):
|
|
|
|
for item in self.sections():
|
|
|
|
if item.title == title:
|
|
|
|
return item
|
|
|
|
raise NotFound('Invalid library section: %s' % title)
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return video.list_items(self.server, '/library/all')
|
|
|
|
|
|
|
|
def onDeck(self):
|
|
|
|
return video.list_items(self.server, '/library/onDeck')
|
|
|
|
|
|
|
|
def recentlyAdded(self):
|
|
|
|
return video.list_items(self.server, '/library/recentlyAdded')
|
|
|
|
|
|
|
|
def get(self, title):
|
|
|
|
return video.find_item(self.server, '/library/all', title)
|
|
|
|
|
2015-06-02 02:28:50 +00:00
|
|
|
def getByKey(self, key):
|
|
|
|
return video.find_key(self.server, key)
|
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def search(self, title, filter='all', vtype=None, **tags):
|
|
|
|
""" Search all available content.
|
|
|
|
title: Title to search (pass None to search all titles).
|
|
|
|
filter: One of {'all', 'onDeck', 'recentlyAdded'}.
|
|
|
|
videotype: One of {'movie', 'show', 'season', 'episode'}.
|
|
|
|
tags: One of {country, director, genre, producer, actor, writer}.
|
|
|
|
"""
|
|
|
|
args = {}
|
|
|
|
if title: args['title'] = title
|
|
|
|
if vtype: args['type'] = video.search_type(vtype)
|
2015-09-05 14:09:15 +00:00
|
|
|
for tag, obj in tags.items():
|
2014-12-29 03:21:58 +00:00
|
|
|
args[tag] = obj.id
|
|
|
|
query = '/library/%s%s' % (filter, utils.joinArgs(args))
|
|
|
|
return video.list_items(self.server, query)
|
|
|
|
|
|
|
|
def cleanBundles(self):
|
|
|
|
self.server.query('/library/clean/bundles')
|
|
|
|
|
|
|
|
def emptyTrash(self):
|
|
|
|
for section in self.sections():
|
|
|
|
section.emptyTrash()
|
|
|
|
|
|
|
|
def optimize(self):
|
|
|
|
self.server.query('/library/optimize')
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
self.server.query('/library/sections/all/refresh')
|
|
|
|
|
|
|
|
|
|
|
|
class LibrarySection(object):
|
|
|
|
|
|
|
|
def __init__(self, server, data, initpath):
|
|
|
|
self.server = server
|
|
|
|
self.initpath = initpath
|
|
|
|
self.type = data.attrib.get('type')
|
|
|
|
self.key = data.attrib.get('key')
|
|
|
|
self.title = data.attrib.get('title')
|
|
|
|
self.scanner = data.attrib.get('scanner')
|
|
|
|
self.language = data.attrib.get('language')
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
title = self.title.replace(' ','.')[0:20]
|
|
|
|
return '<%s:%s>' % (self.__class__.__name__, title.encode('utf8'))
|
|
|
|
|
2015-02-17 20:35:17 +00:00
|
|
|
def _primary_list(self, key):
|
2014-12-29 03:21:58 +00:00
|
|
|
return video.list_items(self.server, '/library/sections/%s/%s' % (self.key, key))
|
|
|
|
|
2015-02-17 20:35:17 +00:00
|
|
|
def _secondary_list(self, key, input=None):
|
2014-12-29 03:21:58 +00:00
|
|
|
choices = list_choices(self.server, '/library/sections/%s/%s' % (self.key, key))
|
|
|
|
if not input:
|
2015-09-05 14:09:15 +00:00
|
|
|
return list(choices.keys())
|
2014-12-29 03:21:58 +00:00
|
|
|
return video.list_items(self.server, '/library/sections/%s/%s/%s' % (self.key, key, choices[input]))
|
|
|
|
|
|
|
|
def all(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('all')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def newest(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('newest')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def onDeck(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('onDeck')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def recentlyAdded(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('recentlyAdded')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def recentlyViewed(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('recentlyViewed')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def unwatched(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('unwatched')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def contentRating(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('contentRating', input)
|
2015-06-08 16:41:47 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def firstCharacter(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('firstCharacter', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def genre(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('genre', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def year(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('year', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def get(self, title):
|
|
|
|
path = '/library/sections/%s/all' % self.key
|
|
|
|
return video.find_item(self.server, path, title)
|
|
|
|
|
|
|
|
def search(self, title, filter='all', vtype=None, **tags):
|
|
|
|
""" Search section content.
|
|
|
|
title: Title to search (pass None to search all titles).
|
|
|
|
filter: One of {'all', 'newest', 'onDeck', 'recentlyAdded', 'recentlyViewed', 'unwatched'}.
|
|
|
|
videotype: One of {'movie', 'show', 'season', 'episode'}.
|
|
|
|
tags: One of {country, director, genre, producer, actor, writer}.
|
|
|
|
"""
|
|
|
|
args = {}
|
|
|
|
if title: args['title'] = title
|
|
|
|
if vtype: args['type'] = video.search_type(vtype)
|
2015-09-05 14:09:15 +00:00
|
|
|
for tag, obj in tags.items():
|
2014-12-29 03:21:58 +00:00
|
|
|
args[tag] = obj.id
|
|
|
|
query = '/library/sections/%s/%s%s' % (self.key, filter, utils.joinArgs(args))
|
|
|
|
return video.list_items(self.server, query)
|
|
|
|
|
|
|
|
def analyze(self):
|
|
|
|
self.server.query('/library/sections/%s/analyze' % self.key)
|
|
|
|
|
|
|
|
def emptyTrash(self):
|
|
|
|
self.server.query('/library/sections/%s/emptyTrash' % self.key)
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
self.server.query('/library/sections/%s/refresh' % self.key)
|
|
|
|
|
|
|
|
|
|
|
|
class MovieSection(LibrarySection):
|
|
|
|
TYPE = 'movie'
|
|
|
|
|
|
|
|
def actor(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('actor', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def country(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('country', input)
|
2015-06-08 16:41:47 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def decade(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('decade', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def director(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('director', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def rating(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('rating', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def resolution(self, input=None):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._secondary_list('resolution', input)
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def search(self, title, filter='all', **tags):
|
|
|
|
return super(MovieSection, self).search(title, filter=filter, vtype=video.Movie.TYPE, **tags)
|
|
|
|
|
|
|
|
|
|
|
|
class ShowSection(LibrarySection):
|
|
|
|
TYPE = 'show'
|
2015-06-08 16:41:47 +00:00
|
|
|
|
2014-12-29 03:21:58 +00:00
|
|
|
def recentlyViewedShows(self):
|
2015-02-17 20:35:17 +00:00
|
|
|
return self._primary_list('recentlyViewedShows')
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
def search(self, title, filter='all', **tags):
|
|
|
|
return super(ShowSection, self).search(title, filter=filter, vtype=video.Show.TYPE, **tags)
|
|
|
|
|
|
|
|
def searchEpisodes(self, title, filter='all', **tags):
|
|
|
|
return super(ShowSection, self).search(title, filter=filter, vtype=video.Episode.TYPE, **tags)
|
|
|
|
|
|
|
|
|
|
|
|
def list_choices(server, path):
|
|
|
|
return {c.attrib['title']:c.attrib['key'] for c in server.query(path)}
|