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
This commit is contained in:
parent
c0c29974d3
commit
22c363a788
@ -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:
|
||||
|
@ -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"]})
|
||||
|
27
grzegorz/playlist_data.py
Normal file
27
grzegorz/playlist_data.py
Normal file
@ -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]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user