#Genres by Dale Swanson June 3rd 2010 #requires: # wget http://users.ugent.be/~bpuype/wget/ # tag http://www.synthetic-soul.co.uk/tag/ # #pulls genre data from last.fm and tags mp3s #goes through all directories wherever it's ran (nonrecursive) and uses each as a band name #Uses the last.fm artist top tags API http://www.last.fm/api/show?service=288 #takes top 5 genres and adds them to a batch file which actually adds the tags (doesn't run the batch file) #This batch file uses the tag command line to add the tags #You should go over the batch file manually, and confirm the genres, then run it manually. #That CLI tagger seems to have a problem with everything except APE2 Mp3 tags. #!/usr/bin/perl use strict; use warnings; use Cwd; use URI::Escape; my $debug = 1; #debug mode, set to 1 to get lot's of output my $maxtags = 5; #how many genre tags you wanted for each band my $tagseparator = ", "; #what to put between seperate genres. ex: ", ", "\0", or "/" Note you can't pass NULLs. Instead pass something unique then use Foobar2000's mass tagger plugin to split Genre feild by that separator. my $dir; #the directory the program is running from my @allfiles; #stores all the files in the directory my $file; #used to access each file in the allfiles array. Will be name of directory, which should be band's name. my $band; #separate variable for band's name so that we can escape it for URI. my $tagcount; #counter of how many tags we have for that band my $taglist; #list of all genres as they will appear in tag my @filearray; #will store the contents of the file my $fileline; #will store each line of the file my $testcount; #testing stuff remove later (i.e. never) $dir = cwd(); #starting directory (print "\ndir - $dir") if ($debug); opendir THISDIR, $dir or die "Can't open directory: $!"; @allfiles = grep !/^\.\.?$/, readdir THISDIR; #gets all the files in this directory, strips . and .. closedir THISDIR; (print "\nALL FILES:\n@allfiles\n") if ($debug); foreach $file (@allfiles) {# go through each file, check to see if it's a directory (print "\nTest File - $file") if ($debug); $taglist = ""; #reset tag list for new band $tagcount=0; #new band reset tag count if (-d $dir . "/" . $file ) {# if it's a directory we assume it's a band name. Thus $file is our band's name. (print "\nList DIR - $file") if ($debug); $band = $file; #file will be unescaped band's name (print "\nBand - $band") if ($debug); $band = uri_escape($file); #band will be escaped band's name (print "\nBand - $band") if ($debug); $band =~ s/%/%%/g; #escape % to %% for batch files to work (print "\nBand - $band") if ($debug); open(ofile, ">downxml.bat"); #output the line print ofile ("wget \"http://ws.audioscrobbler.com/2.0/?method=artist.gettoptags&artist=$band&api_key=b25b959554ed76058ac220b7b2e0a026\" -O tags.xml"); close(ofile); system "downxml.bat"; open(ifile, "tags.xml"); #opens the results of our API search @filearray = ; #loads the contents of the API search into an array close(ifile); #unlink("tags.xml"); #delete file once we are done with it so that it doesn't show up for next band foreach $fileline (@filearray) {#go through each line in XML file searching for genre tags chomp($fileline); if ($fileline =~ m/\/) {#a tag is here (print "\nPossible Tag - $fileline") if ($debug); $tag = $fileline; chomp($tag); $tag =~ s/\s+/ /; #turns multiple whitespace into single space $tag =~ s/\//g; #strips tag $tag =~ s/\<\/name\>//g; #strips tag $tag =~ s/^\s+//;#strip leading spaces $tag =~ s/\s+$//;#strip trailing spaces #here we filter stupid tags, some ideas, band's name, curses, common tags, words count > 3 if ($tag !~ m/$file/ && # no need to tag band's name $tag !~ m/fuck/i && #probably not a descriptive tag $tag !~ m/albums/i && #albums i own $tag !~ m/seen/i && #seen em live $tagcount < $maxtags) { $testcount = ($tag =~ tr/ //); #remove all this later, or don't if ($testcount >= 2) {#quick test to see if long tags tend to be valid open(ofile, ">>longtags.txt"); #output the line print ofile ("\n$tag"); #testing close(ofile); } (print "\nProbable Tag - $tag") if ($debug); $tagcount++; #count this tag if ($tagcount == 1) {#first tag, add without delimiter $taglist = $tag; } else {#adds this tag plus the delimiter to the list $taglist .= $tagseparator . $tag; } } } } (print "\nTaglist - $taglist") if ($debug); open(ofile, ">>addtags.bat"); #output the line print ofile ("\ntag --ape2 --recursive --caps --genre \"$taglist\" \"$file\\*.*\""); #hopefully $taglist is a list of genres, and $file is the bands name/directory close(ofile); } } system ("PAUSE"); print "\nDone!\n";