commit 88882770133ace5af06f6fe299fb25080fdf4b91
parent 3aaff29c203b441bb75cb02117a0654168b8dbbe
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Sat, 1 Sep 2018 21:03:49 +0200
Refactored itunes_style_getter
Diffstat:
1 file changed, 26 insertions(+), 26 deletions(-)
diff --git a/dotfiles/bin/itunes_style_getter b/dotfiles/bin/itunes_style_getter
@@ -3,28 +3,28 @@ while :; do
if [ $# -ne 0 ]; then
case $1 in
"-p")
- artist=`osascript -e "tell application \"iTunes\" to return artist of current track"`;
- album=`osascript -e "tell application \"iTunes\" to return album of current track"`;
- name=`osascript -e "tell application \"iTunes\" to return name of current track"`;
+ artist=$(osascript -e "tell application \"iTunes\" to return artist of current track");
+ album=$(osascript -e "tell application \"iTunes\" to return album of current track");
+ name=$(osascript -e "tell application \"iTunes\" to return name of current track");
;;
"-s")
- artist=`osascript -e "tell application \"iTunes\" to return artist of selection"`;
- album=`osascript -e "tell application \"iTunes\" to return album of selection"`;
- name=`osascript -e "tell application \"iTunes\" to return name of selection"`;
+ artist=$(osascript -e "tell application \"iTunes\" to return artist of selection");
+ album=$(osascript -e "tell application \"iTunes\" to return album of selection");
+ name=$(osascript -e "tell application \"iTunes\" to return name of selection");
esac
else
echo "Which track?"
select sc in "Selected" "Playing"; do
case $sc in
Selected)
- artist=`osascript -e "tell application \"iTunes\" to return artist of selection"`;
- album=`osascript -e "tell application \"iTunes\" to return album of selection"`;
- name=`osascript -e "tell application \"iTunes\" to return name of selection"`;
+ artist=$(osascript -e "tell application \"iTunes\" to return artist of selection");
+ album=$(osascript -e "tell application \"iTunes\" to return album of selection");
+ name=$(osascript -e "tell application \"iTunes\" to return name of selection");
break;;
Playing)
- artist=`osascript -e "tell application \"iTunes\" to return artist of current track"`;
- album=`osascript -e "tell application \"iTunes\" to return album of current track"`;
- name=`osascript -e "tell application \"iTunes\" to return name of current track"`;
+ artist=$(osascript -e "tell application \"iTunes\" to return artist of current track");
+ album=$(osascript -e "tell application \"iTunes\" to return album of current track");
+ name=$(osascript -e "tell application \"iTunes\" to return name of current track");
break;;
esac
done
@@ -32,36 +32,36 @@ while :; do
if [ -z "$album" ]; then
# If there's no album, query without an album
- echo "Querying: \"$artist $name\" (https://www.discogs.com/search/?q=`sed -e 's/ /%20/g' -e 's/&/%26/g' <<< \"$artist $name\"`)"
+ search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $name")
+ echo "Querying: \"$artist $name\" (https://www.discogs.com/search/?q=$search_url)"
genre=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb g "$artist" "$name");
style=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb s "$artist" "$name");
url=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb u "$artist" "$name");
else
# Otherwise, add the album to query for precision
- echo "Querying: \"$artist $album\" (https://www.discogs.com/search/?q=`sed -e 's/ /%20/g' -e 's/&/%26/g' <<< \"$artist $album\"`)"
+ search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $album")
+ echo "Querying: \"$artist $album\" (https://www.discogs.com/search/?q=$search_url)"
genre=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb g "$artist" "$album");
style=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb s "$artist" "$album");
url=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb u "$artist" "$album");
fi
if [ -z "$genre" ] || [ -z "$url" ]; then
- echo "Not found."
- exit 1
+ echo "Not found."
+ exit 1
fi
# Parsing the arrays
# Split the Ruby arrays into Bash arrays, do some replacements to make it look nice
- IFS=',' read -ra genre <<< "${genre}"
- genre[0]=$(echo ${genre[0]} | sed -e 's/\[//') # Remove front [
- genre[$((${#genre[@]}-1))]=$(echo ${genre[$((${#genre[@]}-1))]} | sed -e 's/\]//') # Remove back ]
+ genre=$(sed -e 's/^\[//' -e 's/\]$//' <<< "$genre") # Remove brackets from Ruby array
+ IFS=',' read -ra genre <<< "${genre}"; unset IFS # Read it into an array
- IFS=',' read -ra style <<< "${style}"
- style[0]=$(echo ${style[0]} | sed -e 's/\[//') # Remove front [
- style[$((${#style[@]}-1))]=$(echo ${style[$((${#style[@]}-1))]} | sed -e 's/\]//') # Remove back ]
+ style=$(sed -e 's/^\[//' -e 's/\]$//' <<< "$style") # Remove brackets
+ IFS=',' read -ra style <<< "${style}"; unset IFS # Read it into an array
- IFS=',' read -ra url <<< "${url}"
- url[0]=$(echo ${url[0]} | sed -e 's/\[//') # Remove front [
- url[$((${#url[@]}-1))]=$(echo ${url[$((${#url[@]}-1))]} | sed -e 's/\]//') # Remove back ]
+
+ url=$(sed -e 's/^\[//' -e 's/\]$//' <<< $url)
+ IFS=',' read -ra url <<< "${url}"; unset IFS
# Give the user the source information
echo "URL: $url"
@@ -109,7 +109,7 @@ while :; do
select opt in "${style[@]}"; do
case $opt in
*)
- finals="/"$(echo $opt | cut -d ' ' -f1- | sed -e 's/"//g' -e "s/ $finalg//")
+ finals="/"$(echo $opt | cut -d ' ' -f1- | sed -e "s/\"//g" -e "s/ *$finalg *//")
break;;
esac
done