Update last.lua
This commit is contained in:
parent
4a068267a3
commit
ff9e5b4e24
1 changed files with 85 additions and 8 deletions
85
last.lua
85
last.lua
|
|
@ -2,6 +2,7 @@
|
|||
-- install morefonts in /
|
||||
-- also ask me for a sanjuuni.sad.ovh password or selfhost it
|
||||
-- change the last.fm username in checkTrack, and my name in doLastfm
|
||||
-- https://git.sad.ovh/sophie/convert
|
||||
|
||||
local mf = require("../morefonts")
|
||||
|
||||
|
|
@ -10,8 +11,8 @@ local function drawImage(monitor, image, dx, dy, palette)
|
|||
printError('Image passed to drawImage was nil.')
|
||||
return
|
||||
end
|
||||
|
||||
for _, frame in ipairs(image) do
|
||||
frame = image[1]
|
||||
-- for _, frame in ipairs(image) do
|
||||
for y, row in ipairs(frame) do
|
||||
monitor.setCursorPos(dx+1, dy+y)
|
||||
monitor.blit(table.unpack(row))
|
||||
|
|
@ -22,17 +23,77 @@ local function drawImage(monitor, image, dx, dy, palette)
|
|||
else monitor.setPaletteColor(2^i, c) end
|
||||
end end
|
||||
if image.animation then sleep(frame.duration or image.secondsPerFrame or 0.05) end
|
||||
end
|
||||
-- end
|
||||
monitor.setBackgroundColor(colors.black)
|
||||
monitor.setTextColor(colors.white)
|
||||
monitor.setCursorPos(1, 1)
|
||||
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)
|
||||
if not track["@attr"] then
|
||||
local monitor = peripheral.find("monitor")
|
||||
local width, height = monitor.getSize()
|
||||
monitor.clear()
|
||||
os.queueEvent('audio', '', '')
|
||||
|
||||
mf.writeOn(monitor, "Nothing is being played.", nil, nil, {font="Dogica",anchorHor="center",anchorVer="center", wrapWidth = 2 * width})
|
||||
return
|
||||
end
|
||||
|
|
@ -41,6 +102,7 @@ local function doLastfm(track)
|
|||
local album = track["album"]["#text"]
|
||||
local title = track["name"]
|
||||
|
||||
os.queueEvent('audio', title, artist)
|
||||
print('Displaying ' .. artist .. ' - ' .. title .. ' (on ' .. album ..')')
|
||||
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})
|
||||
|
||||
mf.writeOn(monitor, string.sub(artist, 1, 15), nil, nil, {font="Dogica", dy = 11+b, anchorHor = "center", wrapWidth = 2 * width})
|
||||
|
||||
end
|
||||
|
||||
local lastTrack = nil;
|
||||
|
|
@ -95,10 +158,24 @@ local function checkTrack()
|
|||
doLastfm(track)
|
||||
end
|
||||
|
||||
while true do
|
||||
local function updateLastfm()
|
||||
while true do
|
||||
event, id = os.pullEvent("timer")
|
||||
if timer == id then
|
||||
checkTrack()
|
||||
timer = os.startTimer(1)
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue