Merge pull request #8 from deepjyoti30/master

Added function to get albumcover online.
This commit is contained in:
Siddharth Dushantha 2019-02-19 14:12:23 +01:00 committed by GitHub
commit 00857bd986
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 87 additions and 24 deletions

107
kunst
View File

@ -8,6 +8,36 @@
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
# this is a base64 endcoded image which will be used if
# if the file does not have an emmbeded album art.
@ -17,6 +47,44 @@ iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
EOF
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() {
# 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
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')
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
echo "error: Cover not found online"
ARTLESS=true
else
echo "kunst: Cover found online"
curl -o $COVER -s $IMG_URL
ARTLESS=false
fi
}
update_cover() {
# extract the album art from the mp3 file and dont show the messsy
# output of ffmpeg
@ -31,18 +99,25 @@ update_cover() {
ARTLESS=false
else
echo "error: file does not have an album art"
ARTLESS=true
return
get_cover_online
fi
# resize the image to 250x250
convert $COVER -resize 250x250 $COVER
if [ $ARTLESS == false ]; then
convert $COVER -resize $DIMENSION $COVER
echo "kunst: resized album art to $DIMENSION"
fi
echo "kunst: resized album art to 250x250"
}
main() {
# Flag to run some commands only once in the loop
FIRST_RUN=true
while true; do
update_cover
if [ $ARTLESS == true ];then
@ -58,29 +133,17 @@ main() {
echo "kunst: swapped album art to $(mpc current)"
echo "------------------------------------------"
# display the album art using sxiv
sxiv -g 250x250 -b $COVER -N "Kunst" &
if [ $FIRST_RUN == true ]; then
FIRST_RUN=false
# display the album art using sxiv
sxiv -g $DIMENSION -b $COVER -N "Kunst" &
fi
while true; do
# 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"
update_cover
if [ $ARTLESS == true ];then
# change the path to COVER because the music note
# image is a png not jpg
COVER=/tmp/kunst.png
# decode the base64 encoded image and save it
# to /tmp/kunst.png
echo "$MUSIC_NOTE" | base64 --decode > $COVER
fi
echo "kunst: swapped album art to $(mpc current)"
echo "------------------------------------------"
done
}