diff --git a/kunst b/kunst index c1556c7..c312c41 100755 --- a/kunst +++ b/kunst @@ -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,41 +99,24 @@ 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() { - 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 "------------------------------------------" - - # display the album art using sxiv - sxiv -g 250x250 -b $COVER -N "Kunst" & + # Flag to run some commands only once in the loop + FIRST_RUN=true 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 @@ -81,6 +132,18 @@ main() { echo "kunst: swapped album art to $(mpc current)" echo "------------------------------------------" + + 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" done }