Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Set tags in iTunes based Last.fm Apps
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?]
    •    
  • Currently 2.83 / 5
  You rated: 4 / 5 (24 votes cast)
 
[13,409 views]  

Set tags in iTunes based Last.fm | 19 comments | Create New Account
Click here to return to the 'Set tags in iTunes based Last.fm' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set tags in iTunes based Last.fm
Authored by: david-bo on Aug 13, '10 07:53:29AM
Excellent. I love you!

I would also like to see a version for allmusic.com's moods (previously tones) for songs, albums and artist (that is, if a song has moods, they should be used, otherwise it should fall back to the album moods and finally the artist moods).

See also this old hint.

[ Reply to This | # ]

Set tags in iTunes based Last.fm
Authored by: dfbills on Aug 13, '10 08:25:39AM

I'm not able to get this to work, even after installing wget. I think there are some errors in the wget statment.

---
-d



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: karog on Aug 13, '10 08:48:29AM
dfbills, since $TITLE is the artist, it may have spaces in it which would cause problems. Add quotes as shown here:

wget -q "http://last.fm/music/$TITLE/+tags"

Also, the "exec" before "osascript" (in 2 places) is unnecessary.

Edited on Aug 13, '10 08:52:52AM by karog


[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: dfbills on Sep 08, '10 07:55:18AM

Thanks, this one really seems to be giving me trouble. Seems there are two more issues:

1. The extract.txt is not being created properly. This can be addressed by adding a 'touch extract.txt'

2. The sed string is failing. I'm not able to fix this.

My errrors:


Fetching tags for: Barenaked Ladies
./set_tags.sh: line 13: extract.txt: command not found
awk: can't open file extract.txt
source line number 1
sed: 1: "s/</a>gt;//g": bad flag in substitute command: '/'
./set_tags.sh: line 16: extract.txt: command not found
Setting tags to :
12:45: execution error: cat: extract.txt: No such file or directory (1)
rm: extract.txt: No such file or directory

---
-d



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: DamnItsHot on Aug 13, '10 09:46:34AM
This script uses wget which is a GNU application. Its is not part of Mac OS X and therefore must be installed to allow this script to do what it is supposed to do. You can find the current version of wget for Mac OS X here and the source is available also.
Edited on Aug 13, '10 10:44:54AM by DamnItsHot


[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: crarko on Aug 13, '10 11:06:57AM

Shoot, sorry about that. You can substitute 'curl' which is standard and built-in for wget. Man curl will give the list of options.

I'll fix the script when I get a chance.

Craig A.



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: lullabud on Aug 13, '10 10:17:08AM

I've been wanting something like this for a long time, and just the other day was finally thinking about writing a script to do this, so thanks! I think if anything I'll use this to build on in order to append tags rather than replace my meticulously created genres. (Why the hell won't Apple just add a tag field?!?!) That would then require doing a unique sort and some other logic, so... I doubt I'll get around to it, but thanks for the awesome script.



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: theoz on Aug 13, '10 11:42:00AM
Hello,

this has worked for me so if it could help someone else...

replace wget command with cURL
URL="http://www.lastfm.it/music/$TITLE/+tags"
URL=$(echo $URL | sed 's/ /%20/g')

curl -A "Mozilla/5.0 (X11; U; Mac OSX; it; rv:1.9.0.7) Gecko/2009030422 Firefox/3.0.7" $URL > "tags.txt"
Tz
Edited on Aug 14, '10 05:41:36AM by crarko


[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: VxJasonxV on Aug 13, '10 02:43:19PM

I feel I need to be pedantic and point out that this will break very quickly as soon as Last.fm changes one single thing in their template, or URI scheme, and poof, gone.

There's a reason why last.fm has their API http://www.last.fm/api/show?service=318 , in order to minimize changes, and provide a standard response format to a query, given the data that's contained within.

Meaning, scraping is fine until it breaks. It's all about the content.



[ Reply to This | # ]
Perl version
Authored by: lar3ry on Aug 13, '10 03:23:39PM
The bash script used a lot of utility calls inefficiently. Here's an equivalent Perl script, which should be more efficient, easier to maintain, and a bit more rugged in case Last.fm changes the format of their tag output:
#! /usr/bin/perl -w

sub osascript($);

%INFO = ();

sub osascript($)
{
    my ($script) = @_;
    local $_;

    my @lines = split(/[\r\n]/, $script);
    $script = '';
    foreach (@lines) {
        $script .= "echo " . quotemeta($_) . ';';
    }

    open CMD,"($script) | osascript|"
        or die "Cannot run osascript: $^E";

    my $output = <CMD>;
    close(CMD) or die "Error processing osascript: $^E";
    $output =~ tr /\r\n//d if (defined($output));

    return $output;
}
    
$artist = osascript (<<EOF);
tell application "iTunes"
    get artist of current track
end tell
EOF

print "Artist is $artist\n";

open CMD, "wget -O - -q 'http://last.fm/music/$artist/+tags'|"
    or die "wget failed: $^E";

while (<CMD>) {
    tr /\r\n//;

    $size = undef;
    $tag = undef;

    if (/font-size:\s*(\d+)px;/) {
        $size = $1;
    }
    if (m@rel="tag">([^<]+)</a>@) {
        $tag = $1;
    }
    if (defined($size) && defined($tag)) {
        $INFO{$tag} = $size;
    }
}

@tags = sort { $INFO{$b} <=> $INFO{$a} } keys(%INFO);
@tags = splice @tags,0,3;

$tags = '"' . join('" & return & return & "', @tags) . '"';

foreach (@tags) {
    print "$_\n";
}

osascript(<<EOF);
set tags to $tags
tell application "iTunes"
    set genre of current track to tags
end tell
EOF

Edited on Aug 13, '10 03:27:01PM by lar3ry


[ Reply to This | # ]
Perl version
Authored by: dfbills on Sep 08, '10 07:52:41AM

lar3ry: that was a quick re-write. Unfortunately, I'm getting an error that I can't seem to resolve:

Semicolon seems to be missing at ./set_tags.sh line 65.
syntax error at ./set_tags.sh line 66, near "EOF

osascript"
Can't find string terminator "EOF" anywhere before EOF at ./set_tags.sh line 66.

---
-d



[ Reply to This | # ]
Perl version
Authored by: lar3ry on Sep 08, '10 11:03:03AM

Since the script works (I just copied it, saved it, and ran it), here are a couple of suggestions:

1. Make sure you have the "#! /usr/bin/perl" line in line 1 of your file. Otherwise, it will be interpreted as a shell script, not a Perl program.

2. Try typing "perl -cw set_tag.sh" and see if you get the same message. Otherwise, the script isn't being run as a Perl program (and refer to step 1). If you do get the same message, then make sure that the final line in your file contains "EOF" and nothing else (no leading blanks, etc.).

lar3ry



[ Reply to This | # ]
Perl version
Authored by: dfbills on Sep 09, '10 06:21:38AM
lar3ry: Thanks, buy still no dice, even when running from Perl. I'm running Perl v5.10.0 on 10.6.4.

$ perl -cw set_tags.sh
Can't find string terminator "EOF" anywhere before EOF at set_tags.sh line 65.
I've posted a link to my actual shell app below: http://bit.ly/9AKQPs
---
-d
Edited on Sep 09, '10 06:27:46AM by dfbills


[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: ChuckEye on Aug 13, '10 03:35:36PM
Neither the bash nor the perl seem to work in my case... Something Adobe CS4 installed, from what I can tell, but not sure how best to bypass it...
Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
	/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.


[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: velkr0 on Aug 28, '10 02:16:16PM
If you want to avoid webpage scraping:
Authored by: toc-rox on Aug 14, '10 03:15:19AM

lfmCMD.pl is a generic last.fm webservices client and allows you to call each last.fm API service (for desktop applications) from a command line (terminal). The http request is logged to "last.fm.Request.txt" and the last.fm response "last.fm.Response.xml". Example for requesting the top tags of an artist (service 288):

Request:
perl lfmCMD.pl method=artist.getTopTags artist="Nelly Furtado"

Response:
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<toptags artist="Nelly Furtado">
<tag>
<name>pop</name>
<count>100</count>
<url>http://www.last.fm/tag/pop</url>
</tag>
<tag>
<name>female vocalists</name>
<count>63</count>
<url>http://www.last.fm/tag/female%20vocalists</url>
</tag>
...
</toptags>
</lfm>

With a bit of XML parsing ...

Link to utility: http://www.easyclasspage.de/lastfm/seite-11.html



[ Reply to This | # ]
If you want to avoid webpage scraping:
Authored by: toc-rox on Aug 14, '10 05:02:18AM

Perhaps interesting: last.fm service 289 gets the top tags for a track.

Example:
perl lfmCMD.pl method=track.getTopTags artist="Nelly Furtado" track="Maneater"



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: calgarybear on Aug 15, '10 04:53:21PM

So in some investigating on other postings I found some recommendations to increase the multicast rate to 11Mbps. I found this config within my router was set to auto I changed it to 11. As soon as I made the change my express quit dropping out. Good luck
(using a Dlink DI-524 router)



[ Reply to This | # ]
Set tags in iTunes based Last.fm
Authored by: vincentvw on Aug 16, '10 12:53:25PM

In terms of 'making it easier,' this would be great as either a script continuously running in the background or as a script that runs through your whole library and applies tags to it. Doing it manually seems like a hassle.



[ Reply to This | # ]