Merge pull request #65 from martinmch/master
Fix formatting and make script more unix compliant
This commit is contained in:
commit
9a3d952a66
224
kunst
224
kunst
|
@ -9,14 +9,16 @@
|
||||||
|
|
||||||
VERSION=1.3.4
|
VERSION=1.3.4
|
||||||
COVER=/tmp/kunst.jpg
|
COVER=/tmp/kunst.jpg
|
||||||
MUSIC_DIR=~/Music
|
MUSIC_DIR="${KUNST_MUSIC_DIR:-~/Music}"
|
||||||
SIZE=250x250
|
SIZE="${KUNST_SIZE:-250x250}"
|
||||||
POSITION="+0+0"
|
POSITION="${KUNST_POSITION:-+0+0}"
|
||||||
ONLINE_ALBUM_ART=false
|
ONLINE_ALBUM_ART=""
|
||||||
|
PROG_NAME=$(basename "$0")
|
||||||
|
|
||||||
show_help() {
|
show_help() {
|
||||||
printf "%s" "\
|
printf "%s" "\
|
||||||
usage: kunst [-h] [--size \"px\"] [--position \"+x+y\"][--music_dir \"path/to/dir\"] [--silent] [--version]
|
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
|
--size what size to display the album art in
|
||||||
--position the position where the album art should be displayed
|
--position the position where the album art should be displayed
|
||||||
--music_dir the music directory which MPD plays from
|
--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
|
--force-online force getting cover from the internet
|
||||||
--version show the version of kunst you are using
|
--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
|
# 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"
|
eval set -- "$options"
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
|
@ -54,18 +65,18 @@ while true; do
|
||||||
MUSIC_DIR=$1
|
MUSIC_DIR=$1
|
||||||
;;
|
;;
|
||||||
-h|--help)
|
-h|--help)
|
||||||
show_help
|
show_help
|
||||||
exit
|
exit
|
||||||
|
;;
|
||||||
|
--version)
|
||||||
|
echo $VERSION
|
||||||
|
exit
|
||||||
;;
|
;;
|
||||||
--version)
|
|
||||||
echo $VERSION
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
--force-online)
|
--force-online)
|
||||||
ONLINE_ALBUM_ART=true
|
ONLINE_ALBUM_ART=true
|
||||||
;;
|
;;
|
||||||
--silent)
|
--verbose)
|
||||||
SILENT=true
|
verbose=true
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
shift
|
shift
|
||||||
|
@ -84,109 +95,112 @@ EOF
|
||||||
|
|
||||||
|
|
||||||
is_connected() {
|
is_connected() {
|
||||||
# Check if internet is connected. We are using api.deezer.com to test
|
# 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
|
# if the internet is connected because if api.deezer.com is down or
|
||||||
# the internet is not connected this script will work as expected
|
# the internet is not connected this script will work as expected
|
||||||
if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then
|
if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then
|
||||||
connected=true
|
connected=true
|
||||||
else
|
else
|
||||||
[ ! "$SILENT" ] && echo "kunst: unable to check online for the album art"
|
log "unable to check online for the album art"
|
||||||
connected=false
|
connected=false
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
get_cover_online() {
|
get_cover_online() {
|
||||||
# Check if connected to internet
|
# Check if connected to internet
|
||||||
is_connected
|
is_connected
|
||||||
|
|
||||||
if [ "$connected" == false ];then
|
if [ "$connected" == false ];then
|
||||||
ARTLESS=true
|
ARTLESS=true
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the current playing song ends with .mp3 or something similar, remove
|
# If the current playing song ends with .mp3 or something similar, remove
|
||||||
# it before searching for the album art because including the file extension
|
# it before searching for the album art because including the file extension
|
||||||
# reduces the chance of good results in the search query
|
# reduces the chance of good results in the search query
|
||||||
QUERY=$(mpc current | sed 's/\.[^.]*$//' | iconv -t ascii//TRANSLIT -f utf8)
|
QUERY=$(mpc current | sed 's/\.[^.]*$//' | iconv -t ascii//TRANSLIT -f utf8)
|
||||||
|
|
||||||
# Try to get the album cover online from api.deezer.com
|
# 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'}
|
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
|
# 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
|
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
|
||||||
[ ! "$SILENT" ] && echo "error: cover not found online"
|
log "cover not found online"
|
||||||
ARTLESS=true
|
ARTLESS=true
|
||||||
else
|
else
|
||||||
[ ! "$SILENT" ] && echo "kunst: cover found online"
|
log "cover found online"
|
||||||
curl -o "$COVER" -s "$IMG_URL"
|
curl -o "$COVER" -s "$IMG_URL"
|
||||||
ARTLESS=false
|
ARTLESS=false
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
find_album_art(){
|
find_album_art(){
|
||||||
# Check if the user wants to get the album art from the internet,
|
# 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
|
# regardless if the current song has an embedded album art or not
|
||||||
if [ "$ONLINE_ALBUM_ART" == true ];then
|
if [ ! -z "$ONLINE_ALBUM_ART" ]; then
|
||||||
[ ! "$SILENT" ] && echo "kunst: getting cover from internet"
|
log "getting cover from internet"
|
||||||
get_cover_online
|
get_cover_online
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Extract the album art from the mp3 file and dont show the messsy
|
# Extract the album art from the mp3 file and dont show the messsy
|
||||||
# output of ffmpeg
|
# output of ffmpeg
|
||||||
ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" "$COVER" -y &> /dev/null
|
ffmpeg -i "$MUSIC_DIR$(mpc current -f %file%)" "$COVER" -y &> /dev/null
|
||||||
|
|
||||||
# Get the status of the previous command
|
# Get the status of the previous command
|
||||||
STATUS=$?
|
STATUS=$?
|
||||||
|
|
||||||
# Check if the file has a embbeded album art
|
# Check if the file has a embbeded album art
|
||||||
if [ "$STATUS" -eq 0 ];then
|
if [ "$STATUS" -eq 0 ];then
|
||||||
[ ! "$SILENT" ] && echo "kunst: extracted album art"
|
log "extracted album art"
|
||||||
ARTLESS=false
|
ARTLESS=false
|
||||||
else
|
else
|
||||||
DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")"
|
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.
|
# Check if there is an album cover/art in the folder.
|
||||||
# Look at issue #9 for more details
|
# Look at issue #9 for more details
|
||||||
for CANDIDATE in "$DIR/cover."{png,jpg}; do
|
for CANDIDATE in "$DIR/cover."{png,jpg}; do
|
||||||
if [ -f "$CANDIDATE" ]; then
|
if [ -f "$CANDIDATE" ]; then
|
||||||
STATUS=0
|
STATUS=0
|
||||||
ARTLESS=false
|
ARTLESS=false
|
||||||
convert "$CANDIDATE" $COVER &> /dev/null
|
convert "$CANDIDATE" $COVER &> /dev/null
|
||||||
[ ! "$SILENT" ] && echo "kunst: found cover.png"
|
log "found cover.png"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$STATUS" -ne 0 ];then
|
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
|
get_cover_online
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
update_cover() {
|
update_cover() {
|
||||||
find_album_art
|
find_album_art
|
||||||
|
|
||||||
if [ "$ARTLESS" == false ]; then
|
if [ "$ARTLESS" == false ]; then
|
||||||
convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null
|
convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null
|
||||||
[ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE"
|
log "resized album art to $SIZE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pre_exit() {
|
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
|
# We are dumping the output of kill to /dev/null
|
||||||
# because if the user quits sxiv before they
|
# because if the user quits sxiv before they
|
||||||
# exit kunst, an error will be shown
|
# exit kunst, an error will be shown
|
||||||
# from kill and we dont want that
|
# from kill and we dont want that
|
||||||
kill -9 "$(cat /tmp/kunst.pid)" &>/dev/null
|
kill -9 "$(cat /tmp/kunst.pid)" &>/dev/null
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,54 +209,46 @@ main() {
|
||||||
dependencies=(sxiv convert bash ffmpeg mpc jq mpd)
|
dependencies=(sxiv convert bash ffmpeg mpc jq mpd)
|
||||||
for dependency in "${dependencies[@]}"; do
|
for dependency in "${dependencies[@]}"; do
|
||||||
type -p "$dependency" &>/dev/null || {
|
type -p "$dependency" &>/dev/null || {
|
||||||
echo "error: Could not find '${dependency}', is it installed?" >&2
|
logError "could not find '${dependency}', is it installed?"
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
done
|
done
|
||||||
|
|
||||||
[ "$KUNST_MUSIC_DIR" != "" ] && MUSIC_DIR="$KUNST_MUSIC_DIR"
|
# Flag to run some commands only once in the loop
|
||||||
[ "$KUNST_SIZE" != "" ] && SIZE="$KUNST_SIZE"
|
FIRST_RUN=true
|
||||||
[ "$KUNST_POSITION" != "" ] && POSITION="$KUNST_POSITION"
|
|
||||||
|
|
||||||
# Flag to run some commands only once in the loop
|
while true; do
|
||||||
FIRST_RUN=true
|
update_cover
|
||||||
|
|
||||||
while true; do
|
if [ "$ARTLESS" == true ];then
|
||||||
update_cover
|
# Dhange the path to COVER because the music note
|
||||||
|
# image is a png not jpg
|
||||||
|
COVER=/tmp/kunst.png
|
||||||
|
|
||||||
if [ "$ARTLESS" == true ];then
|
# Decode the base64 encoded image and save it
|
||||||
# Dhange the path to COVER because the music note
|
# to /tmp/kunst.png
|
||||||
# image is a png not jpg
|
echo "$MUSIC_NOTE" | base64 --decode > "$COVER"
|
||||||
COVER=/tmp/kunst.png
|
|
||||||
|
|
||||||
# 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
|
fi
|
||||||
|
|
||||||
if [ "$FIRST_RUN" == true ]; then
|
log "swapped album art to $(mpc current)"
|
||||||
FIRST_RUN=false
|
|
||||||
|
|
||||||
# Display the album art using sxiv
|
if [ "$FIRST_RUN" == true ]; then
|
||||||
sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" &
|
FIRST_RUN=false
|
||||||
|
|
||||||
# Save the process ID so that we can kill
|
# Display the album art using sxiv
|
||||||
# sxiv when the user exits the script
|
sxiv -g "$SIZE$POSITION" -b "$COVER" -N "Kunst" &
|
||||||
echo $! >/tmp/kunst.pid
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Waiting for an event from mpd; play/pause/next/previous
|
# Save the process ID so that we can kill
|
||||||
# this is lets kunst use less CPU :)
|
# sxiv when the user exits the script
|
||||||
while true; do
|
echo $! >/tmp/kunst.pid
|
||||||
mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break
|
fi
|
||||||
done
|
|
||||||
[ ! "$SILENT" ] && echo "kunst: received event from mpd"
|
# Waiting for an event from mpd; play/pause/next/previous
|
||||||
done
|
# this is lets kunst use less CPU :)
|
||||||
|
while true; do
|
||||||
|
mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break
|
||||||
|
done
|
||||||
|
log "received event from mpd"
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# Disable CTRL-Z because if we allowed this key press,
|
# Disable CTRL-Z because if we allowed this key press,
|
||||||
|
|
Loading…
Reference in New Issue