Helios

 view release on metacpan or  search on metacpan

eg/Helios-App-MP3IndexerService/lib/Helios/App/MP3IndexerService.pm  view on Meta::CPAN

	
	eval {
		$self->logMsg($job, LOG_INFO, "Filename: $filename");
		unless (-r $filename ) { Helios::Error::InvalidArg->throw("Cannot read file $filename"); }

		# parse the MP3 tags and info
		my $mp3i = $self->parseMP3Info($filename);
		$self->logMsg($job, LOG_INFO, 'Parsed artist: '.$mp3i->artist.' title: '.$mp3i->title.' album: '.$mp3i->album);		
		if ($self->debug) { print Dumper($mp3i),"\n"; }

		# update the database with the new MP3 info		
		$self->updateDb($filename, $mp3i);

		# mark the job as completed successfully		
		$self->completedJob($job);
		1;
	} or do {
		# uhoh, an error occurred
		# we'll log an error message, and mark the job as failed
		my $E = $@;
		$self->logMsg($job, LOG_ERR, "$E");
		$self->failedJob($job,"$E");
	};
	
}

=head1 OTHER METHODS

=head2 parseMP3Info($filename)

Given a filename, parseMP3info() returns an MP3::Info object with information 
on the given MP3 file.

=cut

sub parseMP3Info 
{
	my $self = shift;
	my $filename = shift;
	
	return MP3::Info->new($filename);	
	
}


=head2 updateDb($filename, $mp3info_object)

Given the MP3 file and the MP3::Info object with that file's info, updateDb() 
adds the information on that MP3 to the database table, either through 
an UPDATE (if the filename is already in the table) or an INSERT (if the 
filename isn't already there).

Note that this method uses the Helios::Service->dbConnect() method to connect 
to the database, and uses database connection information set in the Helios 
configuration subsystem.  All the method need do is call the getConfig() 
method, and it has a hash with all of the configuration parameters for the 
current application running on the current host.

=cut 

sub updateDb 
{
	my $self = shift;
	my $filename = shift;
	my $mp3i = shift;
	my $config = $self->getConfig();

	my $sql;
	my $sth;

	# connect to the database	
	my $dbh = $self->dbConnect($config->{mp3db_dsn}, $config->{mp3db_user}, $config->{mp3db_pass});
	# is the file already in the table?
	my $fc = $dbh->selectrow_arrayref("SELECT COUNT(*) FROM mp3_index_tb WHERE pathname = ?", undef, ($filename));
	
	if ($fc->[0]) 
	{
		# this file is already in the table so we'll UPDATE it
		$sql = <<ENDUPDATESQL;
UPDATE mp3_index_tb 
SET
	artist = ?,
	title = ?,
	album = ?,
	tracknum = ?,
	genre = ?,
	year = ?,
	tracktime = ?,
	tracksize = ?,
	bitrate = ?,
	tagversion = ?,
	comment = ?
WHERE
	pathname = ?		
ENDUPDATESQL
		$sth = $dbh->prepare_cached($sql);
		$sth->execute(
			$mp3i->artist,
			$mp3i->title,
			$mp3i->album,
			$mp3i->tracknum,
			$mp3i->genre,
			$mp3i->year,
			$mp3i->time,
			$mp3i->size,
			$mp3i->bitrate,
			$mp3i->version,
			$mp3i->comment,
			$filename
		);
		$sth->finish();
		$self->logMsg($self->getJob(), LOG_INFO,"Updated $filename in database");
	}
	else
	{
		# it's not yet in the table, so we'll INSERT it
		$sql = <<ENDSQL;
INSERT INTO mp3_index_tb
	(pathname, artist, title, album, tracknum, genre, year, tracktime, tracksize, bitrate, tagversion, comment)
VALUES
	(?,?,?,?,?,?,?,?,?,?,?,?)



( run in 0.618 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )