dotfiles/.local/bin/mpd-notif

54 lines
1.6 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
export MPD_HOST="$HOME/.mpd/socket"
DUNST_ID=$(sum <<<"mpd" | cut -d' ' -f1)
# Déclaration des variables globales
declare -- prevfile
declare -a -- infos
# Tant que le service MPD fonctionne
while systemctl --user is-active mpd.service &>/dev/null
do
# Toutes les 5 secondes, ou dès que le morceau se termine
timeout 5 mpc -w current
mapfile -t infos < <(mpc -f '[%artist%|%name%]\n%title%\n%album%\n%file%\n%time%' current)
# Si le morceau a changé depuis la dernière vérification
if [[ "${infos[1]:-${infos[3]}}" != "$title" && "$title" != "<i>Arrêt</i>" ]]
then
artist="${infos[0]}"
title="${infos[1]:-${infos[3]}}"
album="${infos[2]}"
[[ -n "${infos[4]}" ]] && length="$(( ${infos[4]%%:*} * 60 + ${infos[4]##*:} ))"
# Si on obtiens un chemin absolu, on naffiche que le nom, pas
# le chemin.
[[ -f "/${title}" ]] && title="$(basename "$title")"
# Si aucune information nest retournée, cest que la lecture
# sest arrêtée.
if [[ "${#infos[@]}" -eq 0 ]]
then
title="<i>Arrêt</i>"
unset artist title album length
fi
# Notification du morceau
dunstify -i mpd -r "$DUNST_ID" "MPD" "${artist:+${artist}$'\n'}${title}"$'\n'"${album}"
# Ajout au scrobbler Pleroma
curl -s -X POST -u "breizh:$(pass show Web/pleroma.breizh.pm)" \
--data-urlencode "title=${title}" \
--data-urlencode "artist=${artist}" \
--data-urlencode "album=${album}" \
${length:+--data-urlencode "length=${length}"} \
https://pleroma.breizh.pm/api/v1/pleroma/scrobble &>/dev/null
fi
done
# Si on sort de la boucle cest que MPD est arrêté, cest pas normal alors on
# sort en erreur.
exit 1