Skip to content

Commit

Permalink
Upd: use new mympd_state.current_song
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Sep 5, 2024
1 parent 576cb07 commit cf70991
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 110 deletions.
8 changes: 4 additions & 4 deletions Jukebox/JukeboxBlissify.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- {"name": "JukeboxBlissify", "file": "Jukebox/JukeboxBlissify.lua", "version": 6, "desc": "Uses blissify-rs to populate the jukebox queue.", "order":0,"arguments":["addToQueue"]}
-- {"name": "JukeboxBlissify", "file": "Jukebox/JukeboxBlissify.lua", "version": 7, "desc": "Uses blissify-rs to populate the jukebox queue.", "order":0,"arguments":["addToQueue"]}
local blissify_path = mympd_env.var_blissify_path
local blissify_config = ""
if mympd_env.var_blissify_config ~= nil and
Expand Down Expand Up @@ -39,9 +39,9 @@ end

if last_song == nil then
-- fallback to playing song
rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc == 0 and result.uri then
last_song = result.uri
if mympd_state.current_song ~= nil
then
last_song = mympd_state.current_song.uri
end
end

Expand Down
38 changes: 17 additions & 21 deletions ListenBrainz/ListenBrainzPlayer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- {"name": "ListenBrainzPlayer", "file": "ListenBrainz/ListenBrainzPlayer.lua", "version": 1, "desc": "Sends the now playing info to ListenBrainz.", "order":0, "arguments":[]}
-- {"name": "ListenBrainzPlayer", "file": "ListenBrainz/ListenBrainzPlayer.lua", "version": 2, "desc": "Sends the now playing info to ListenBrainz.", "order":0, "arguments":[]}
if mympd_env.var_listenbrainz_token == nil then
return "No ListenBrainz token set"
end
Expand All @@ -9,35 +9,32 @@ local extra_headers = "Content-type: application/json\r\n"..

mympd.init()

local play_state = mympd_state.play_state
local elapsed_time = mympd_state.elapsed_time

if play_state ~= 2 or elapsed_time > 5 then
if mympd_state.play_state ~= 2 or
mympd_state.elapsed_time > 5
then
return
end

local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return
end

if result.webradio or
string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.uri, 1, 7) == "http://"
if string.sub(mympd_state.current_song.uri, 1, 8) == "https://" or
string.sub(mympd_state.current_song.uri, 1, 7) == "http://"
then
return
end

local artist_mbids = {}
if result.MUSICBRAINZ_ARTISTID ~= nil then
for _, v in pairs(result["MUSICBRAINZ_ARTISTID"]) do
if mympd_state.current_song.MUSICBRAINZ_ARTISTID ~= nil then
for _, v in pairs(mympd_state.current_song["MUSICBRAINZ_ARTISTID"]) do
if v ~= "" then
artist_mbids[#artist_mbids + 1] = v
end
end
end
if result.MUSICBRAINZ_ALBUMARTISTID ~= nil then
for _, v in pairs(result.MUSICBRAINZ_ALBUMARTISTID) do
if mympd_state.current_song.MUSICBRAINZ_ALBUMARTISTID ~= nil then
for _, v in pairs(mympd_state.current_song.MUSICBRAINZ_ALBUMARTISTID) do
if v ~= "" then
artist_mbids[#artist_mbids + 1] = v
end
Expand All @@ -48,18 +45,17 @@ local payload = json.encode({
payload = {{
track_metadata = {
additional_info = {
release_mbid = result.MUSICBRAINZ_RELEASETRACKID,
recording_mbid = result.MUSICBRAINZ_TRACKID,
release_mbid = mympd_state.current_song.MUSICBRAINZ_RELEASETRACKID,
recording_mbid = mympd_state.current_song.MUSICBRAINZ_TRACKID,
artist_mbids = artist_mbids
},
artist_name = result.Artist[1],
track_name = result.Title,
release_name = result.Album
artist_name = mympd_state.current_song.Artist[1],
track_name = mympd_state.current_song.Title,
release_name = mympd_state.current_song.Album
}
}}
});
local code, header, body
rc, code, header, body = mympd.http_client("POST", uri, extra_headers, payload)
local rc, code, header, body = mympd.http_client("POST", uri, extra_headers, payload)
if rc > 0 then
return body
end
33 changes: 15 additions & 18 deletions ListenBrainz/ListenBrainzScrobbler.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- {"name": "ListenBrainzScrobbler", "file": "ListenBrainz/ListenBrainzScrobbler.lua", "version": 1, "desc": "Scrobbles songs to ListenBrainz.", "order":0, "arguments":[]}
-- {"name": "ListenBrainzScrobbler", "file": "ListenBrainz/ListenBrainzScrobbler.lua", "version": 2, "desc": "Scrobbles songs to ListenBrainz.", "order":0, "arguments":[]}
if mympd_env.var_listenbrainz_token == nil then
return "No ListenBrainz token set"
end
Expand All @@ -7,28 +7,26 @@ local uri = "https://api.listenbrainz.org/1/submit-listens"
local extra_headers = "Content-type: application/json\r\n"..
"Authorization: Token " .. mympd_env.var_listenbrainz_token .. "\r\n"

local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return
end

if result.webradio or
string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.uri, 1, 7) == "http://"
if string.sub(mympd_state.current_song.uri, 1, 8) == "https://" or
string.sub(mympd_state.current_song.uri, 1, 7) == "http://"
then
return
end

local artist_mbids = {}
if result.MUSICBRAINZ_ARTISTID ~= nil then
for _, v in pairs(result["MUSICBRAINZ_ARTISTID"]) do
if mympd_state.current_song.MUSICBRAINZ_ARTISTID ~= nil then
for _, v in pairs(mympd_state.current_song["MUSICBRAINZ_ARTISTID"]) do
if v ~= "" then
artist_mbids[#artist_mbids + 1] = v
end
end
end
if result.MUSICBRAINZ_ALBUMARTISTID ~= nil then
for _, v in pairs(result.MUSICBRAINZ_ALBUMARTISTID) do
if mympd_state.current_song.MUSICBRAINZ_ALBUMARTISTID ~= nil then
for _, v in pairs(mympd_state.current_song.MUSICBRAINZ_ALBUMARTISTID) do
if v ~= "" then
artist_mbids[#artist_mbids + 1] = v
end
Expand All @@ -37,21 +35,20 @@ end
local payload = json.encode({
listen_type = "single",
payload = {{
listened_at = result.startTime,
listened_at = mympd_state.start_time,
track_metadata = {
additional_info = {
release_mbid = result.MUSICBRAINZ_RELEASETRACKID,
recording_mbid = result.MUSICBRAINZ_TRACKID,
release_mbid = mympd_state.current_song.MUSICBRAINZ_RELEASETRACKID,
recording_mbid = mympd_state.current_song.MUSICBRAINZ_TRACKID,
artist_mbids = artist_mbids
},
artist_name = result.Artist[1],
track_name = result.Title,
release_name = result.Album
artist_name = mympd_state.current_song.Artist[1],
track_name = mympd_state.current_song.Title,
release_name = mympd_state.current_song.Album
}
}}
});
local code, header, body
rc, code, header, body = mympd.http_client("POST", uri, extra_headers, payload)
local rc, code, header, body = mympd.http_client("POST", uri, extra_headers, payload)
if rc > 0 then
return body
end
27 changes: 12 additions & 15 deletions Maloja/MalojaScrobbler.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- {"name": "MalojaScrobbler", "file": "Maloja/MalojaScrobbler.lua", "version": 1, "desc": "Scrobbles songs to your Maloja server.", "order":0, "arguments":[]}
-- {"name": "MalojaScrobbler", "file": "Maloja/MalojaScrobbler.lua", "version": 2, "desc": "Scrobbles songs to your Maloja server.", "order":0, "arguments":[]}
if mympd_env.var_maloja_token == nil then
return "No Maloja token set"
end
Expand All @@ -7,14 +7,12 @@ if mympd_env.var_maloja_uri == nil then
return "No Maloja URI set"
end

local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return
end

if result.webradio or
string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.uri, 1, 7) == "http://"
if string.sub(mympd_state.current_song.uri, 1, 8) == "https://" or
string.sub(mympd_state.current_song.uri, 1, 7) == "http://"
then
return
end
Expand All @@ -23,20 +21,19 @@ local uri = mympd_env.var_maloja_host .. "/apis/mlj_1/newscrobble?key=" .. mympd
local extra_headers = "Content-type: application/json\r\n"

local payload = json.encode({
artists = result.Artist,
title = result.Title,
album = result.Album,
time = result.startTime
artists = mympd_state.current_song.Artist,
title = mympd_state.current_song.Title,
album = mympd_state.current_song.Album,
time = mympd_state.start_time
});

if result.AlbumArtist ~= nil and
#result.AlbumArtist > 0
if mympd_state.current_song.AlbumArtist ~= nil and
#mympd_state.current_song.AlbumArtist > 0
then
payload.albumartists = result.AlbumArtist
payload.albumartists = mympd_state.current_song.AlbumArtist
end

local code, headers, body
rc, code, headers, body = mympd.http_client("POST", uri, extra_headers, payload)
local rc, code, headers, body = mympd.http_client("POST", uri, extra_headers, payload)
if rc > 0 then
return body
end
16 changes: 8 additions & 8 deletions Playcounts/TagPlaycounts.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
-- {"name": "TagPlaycounts", "file": "Playcounts/TagPlaycounts.lua", "version": 1, "desc": "Sets playcounts for tags.", "order":0, "arguments":["tags"]}
local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
-- {"name": "TagPlaycounts", "file": "Playcounts/TagPlaycounts.lua", "version": 2, "desc": "Sets playcounts for tags.", "order":0, "arguments":["tags"]}
mympd.init()

if mympd_state.current_song == nil then
return
end

if result.webradio or
string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.uri, 1, 7) == "http://"
if string.sub(mympd_state.current_song.uri, 1, 8) == "https://" or
string.sub(mympd_state.current_song.uri, 1, 7) == "http://"
then
return
end

for tag in string.gmatch(mympd_arguments.tags, "[^,]+") do
if result[tag] and result[tag][1]
if mympd_state.current_song[tag] and mympd_state.current_song[tag][1]
then
mympd.api("MYMPD_API_STICKER_INC", {
uri = result[tag][1],
uri = mympd_state.current_song[tag][1],
type = tag,
name = "playCount"
})
Expand Down
68 changes: 24 additions & 44 deletions last.fm/lastfm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,82 +45,67 @@ if mympd_arguments.trigger == "player" then
return "Now Playing: Not Playing"
end

local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return "Now Playing: Not Playing"
end

if result.webradio or
string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.uri, 1, 7) == "http://" then
if string.sub(mympd_state.current_song.uri, 1, 8) == "https://" or
string.sub(mympd_state.current_song.uri, 1, 7) == "http://" then
return "webradio"
end

