Skip to content

Commit

Permalink
Feat: Add jukebox example script
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Jun 2, 2024
1 parent 0c23ddc commit 78d17e8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Jukebox/Jukebox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-- {"order":1,"arguments":[]}

local addSongs = 1
local min_jukebox_length = 50

local function send_error(message)
mympd.api("INTERNAL_API_JUKEBOX_ERROR", {
error = message
})
end

-- Get the first 2000 playlists
local rc, result = mympd.api("MYMPD_API_PLAYLIST_LIST", {
offset = 0,
limit = 2000,
searchstr = "",
type = 0
})
if rc == 1 then
send_error(result.message)
return
end

-- Get playlist by random number
math.randomseed(os.time())
local number = math.random(1, #result.data)
local playlist = result.data[number].uri

-- Get length of the jukebox queue
rc, result = mympd.api("MYMPD_API_JUKEBOX_LENGTH", {})
if rc == 1 then
send_error("Failure getting jukebox queue length " .. playlist)
return
end
local jukebox_length = result.length

-- Get the songs from the playlist
local songs
rc, songs = mympd.api("MYMPD_API_PLAYLIST_CONTENT_LIST", {
plist = playlist,
expression = "",
offset = 0,
limit = addSongs + min_jukebox_length - jukebox_length,
fields = {}
})
if rc == 1 then
send_error("Failure getting playlist " .. playlist)
return
end

-- Add addSongs entries from playlist to the MPD queue
local addUris = {}
for i = 1, addSongs do
table.insert(addUris, songs.data[i].uri)
end
rc, result = mympd.api("MYMPD_API_QUEUE_APPEND_URIS", {
uris = addUris,
play = true
})
if rc == 1 then
send_error(result.message)
return
end

-- Add additional songs to the jukebox queue
addUris = {}
addSongs = addSongs + 1
for i = addSongs, songs.returnedEntities do
table.insert(addUris, songs.data[i].uri)
end
rc, result = mympd.api("MYMPD_API_JUKEBOX_APPEND_URIS", {
uris = addUris
})
if rc == 1 then
send_error(result.message)
return
end

-- Send signal that filling jukebox queue has finished successfully
mympd.api("INTERNAL_API_JUKEBOX_CREATED", {})
11 changes: 11 additions & 0 deletions Jukebox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Jukebox

This script can be used to fill the jukebox queue. It is only an usage example for the jukebox API.

## Usage

1. Import the `Jukebox.lua` script
2. Add a new trigger
- Event: mympd_jukebox
- Script: Jukebox
3. Change jukebox mode to `Script`

0 comments on commit 78d17e8

Please sign in to comment.