ark/users/modules/eww/scripts/music

83 lines
1.7 KiB
Plaintext
Raw Normal View History

2022-09-14 15:35:52 +03:00
#!/usr/bin/env bash
get_status() {
2022-10-04 02:08:44 +03:00
s=$1
if [ "$s" = "Playing" ]; then
echo ""
else
echo ""
fi
2022-09-14 15:35:52 +03:00
}
get_length_sec() {
2022-10-04 02:08:44 +03:00
len=$1
if [ -z "$len" ]; then
echo 0
else
bc <<< "$len / 1000000"
fi
2022-09-14 15:35:52 +03:00
}
get_length_time() {
2022-10-04 02:08:44 +03:00
len=$1
if [ -n "$len" ]; then
len=$(bc <<< "$len / 1000000 + 1")
date -d@"$len" +%M:%S
else
echo ""
fi
2022-09-14 15:35:52 +03:00
}
get_position() {
2022-10-04 02:08:44 +03:00
pos=$1
len=$2
if [ -n "$pos" ]; then
bc -l <<< "$pos / $len * 100"
else
echo 0
fi
2022-09-14 15:35:52 +03:00
}
get_position_time() {
2022-10-04 02:08:44 +03:00
pos=$1
len=$2
if [ -n "$pos" ]; then
date -d@"$(bc <<< "$pos / 1000000")" +%M:%S
else
echo ""
fi
2022-09-14 15:35:52 +03:00
}
get_cover() {
2022-10-04 02:08:44 +03:00
# COVER_URL=$1
2022-09-14 15:35:52 +03:00
mkdir -p "$XDG_CACHE_HOME/eww_covers"
cd "$XDG_CACHE_HOME/eww_covers" || exit
IMGPATH="$XDG_CACHE_HOME/eww_covers/cover_art.png"
2022-10-04 02:08:44 +03:00
playerctl -F metadata mpris:artUrl 2>/dev/null | while read -r COVER_URL; do
2022-09-14 15:35:52 +03:00
if [[ "$COVER_URL" = https* ]]; then
2022-10-04 02:08:44 +03:00
if [ ! -e "$XDG_CACHE_HOME/eww_covers/$(basename "$COVER_URL")" ]; then
wget -N "$COVER_URL" -o /dev/null
fi
2022-09-14 15:35:52 +03:00
rm "$IMGPATH"
ln -s "$(basename "$COVER_URL")" "$IMGPATH"
echo "$IMGPATH"
elif [ "$COVER_URL" = "" ]; then
echo "images/music.png"
else
echo "$COVER_URL"
fi
done
}
2022-10-04 02:08:44 +03:00
if [ "$1" = "cover" ]; then
2022-09-14 15:35:52 +03:00
get_cover
2022-10-04 02:08:44 +03:00
else
playerctl -F metadata -f '{{title}}\{{artist}}\{{status}}\{{position}}\{{mpris:length}}' 2>/dev/null | while IFS="$(printf '\\')" read -r title artist status position len; do
echo '{"artist": "'"$artist"'", "title": "'"$title"'", "status": "'"$(get_status "$status")"'", "position": "'"$(get_position "$position" "$len")"'", "position_time": "'"$(get_position_time "$position" "$len")"'", "length": "'"$(get_length_time "$len")"'"}' #, "cover": "'"$(get_cover $art)"'"
done
2022-09-14 15:35:52 +03:00
fi