commit 5b00fa0a53c61ac4239aa2c025844d1d29438067
parent f6dc451236cfaaf571db4a76805cc5556b328845
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Sun, 16 Dec 2018 16:24:35 +0100
Renamed itunes_style_getter to it-style
It's shorter and easier to type.
Former-commit-id: 3382f453be2483584111afc08320509718de9b7d
Diffstat:
4 files changed, 151 insertions(+), 151 deletions(-)
diff --git a/README.md b/README.md
@@ -24,7 +24,7 @@ Scripts:
* `executable`: my first script. Makes any file executable, along with further options. Usage: `executable file.sh`
* `gfx2gfx`: convert SWF into PDF. Usage: `gfx2gfx page.swf -o page.pdf`
* `icalBuddy`: can extract information about events from the iCal application
-* `itunes_style_getter`: can find and set the genre in a track in iTunes (along with its Ruby helper), using the Discogs database (you need an API key, set $DISCOGS_API_TOKEN in your profile). Usage: click/play a track in iTunes, and run the program.
+* `it-style`: can find and set the genre in a track in iTunes (along with its Ruby helper), using the Discogs database (you need an API key, set $DISCOGS_API_TOKEN in your profile). Usage: click/play a track in iTunes, and run the program.
* `linkdir`: symlink all files/folders from source dir to target dir. Usage: `linkdir $source` to create symlinks in cwd, `linkdir $source $target` otherwise.
* `mdvl`: markdown renderer in Python
* `modified_cfscrape.py`: can scrape and download CloudFlare-protected websites. Usage: `python3 modified_cfscrape.py http://url.com`
diff --git a/bin/it-style b/bin/it-style
@@ -0,0 +1,149 @@
+#!/bin/bash
+oldifs="$IFS"
+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");
+ ;;
+ "-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");
+ 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");
+ 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");
+ break;;
+ esac
+ done
+ fi
+
+ if [ -z "$album" ]; then
+ # If there's no album, query without an album
+ search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $name")
+ echo "Querying: \"$artist $name\" ($search_url)"
+ response=$(ruby /Users/alex/.bin/it-style-helper.rb "$artist" "$name");
+ else
+ # Otherwise, add the album to query for precision
+ search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $album")
+ echo "Querying: \"$artist $album\" ($search_url)"
+ response=$(ruby /Users/alex/.bin/it-style-helper.rb "$artist" "$album");
+ fi
+
+ # Remove brackets from the Ruby array
+ response=$(sed -e 's/^\[//' -e 's/\]$//' <<< "$response")
+
+ # Read the response into a bash array (genres, styles, url)
+ IFS=',' read -ra response <<< "${response}"; unset IFS
+
+ # Create a bash array from the genres, stripping quotes
+ IFS='~' read -ra genre <<< "${response[0]//\"/}"; unset IFS
+ genre=$(sed -e 's/^ //' <<< $genre)
+
+ # Create a bash array from the styles, stripping quotes
+ # potential regex to surround each style in quotes: 's/\([^~]*\)/"\1"/g'
+
+ IFS='~' read -ra style <<< "${response[1]//\"/}"; unset IFS
+ style=$(sed -e 's/^ //' <<< $style)
+
+ # Save the url as a string, stripping quotes (and remove the brackets entered by Ruby)
+ url=$(sed -e 's/^ *\[//' -e 's/\]$//' <<<${response[2]//\"/})
+
+ # If there's no url, there's no result
+ if [ -z "$url" ]; then
+ echo -e "\nNot found.\n"
+ continue
+ fi
+
+ IFS="$oldifs"
+
+ # Give the user the source information
+ echo "URL: $url"
+ echo "You're changing the album \"$album\" by \"$artist\""
+ printf '\n'
+
+ # 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 "${genre[@]}"; do
+ case $opt in
+ *)
+ finalg=$(sed -e 's/"//g' <<< $opt)
+ if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency
+ break;;
+ esac
+ done
+ break;;
+ No)
+ echo "Write your own then:"
+ read finalg
+ if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency
+ if [[ $finalg = "Post-Rock" ]]; then finalg="Post Rock"; fi # consistency
+ break;;
+ esac
+ done
+
+ # Select menu for style
+ if [ -z "$style" ]; then
+ echo "No style from Discogs."
+ echo "Write your own:"
+ read finals
+ finals="/$finals"
+ else
+ 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
+ *)
+ # Add a forward slash at start, remove extra spaces,
+ # also remove repetitions in genre/style like "Rock/Alternative Rock"
+ finals="/"$(echo $opt | cut -d ' ' -f1- | sed -e "s/\"//g" -e "s/ *$finalg *//")
+ break;;
+ esac
+ done
+ break;;
+ No)
+ echo "Write your own then:"
+ read finals
+ finals="/$finals"
+ break;;
+ "None needed")
+ finals=""
+ break;;
+ esac
+ done
+ fi
+
+ # Finally, write the info to the iTunes track (AppleScript)
+ # If there's an album, change the genre of the whole album
+ 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
+ # Otherwise, just change the genre of that one track
+ 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"
+done+
\ No newline at end of file
diff --git a/bin/itunes_style_getter_helper.rb b/bin/it-style-helper.rb
diff --git a/bin/itunes_style_getter b/bin/itunes_style_getter
@@ -1,149 +0,0 @@
-#!/bin/bash
-oldifs="$IFS"
-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");
- ;;
- "-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");
- 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");
- 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");
- break;;
- esac
- done
- fi
-
- if [ -z "$album" ]; then
- # If there's no album, query without an album
- search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $name")
- echo "Querying: \"$artist $name\" ($search_url)"
- response=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb "$artist" "$name");
- else
- # Otherwise, add the album to query for precision
- search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "$artist $album")
- echo "Querying: \"$artist $album\" ($search_url)"
- response=$(ruby /Users/alex/.bin/itunes_style_getter_helper.rb "$artist" "$album");
- fi
-
- # Remove brackets from the Ruby array
- response=$(sed -e 's/^\[//' -e 's/\]$//' <<< "$response")
-
- # Read the response into a bash array (genres, styles, url)
- IFS=',' read -ra response <<< "${response}"; unset IFS
-
- # Create a bash array from the genres, stripping quotes
- IFS='~' read -ra genre <<< "${response[0]//\"/}"; unset IFS
- genre=$(sed -e 's/^ //' <<< $genre)
-
- # Create a bash array from the styles, stripping quotes
- # potential regex to surround each style in quotes: 's/\([^~]*\)/"\1"/g'
-
- IFS='~' read -ra style <<< "${response[1]//\"/}"; unset IFS
- style=$(sed -e 's/^ //' <<< $style)
-
- # Save the url as a string, stripping quotes (and remove the brackets entered by Ruby)
- url=$(sed -e 's/^ *\[//' -e 's/\]$//' <<<${response[2]//\"/})
-
- # If there's no url, there's no result
- if [ -z "$url" ]; then
- echo -e "\nNot found.\n"
- continue
- fi
-
- IFS="$oldifs"
-
- # Give the user the source information
- echo "URL: $url"
- echo "You're changing the album \"$album\" by \"$artist\""
- printf '\n'
-
- # 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 "${genre[@]}"; do
- case $opt in
- *)
- finalg=$(sed -e 's/"//g' <<< $opt)
- if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency
- break;;
- esac
- done
- break;;
- No)
- echo "Write your own then:"
- read finalg
- if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency
- if [[ $finalg = "Post-Rock" ]]; then finalg="Post Rock"; fi # consistency
- break;;
- esac
- done
-
- # Select menu for style
- if [ -z "$style" ]; then
- echo "No style from Discogs."
- echo "Write your own:"
- read finals
- finals="/$finals"
- else
- 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
- *)
- # Add a forward slash at start, remove extra spaces,
- # also remove repetitions in genre/style like "Rock/Alternative Rock"
- finals="/"$(echo $opt | cut -d ' ' -f1- | sed -e "s/\"//g" -e "s/ *$finalg *//")
- break;;
- esac
- done
- break;;
- No)
- echo "Write your own then:"
- read finals
- finals="/$finals"
- break;;
- "None needed")
- finals=""
- break;;
- esac
- done
- fi
-
- # Finally, write the info to the iTunes track (AppleScript)
- # If there's an album, change the genre of the whole album
- 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
- # Otherwise, just change the genre of that one track
- 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"
-done-
\ No newline at end of file