Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

eMusic.com filename fixerupper

by sedhed (Scribe)
on Jul 12, 2002 at 07:49 UTC ( #181227=CUFP: print w/ replies, xml ) Need Help??

Note: this was written for a <duck> windows computer, and is not directly portable without a few edits... I run it as a service under win2k. You can also run it from command-line with -d, it will just process once and quit.

Note 2: For them what don't know, eMusic.com is a great DRM-free mp3 service. Their jazz collection is huge. They just don't offer much customizability in the naming of files, and I like mine [artist - album] 01 songname.mp3" hence....

#!/usr/bin/perl # emusicfix.pl # # Will watch a specified folder for new MP3 files downloaded from emus +ic.com # and fix the filenames, then place them in their own directories by a +rtist. ################################################## # Dir where you want the artist directories created: my $basedir = "d:\\music\\"; # Dir where you will place newly-downloaded files: my $watch = "d:\\music\\em_save\\"; # Delimiter used by the filenames to separate artist, track etc.: my $delimiter = '-'; # Field orders: # The default is for names like: # Artist_Name-Album_Name-Track_Name-Song_Name.mp3 my %f = ( artist => 0, album => 1, track => 2, song => 3, ); # That's it! my $count = 0; my $debug = 0; if ($ARGV[0] =~ /-d/) { $debug = 1; processdir(); } else { daemonize(); } # You may want to consider running this as a service under win2k et al +. sub daemonize { while (1) { #run forever processdir(); sleep 20; } } sub processdir { my (@list); $count = 0; for (@list = sort `dir /b $watch\\*.mp3`) { chomp; fixfile($_) unless ($_ =~ /.*File Not Found.*/i); } if ($debug) { if ($count) { print "Moved $count files\n"} } } sub fixfile { my $filename = shift; chomp $filename; die "Bad infile" unless ($filename =~ /\.mp3$/); my (@file) = split/-/,$filename; for (@file) {$_ =~ s/_/ /g;} my $newfilename = '['.$file[$f{artist}].' - '.$file[$f{album}].'] +'.$file[$f{track}].' '.$file[$f{song}].''; $newfilename =~ s/ s /\'s /gi; $newfilename =~ s/ m /\'m /gi; my $source = $watch.$filename; my $dest = $basedir.$file[$f{artist}].'\\'.$newfilename; if (! makedir($basedir.$file[$f{artist}])) { print "Skipping file, can't make dir for $file[$f{artist}]" if + $debug; next; } if (! -e $dest) { my $result = `copy "$source" "$dest"`; if ($result =~ /1 file\(s\) copied\./) { $count++; print "$dest [OK]\n" if $debug; $del = `del "$watch$filename"`; print "Error deleting: $del\n" if ($del && $debug); }else{ print "Can't copy\n $source to\n $dest\n" if $debug; } } else { print "Skipping existing $filename\n" if $debug; } } sub makedir { my $dir = shift; if (-e $dir) { return 1; }else{ return mkdir $dir; } }

Comment on eMusic.com filename fixerupper
Select or Download Code
Re: eMusic.com filename fixerupper
by John M. Dlugosz (Monsignor) on Jul 12, 2002 at 19:38 UTC
    I've had some success using the Win32 functions to watch for directory changes, using Perl. Instead of scanning the directory every 20 seconds, it is awakened when there is a change.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://181227]
Approved by smitz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2014-09-16 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (157 votes), past polls