it-style (6966B)
1 #!/usr/bin/env bash 2 # Set a track's genre in iTunes from Discogs. 3 # No longer updated, I stopped using iTunes. 4 5 oldifs="$IFS" 6 console=0 7 print_help() { 8 echo "Options: " 9 echo "-p Currently playing in iTunes" 10 echo "-s Selected in iTunes" 11 echo "-c Console mode, supply a file and use eyeD3" 12 echo "-h Print this help text" 13 } 14 if [[ $(uname -s) != "Darwin" ]]; then 15 if [ $1 != "-c" ]; then 16 echo "You're not on a Mac, so this will only work with the '-c' switch (since Applescript is needed to be able to control iTunes)." 17 exit 1 18 fi 19 fi 20 while :; do 21 if [ $# -ne 0 ]; then 22 case $1 in 23 "-p") 24 artist=$(osascript -e "tell application \"iTunes\" to return artist of current track"); 25 album=$(osascript -e "tell application \"iTunes\" to return album of current track"); 26 name=$(osascript -e "tell application \"iTunes\" to return name of current track"); 27 ;; 28 "-s") 29 artist=$(osascript -e "tell application \"iTunes\" to return artist of selection"); 30 album=$(osascript -e "tell application \"iTunes\" to return album of selection"); 31 name=$(osascript -e "tell application \"iTunes\" to return name of selection"); 32 ;; 33 "-c") 34 if ! command -v eyed3 &> /dev/null; then echo "Please install eyeD3." && exit 1; fi 35 if [ $# -ne 2 ]; then echo "Filename required." && exit 1; fi 36 if ! [ -f "$2" ]; then echo "File $2 does not exist." && exit 1; fi 37 artist=$(eyed3 "$2" 2>/dev/null | grep artist | cut -d: -f2- | sed 's/^ *//g') 38 name=$(eyed3 "$2" 2>/dev/null | grep title | cut -d: -f2- | sed 's/^ *//g') 39 album=$(eyed3 "$2" 2>/dev/null | grep album | cut -d: -f2- | sed 's/^ *//g') 40 console=1 41 ;; 42 "-h") 43 print_help 44 exit 0 45 ;; 46 *) 47 echo "Unsupported argument." 48 exit 1 49 ;; 50 esac 51 else 52 echo "Which track?" 53 select sc in "Selected" "Playing"; do 54 case $sc in 55 Selected) 56 artist=$(osascript -e "tell application \"iTunes\" to return artist of selection"); 57 if [ $? -ne 0 ]; then echo "No selection." && continue; fi 58 album=$(osascript -e "tell application \"iTunes\" to return album of selection"); 59 if [ $? -ne 0 ]; then echo "No selection." && continue; fi 60 name=$(osascript -e "tell application \"iTunes\" to return name of selection"); 61 if [ $? -ne 0 ]; then echo "No selection." && continue; fi 62 break;; 63 Playing) 64 artist=$(osascript -e "tell application \"iTunes\" to return artist of current track"); 65 if [ $? -ne 0 ]; then echo "No track playing." && continue; fi 66 album=$(osascript -e "tell application \"iTunes\" to return album of current track"); 67 if [ $? -ne 0 ]; then echo "No track playing." && continue; fi 68 name=$(osascript -e "tell application \"iTunes\" to return name of current track"); 69 if [ $? -ne 0 ]; then echo "No track playing." && continue; fi 70 break;; 71 esac 72 done 73 if [ -z "$sc" ]; then continue; fi 74 fi 75 76 if [ -z "$album" ]; then 77 # If there's no album, query without an album 78 search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "${artist%%feat*} $name") 79 echo "Querying: \"$artist $name\" ($search_url)" 80 response=$(ruby $DOTFILES/scripts/discogs -g "${artist%%feat*}" "$name"); 81 else 82 # Otherwise, add the album to query for precision 83 search_url="https://www.discogs.com/search/?q="$(sed -e 's/ /%20/g' -e 's/&/%26/g' <<< "${artist%%feat*} $album") 84 echo "Querying: \"$artist $album $name\" ($search_url)" 85 response=$(ruby $DOTFILES/scripts/discogs -g "${artist%%feat*}" "$album"); 86 fi 87 88 # Remove brackets from the Ruby array 89 response=$(sed -e 's/^\[//' -e 's/\]$//' <<< "$response") 90 91 # Read the response into a bash array (genres, styles, url) 92 IFS=',' read -ra response <<< "${response}"; unset IFS 93 94 # Create a bash array from the genres, stripping quotes 95 IFS='~' read -ra genre <<< "${response[0]//\"/}"; unset IFS 96 genre=$(sed -e 's/^ //' <<< $genre) 97 98 # Create a bash array from the styles, stripping quotes 99 # potential regex to surround each style in quotes: 's/\([^~]*\)/"\1"/g' 100 101 IFS='~' read -ra style <<< "${response[1]//\"/}"; unset IFS 102 style=$(sed -e 's/^ //' <<< $style) 103 104 # Save the url as a string, stripping quotes (and remove the brackets entered by Ruby) 105 url=$(sed -e 's/^ *\[//' -e 's/\]$//' <<<${response[2]//\"/}) 106 107 # If there's no url, there's no result 108 if [ -z "$url" ]; then 109 echo -e "\nNot found.\n" 110 echo "Please enter the genre string (format \"Genre/Style\"):" 111 read finalg 112 finals="" 113 else 114 IFS="$oldifs" 115 116 # Give the user the source information 117 echo "URL: $url" 118 echo "You're changing the album \"$album\" by \"$artist\"" 119 printf '\n' 120 121 # Select menu for genre 122 echo "Genre options:" 123 printf '%s\n' "${genre[@]}" 124 printf "\nWould you like to use one of these?\n" 125 select yn in "Yes" "No"; do 126 case $yn in 127 Yes) 128 select opt in "${genre[@]}"; do 129 case $opt in 130 *) 131 finalg=$(sed -e 's/"//g' <<< $opt) 132 if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency 133 break;; 134 esac 135 done 136 break;; 137 No) 138 echo "Write your own then:" 139 read finalg 140 if [[ $finalg = "Hip Hop" ]]; then finalg="Hip-Hop"; fi # consistency 141 if [[ $finalg = "Post-Rock" ]]; then finalg="Post Rock"; fi # consistency 142 break;; 143 esac 144 done 145 146 # Select menu for style 147 if [ -z "$style" ]; then 148 echo "No style from Discogs." 149 echo "Write your own:" 150 read finals 151 finals="/$finals" 152 else 153 echo "Style options:" 154 printf '%s\n' "${style[@]}" 155 printf "\nWould you like to use one of these?\n" 156 select yn in "Yes" "No" "None needed"; do 157 case $yn in 158 Yes) 159 select opt in "${style[@]}"; do 160 case $opt in 161 *) 162 # Add a forward slash at start, remove extra spaces, 163 # also remove repetitions in genre/style like "Rock/Alternative Rock" 164 finals="/"$(echo $opt | cut -d ' ' -f1- | sed -e "s/\"//g" -e "s/ *$finalg *//") 165 break;; 166 esac 167 done 168 break;; 169 No) 170 echo "Write your own then:" 171 read finals 172 finals="/$finals" 173 break;; 174 "None needed") 175 finals="" 176 break;; 177 esac 178 done 179 fi 180 181 fi 182 183 if ! [ -z "$finalg$finals" ]; then 184 # Finally, write the info to the iTunes track (AppleScript) 185 # If there's an album, change the genre of the whole album 186 if [ $console -eq 1 ]; then 187 eyed3 -G "$finalg$finals" "$2" &> /dev/null 188 else 189 if [ -z "$album" ]; then 190 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 191 # Otherwise, just change the genre of that one track 192 else 193 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 194 fi 195 fi 196 # ...and give a status message. 197 printf "\nGenre set to: $finalg$finals\n" 198 fi 199 if [ $console -eq 1 ]; then exit 0; fi 200 done