Update last.lua

This commit is contained in:
Soph :3 2025-07-12 09:20:34 +00:00
parent 4a068267a3
commit ff9e5b4e24

View file

@ -2,6 +2,7 @@
-- install morefonts in / -- install morefonts in /
-- also ask me for a sanjuuni.sad.ovh password or selfhost it -- also ask me for a sanjuuni.sad.ovh password or selfhost it
-- change the last.fm username in checkTrack, and my name in doLastfm -- change the last.fm username in checkTrack, and my name in doLastfm
-- https://git.sad.ovh/sophie/convert
local mf = require("../morefonts") local mf = require("../morefonts")
@ -10,8 +11,8 @@ local function drawImage(monitor, image, dx, dy, palette)
printError('Image passed to drawImage was nil.') printError('Image passed to drawImage was nil.')
return return
end end
frame = image[1]
for _, frame in ipairs(image) do -- for _, frame in ipairs(image) do
for y, row in ipairs(frame) do for y, row in ipairs(frame) do
monitor.setCursorPos(dx+1, dy+y) monitor.setCursorPos(dx+1, dy+y)
monitor.blit(table.unpack(row)) monitor.blit(table.unpack(row))
@ -22,17 +23,77 @@ local function drawImage(monitor, image, dx, dy, palette)
else monitor.setPaletteColor(2^i, c) end else monitor.setPaletteColor(2^i, c) end
end end end end
if image.animation then sleep(frame.duration or image.secondsPerFrame or 0.05) end if image.animation then sleep(frame.duration or image.secondsPerFrame or 0.05) end
end -- end
monitor.setBackgroundColor(colors.black) monitor.setBackgroundColor(colors.black)
monitor.setTextColor(colors.white) monitor.setTextColor(colors.white)
monitor.setCursorPos(1, 1) monitor.setCursorPos(1, 1)
end end
local function playAudio(title, artist)
if title == '' or artist == '' then
print("empty 'audio' sent out")
return
end
local audioReq = http.post({
url = "https://sanjuuni.sad.ovh/youtube",
body = textutils.serialiseJSON({
["title"] = title,
["artist"] = artist
}),
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "<..>"
}
})
if audioReq == nil then -- usually works on the second one, not sure what's broken
audioReq = http.post({
url = "https://sanjuuni.sad.ovh/youtube",
body = textutils.serialiseJSON({
["title"] = title,
["artist"] = artist
}),
headers = {
["Content-Type"] = "application/json",
["Authorization"] = "<..>"
}
})
end
local speaker = peripheral.find("speaker")
local decoder = require("cc.audio.dfpwm").make_decoder()
local play = true
parallel.waitForAny(
function ()
while decoder do
local chunk = audioReq.read(16 * 1024)
if not chunk then break end
local decoded = decoder(chunk)
while not speaker.playAudio(decoded) do
os.pullEvent("speaker_audio_empty")
end
end
end,
function ()
while play do
event, title, artist = os.pullEvent("audio")
os.queueEvent("audio", title, artist)
print("play = false")
play = false
end
end
)
end
local function doLastfm(track) local function doLastfm(track)
if not track["@attr"] then if not track["@attr"] then
local monitor = peripheral.find("monitor") local monitor = peripheral.find("monitor")
local width, height = monitor.getSize() local width, height = monitor.getSize()
monitor.clear() monitor.clear()
os.queueEvent('audio', '', '')
mf.writeOn(monitor, "Nothing is being played.", nil, nil, {font="Dogica",anchorHor="center",anchorVer="center", wrapWidth = 2 * width}) mf.writeOn(monitor, "Nothing is being played.", nil, nil, {font="Dogica",anchorHor="center",anchorVer="center", wrapWidth = 2 * width})
return return
end end
@ -41,6 +102,7 @@ local function doLastfm(track)
local album = track["album"]["#text"] local album = track["album"]["#text"]
local title = track["name"] local title = track["name"]
os.queueEvent('audio', title, artist)
print('Displaying ' .. artist .. ' - ' .. title .. ' (on ' .. album ..')') print('Displaying ' .. artist .. ' - ' .. title .. ' (on ' .. album ..')')
local image = track["image"][1]["#text"] local image = track["image"][1]["#text"]
@ -72,6 +134,7 @@ local function doLastfm(track)
local _, b = mf.writeOn(monitor, string.sub(title, 1, 14), nil, nil, {font="Dogica", dy = 8, anchorHor = "center", wrapWidth = 2 * width}) local _, b = mf.writeOn(monitor, string.sub(title, 1, 14), nil, nil, {font="Dogica", dy = 8, anchorHor = "center", wrapWidth = 2 * width})
mf.writeOn(monitor, string.sub(artist, 1, 15), nil, nil, {font="Dogica", dy = 11+b, anchorHor = "center", wrapWidth = 2 * width}) mf.writeOn(monitor, string.sub(artist, 1, 15), nil, nil, {font="Dogica", dy = 11+b, anchorHor = "center", wrapWidth = 2 * width})
end end
local lastTrack = nil; local lastTrack = nil;
@ -95,10 +158,24 @@ local function checkTrack()
doLastfm(track) doLastfm(track)
end end
while true do local function updateLastfm()
while true do
event, id = os.pullEvent("timer") event, id = os.pullEvent("timer")
if timer == id then if timer == id then
checkTrack() checkTrack()
timer = os.startTimer(1) timer = os.startTimer(1)
end end
end
end end
local function playAudioEvent()
while true do
event, title, artist = os.pullEvent("audio")
parallel.waitForAll(function ()
playAudio(title, artist)
end)
end
end
parallel.waitForAll(updateLastfm, playAudioEvent)