Skip to content

Commit fb8c50f

Browse files
authored
Implement ONEVENT_COMMAND and ONEVENT_POST_ENDPOINT config options (#92)
* add ON_EVENT configuration option that controls --onevent flag * ONEVENT_COMMAND instead of ON_EVENT flag * add ONEVENT_POST_ENDPOINT config option * adjust documentation
1 parent 8e8057b commit fb8c50f

4 files changed

Lines changed: 66 additions & 0 deletions

File tree

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ ENV AUDIO_GID=""
9797
ENV PARAMETER_PRIORITY=""
9898
ENV LOG_COMMAND_LINE=""
9999

100+
ENV ONEVENT_COMMAND=""
101+
ENV ONEVENT_POST_ENDPOINT=""
102+
100103
VOLUME /data/cache
101104
VOLUME /data/system-cache
102105
VOLUME /user/config
@@ -112,6 +115,7 @@ RUN which librespot
112115
COPY app/bin/run-librespot.sh /app/bin/
113116
COPY app/bin/read-file.sh /app/bin/
114117
COPY app/bin/get-value.sh /app/bin/
118+
COPY app/bin/post-event-data.sh /app/bin/
115119

116120
RUN chmod u+x /app/bin/*.sh
117121

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ PUID||Set this value the the user which should run the application, defaults to
107107
PGID||Set this value the the user which should run the application, defaults to `1000` if not set when using the `pulseaudio` backend
108108
AUDIO_GID||Specifies the gid for the group `audio`, it is required if you want to use, e.g., the `alsa` backend in user mode. Refer to [this page](https://github.com/GioF71/squeezelite-docker/blob/main/doc/example-alsa-user-mode.md) from my squeezelite-docker repository for more details.
109109
PARAMETER_PRIORITY||Where to look for a parameter first: `env` or `file`. For example, the `credentials.txt` file compared to `SPOTIFY_USERNAME` and `SPOTIFY_PASSWORD` environment variables. Defaults to `file`, meaning that each file is considered if it exists and if it contains the required values.
110+
ONEVENT_COMMAND||Specifies the name of a user defined script/executable that will be executed whenever a player event occurs. User defined scripts must be mounted to the `/userscripts/` folder and be made executable via `chmod u+x`. Internally maps to the `--onevent` flag of `librespot`. More info about usage can be found in [librespot's player event handler](https://github.com/librespot-org/librespot/blob/dev/src/player_event_handler.rs).
111+
ONEVENT_POST_ENDPOINT||Send a `POST` request with event data to the specified endpoint URL whenever a player event occurs. Request body is `json` encoded and contains all available fields specified by the [librespot's player event handler](https://github.com/librespot-org/librespot/blob/dev/src/player_event_handler.rs). Will be ignored if `ONEVENT_COMMAND` is set.
110112
LOG_COMMAND_LINE||Set to `Y` or `y` to enable, `N` or `n` to disable. Defaults to `Y`.
111113

112114
### Volumes

app/bin/post-event-data.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# read url input parameter
4+
while [ $# -gt 0 ]; do
5+
case "$1" in
6+
--url=*)
7+
endpoint_url="${1#*=}"
8+
;;
9+
esac
10+
shift
11+
done
12+
13+
# post current event data the given endpoint URL
14+
curl -X POST \
15+
-s \
16+
-H "Content-Type: application/json" \
17+
-d '{
18+
"PLAYER_EVENT": "'"$PLAYER_EVENT"'",
19+
"TRACK_ID": "'"$TRACK_ID"'",
20+
"URI": "'"$URI"'",
21+
"NAME": "'"$NAME"'",
22+
"COVERS": "'"${COVERS//$'\n'/,}"'",
23+
"LANGUAGE": "'"$LANGUAGE"'",
24+
"DURATION_MS": "'"$DURATION_MS"'",
25+
"NAME": "'"$NAME"'",
26+
"IS_EXPLICIT": "'"$IS_EXPLICIT"'",
27+
"ITEM_TYPE": "'"$ITEM_TYPE"'",
28+
"ARTISTS": "'"$ARTISTS"'",
29+
"ALBUM_ARTISTS": "'"$ALBUM_ARTISTS"'",
30+
"ALBUM": "'"$ALBUM"'",
31+
"NUMBER": "'"$NUMBER"'",
32+
"DISC_NUMBER": "'"$DISC_NUMBER"'",
33+
"DESCRIPTION": "'"$DESCRIPTION"'",
34+
"PUBLISH_TIME": "'"$PUBLISH_TIME"'",
35+
"SHOW_NAME": "'"$SHOW_NAME"'",
36+
"POSITION_MS": "'"$POSITION_MS"'",
37+
"CONNECTION_ID": "'"$CONNECTION_ID"'",
38+
"USER_NAME": "'"$USER_NAME"'",
39+
"CLIENT_ID": "'"$CLIENT_ID"'",
40+
"CLIENT_NAME": "'"$CLIENT_NAME"'",
41+
"CLIENT_BRAND_NAME": "'"$CLIENT_BRAND_NAME"'",
42+
"CLIENT_MODEL_NAME": "'"$CLIENT_MODEL_NAME"'",
43+
"VOLUME": "'"$VOLUME"'",
44+
"SHUFFLE": "'"$SHUFFLE"'",
45+
"REPEAT": "'"$REPEAT"'",
46+
"AUTO_PLAY": "'"$AUTO_PLAY"'",
47+
"FILTER": "'"$FILTER"'",
48+
"SINK_STATUS": "'"$SINK_STATUS"'"
49+
}' \
50+
$endpoint_url
51+

app/bin/run-librespot.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ if [ "${PASSTHROUGH^^}" = "Y" ]; then
286286
CMD_LINE="$CMD_LINE --passthrough"
287287
fi
288288

289+
# parse onevent related config options
290+
# if both onevent options are set,
291+
# the user defined script takes precedence
292+
if [[ -n "$ONEVENT_COMMAND" ]]; then
293+
CMD_LINE="$CMD_LINE --onevent '/userscripts/$ONEVENT_COMMAND'"
294+
elif [[ -n "$ONEVENT_POST_ENDPOINT" ]]; then
295+
CMD_LINE="$CMD_LINE --onevent '/app/bin/post-event-data.sh --url=$ONEVENT_POST_ENDPOINT'"
296+
fi
297+
289298
if [[ -z "${LOG_COMMAND_LINE}" || "${LOG_COMMAND_LINE^^}" = "Y" ]]; then
290299
ur=$(printf '*%.0s' $(seq 1 ${#SPOTIFY_USERNAME}))
291300
pr=$(printf '*%.0s' $(seq 1 ${#SPOTIFY_PASSWORD}))

0 commit comments

Comments
 (0)