2019-06-13 20:12:29 +02:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
export MPD_HOST="$HOME/.mpd/socket"
|
2020-01-09 21:17:01 +01:00
|
|
|
|
DUNST_ID=$(sum <<<"mpd" | cut -d' ' -f1)
|
|
|
|
|
|
2022-11-10 01:14:44 +01:00
|
|
|
|
# Déclaration des variables globales
|
2024-05-16 16:42:12 +02:00
|
|
|
|
declare -- prevfile
|
|
|
|
|
declare -a -- infos
|
2019-06-13 20:12:29 +02:00
|
|
|
|
|
2022-11-10 01:14:44 +01:00
|
|
|
|
# Tant que le service MPD fonctionne
|
|
|
|
|
while systemctl --user is-active mpd.service &>/dev/null
|
2019-06-13 20:12:29 +02:00
|
|
|
|
do
|
2022-11-10 01:14:44 +01:00
|
|
|
|
# Toutes les 5 secondes, ou dès que le morceau se termine
|
2020-01-09 21:17:01 +01:00
|
|
|
|
timeout 5 mpc -w current
|
2022-11-10 01:14:44 +01:00
|
|
|
|
|
2024-05-16 16:42:12 +02:00
|
|
|
|
mapfile -t infos < <(mpc -f '[%artist%|%name%]\n%title%\n%album%\n%file%\n%time%' current)
|
2022-11-10 01:14:44 +01:00
|
|
|
|
|
|
|
|
|
# Si le morceau a changé depuis la dernière vérification
|
2024-05-29 01:16:25 +02:00
|
|
|
|
if [[ "${infos[1]:-${infos[3]}}" != "$title" && "$title" != "<i>Arrêt</i>" ]]
|
2019-06-13 20:12:29 +02:00
|
|
|
|
then
|
2022-11-10 01:14:44 +01:00
|
|
|
|
|
2024-05-16 16:42:12 +02:00
|
|
|
|
artist="${infos[0]}"
|
|
|
|
|
title="${infos[1]:-${infos[3]}}"
|
|
|
|
|
album="${infos[2]}"
|
2024-08-02 15:25:49 +02:00
|
|
|
|
file="${infos[3]}"
|
2024-08-02 16:09:56 +02:00
|
|
|
|
[[ -n "${infos[4]}" ]] && length="$(( 10#${infos[4]%%:*} * 60 + 10#${infos[4]##*:} ))"
|
2022-11-10 02:30:19 +01:00
|
|
|
|
|
|
|
|
|
# Si on obtiens un chemin absolu, on n’affiche que le nom, pas
|
|
|
|
|
# le chemin.
|
|
|
|
|
[[ -f "/${title}" ]] && title="$(basename "$title")"
|
2022-11-10 01:14:44 +01:00
|
|
|
|
|
|
|
|
|
# Si aucune information n’est retournée, c’est que la lecture
|
|
|
|
|
# s’est arrêtée.
|
2024-05-16 16:42:12 +02:00
|
|
|
|
if [[ "${#infos[@]}" -eq 0 ]]
|
2022-11-10 01:14:44 +01:00
|
|
|
|
then
|
2024-05-16 16:42:12 +02:00
|
|
|
|
title="<i>Arrêt</i>"
|
|
|
|
|
unset artist title album length
|
2022-11-10 01:14:44 +01:00
|
|
|
|
fi
|
|
|
|
|
|
2024-08-02 15:25:49 +02:00
|
|
|
|
# Si ce n’est pas un fichier sur le disque, c’est probablement
|
|
|
|
|
# un stream, donc on ne remplace l’URL que pour les locaux.
|
|
|
|
|
[[ -f "$HOME/Musique/${file}" ]] && file="https://fichiers.breizh.pm/Musique/${file}"
|
|
|
|
|
|
2022-11-10 01:14:44 +01:00
|
|
|
|
# Notification du morceau
|
2024-05-31 21:31:45 +02:00
|
|
|
|
dunstify -i mpd -r "$DUNST_ID" "MPD" "${artist:+${artist}$'\n'}${title}"$'\n'"${album}"
|
2022-11-10 01:14:44 +01:00
|
|
|
|
|
|
|
|
|
# Ajout au scrobbler Pleroma
|
2024-05-16 16:42:12 +02:00
|
|
|
|
curl -s -X POST -u "breizh:$(pass show Web/pleroma.breizh.pm)" \
|
|
|
|
|
--data-urlencode "title=${title}" \
|
|
|
|
|
--data-urlencode "artist=${artist}" \
|
|
|
|
|
--data-urlencode "album=${album}" \
|
2024-08-02 15:25:49 +02:00
|
|
|
|
--data-urlencode "externalLink=${file}" \
|
2024-05-29 01:16:25 +02:00
|
|
|
|
${length:+--data-urlencode "length=${length}"} \
|
2024-05-16 16:42:12 +02:00
|
|
|
|
https://pleroma.breizh.pm/api/v1/pleroma/scrobble &>/dev/null
|
2019-06-13 20:12:29 +02:00
|
|
|
|
fi
|
|
|
|
|
done
|
2020-01-09 21:17:01 +01:00
|
|
|
|
|
2022-11-10 01:14:44 +01:00
|
|
|
|
# Si on sort de la boucle c’est que MPD est arrêté, c’est pas normal alors on
|
|
|
|
|
# sort en erreur.
|
|
|
|
|
exit 1
|