commit ea5c8031e32229d345166d57a600c7a1b1179577
parent dea9d3db496511eef82cd25864e47a7e4cc3f20a
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Fri, 4 Aug 2017 18:10:05 +0200
iTunes Style Getter improvements
Changed it to write info in the format "Genre/Style". Added comments. Also, split up the genre, style, and URL queries into three separate parts, using flags (arguments to the Ruby script) to switch between them.
To query for a genre, use the 'g' flag. For a style, use the 's' flag. For a URL, use any or no flag.
Diffstat:
2 files changed, 104 insertions(+), 40 deletions(-)
diff --git a/itunes_style_getter b/itunes_style_getter
@@ -1,46 +1,93 @@
#!/bin/bash
+# Get info about currently selected track from iTunes (AppleScript)
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"`;
if [ -z "$album" ]; then
- style=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb "$artist" "$name");
+ # If there's no album, query without an album
+ 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
- style=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb "$artist" "$album" "$name");
+ # Otherwise, add the album to query for precision
+ genre=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb g "$artist" "$album" "$name");
+ style=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb s "$artist" "$album" "$name");
+ url=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb u "$artist" "$name");
fi
-# Parsing the array
-# Split the Ruby array into a Bash array, do some replacements
+# 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 ]
+
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 ]
-echo "URL: $style"
-delete=${style[0]}
-style=( "${style[@]/$delete}" )
-style=("${style[@]:1}")
+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 ]
+# Give the user the source information
+echo "URL: $url"
echo "You're changing the album \"$album\" by \"$artist\""
printf '\n'
-echo "The options:"
-printf '%s\n' "${style[@]}"
-printf "\nWould you like to use one of these?"
+
+# Select menu for genre
+echo "Genre options:"
+printf '%s\n' "${genre[@]}"
+printf "\nWould you like to use one of these?\n"
select yn in "Yes" "No"; do
- case $yn in
- Yes)
- select opt in "${style[@]}"; do
- case $opt in
- *)
- final=$(echo $opt | sed -e 's/"//g')
- break;;
- esac
- done
- break;;
- No)
- echo "Write your own then:"
- read final
- break;;
- esac
+ case $yn in
+ Yes)
+ select opt in "${genre[@]}"; do
+ case $opt in
+ *)
+ finalg=$(echo $opt | sed -e 's/"//g')
+ break;;
+ esac
+ done
+ break;;
+ No)
+ echo "Write your own then:"
+ read finalg
+ break;;
+ esac
+done
+
+# Select menu for style
+echo "Style options:"
+printf '%s\n' "${style[@]}"
+printf "\nWould you like to use one of these?\n"
+select yn in "Yes" "No" "None needed"; do
+ case $yn in
+ Yes)
+ select opt in "${style[@]}"; do
+ case $opt in
+ *)
+ finals=$(echo \/$opt | sed -e 's/"//g')
+ break;;
+ esac
+ done
+ break;;
+ No)
+ echo "Write your own then:"
+ read finals
+ finals="/$finals"
+ break;;
+ "None needed")
+ break;;
+ esac
done
-osascript -e "tell application \"iTunes\" to set the genre of every track whose album is \"$album\" and artist is \"${artist}\" to \"$final\"" > /dev/null
+# Finally, write the info to the iTunes track (AppleScript)
+if [ -z "$album" ]; then
+ osascript -e "tell application \"iTunes\" to set the genre of every track whose name is \"$name\" and artist is \"${artist}\" to \"$finalg$finals\"" > /dev/null
+else
+ osascript -e "tell application \"iTunes\" to set the genre of every track whose album is \"$album\" and artist is \"${artist}\" to \"$finalg$finals\"" > /dev/null
+fi
+
+# ...and give a status message.
+printf "\nGenre set to: $finalg$finals\n"+
\ No newline at end of file
diff --git a/itunes_style_getter_helper.rb b/itunes_style_getter_helper.rb
@@ -1,25 +1,40 @@
-require "discogs-wrapper"
+require "discogs-wrapper" # Needed for API access
+# Remove this if sharing. This is required and unique for each user.
my_user_token = ENV["DISCOGS_API_TOKEN"]
+flag=ARGV.shift # Whether searching for genre or style
+query=ARGV.join(" ") # Join the rest of the arguments into a search string
-query=ARGV.join(" ")
+# Search, and wrap the results in an array
wrapper=Discogs::Wrapper.new("iTunesStyleGetter", user_token: my_user_token)
res=[]
search=wrapper.search("#{query}")
-search.results.each do |result|
- if result.style? && !result.style.empty?
- res="http://discogs.com#{result.uri}".split(" ")
- result.style.to_a.each {|x| res.push x }
- break
+
+# If searching for a genre
+if (flag=="g")
+ search.results.each do |result|
+ if result.genre? && !result.genre.empty?
+ result.genre.each do |g|
+ res.push("#{g}")
+ end
+ p res
end
end
-search.results.each do |result|
- if result.genre? && !result.genre.empty?
- result.genre.each do |g|
- res.push("#{g}")
+# If searching for a style
+elsif (flag=="s")
+ search.results.each do |result|
+ if result.style? && !result.style.empty?
+ result.style.to_a.each {|x| res.push x}
end
p res
- exit
end
-end
+# If searching for url
+else
+ search.results.each do |result|
+ if result.style? && !result.style.empty?
+ res="http://discogs.com#{result.uri}".split(" ")
+ end
+ p res
+ end
+end+
\ No newline at end of file