From ea4dc5eeca5bec7f60e796267e1c66def5102308 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Thu, 26 Jan 2023 20:05:01 +0300 Subject: [PATCH] Worst reusable cache wrapper (simular cache-on-disk) --- src/mpv_control.py | 3 ++- src/mpv_scripts/mpv_cache.lua | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/mpv_scripts/mpv_cache.lua diff --git a/src/mpv_control.py b/src/mpv_control.py index cf53451..4a562c9 100644 --- a/src/mpv_control.py +++ b/src/mpv_control.py @@ -11,7 +11,8 @@ import re fzf = FzfPrompt() -player = mpv.MPV(cache=True, demuxer_max_bytes=25*1024*1024) +player = mpv.MPV(cache=True, demuxer_max_bytes=25*1024*1024, + scripts='src/mpv_scripts/mpv_cache.lua') player.ytdl = False # Prevent attempts load track with yt-dlp player.volume = get_config('mpv_volume') player.prefetch_playlist = get_config('prefetch_playlist') diff --git a/src/mpv_scripts/mpv_cache.lua b/src/mpv_scripts/mpv_cache.lua new file mode 100644 index 0000000..01e4d7e --- /dev/null +++ b/src/mpv_scripts/mpv_cache.lua @@ -0,0 +1,50 @@ +local utils = require 'mp.utils' +local msg = require 'mp.msg' +local options = require 'mp.options' + + +function sleep(n) + os.execute("sleep " .. tonumber(n)) +end + + +function createDir(dirname) + os.execute("mkdir -p -m 711 " .. dirname) +end + + +function file_exists(name) + local f = io.open(name, "r") + return f ~= nil and io.close(f) +end + + +function get_url_host(s) + return (s.."/"):match("://(.-)/") +end + + +function make_cache_track(url) + find_uuid = "%x+-%x+-%x+-%x+-%x+" + uuid = string.sub(url, string.find(url, find_uuid)) + host = get_url_host(url) + cache_path_file = 'cache/' .. host .. '/' .. uuid .. '' + if false == file_exists(cache_path_file) then + createDir('cache/' .. host .. '/') + msg.verbose('Caching ' .. cache_path_file .. '') + os.execute('curl -s "' .. url .. '" -o "' .. cache_path_file .. '"') + mp.set_property("stream-open-filename", cache_path_file) + else + msg.verbose('Already cached ' .. cache_path_file .. '') + mp.set_property("stream-open-filename", cache_path_file) + end +end + + +mp.add_hook("on_load", 11, function() + msg.verbose('reusable cache hook activated') + local url = mp.get_property("stream-open-filename", "") + if true == (url:find("https?://") == 1) then + make_cache_track(url) + end +end)