Set tags in iTunes based Last.fm

Aug 13, '10 07:30:00AM

Contributed by: Anonymous

I lacked a function that would automatically set tags on my tracks according to Last.fm.

I therefore wrote a bash shell script that leeches the three most popular tags for a given band name and applied them to the track. I can then just have iTunes use these tags for the entire album.

Here is the script code:

#!/bin/bash
TAGS="extract.txt"

TITLE=`exec osascript <<EOF 
tell application "iTunes"
  get artist of current track
    --set genre of current track to "HORSE POP"
 end tell`

echo "Fetching tags for: "  $TITLE

wget -q  http://last.fm/music/$TITLE/+tags  
sed -n  's/rel="tag"//p' +tags >gt;>gt; $TAGS
TAGLIST=`awk '{print $4,$6,$7,$8,$9,$10}' $TAGS |sort -r|head -3|sed 's/.*" >gt;//g'|sed 's/</a>gt;//g'`
TAGLIST=`echo $TAGLIST|sed '/SCENE.*[^.]$/N;s/n */ /'`
echo $TAGLIST >gt; $TAGS

echo "Setting tags to :" $TAGLIST
NEWTAGS=`exec osascript <</EOF 
set tags to do shell script "cat extract.txt"
tell application "iTunes"
  set genre of current track to tags
end tell`

rm +tags
rm $TAGS
It is not perfect but it is a start for making tagging easier, for me at least. Hopefully some of you will find it useful or inspiring.

[crarko adds: I haven't tested this one. Anybody want to modify it to work with Pandora?]

Comments (19)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2010060614182982