added keyword searching for unfiltered metadata
added filtered searching
This commit is contained in:
parent
29566aa47a
commit
bfdf94fc14
@ -76,6 +76,7 @@ import requests
|
|||||||
import argparse
|
import argparse
|
||||||
import operator
|
import operator
|
||||||
import datetime
|
import datetime
|
||||||
|
import unicodedata
|
||||||
from plexapi.server import PlexServer, CONFIG
|
from plexapi.server import PlexServer, CONFIG
|
||||||
|
|
||||||
### EDIT SETTINGS ###
|
### EDIT SETTINGS ###
|
||||||
@ -139,6 +140,7 @@ def selectors():
|
|||||||
'historyMonth': 'Aired in {month}',
|
'historyMonth': 'Aired in {month}',
|
||||||
'popularTv': 'Most Popular TV Shows ({days} days)',
|
'popularTv': 'Most Popular TV Shows ({days} days)',
|
||||||
'popularMovies': 'Most Popular Movies ({days} days)',
|
'popularMovies': 'Most Popular Movies ({days} days)',
|
||||||
|
'keyword':'{keyword} Playlist',
|
||||||
'genre': '{title} Playlist',
|
'genre': '{title} Playlist',
|
||||||
'random': '{count} Random Playlist',
|
'random': '{count} Random Playlist',
|
||||||
'studio': 'Studio: {title} Playlist',
|
'studio': 'Studio: {title} Playlist',
|
||||||
@ -206,14 +208,14 @@ def sort_by_dates(video, date_type):
|
|||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def get_content(library_name, search, term):
|
def get_content(library_name, jbop, search=None):
|
||||||
"""Get all movies or episodes from LIBRARY_NAME
|
"""Get all movies or episodes from LIBRARY_NAME
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
library_name: list
|
library_name: list
|
||||||
list of library objects
|
list of library objects
|
||||||
search: str
|
jbop: str
|
||||||
jbop value for searching
|
jbop value for searching
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
@ -224,39 +226,71 @@ def get_content(library_name, search, term):
|
|||||||
"""
|
"""
|
||||||
# todo-me expand function for keyword searching
|
# todo-me expand function for keyword searching
|
||||||
child_lst = []
|
child_lst = []
|
||||||
|
if search and jbop == 'keyword':
|
||||||
for library in library_name:
|
if search.keys()[0] in selectors().keys():
|
||||||
for child in plex.library.section(library).all():
|
searchable = True
|
||||||
if child.type == 'movie':
|
keyword = search
|
||||||
if sort_by_dates(child, search):
|
else:
|
||||||
item_date = sort_by_dates(child, search)
|
searchable = False
|
||||||
child_lst += item_date
|
keyword = {key + '__icontains': value for key, value in search.items()}
|
||||||
elif child.type == 'show':
|
|
||||||
for episode in child.episodes():
|
for library in library_name:
|
||||||
if sort_by_dates(episode, search):
|
plex_library = plex.library.section(library)
|
||||||
item_date = sort_by_dates(episode, search)
|
library_type = plex_library.type
|
||||||
child_lst += item_date
|
if library_type == 'movie':
|
||||||
|
if searchable:
|
||||||
|
child_lst = [x.ratingKey for x in plex_library.search(**keyword)]
|
||||||
|
else:
|
||||||
|
child_lst = [x.ratingKey for x in plex_library.all(**keyword)]
|
||||||
|
elif library_type == 'show':
|
||||||
|
child = plex.library.section(library).all()
|
||||||
|
if searchable:
|
||||||
|
for episode in child.search(**keyword):
|
||||||
|
child_lst += episode.ratingKey
|
||||||
|
else:
|
||||||
|
for episode in child.episodes(**keyword):
|
||||||
|
child_lst += episode.ratingKey
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
play_lst = child_lst
|
||||||
|
|
||||||
# Sort by original air date, oldest first
|
else:
|
||||||
# todo-me move sorting and add more sorting options
|
for library in library_name:
|
||||||
aired_lst = sorted(child_lst, key=operator.itemgetter(1))
|
for child in plex.library.section(library).all():
|
||||||
|
if child.type == 'movie':
|
||||||
|
if sort_by_dates(child, jbop):
|
||||||
|
item_date = sort_by_dates(child, jbop)
|
||||||
|
child_lst += item_date
|
||||||
|
elif child.type == 'show':
|
||||||
|
for episode in child.episodes():
|
||||||
|
if sort_by_dates(episode, jbop):
|
||||||
|
item_date = sort_by_dates(episode, jbop)
|
||||||
|
child_lst += item_date
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
# Remove date used for sorting
|
# Sort by original air date, oldest first
|
||||||
play_lst = [x[0] for x in aired_lst]
|
# todo-me move sorting and add more sorting options
|
||||||
|
aired_lst = sorted(child_lst, key=operator.itemgetter(1))
|
||||||
|
|
||||||
|
# Remove date used for sorting
|
||||||
|
play_lst = [x[0] for x in aired_lst]
|
||||||
|
|
||||||
return play_lst
|
return play_lst
|
||||||
|
|
||||||
|
|
||||||
def build_playlist(jbop, libraries=None, days=None, top=None):
|
def build_playlist(jbop, libraries=None, days=None, top=None, search=None):
|
||||||
"""
|
"""
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
jbop: str
|
jbop: str
|
||||||
|
The predefined Playlist type
|
||||||
libraries: list
|
libraries: list
|
||||||
|
Libraries to use to build Playlist
|
||||||
days: int
|
days: int
|
||||||
|
Days to search for Top Movies/Tv Shows
|
||||||
top: int
|
top: int
|
||||||
|
Limit to search for Top Movies/Tv Shows
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
@ -267,7 +301,7 @@ def build_playlist(jbop, libraries=None, days=None, top=None):
|
|||||||
title = ''
|
title = ''
|
||||||
if jbop == 'historyToday':
|
if jbop == 'historyToday':
|
||||||
try:
|
try:
|
||||||
keys_list = get_content(libraries, jbop, '')
|
keys_list = get_content(libraries, jbop)
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
||||||
exit("Error: {}".format(e))
|
exit("Error: {}".format(e))
|
||||||
@ -275,7 +309,7 @@ def build_playlist(jbop, libraries=None, days=None, top=None):
|
|||||||
|
|
||||||
elif jbop == 'historyWeek':
|
elif jbop == 'historyWeek':
|
||||||
try:
|
try:
|
||||||
keys_list = get_content(libraries, jbop, '')
|
keys_list = get_content(libraries, jbop)
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
||||||
exit("Error: {}".format(e))
|
exit("Error: {}".format(e))
|
||||||
@ -283,11 +317,19 @@ def build_playlist(jbop, libraries=None, days=None, top=None):
|
|||||||
|
|
||||||
elif jbop == 'historyMonth':
|
elif jbop == 'historyMonth':
|
||||||
try:
|
try:
|
||||||
keys_list = get_content(libraries, jbop, '')
|
keys_list = get_content(libraries, jbop)
|
||||||
except TypeError as e:
|
except TypeError as e:
|
||||||
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
||||||
exit("Error: {}".format(e))
|
exit("Error: {}".format(e))
|
||||||
title = selectors()['historyMonth'].format(month=today.strftime("%B"))
|
title = selectors()['historyMonth'].format(month=today.strftime("%B"))
|
||||||
|
|
||||||
|
elif jbop == 'keyword':
|
||||||
|
try:
|
||||||
|
keys_list = get_content(libraries, jbop, search)
|
||||||
|
except TypeError as e:
|
||||||
|
print("Libraries are not defined for {}. Use --libraries.".format(jbop))
|
||||||
|
exit("Error: {}".format(e))
|
||||||
|
title = selectors()['keyword'].format(keyword=' '.join(search.values()).capitalize())
|
||||||
|
|
||||||
elif jbop == 'popularTv':
|
elif jbop == 'popularTv':
|
||||||
home_stats = get_home_stats(days, top)
|
home_stats = get_home_stats(days, top)
|
||||||
@ -305,6 +347,7 @@ def build_playlist(jbop, libraries=None, days=None, top=None):
|
|||||||
|
|
||||||
return keys_list, title
|
return keys_list, title
|
||||||
|
|
||||||
|
|
||||||
def share_playlists(playlist_titles, users):
|
def share_playlists(playlist_titles, users):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -337,9 +380,11 @@ def show_playlist(playlist_title, playlist_keys):
|
|||||||
if plex_obj.type == 'show':
|
if plex_obj.type == 'show':
|
||||||
for episode in plex_obj.episodes():
|
for episode in plex_obj.episodes():
|
||||||
title = "{}".format(episode._prettyfilename())
|
title = "{}".format(episode._prettyfilename())
|
||||||
|
title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'")
|
||||||
playlist_list.append(title)
|
playlist_list.append(title)
|
||||||
else:
|
else:
|
||||||
title = u"{} ({})".format(plex_obj._prettyfilename(), plex_obj.year)
|
title = u"{} ({})".format(plex_obj._prettyfilename(), plex_obj.year)
|
||||||
|
title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'")
|
||||||
playlist_list.append(title)
|
playlist_list.append(title)
|
||||||
|
|
||||||
print(u"Contents of Playlist {title}:\n{playlist}".format(title=playlist_title,
|
print(u"Contents of Playlist {title}:\n{playlist}".format(title=playlist_title,
|
||||||
@ -468,7 +513,6 @@ if __name__ == "__main__":
|
|||||||
'filter types (genre, actors, director, studio, etc.')
|
'filter types (genre, actors, director, studio, etc.')
|
||||||
|
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
# print(opts)
|
|
||||||
|
|
||||||
users = ''
|
users = ''
|
||||||
plex_servers = []
|
plex_servers = []
|
||||||
@ -480,6 +524,8 @@ if __name__ == "__main__":
|
|||||||
'pop_tv': pop_tv_title,
|
'pop_tv': pop_tv_title,
|
||||||
'pop_movie': pop_movie_title,
|
'pop_movie': pop_movie_title,
|
||||||
'limit': opts.limit}
|
'limit': opts.limit}
|
||||||
|
if opts.search:
|
||||||
|
search = dict(opts.search)
|
||||||
|
|
||||||
# Defining users
|
# Defining users
|
||||||
if opts.allUsers and not opts.user:
|
if opts.allUsers and not opts.user:
|
||||||
@ -517,10 +563,10 @@ if __name__ == "__main__":
|
|||||||
delete_playlist(playlist_dict)
|
delete_playlist(playlist_dict)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
keys_list, title = build_playlist(opts.jbop, opts.libraries, opts.days, opts.top)
|
keys_list, title = build_playlist(opts.jbop, opts.libraries, opts.days, opts.top, search)
|
||||||
|
|
||||||
if opts.jbop and opts.action == 'show':
|
if opts.jbop and opts.action == 'show':
|
||||||
show_playlist(title, keys_list)
|
show_playlist(title.title(), keys_list)
|
||||||
|
|
||||||
if opts.action == 'update':
|
if opts.action == 'update':
|
||||||
print("Deleting the playlist(s)...")
|
print("Deleting the playlist(s)...")
|
||||||
@ -530,12 +576,12 @@ if __name__ == "__main__":
|
|||||||
delete_playlist(playlist_dict)
|
delete_playlist(playlist_dict)
|
||||||
print('Creating playlist(s)...')
|
print('Creating playlist(s)...')
|
||||||
for x in plex_servers:
|
for x in plex_servers:
|
||||||
create_playlist(title, keys_list, x['server'], x['user'])
|
create_playlist(title.title(), keys_list, x['server'], x['user'])
|
||||||
|
|
||||||
if opts.action == 'add':
|
if opts.action == 'add':
|
||||||
print('Creating playlist(s)...')
|
print('Creating playlist(s)...')
|
||||||
for x in plex_servers:
|
for x in plex_servers:
|
||||||
create_playlist(title, keys_list, x['server'], x['user'])
|
create_playlist(title.title(), keys_list, x['server'], x['user'])
|
||||||
|
|
||||||
if opts.action == 'show':
|
if opts.action == 'show':
|
||||||
print("Displaying the user's playlist(s)...")
|
print("Displaying the user's playlist(s)...")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user