|Do you know where your variables are?|
Save MP3 Tag's to Database with SPOPSby jeffa (Bishop)
|on Jan 07, 2002 at 23:51 UTC||Need Help??|
I have just begun to learn SPOPS by our own lachoy. Last night (after RTFM'ing for a good two hours), i finally made some progress. The result is a script that reads MP3 files in a given directory and stores their ID3 tag's into a database table. The table i used has an 'id' field which is an auto incremented primary key, and five other fields (title, artist, album, year, and genre) which can be any database type you want. Mine were just varchars that can be NULL, for simplicity's sake.
SPOPS stands for Simple Perl Object Persistence with Security. SPOPS is not one module, but instead a collection of almost FIFTY modules! There is a lot to learn up front, and this post will not go deep into SPOPS (it barely scratches the surface). This post only covers instantiating fairly simple objects that can communicate to a database, it doesn't cover persistence. A review and a tutorial for SPOPS are 'in the works'. Think of this as just a primer.
This code uses File::Find and MP3::Info to find the MP3 files and extract the tag info, respectively. With these two tools, it is easy to write DBI code to store the found tags into a database. SPOPS is used here to abstract the SQL completely away from the client. This is currently implemented in SPOPS by subclassing a SPOPS::DBI object:
This subclassed SPOPS::DBI class can be reused by different clients. The client that i wrote uses the SPOPS::Initialize object to create my MySPOPS::MP3 object.
I use File::Find in a similar manner as the code from the Perl Cookbook, recipe number 9.7. If no argument is supplied, then the current directory is recursively scanned.
MP3::Info is used to obtain the MP3 tag from the file. I have not bothered to validate in the interest of keeping the code simple. Adding validation should be trivial, see Identifying MP3 files with no MP3 tag for some tips on that. Another possiblity is to use CDDB.
The trick is the instantiation of the MySPOPS::MP3 object. If you compare the Data::Dumper outputs of a MP3::Info with the Dumper output of what a MySPOSP::MP3 object _should_ look like, you will see that they are very similar:
The MySPOPS::MP3 constructor accepts a hash reference as an argument and will use that hash reference to define it's attributes. All that is needed is to lower case the keys of the MP3::Info object and the two will have virtually the same keys ('comment' will be ignored because it is not in the configuration and 'id' is not needed because it will be handled for you).
So, the MySPOPS::MP3 object is instantiated with a transformed copy of the MP3::Info's internal attribute hash. I could have have named my database table fields with all upper case letters and there would be no need for the transformation.
Finally, a message is printed to standard error and the MySPOPS::MP3::save() method is called, which stores the object's attributes in the database.
The next version of SPOPS (0.56) will allow you skip having to subclass a SPOPS::DBI object and simply pass the the connection credentials along with your configuration:
But this should be used for 'one-offs' only. By subclassing SPOPS::DBI you allow other clients to share and have the databse connection code abstracted away.
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle)