Skip to content

Commit

Permalink
Upd: import scripts from myMPD repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed May 25, 2024
1 parent 13e6db7 commit 026fa80
Show file tree
Hide file tree
Showing 16 changed files with 479 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: lint
on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: install linters
run: |
sudo apt-get update
sudo apt-get install -y lua-check
- name: lint lua scripts
run: ./lint.sh
27 changes: 27 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- these globals can be set and accessed.
globals = {
"json",
"mympd"
}

-- these globals can only be accessed.
read_globals = {
"mympd_arguments",
"mympd_env",
"mympd_state",
"mympd_api",
"mympd_http_client",
"mympd_http_download",
"mympd_util_covercache_write",
"mympd_util_notify",
"mympd_util_log",
"mympd_util_hash",
"mympd_util_urlencode",
"mympd_util_urldecode",
"mygpio_gpio_blink",
"mygpio_gpio_get",
"mygpio_gpio_set",
"mygpio_gpio_toggle"
}

unused = false
23 changes: 23 additions & 0 deletions BatteryIndicator/BatteryIndicator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- {"order":1,"arguments":[]}
local battery = mympd.os_capture("cat /sys/class/power_supply/YourBattery/capacity")
battery = tonumber(battery)

local icon = "battery_full"
for i = 6, 0, -1 do
if battery < math.floor((i * 16.7) + 0.5) then
icon = "battery_" .. i .. "_bar"
end
end

battery = battery .. "%"
mympd.api("MYMPD_API_HOME_ICON_SAVE", {
replace = true,
oldpos = 0,
name = battery,
ligature = icon,
bgcolor = "#ffffff",
color = "#000000",
image = "",
cmd = "execScriptFromOptions",
options = {"BatteryIndicator"}
})
9 changes: 9 additions & 0 deletions BatteryIndicator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Battery Indicator

The `BatteryIndicator.lua` script replaces the first icon on your homescreen with a battery level indicator that updates itself every time you click it. It makes use of the ligatures myMPD ships with and relies on your operating system reporting the battery level to a text file in `/sys`.

- Determine where said file is: most likely `/sys/class/power_supply/[…]/capacity`
- Create a new script and import `BatteryIndicator.lua`
- Copy and paste the path to your `capacity` file into line 1
- Make sure the name you gave to your new script is the same as the last attribute in the last line and save
- Create a timer that runs the script regularly.
41 changes: 41 additions & 0 deletions ListenBrainz/ListenBrainz-Feedback.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- {"order":1,"arguments":["uri","vote","type"]}
mympd.init()
if mympd_state.var_listenbrainz_token == nil then
return "No ListenBrainz token set"
end

local uri = "https://api.listenbrainz.org/1/feedback/recording-feedback"
local headers = "Content-type: application/json\r\n"..
"Authorization: Token "..mympd_state["var_listenbrainz_token"].."\r\n"

local vote
if mympd_arguments.type == "like" then
-- thumbs up/down
vote = mympd_arguments.vote - 1
else
-- stars rating
if mympd_arguments.vote > 5 then
-- treat more than 5 stars as like
vote = 1
else
-- do not send feedback to ListenBrainz
return
end
end

-- get song details
local rc, song = mympd.api("MYMPD_API_SONG_DETAILS", { uri = mympd_arguments.uri })
if rc == 0 then
local mbid = song["MUSICBRAINZ_TRACKID"]
if mbid ~= nil and mbid ~= "" then
local payload = json.encode({
recording_mbid = mbid,
score = vote
});
local code, header, body
rc, code, header, body = mympd.http_client("POST", uri, headers, payload)
if rc > 0 then
return body
end
end
end
36 changes: 36 additions & 0 deletions ListenBrainz/ListenBrainz-Pin.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- {"order":1,"arguments":["uri","blurb_content","pinned_until"]}
mympd.init()
if mympd_state.var_listenbrainz_token == nil then
return "No ListenBrainz token set"
end

local pin_uri = "https://api.listenbrainz.org/1/pin"
local unpin_uri = "https://api.listenbrainz.org/1/pin/unpin"
local headers = "Content-type: application/json\r\n"..
"Authorization: Token "..mympd_state["var_listenbrainz_token"].."\r\n"
local payload = ""
local uri = ""

if mympd_arguments.uri ~= "" then
local rc, song = mympd.api("MYMPD_API_SONG_DETAILS", {uri = mympd_arguments.uri})
if rc == 0 then
local mbid = song["MUSICBRAINZ_TRACKID"]
if mbid ~= nil then
payload = json.encode({
recording_mbid = mbid,
blurb_content = mympd_arguments.blurb_content,
pinned_until = mympd_arguments.pinned_until
});
uri = pin_uri
end
end
else
uri = unpin_uri
end

if uri ~= "" then
local rc, code, header, body = mympd.http_client("POST", uri, headers, payload)
if rc > 0 then
return body
end
end
60 changes: 60 additions & 0 deletions ListenBrainz/ListenBrainz-Scrobbler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- {"order":1,"arguments":[]}
mympd.init()
if mympd_state.var_listenbrainz_token == nil then
return "No ListenBrainz token set"
end

local uri = "https://api.listenbrainz.org/1/submit-listens"
local headers = "Content-type: application/json\r\n"..
"Authorization: Token "..mympd_state["var_listenbrainz_token"].."\r\n"

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

if result.webradio then
return
end

if string.sub(result.uri, 1, 8) == "https://" or
string.sub(result.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 v ~= "" or v == nil 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 v ~= "" or v == nil then
artist_mbids[#artist_mbids + 1] = v
end
end
end
local payload = json.encode({
listen_type = "single",
payload = {{
listened_at = result["startTime"],
track_metadata = {
additional_info = {
release_mbid = result["MUSICBRAINZ_RELEASETRACKID"],
recording_mbid = result["MUSICBRAINZ_TRACKID"],
artist_mbids = artist_mbids
},
artist_name = result["Artist"][1],
track_name = result["Title"],
release_name = result["Album"]
}
}}
});
local code, header, body
rc, code, header, body = mympd.http_client("POST", uri, headers, payload)
if rc > 0 then
return body
end
32 changes: 32 additions & 0 deletions ListenBrainz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ListenBrainz

## General

- Set your ListenBrainz API token: Scripts -> Variables
- New variable with key `var_listenbrainz_token` and your API token as value
- Enable tags in MPD and myMPD:
- MUSICBRAINZ_ALBUMARTISTID
- MUSICBRAINZ_ARTISTID
- MUSICBRAINZ_RELEASETRACKID
- MUSICBRAINZ_TRACKID

## Scrobbling

You can send your listening habits to ListenBrainz.

- Create a new script
- Import the `ListenBrainz-Scrobbler.lua` script
- Create a new trigger
- Event: `mympd_scrobble`
- Action: above script

## Feedback

You can send hate/love feedback to ListenBrainz with the thumbs up and down buttons in the playback view.

- Create a new script
- Import the `ListenBrainz-Feedback.lua` script
- Create a new trigger
- Event: `mympd_feedback`
- Action: above script
- Leave the arguments empty
14 changes: 14 additions & 0 deletions Misc/EnableJukebox.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- {"order":1,"arguments":[]}

mympd.api("MYMPD_API_PLAYER_STOP");
mympd.api("MYMPD_API_PLAYER_OPTIONS_SET", {
jukeboxMode = "song",
jukeboxPlaylist = "Database",
jukeboxQueueLength = 1,
jukeboxUniqueTag = "Title",
jukeboxLastPlayed = 24,
jukeboxIgnoreHated = true,
consume = "1"
});
mympd.api("MYMPD_API_QUEUE_CLEAR");
mympd.api("MYMPD_API_PLAYER_PLAY");
21 changes: 21 additions & 0 deletions Misc/PlayRandomPlaylist.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- {"order":1,"arguments":[]}
-- get the first 2000 playlists
local rc, result = mympd.api("MYMPD_API_PLAYLIST_LIST", {
offset = 0,
limit = 2000,
searchstr = "",
type = 0
})
-- random number
math.randomseed(os.time())
local number = math.random(1, #result.result.data)
-- get playlist by random number
local playlist = result.data[number].uri
-- play the playlist
local raw_result
rc, raw_result = mympd.api("MYMPD_API_QUEUE_REPLACE_PLAYLISTS", {
plist = { playlist },
play = true
})
-- return the playlist name
return "Playing " .. playlist
4 changes: 4 additions & 0 deletions Misc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Misc

- Enable Jukebox
- Play Random Playlist
1 change: 1 addition & 0 deletions index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"scripts":["BatteryIndicator.lua","ListenBrainz-Feedback.lua","ListenBrainz-Pin.lua","ListenBrainz-Scrobbler.lua","EnableJukebox.lua","PlayRandomPlaylist.lua","yt-dlp.lua"]}
28 changes: 28 additions & 0 deletions index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh
#
#SPDX-License-Identifier: GPL-3.0-or-later
#myMPD (c) 2018-2024 Juergen Mang <[email protected]>
#https://github.com/jcorporation/mympd

#exit on error
set -e

#exit on undefined variable
set -u

# Create index of lua scripts
rm -f "index.json"
exec 3<> "index.json"
printf "{\"scripts\":[" >&3
I=0
for F in */*.lua
do
[ "$I" -gt 0 ] && printf "," >&3
SCRIPTNAME=$(basename "$F")
printf "\"%s\"" "$SCRIPTNAME" >&3
I=$((I+1))
done
printf "]}\n" >&3
exec 3>&-

jq "." < index.json > /dev/null
13 changes: 13 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
#
#SPDX-License-Identifier: GPL-3.0-or-later
#myMPD (c) 2018-2024 Juergen Mang <[email protected]>
#https://github.com/jcorporation/mympd

#exit on error
set -e

#exit on undefined variable
set -u

luacheck */*.lua
11 changes: 11 additions & 0 deletions yt-dlp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# yt-dlp

This script extracts the audio links and the associated metadata from an YouTube link and appends the result to the MPD queue. On playback MPD calls the script again to get the real streaming uri.

The script was written by [sevmonster](https://github.com/sevmonster) - [Discussion](https://github.com/jcorporation/myMPD/discussions/1276).

## Usage

1. Install `yt-dlp`
2. Import the script `yt-dlp`
3. Start it from the main menu and copy the YouTube link in URI field.
Loading

0 comments on commit 026fa80

Please sign in to comment.