local artist = result.Artist[1]
local title = result.Title
local album = result.Album
local albumArtist = result.AlbumArtist[1]

local data = {
method = "track.updateNowPlaying",
api_key = mympd_env.var_lastfm_api_key,
track = title,
artist = artist,
album = album,
albumArtist = albumArtist,
track = mympd_state.current_song.Title,
artist = mympd_state.current_song.Artist[1],
album = mympd_state.current_song.Album,
albumArtist = mympd_state.current_song.AlbumArtist[1],
sk = mympd_env.var_lastfm_session_key,
}

local body
rc, body = sendData(data)
local rc, body = sendData(data)
if rc ~= 0 then
return "Now Playing: Error"
end

local ret = json.decode(body)
local code = ret.nowplaying.ignoredMessage.code
if code ~= "0" then
return "Now Playing: " .. artist .. " - " .. title .. " - ignored code " .. code
return "Now Playing: " .. mympd_state.current_song.Artist[1] .. " - " .. mympd_state.current_song.Title .. " - ignored code " .. code
end

return "Now Playing: " .. artist .. " - " .. title .. " - OK"
return "Now Playing: " .. mympd_state.current_song.Artist[1] .. " - " .. mympd_state.current_song.Title .. " - OK"
end

if mympd_arguments.trigger == "scrobble" then
local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return "Scrobble: Not playing"
end

local artist = result.Artist[1]
local title = result.Title
local album = result.Album
local albumArtist = result.AlbumArtist[1]

local data = {
method = "track.scrobble",
api_key = mympd_env.var_lastfm_api_key,
timestamp = tostring(os.time()-mympd_state.elapsed_time),
track = title,
artist = artist,
album = album,
albumArtist = albumArtist,
timestamp = tostring(mympd_state.start_time),
track = mympd_state.current_song.Title,
artist = mympd_state.current_song.Artist[1],
album = mympd_state.current_song.Album,
albumArtist = mympd_state.current_song.AlbumArtist[1],
sk = mympd_env.var_lastfm_session_key,
}

local body
rc, body = sendData(data)
local rc, body = sendData(data)
if rc ~= 0 then
return "Scrobble: Error"
end

local ret = json.decode(body)
local code = ret.scrobbles.scrobble.ignoredMessage.code
if code ~= "0" then
return "Scrobble: " .. artist .. " - " .. title .. " ignored, code " .. code
return "Scrobble: " .. mympd_state.current_song.Artist[1] .. " - " .. mympd_state.current_song.Title .. " ignored, code " .. code
end

return "Scrobble: " .. artist .. " - " .. title .. " OK"
return "Scrobble: " .. mympd_state.current_song.Artist[1] .. " - " .. mympd_state.current_song.Title .. " OK"
end

if mympd_arguments.trigger == "feedback" then
Expand All @@ -134,29 +119,24 @@ if mympd_arguments.trigger == "feedback" then
end
end

local rc, result = mympd.api("MYMPD_API_PLAYER_CURRENT_SONG")
if rc ~= 0 then
if mympd_state.current_song == nil then
return "Feedback: Not playing"
end

local artist = result.Artist[1]
local title = result.Title

local data = {
method = "track.love",
api_key = mympd_env.var_lastfm_api_key,
track = title,
artist = artist,
track = mympd_state.current_song.Title,
artist = mympd_state.current_song.Artist[1],
sk = mympd_env.var_lastfm_session_key,
}

local body
rc, body = sendData(data)
local rc, body = sendData(data)
if rc ~= 0 then
return "Feedback: Error"
end

return "Feedback: " .. artist .. " - " .. title .. " OK"
return "Feedback: " .. mympd_state.current_song.Artist[1] .. " - " .. mympd_state.current_song.Title .. " OK"
end

if mympd_arguments.trigger == "key" then
Expand Down

0 comments on commit cf70991

Please sign in to comment.