#131 now popularTV/Movies can use libraries to select specific libraries

This commit is contained in:
Blacktwin 2019-01-03 10:39:55 -05:00
parent f139b19d35
commit 83990a7620

View File

@ -128,7 +128,7 @@ account = plex.myPlexAccount()
user_lst = [x.title for x in plex.myPlexAccount().users()] user_lst = [x.title for x in plex.myPlexAccount().users()]
sections = plex.library.sections() sections = plex.library.sections()
sections_lst = [x.title for x in sections] sections_dict = {x.key: x.title for x in sections}
filter_lst = list(set([y for x in sections if x.type != 'photo' for y in x.ALLOWED_FILTERS])) filter_lst = list(set([y for x in sections if x.type != 'photo' for y in x.ALLOWED_FILTERS]))
playlist_lst = [x.title for x in plex.playlists()] playlist_lst = [x.title for x in plex.playlists()]
today = datetime.datetime.now().date() today = datetime.datetime.now().date()
@ -214,13 +214,13 @@ def sort_by_dates(video, date_type):
return return
def get_content(library_name, jbop, filters=None, search=None): def get_content(libraries, jbop, filters=None, search=None):
"""Get all movies or episodes from LIBRARY_NAME """Get all movies or episodes from LIBRARY_NAME
Parameters Parameters
---------- ----------
library_name: list libraries: dict
list of library objects dict of libraries key and name
jbop: str jbop: str
jbop value for searching jbop value for searching
@ -240,8 +240,8 @@ def get_content(library_name, jbop, filters=None, search=None):
# todo-me replace with documentation showing the available search operators # todo-me replace with documentation showing the available search operators
keyword = {key + '__icontains': value for key, value in search.items()} keyword = {key + '__icontains': value for key, value in search.items()}
# Loop through each library # Loop through each library
for library in library_name: for library in libraries.keys():
plex_library = plex.library.section(library) plex_library = plex.library.sectionByID(library)
library_type = plex_library.type library_type = plex_library.type
# Find media type, if show then search/filter episodes # Find media type, if show then search/filter episodes
if library_type == 'movie': if library_type == 'movie':
@ -274,8 +274,8 @@ def get_content(library_name, jbop, filters=None, search=None):
play_lst = child_lst play_lst = child_lst
else: else:
for library in library_name: for library in libraries.keys():
for child in plex.library.section(library).all(): for child in plex.library.sectionByID(library).all():
if child.type == 'movie': if child.type == 'movie':
if sort_by_dates(child, jbop): if sort_by_dates(child, jbop):
item_date = sort_by_dates(child, jbop) item_date = sort_by_dates(child, jbop)
@ -304,7 +304,8 @@ def build_playlist(jbop, libraries=None, days=None, top=None, filters=None, sear
---------- ----------
jbop: str jbop: str
The predefined Playlist type The predefined Playlist type
libraries: list libraries: dict
{key: name}
Libraries to use to build Playlist Libraries to use to build Playlist
days: int days: int
Days to search for Top Movies/Tv Shows Days to search for Top Movies/Tv Shows
@ -362,15 +363,23 @@ def build_playlist(jbop, libraries=None, days=None, top=None, filters=None, sear
home_stats = get_home_stats(days, top) home_stats = get_home_stats(days, top)
for stat in home_stats: for stat in home_stats:
if stat['stat_id'] == 'popular_tv': if stat['stat_id'] == 'popular_tv':
keys_list = [x['rating_key'] for x in stat['rows']] if libraries:
title = pop_tv_title keys_list = [x['rating_key'] for x in stat['rows'] if
str(x['section_id']) in libraries.keys()]
else:
keys_list = [x['rating_key'] for x in stat['rows']]
title = selectors()['popularTv'].format(days=days)
elif jbop == 'popularMovies': elif jbop == 'popularMovies':
home_stats = get_home_stats(days, top) home_stats = get_home_stats(days, top)
for stat in home_stats: for stat in home_stats:
if stat['stat_id'] == 'popular_movies': if stat['stat_id'] == 'popular_movies':
keys_list = [x['rating_key'] for x in stat['rows']] if libraries:
title = pop_movie_title keys_list = [x['rating_key'] for x in stat['rows']
if str(x['section_id']) in libraries.keys()]
else:
keys_list = [x['rating_key'] for x in stat['rows']]
title = selectors()['popularMovies'].format(days=days)
return keys_list, title return keys_list, title
@ -403,6 +412,7 @@ def show_playlist(playlist_title, playlist_keys):
""" """
playlist_list = [] playlist_list = []
for key in playlist_keys: for key in playlist_keys:
# todo-me add try to catch when Tautulli reports a rating key that is now missing from Plex
plex_obj = plex.fetchItem(key) plex_obj = plex.fetchItem(key)
if plex_obj.type == 'show': if plex_obj.type == 'show':
for episode in plex_obj.episodes(): for episode in plex_obj.episodes():
@ -514,7 +524,7 @@ if __name__ == "__main__":
'Choices: %(choices)s') 'Choices: %(choices)s')
parser.add_argument('--allUsers', default=False, action='store_true', parser.add_argument('--allUsers', default=False, action='store_true',
help='Select all users.') help='Select all users.')
parser.add_argument('--libraries', nargs='+', choices=sections_lst, metavar='', parser.add_argument('--libraries', nargs='+', choices=sections_dict.values(), metavar='',
help='Space separated list of case sensitive names to process. Allowed names are:\n' help='Space separated list of case sensitive names to process. Allowed names are:\n'
'Choices: %(choices)s') 'Choices: %(choices)s')
parser.add_argument('--allLibraries', default=False, action='store_true', parser.add_argument('--allLibraries', default=False, action='store_true',
@ -550,7 +560,7 @@ if __name__ == "__main__":
title = '' title = ''
search = '' search = ''
filters = '' filters = ''
libraries = '' libraries = {}
keys_list = [] keys_list = []
plex_servers = [] plex_servers = []
pop_movie_title = selectors()['popularMovies'].format(days=opts.days) pop_movie_title = selectors()['popularMovies'].format(days=opts.days)
@ -585,14 +595,16 @@ if __name__ == "__main__":
# Defining libraries # Defining libraries
if opts.allLibraries and not opts.libraries: if opts.allLibraries and not opts.libraries:
libraries = sections_lst libraries = sections_dict
elif not opts.allLibraries and opts.libraries: elif not opts.allLibraries and opts.libraries:
libraries = opts.libraries for key, name in sections_dict.items():
if name in opts.libraries:
libraries[key] = name
elif opts.allLibraries and opts.libraries: elif opts.allLibraries and opts.libraries:
# If allLibraries is used then any libraries listed will be excluded # If allLibraries is used then any libraries listed will be excluded
for library in opts.libraries: for key, name in sections_dict.items():
sections_lst.remove(library) if name not in opts.libraries:
libraries = sections_lst libraries[key] = name
# Create user server objects # Create user server objects
if users: if users: