kunst/kunst

152 lines
4.2 KiB
Plaintext
Raw Normal View History

2019-02-14 11:17:16 +00:00
#!/usr/bin/env bash
2019-02-14 08:11:30 +00:00
# ┬┌─┬ ┬┌┐┌┌─┐┌┬┐
2019-02-17 12:05:53 +00:00
# ├┴┐│ ││││└─┐ │
# ┴ ┴└─┘┘└┘└─┘ ┴
2019-02-14 08:11:30 +00:00
# Created by Siddharth Dushantha
2019-02-13 09:33:48 +00:00
2019-02-16 19:33:47 +00:00
# VERSION=1.0
2019-02-13 09:33:48 +00:00
COVER=/tmp/kunst.jpg
MUSIC_DIR=~/Music/
DIMENSION=250x250
# Parse the arguments
options=$(getopt -o d:D:h --long dimension:,DIR:,help -- "$@")
eval set -- "$options"
while true; do
case "$1" in
-d|--dimension)
shift;
DIMENSION=$1
;;
-D|--DIR)
shift;
MUSIC_DIR=$1
;;
-h|--help)
echo "kunst"
echo " -d, --dimension: Pass the dimension for the image.(Default: $DIMENSION)"
echo " -D, --DIR: Pass the Music directory that MPD plays from. (Default: $MUSIC_DIR)"
echo " -h, --help: Show this message and exit."
exit
;;
--)
shift
break
;;
esac
shift
done
2019-02-13 09:33:48 +00:00
2019-02-14 11:24:18 +00:00
# this is a base64 endcoded image which will be used if
# if the file does not have an emmbeded album art.
# the image is a image of a music note
read -d '' MUSIC_NOTE << EOF
iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJESURBVGhD7Zg/axRRFMVXAtpYphEVREKClnHfJI0MmReSfAC3tRejhaBgo70fwN7aD2BvEU0gfztbu5AqMxNjoVnvG87KZXy7z5m5dxLI/OCw8Pade+7M3n3Dbq+jo6OjY8RwMJhKk+hhlph3eRJ9w/LF5jCOr1PTj6jpD7mNjkjDkbDl4vFjpX87teZJlkSfSD9501zYfv5QJ1fyZHGexuJtZs12ZqMzX8NlwX4+nK3NXMutWaOm39Nd/u5rMCSUao80fjBNwY+p8Y+krNxQVaGsLsfWzFLYS2r4M30Rf5WbaCJE6OILlhIidPEFSwkRuviCpYQIXXzB1WX26bR6ky4v3OPriNCFB1YRHa079Pr6eKk/h1IFfA+WdOGBk+QeXtT0Ft3pV6e2fxf2f+AeLOnCA8tC0xv09H1xGi/cgWUi3I8lXXigEzX8u3gmWPP8JI5uYdt/w2thSRceSM0/zVfnb+CtWvB6WNJFOlC6XhDpQOl6QaQDpesFkQ6UrhdEOlC6XpA6gcPB/avumKXnxCadXHkha766tTr1GlE18CRZvEmN7nHfOMGiS5XA4mdmYg64Z5Jg06VKYHlEQoKtOVIz6zx8f0iwNUNyZt2F+3zjBFt9pGe22gWYFLb6lEckJNjGUmWEssR8ga0+0jNL9Z75fD7Rp7UOW32kZxb/1u37vFyUu+sODtjqozGzxaFADfprFM3vuD3Y3gytmf17LJPHXbgTNb5BWhe58yNan1lpWp9ZDVqdWS1am9mOjo7LRq/3B1ESKyYUVquzAAAAAElFTkSuQmCC
EOF
2019-02-13 09:33:48 +00:00
2019-02-14 05:21:15 +00:00
2019-02-19 05:51:14 +00:00
is_connected() {
# Check if internet is connected
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
connected=true
else
echo "kunst: Can't check online for the cover"
connected=false
fi
}
get_cover_online() {
2019-02-19 05:51:14 +00:00
# Check if connected to internet
is_connected
if [ $connected == false ];then
ARTLESS=true
return
fi
# Try to get the album cover online from api.deezer.com
2019-02-19 08:33:47 +00:00
API_URL="http://api.deezer.com/search/autocomplete?q=$(mpc current)" && API_URL=${API_URL//' '/'%20'}
# extract the albumcover from the json returned
IMG_URL=$(curl -s "$API_URL" | jq -r '.playlists.data[0] | .picture_big')
2019-02-18 20:11:11 +00:00
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
echo "error: Cover not found online"
ARTLESS=true
else
echo "kunst: Cover found online"
2019-02-19 05:41:28 +00:00
curl -o $COVER -s $IMG_URL
ARTLESS=false
fi
}
2019-02-13 09:33:48 +00:00
update_cover() {
# extract the album art from the mp3 file and dont show the messsy
# output of ffmpeg
ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" $COVER -y &> /dev/null
2019-02-14 05:21:15 +00:00
2019-02-17 12:05:53 +00:00
# get the status of the previous command
STATUS=$?
2019-02-14 05:21:15 +00:00
2019-02-17 12:05:53 +00:00
# check if the file has a embbeded album art
if [ $STATUS -eq 0 ];then
echo "kunst: extracted album art"
ARTLESS=false
else
2019-02-17 12:05:53 +00:00
echo "error: file does not have an album art"
get_cover_online
fi
2019-02-13 09:33:48 +00:00
# resize the image to 250x250
2019-02-18 20:20:24 +00:00
if [ $ARTLESS == false ]; then
convert $COVER -resize $DIMENSION $COVER
echo "kunst: resized album art to $DIMENSION"
2019-02-18 20:20:24 +00:00
fi
2019-02-13 09:33:48 +00:00
}
main() {
2019-02-19 12:58:32 +00:00
# Flag to run some commands only once in the loop
FIRST_RUN=true
2019-02-13 09:33:48 +00:00
while true; do
update_cover
2019-02-13 09:33:48 +00:00
if [ $ARTLESS == true ];then
2019-02-14 05:21:15 +00:00
# change the path to COVER because the music note
# image is a png not jpg
COVER=/tmp/kunst.png
2019-02-14 05:21:15 +00:00
# decode the base64 encoded image and save it
# to /tmp/kunst.png
2019-02-16 19:33:47 +00:00
echo "$MUSIC_NOTE" | base64 --decode > $COVER
2019-02-13 09:33:48 +00:00
fi
echo "kunst: swapped album art to $(mpc current)"
2019-02-18 17:50:48 +00:00
echo "------------------------------------------"
2019-02-19 12:58:32 +00:00
if [ $FIRST_RUN == true ]; then
FIRST_RUN=false
# display the album art using sxiv
sxiv -g $DIMENSION -b $COVER -N "Kunst" &
fi
# waiting for an event from mpd; play/pause/next/previous
# this is lets kunst use less CPU :)
mpc idle &> /dev/null
echo "kunst: received event from mpd"
2019-02-13 09:33:48 +00:00
done
}
main