smgfc has asked for the wisdom of the Perl Monks concerning the following question:

As a follow up to the mp3 id3 cataloger I posted to SoPW earlier I decided to post the cgi that searchs and returns results from the catalog.

There are some issues i am trying to work out. Right now, the catalog is plain text file with the format:

artist::album::title::track number::path
first of all it isnt very fast. I thought of a couple of ways to speed it up though.

Since the list of mp3s is alphabatized by artist it wouldnt be hard to add a line at the top of the file that specified the start of a particular character, and then jump there when an artist is specified. i.e. someone searchs for 'Fabolous' as the artist, and the F's start on the 200th line, and go till the 240th, i could limit the search to 40 lines rather easily. Another way would be to use a database instead of a plain file. This is really a bigger step then I want to take right now, if only because I dont want to deal with databases under OS X (MySQL got messed up when i tried to install it, even from the package). But if a database is the only way (or the only workable way) then I am more then willing to get MySQL or something similar working.

However, if someone searchs only by album or title, not artist, this is breaks. You could have a couple lines of code at the beginning of the plain file mapping albums to artist, but this is getting overly complicated, and will not work for title (at least not performance wise).

On the feature side of things I would like a search for artist alone to display that artists albums with the correct track order. This I dont know how to do with out reading the matches into an array of array (albums for artist and tracks for album) which is again a little more then I would like to do. But this is a important feature, so I will probably implement it this way anyway. Or, since the performance of the cataloger isn't important, I could have the cataloger just print the id3 info in correct order in the first place.

Another thing that has been requested is approx. searching. i.e. if you search for 'Camron' vs. 'Cam'ron' or 'Jay-z' vs. 'Jay z' vs. 'Jayz'. Is there some module to do this? I would like to avoid re-inventing the wheel, and this feature is pretty important so the a stable module based wheel is probably better then any wobbly code based wheel i could hack together.

A quick note on the formatting. I hate unordered lists, and plan to replace the visual/data repersentation of the results soon with something nicer (tables). If you have any suggestions, that would be great, but that is not paramount to sucess.

Thanks alot! Beware, the code is ugly! Any suggestions are really welcome! Code is below
#!/usr/bin/perl -w use strict; use CGI; my ($q, $artist, $title, $album, $file, $arti, $albu, $tite, $track, $ +path, $results); $file = "/Library/Webserver/Documents/mp3.info"; $q = new CGI; $artist = $q->param( "artist" ); $album = $q->param( "album" ); $title = $q->param( "title" ); if ( length($artist) > 20 or length($title) > 20 or length($album) > 2 +0) { display_page( "I am sorry but the was a malformed query" ); exit; } open (FILE, $file) or die ("Can't open $file: $!\n"); while (<FILE>) { ($arti, $albu, $tite, $track, $path) = $_ =~ m/^(.*?)::(.*?)::(.*? +)::(.*?)::(.+?)$/; if ($arti =~ /*+?$artist*+?/i or $tite =~ /*+?$title*+?/i or $albu + =~ /*+?$album*+?/i) { $results .= $q->li( $q->a( { href => $path }, "$artist - $title") ); } } close (FILE); print $q->header, $q->start_html, $q->h1("Results for $artist :: $title"), $q->ul( $results || "No results found" ), $q->end_html; sub display_page { my $message = shift; print $q->header, $q->start_html, $q->p( $message ), $q->end_html; }

Edit kudra, 2002-05-30 Made reference to previous post a link.

Replies are listed 'Best First'.
(jeffa) Re: MP3/CGI Search Features
by jeffa (Bishop) on May 30, 2002 at 03:20 UTC
Re: MP3/CGI Search Features
by BUU (Prior) on May 30, 2002 at 02:25 UTC
    If you want to do 'real' sorting and searching of various kinds, your going to want to go with a database, i would recommend mysql, but you might be able to get by with some other smaller database, dunno, berkley db or something? And as for the searches, mysql supports LIKE queries that should do more or less what your looking for (with some creative wild cards)