Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modify rss perl program to add fields from ID3 header of MP3

Status
Not open for further replies.

cgonan

MIS
Feb 23, 2005
39
US
I have a program that generates an rss feed by reading each line in a linux directory which contains mp3s and printing it out. This works fine.

I was asked to modify the program so that some of the fields in the MP3s ID3 tag are included on the line. I was able to write a program to extract the fields I need in the tag, so I knew I had the right info. (A section is below). I just want to add those fields to the first program so that they print on the same line with the other mp3 info on the rss feed. When I tried to incorporate it into the rss program, I get errors that I do not understand. I am not a perl programmer but my background and the internet allowed me to get the first part done.

Any help would be appreciated.

Thanks so much.

*******************
## my code which pulls the fields I need from the ID3 tag
foreach $frame (keys %$frames)
{
($value, $desc) = $mp3->{ID3v2}->get_frame($frame);

SWITCH: {
$frame eq 'TIT2' && do {
$mp3title = $value;
last SWITCH;
};
$frame eq 'TYER' && do {
$mp3year = $value;
last SWITCH;
};
last SWITCH;
};
}

## current program to generate rss with mp3s in directory
#ADD THE ITEMS TO THE RSS DOCUMENT.

my $counter = 0;
foreach my $file ( @sortedfileinfo ) {
if ( $counter > $maxItems ) {
last;
}
if (!($file->{filename} =~ m/\.([^.]+)$/) || !$mimeTypes->{$1}) {
#Skip files without extensions or whose extensions we don't know about.
next;
}
my $item = $doc->createElement('item');
my $title = $doc->createElement('title');
$title->appendChild($doc->createTextNode($file->{filename}));
$item->appendChild($title);
my $url = $urlBase . '/' . $file->{filename};
my $guid = $doc->createElement('guid');
$guid->appendChild($doc->createTextNode($url));
$item->appendChild($guid);
my $enclosure = $doc->createElement('enclosure');
$enclosure->setAttribute('url', $url);
$enclosure->setAttribute('length', $file->{size});
$enclosure->setAttribute('type', $mimeTypes->{$1});
$item->appendChild($enclosure);
$channel->appendChild($item);
$counter++;

}

print $xml_pi->toString;
print $rss->toString;
print "\n";
-----------------------------------------------------------
 
Personally, I would add your ID3 tag code into a sub routine that gets called and returns either a listing of the data you need so you can parse it into the RSS or just adds the appropriate fields to the RSS feed itself. Either would work.

Psuedo code below:
Code:
my $counter = 0;
foreach my $file ( @sortedfileinfo ) {
    if ( $counter > $maxItems ) {
    last;
    }
    if (!($file->{filename} =~ m/\.([^.]+)$/) || !$mimeTypes->{$1}) {
        #Skip files without extensions or whose extensions we don't know about.
    next;
    }
    my $item = $doc->createElement('item');
    my $title = $doc->createElement('title');
    $title->appendChild($doc->createTextNode($file->{filename}));
    $item->appendChild($title);
    my $url = $urlBase . '/' . $file->{filename};
    my $guid = $doc->createElement('guid');
    $guid->appendChild($doc->createTextNode($url));
    $item->appendChild($guid);
    my $enclosure = $doc->createElement('enclosure');
    $enclosure->setAttribute('url', $url);
    $enclosure->setAttribute('length', $file->{size});
    $enclosure->setAttribute('type', $mimeTypes->{$1});
    my @id3Info = addID3(\$file->{filename});
    foreach my $data (@id3Info) {
        $enclosure->setAttribute('id3Tag[x]', $data);
    }
    $item->appendChild($enclosure);
    $channel->appendChild($item);
    $counter++;

}

sub addID3() {
my $filename = shift;
$filename = $$filename;

foreach $frame (keys %$frames)
{
      ($value, $desc) = $mp3->{ID3v2}->get_frame($frame);

SWITCH: {
        $frame eq 'TIT2' && do {
    $mp3title = $value;
        last SWITCH;
        };
        $frame eq 'TYER' && do {
        $mp3year = $value;
        last SWITCH;
        };
        last SWITCH;
        };
}
}

- George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top