From 22c363a788e5bbff13446e3378477a5750016cdd Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sun, 4 Mar 2018 03:54:29 +0100 Subject: [PATCH] Add PlaylistDataCache PlaylistDataCache stores arbitrary data supplied by the client while issuing a loadfile command The contents of this cache matches with filenames and returns its matched content when any user fetches the playlist This is mainly used to store metadata. Could be expanded to having the api automatically fetch the metadata using youtube-dl or something --- grzegorz/api.py | 7 +++++++ grzegorz/mpv.py | 12 ++++++++++++ grzegorz/playlist_data.py | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 grzegorz/playlist_data.py diff --git a/grzegorz/api.py b/grzegorz/api.py index 20644d3..3f00172 100644 --- a/grzegorz/api.py +++ b/grzegorz/api.py @@ -3,6 +3,7 @@ from sanic import Blueprint, response from sanic_openapi import doc from functools import wraps from . import mpv +from .playlist_data import PlaylistDataCache bp = Blueprint("grzegorz-api", strict_slashes=True) # this blueprint assumes a mpv.MPVControl instance is available @@ -38,6 +39,8 @@ def response_text(func): class APIError(Exception): pass +PLAYLIST_DATA_CACHE = PlaylistDataCache() + #routes: @bp.get("") @doc.exclude(True) @@ -48,10 +51,13 @@ async def root(request): @bp.post("/load") @doc.summary("Add item to playlist") @doc.consumes({"path": doc.String("Link to the resource to enqueue")}, required=True) +@doc.consumes({"body":doc.Dictionary(description="Any data you want stored with the queued item")}, location="body") @response_json async def loadfile(request, mpv_control): if "path" not in request.args: raise APIError("No query parameter \"path\" provided") + if request.json: + PLAYLIST_DATA_CACHE.add_data(request.args["path"][0], request.json) success = await mpv_control.loadfile(request.args["path"][0]) return locals() @@ -127,6 +133,7 @@ async def noe(request, mpv_control): @response_json async def playlist_get(request, mpv_control): value = await mpv_control.playlist_get() + value = list(PLAYLIST_DATA_CACHE.add_data_to_playlist(value)) for i, v in enumerate(value): v["index"] = i if "current" in v and v["current"] == True: diff --git a/grzegorz/mpv.py b/grzegorz/mpv.py index b63e29e..00db9b1 100644 --- a/grzegorz/mpv.py +++ b/grzegorz/mpv.py @@ -92,6 +92,18 @@ class MPVControl: self.mpv.requests.put_nowait(msg) return await self.mpv.responses.get() + #other commands: + async def wake_screen(self): + p = await asyncio.create_subprocess_exec( + "xset", + "-display", + os.environ["DISPLAY"], + "dpms", + "force", + "on" + ) + code = await process.wait() + #Shorthand command requests: async def loadfile(self, file):#appends to playlist and start playback if paused resp = await self.send_request({"command":["loadfile", file, "append-play"]}) diff --git a/grzegorz/playlist_data.py b/grzegorz/playlist_data.py new file mode 100644 index 0000000..0087a29 --- /dev/null +++ b/grzegorz/playlist_data.py @@ -0,0 +1,27 @@ + +#Used in api.playlist_get() and api.loadfile() +class PlaylistDataCache: + def __init__(self): + self.filepath_data_map = {} + + def add_data(self, filepath, data=None): + if data: + self.filepath_data_map[filepath] = data + def add_data_to_playlist(self, playlist): + seen = set() + + for item in playlist: + if "filename" in item: + seen.add(item["filename"]) + if item["filename"] in self.filepath_data_map: + new_item = item.copy() + new_item["data"] = self.filepath_data_map[item["filename"]] + yield new_item + continue + yield item + + not_seen = set(self.filepath_data_map.keys()) - seen + for name in not_seen: + del self.filepath_data_map[name] + +