Add Recently Added and On Deck to Plex media browser (#39232)

This commit is contained in:
jjlawren authored and GitHub committed 2020-09-03 09:26:30 +02:00
1 parent 49daf57c54
commit d66bc6a873
3 files changed
+126 -5

No files matched your search

+54 -1
View File
@@ -13,6 +13,10 @@ PLAYLISTS_BROWSE_PAYLOAD = {
"can_play": False,
"can_expand": True,
}
SPECIAL_METHODS = {
"On Deck": "onDeck",
"Recently Added": "recentlyAdded",
}
_LOGGER = logging.getLogger(__name__)
@@ -36,14 +40,43 @@ def browse_media(
media_info["children"].append(item_payload(item))
return media_info
if media_content_id and ":" in media_content_id:
media_content_id, special_folder = media_content_id.split(":")
else:
special_folder = None
if (
media_content_type == "server"
media_content_type
and media_content_type == "server"
and media_content_id != plex_server.machine_identifier
):
raise BrowseError(
f"Plex server with ID '{media_content_id}' is not associated with {entity_id}"
)
if special_folder:
if media_content_type == "server":
library_or_section = plex_server.library
title = plex_server.friendly_name
elif media_content_type == "library":
library_or_section = plex_server.library.sectionByID(media_content_id)
title = library_or_section.title
payload = {
"title": title,
"media_content_id": f"{media_content_id}:{special_folder}",
"media_content_type": media_content_type,
"can_play": False,
"can_expand": True,
"children": [],
}
method = SPECIAL_METHODS[special_folder]
items = getattr(library_or_section, method)()
for item in items:
payload["children"].append(item_payload(item))
return payload
if media_content_type in ["server", None]:
return server_payload(plex_server)
@@ -89,6 +122,18 @@ def library_section_payload(section):
}
def special_library_payload(parent_payload, special_type):
"""Create response payload for special library folders."""
title = f"{special_type} ({parent_payload['title']})"
return {
"title": title,
"media_content_id": f"{parent_payload['media_content_id']}:{special_type}",
"media_content_type": parent_payload["media_content_type"],
"can_play": False,
"can_expand": True,
}
def server_payload(plex_server):
"""Create response payload to describe libraries of the Plex server."""
server_info = {
@@ -99,6 +144,10 @@ def server_payload(plex_server):
"can_expand": True,
}
server_info["children"] = []
server_info["children"].append(special_library_payload(server_info, "On Deck"))
server_info["children"].append(
special_library_payload(server_info, "Recently Added")
)
for library in plex_server.library.sections():
if library.type == "photo":
continue
@@ -112,6 +161,10 @@ def library_payload(plex_server, library_id):
library = plex_server.library.sectionByID(library_id)
library_info = library_section_payload(library)
library_info["children"] = []
library_info["children"].append(special_library_payload(library_info, "On Deck"))
library_info["children"].append(
special_library_payload(library_info, "Recently Added")
)
for item in library.all():
library_info["children"].append(item_payload(item))
return library_info