Merge pull request #65 from martinmch/master

Fix formatting and make script more unix compliant
This commit is contained in:
Siddharth Dushantha 2022-10-18 22:42:27 +02:00 committed by GitHub
commit 9a3d952a66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 115 additions and 109 deletions

74
kunst
View File

@ -9,14 +9,16 @@
VERSION=1.3.4
COVER=/tmp/kunst.jpg
MUSIC_DIR=~/Music
SIZE=250x250
POSITION="+0+0"
ONLINE_ALBUM_ART=false
MUSIC_DIR="${KUNST_MUSIC_DIR:-~/Music}"
SIZE="${KUNST_SIZE:-250x250}"
POSITION="${KUNST_POSITION:-+0+0}"
ONLINE_ALBUM_ART=""
PROG_NAME=$(basename "$0")
show_help() {
printf "%s" "\
usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--silent] [--version]
printf "%s" "\
usage: kunst [-h|--help] [--size \"px\"] [--position \"+x+y\"]
[--music_dir \"path/to/dir\"] [--verbose] [--version]
┬┌─┬ ┬┌┐┌┌─┐┌┬┐
├┴┐│ ││││└─┐ │
@ -28,15 +30,24 @@ 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() {
[ "$VERBOSE" ] && 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 -- "$@")
options=$(getopt -o h --long 'position:,size:,music_dir:,version,force-online,verbose,help' -- "$@")
eval set -- "$options"
while true; do
@ -64,8 +75,8 @@ while true; do
--force-online)
ONLINE_ALBUM_ART=true
;;
--silent)
SILENT=true
--verbose)
verbose=true
;;
--)
shift
@ -90,7 +101,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
}
@ -112,15 +123,18 @@ 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
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"
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
@ -129,9 +143,9 @@ 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
[ ! "$SILENT" ] && echo "kunst: getting cover from internet"
# 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
fi
@ -145,11 +159,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
@ -158,13 +172,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
}
@ -175,7 +189,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
}
@ -195,15 +209,10 @@ 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
[ "$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
@ -220,10 +229,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
@ -241,7 +247,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
}