Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FliegendeWurst committed Jul 26, 2020
0 parents commit 5259fef
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions input.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Alt+r script-message RBS-init
Alt+UP script-message RBS-upvote
Alt+DOWN script-message RBS-downvote
2 changes: 2 additions & 0 deletions ratings-based-shuffle.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
directory=/home/username/Music
ratings_file=/home/username/.config/mpv/rbs-ratings.txt
102 changes: 102 additions & 0 deletions ratings-based-shuffle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
utils = require 'mp.utils'
require 'mp.options'

local options = {
directory = ".",
ratings_file = "RBS-ratings.txt",
}
read_options(options, "ratings-based-shuffle")

all_files = {}
ratings = {}

function init_playlist()
mp.osd_message("Initializing ratings-based shuffle..")
mp.set_property("shuffle", "no")
mp.set_property("loop-playlist", "no")

load_ratings(options.ratings_file)
load(options.directory)

mp.commandv('loadfile', all_files[math.random(#all_files)], 'replace')

mp.register_event("end-file", auto_add_file)
auto_add_file(nil)
auto_add_file(nil)
end

function load(path)
for idx, name in ipairs(utils.readdir(path, "dirs")) do
load(utils.join_path(path, name))
end
for idx, name in ipairs(utils.readdir(path, "files")) do
table.insert(all_files, utils.join_path(path, name))
end
end

function load_ratings(path)
info = utils.file_info(path)
if info == nil then
-- nothing to read
elseif info.is_file then
file = io.open(path, "r")
io.input(file)
ratings, err = utils.parse_json(io.read())
io.close(file)
else
msg.warn("could not load ratings")
end
end

function save_ratings(path)
info = utils.file_info(path)
if info == nil or info.is_file then
file = io.open(path, "w")
io.output(file)
json, err = utils.format_json(ratings)
io.write(json)
io.close(file)
else
msg.error("could not save ratings")
end
end

function upvote()
file = mp.get_property("path")
if ratings[file] == nil then
ratings[file] = 1.1
else
ratings[file] = ratings[file] * 1.1
end
save_ratings(options.ratings_file)
end

function downvote()
file = mp.get_property("path")
if ratings[file] == nil then
ratings[file] = 0.9
else
ratings[file] = ratings[file] * 0.9
end
save_ratings(options.ratings_file)
end

function auto_add_file(event)
while true do
idx = math.random(#all_files)
file = all_files[idx]
chance = ratings[file]
if chance == nil then
chance = 1.0
end
if math.random() < (chance/#all_files) then
mp.commandv('loadfile', file, 'append')
break
end
end
end

math.randomseed(math.sin(os.time())*10000)
mp.register_script_message("RBS-init", init_playlist)
mp.register_script_message("RBS-upvote", upvote)
mp.register_script_message("RBS-downvote", downvote)

0 comments on commit 5259fef

Please sign in to comment.