From 83ad18b0eb50319abf4180fa41a258ef54608396 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 10:40:25 +0200 Subject: [PATCH 1/8] Retabularize script The script had an ugly mix of spaces and tabs. Now it's only using spaces. --- kunst | 152 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/kunst b/kunst index f133e89..7de826d 100755 --- a/kunst +++ b/kunst @@ -54,13 +54,13 @@ while true; do MUSIC_DIR=$1 ;; -h|--help) - show_help - exit + show_help + exit + ;; + --version) + echo $VERSION + exit ;; - --version) - echo $VERSION - exit - ;; --force-online) ONLINE_ALBUM_ART=true ;; @@ -84,46 +84,46 @@ EOF is_connected() { - # Check if internet is connected. We are using api.deezer.com to test - # if the internet is connected because if api.deezer.com is down or - # the internet is not connected this script will work as expected - if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then - connected=true - else + # Check if internet is connected. We are using api.deezer.com to test + # if the internet is connected because if api.deezer.com is down or + # the internet is not connected this script will work as expected + if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then + connected=true + else [ ! "$SILENT" ] && echo "kunst: unable to check online for the album art" - connected=false - fi + connected=false + fi } get_cover_online() { - # Check if connected to internet - is_connected + # Check if connected to internet + is_connected - if [ "$connected" == false ];then - ARTLESS=true - return - fi + if [ "$connected" == false ];then + ARTLESS=true + return + fi # If the current playing song ends with .mp3 or something similar, remove # it before searching for the album art because including the file extension # reduces the chance of good results in the search query QUERY=$(mpc current | sed 's/\.[^.]*$//' | iconv -t ascii//TRANSLIT -f utf8) - # Try to get the album cover online from api.deezer.com - API_URL="https://api.deezer.com/search/autocomplete?q=$QUERY" && API_URL=${API_URL//' '/'%20'} + # Try to get the album cover online from api.deezer.com + API_URL="https://api.deezer.com/search/autocomplete?q=$QUERY" && API_URL=${API_URL//' '/'%20'} echo $API_URL - # Extract the albumcover from the json returned - IMG_URL=$(curl -s "$API_URL" | jq -r '.playlists.data[0].picture_big') + # 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 + if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then [ ! "$SILENT" ] && echo "error: cover not found online" - ARTLESS=true - else + ARTLESS=true + else [ ! "$SILENT" ] && echo "kunst: cover found online" - curl -o "$COVER" -s "$IMG_URL" - ARTLESS=false - fi + curl -o "$COVER" -s "$IMG_URL" + ARTLESS=false + fi } @@ -132,27 +132,27 @@ find_album_art(){ # regardless if the curent song has an embedded album art or not if [ "$ONLINE_ALBUM_ART" == true ];then [ ! "$SILENT" ] && echo "kunst: getting cover from internet" - get_cover_online + get_cover_online return fi - # 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 + # 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 - # Get the status of the previous command - STATUS=$? + # Get the status of the previous command + STATUS=$? - # Check if the file has a embbeded album art - if [ "$STATUS" -eq 0 ];then + # Check if the file has a embbeded album art + if [ "$STATUS" -eq 0 ];then [ ! "$SILENT" ] && echo "kunst: extracted album art" - ARTLESS=false - else + ARTLESS=false + else DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")" [ ! "$SILENT" ] && echo "kunst: inspecting $DIR" - # Check if there is an album cover/art in the folder. - # Look at issue #9 for more details + # Check if there is an album cover/art in the folder. + # Look at issue #9 for more details for CANDIDATE in "$DIR/cover."{png,jpg}; do if [ -f "$CANDIDATE" ]; then STATUS=0 @@ -163,30 +163,30 @@ find_album_art(){ done fi - if [ "$STATUS" -ne 0 ];then + if [ "$STATUS" -ne 0 ];then [ ! "$SILENT" ] && echo "error: file does not have an album art" - get_cover_online - fi + get_cover_online + fi } update_cover() { find_album_art - if [ "$ARTLESS" == false ]; then - convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null + if [ "$ARTLESS" == false ]; then + convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null [ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE" - fi + fi } pre_exit() { - # Get the proccess ID of kunst and kill it. + # Get the proccess ID of kunst and kill it. # We are dumping the output of kill to /dev/null # because if the user quits sxiv before they # exit kunst, an error will be shown # from kill and we dont want that - kill -9 "$(cat /tmp/kunst.pid)" &>/dev/null + kill -9 "$(cat /tmp/kunst.pid)" &>/dev/null } @@ -204,45 +204,45 @@ main() { [ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE" [ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION" - # Flag to run some commands only once in the loop - FIRST_RUN=true + # Flag to run some commands only once in the loop + FIRST_RUN=true - while true; do - update_cover + while true; do + update_cover - if [ "$ARTLESS" == true ];then - # Dhange the path to COVER because the music note - # image is a png not jpg - COVER=/tmp/kunst.png + if [ "$ARTLESS" == true ];then + # Dhange 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 + # Decode the base64 encoded image and save it + # to /tmp/kunst.png + echo "$MUSIC_NOTE" | base64 --decode > "$COVER" + fi if [ ! "$SILENT" ];then echo "kunst: swapped album art to $(mpc current)" printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - fi - if [ "$FIRST_RUN" == true ]; then - FIRST_RUN=false + if [ "$FIRST_RUN" == true ]; then + FIRST_RUN=false - # Display the album art using sxiv - sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" & + # Display the album art using sxiv + sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" & - # Save the process ID so that we can kill - # sxiv when the user exits the script - echo $! >/tmp/kunst.pid - fi + # Save the process ID so that we can kill + # sxiv when the user exits the script + echo $! >/tmp/kunst.pid + fi - # Waiting for an event from mpd; play/pause/next/previous - # this is lets kunst use less CPU :) - while true; do - mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break - done + # Waiting for an event from mpd; play/pause/next/previous + # this is lets kunst use less CPU :) + while true; do + mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break + done [ ! "$SILENT" ] && echo "kunst: received event from mpd" - done + done } # Disable CTRL-Z because if we allowed this key press, From 51fb3ec6d494f384a4465e58f6017275b2e60744 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 10:42:14 +0200 Subject: [PATCH 2/8] Use album or artist picture when downloading --- kunst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kunst b/kunst index 7de826d..d33730c 100755 --- a/kunst +++ b/kunst @@ -114,7 +114,10 @@ get_cover_online() { API_URL="https://api.deezer.com/search/autocomplete?q=$QUERY" && API_URL=${API_URL//' '/'%20'} echo $API_URL # Extract the albumcover from the json returned - IMG_URL=$(curl -s "$API_URL" | jq -r '.playlists.data[0].picture_big') + JSON=$(curl -s "$API_URL") + ALBUM=$(echo "$JSON" | jq -r '.tracks.data[0].album.picture_big' | sed 's/null//'); + ARTIST=$(echo "$JSON" | jq -r '.tracks.data[0].artist.picture_big' | sed 's/null//'); + IMG_URL="${ALBUM:-$ARTIST}" if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then [ ! "$SILENT" ] && echo "error: cover not found online" From a293b548181a67c217ef4770b2de1329e75b236f Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 10:52:33 +0200 Subject: [PATCH 3/8] Use functions for logging To prevent duplicate logic for formatting and writing out log messages, a standardized function is made for logging messages and logging errors. --- kunst | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/kunst b/kunst index d33730c..7d2989d 100755 --- a/kunst +++ b/kunst @@ -13,6 +13,7 @@ MUSIC_DIR=~/Music SIZE=250x250 POSITION="+0+0" ONLINE_ALBUM_ART=false +PROG_NAME=$(basename "$0") show_help() { printf "%s" "\ @@ -34,6 +35,15 @@ optional arguments: " } +log() { + [ ! "$SILENT" ] && echo "$PROG_NAME: $1" +} + +logError() { + echo "$PROG_NAME: $1" >&2 + exit 1 +} + # Parse the arguments options=$(getopt -o h --long position:,size:,music_dir:,version,force-online,silent,help -- "$@") @@ -90,7 +100,7 @@ is_connected() { if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then connected=true else - [ ! "$SILENT" ] && echo "kunst: unable to check online for the album art" + log "unable to check online for the album art" connected=false fi } @@ -120,10 +130,10 @@ get_cover_online() { IMG_URL="${ALBUM:-$ARTIST}" if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then - [ ! "$SILENT" ] && echo "error: cover not found online" + log "cover not found online" ARTLESS=true else - [ ! "$SILENT" ] && echo "kunst: cover found online" + log "cover found online" curl -o "$COVER" -s "$IMG_URL" ARTLESS=false fi @@ -134,7 +144,7 @@ find_album_art(){ # Check if the user wants to get the album art from the internet, # regardless if the curent song has an embedded album art or not if [ "$ONLINE_ALBUM_ART" == true ];then - [ ! "$SILENT" ] && echo "kunst: getting cover from internet" + log "getting cover from internet" get_cover_online return fi @@ -148,11 +158,11 @@ find_album_art(){ # Check if the file has a embbeded album art if [ "$STATUS" -eq 0 ];then - [ ! "$SILENT" ] && echo "kunst: extracted album art" + log "extracted album art" ARTLESS=false else DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")" - [ ! "$SILENT" ] && echo "kunst: inspecting $DIR" + log "inspecting $DIR" # Check if there is an album cover/art in the folder. # Look at issue #9 for more details @@ -161,13 +171,13 @@ find_album_art(){ STATUS=0 ARTLESS=false convert "$CANDIDATE" $COVER &> /dev/null - [ ! "$SILENT" ] && echo "kunst: found cover.png" + log "found cover.png" fi done fi if [ "$STATUS" -ne 0 ];then - [ ! "$SILENT" ] && echo "error: file does not have an album art" + log "file does not have an album art" get_cover_online fi } @@ -178,7 +188,7 @@ update_cover() { if [ "$ARTLESS" == false ]; then convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null - [ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE" + log "resized album art to $SIZE" fi } @@ -198,8 +208,7 @@ main() { dependencies=(sxiv convert bash ffmpeg mpc jq mpd) for dependency in "${dependencies[@]}"; do type -p "$dependency" &>/dev/null || { - echo "error: Could not find '${dependency}', is it installed?" >&2 - exit 1 + logError "could not find '${dependency}', is it installed?" } done @@ -223,10 +232,7 @@ main() { echo "$MUSIC_NOTE" | base64 --decode > "$COVER" fi - if [ ! "$SILENT" ];then - echo "kunst: swapped album art to $(mpc current)" - printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - - fi + log "swapped album art to $(mpc current)" if [ "$FIRST_RUN" == true ]; then FIRST_RUN=false @@ -244,7 +250,7 @@ main() { while true; do mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break done - [ ! "$SILENT" ] && echo "kunst: received event from mpd" + log "received event from mpd" done } From 19fb4c78a159f18b638483d8256f54c8739a8b08 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 10:54:07 +0200 Subject: [PATCH 4/8] Replace API_URL trace with logging. Uncertain why this was being echoed. If the intention was to trace it, this should be done using `set -x`. If the intention was to inform the user, it should be properly logged. --- kunst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kunst b/kunst index 7d2989d..b74e3ca 100755 --- a/kunst +++ b/kunst @@ -122,7 +122,7 @@ get_cover_online() { # Try to get the album cover online from api.deezer.com API_URL="https://api.deezer.com/search/autocomplete?q=$QUERY" && API_URL=${API_URL//' '/'%20'} - echo $API_URL + log "fetching $API_URL" # Extract the albumcover from the json returned JSON=$(curl -s "$API_URL") ALBUM=$(echo "$JSON" | jq -r '.tracks.data[0].album.picture_big' | sed 's/null//'); From e0a0e9ce1c01fa9409a4c067615343a2969d9f06 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 11:00:19 +0200 Subject: [PATCH 5/8] Make script silent/quiet by default It's common for unix tools to be quiet by default, and verbose by toggle. --- kunst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kunst b/kunst index b74e3ca..03558fe 100755 --- a/kunst +++ b/kunst @@ -17,7 +17,7 @@ PROG_NAME=$(basename "$0") show_help() { printf "%s" "\ -usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--silent] [--version] +usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--verbose] [--version] ┬┌─┬ ┬┌┐┌┌─┐┌┬┐ ├┴┐│ ││││└─┐ │ @@ -29,14 +29,14 @@ optional arguments: --size what size to display the album art in --position the position where the album art should be displayed --music_dir the music directory which MPD plays from - --silent dont show the output + --verbose show the output --force-online force getting cover from the internet --version show the version of kunst you are using " } log() { - [ ! "$SILENT" ] && echo "$PROG_NAME: $1" + [ "$VERBOSE" ] && echo "$PROG_NAME: $1" } logError() { @@ -46,7 +46,7 @@ logError() { # Parse the arguments -options=$(getopt -o h --long position:,size:,music_dir:,version,force-online,silent,help -- "$@") +options=$(getopt -o h --long 'position:,size:,music_dir:,version,force-online,verbose,help' -- "$@") eval set -- "$options" while true; do @@ -74,8 +74,8 @@ while true; do --force-online) ONLINE_ALBUM_ART=true ;; - --silent) - SILENT=true + --verbose) + verbose=true ;; --) shift From 866aafc8cd62ca809c98c18e5d6dc4fa9a45f03b Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 11:10:06 +0200 Subject: [PATCH 6/8] Move environment variable defaults to initial variable declaration Reduce the verbose syntax and use bash idioms instead. --- kunst | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/kunst b/kunst index 03558fe..1b11af6 100755 --- a/kunst +++ b/kunst @@ -9,9 +9,9 @@ VERSION=1.3.4 COVER=/tmp/kunst.jpg -MUSIC_DIR=~/Music -SIZE=250x250 -POSITION="+0+0" +MUSIC_DIR="${KUNST_MUSIC_DIR:-~/Music}" +SIZE="${KUNST_SIZE:-250x250}" +POSITION="${KUNST_POSITION:-+0+0}" ONLINE_ALBUM_ART=false PROG_NAME=$(basename "$0") @@ -212,10 +212,6 @@ main() { } done - [ "$KUNST_MUSIC_DIR" != "" ] && MUSIC_DIR="$KUNST_MUSIC_DIR" - [ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE" - [ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION" - # Flag to run some commands only once in the loop FIRST_RUN=true From e8f9deab4372ce220ef9f065749c72dd8d4e46f4 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 11:11:06 +0200 Subject: [PATCH 7/8] Wrap usage to fit within 80 characters --- kunst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kunst b/kunst index 1b11af6..42330a6 100755 --- a/kunst +++ b/kunst @@ -16,8 +16,9 @@ ONLINE_ALBUM_ART=false PROG_NAME=$(basename "$0") show_help() { -printf "%s" "\ -usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--verbose] [--version] + printf "%s" "\ +usage: kunst [-h|--help] [--size \"px\"] [--position \"+x+y\"] + [--music_dir \"path/to/dir\"] [--verbose] [--version] ┬┌─┬ ┬┌┐┌┌─┐┌┬┐ ├┴┐│ ││││└─┐ │ From c08c39e02d57db4a016dd31809da8fef49412f69 Mon Sep 17 00:00:00 2001 From: "M.B. Christiansen" Date: Mon, 10 Oct 2022 11:31:20 +0200 Subject: [PATCH 8/8] Use bash idiom instead of true and false string --- kunst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kunst b/kunst index 42330a6..9d0a8f4 100755 --- a/kunst +++ b/kunst @@ -12,7 +12,7 @@ COVER=/tmp/kunst.jpg MUSIC_DIR="${KUNST_MUSIC_DIR:-~/Music}" SIZE="${KUNST_SIZE:-250x250}" POSITION="${KUNST_POSITION:-+0+0}" -ONLINE_ALBUM_ART=false +ONLINE_ALBUM_ART="" PROG_NAME=$(basename "$0") show_help() { @@ -143,8 +143,8 @@ get_cover_online() { find_album_art(){ # Check if the user wants to get the album art from the internet, - # regardless if the curent song has an embedded album art or not - if [ "$ONLINE_ALBUM_ART" == true ];then + # regardless if the current song has an embedded album art or not + if [ ! -z "$ONLINE_ALBUM_ART" ]; then log "getting cover from internet" get_cover_online return