dotfiles/.local/bin/mpd-notif

60 lines
1.8 KiB
Bash
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 -- cursong prevsong
# 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
prevsong="$cursong"
cursong="$(mpc -f '[[%artist%\n|%name%\n]%title%[\n%album%]]|[%file%]' current)"
# Si le morceau a changé depuis la dernière vérification
if [[ "$cursong" != "$prevsong" ]]
then
# Extractions des informations depuis la chaîne récupérée plus
# tôt, vers un format plus lisible.
artist="$(sed -n 1p <<<"$cursong")"
title="$(sed -n 2p <<<"$cursong")"
album="$(sed -n 3p <<<"$cursong")"
# Petit trick : si le titre sest retrouvé sur la première
# ligne parce quil ny a pas dartiste renseigné, on corrige
# le contenu des variables (pas forcément fiable, mais
# chez moi ça marche™).
[[ -z "$title" ]] && title="$artist" && artist=""
# 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 [[ -z "$cursong" ]]
then
cursong="<i>Arrêt</i>"
unset artist title album
fi
# Notification du morceau
dunstify -r "$DUNST_ID" "MPD" "$cursong"
# 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}" \
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