Commit d5a59aed authored by Citronalco's avatar Citronalco
Browse files

first commit

parents
This diff is collapsed.
# MP3 to Atom Feed
Creates a RSS Atom feed for all MP3 files in a given directory, suitable for the usual podcast players.
### Synopsis ###
./mp3-to-atomfeed.pl <mp3 files directory> <feed title> <mp3 files url>
### Example ###
Your MP3 files are in directory "/data/mp3files/" and accessible via https below "https://www.example.net/public/podcast/".
The following would create a valid RSS Atom feed with the title "My Podcast":
```./mp3-to-atomfeed.pl "/data/mp3files/" "My Podcast" "https://www.example.net/public/podcast"```
The script saves the feed file as "podcast.xml" in the MP3 files' directory.
"podcast.xml" contains all MP3 files from "/data/mp3files" sorted from the newest files to the oldest.
Titles and descriptions get read from the MP3 files (ID3 tags title and comment).
\ No newline at end of file
#!/usr/bin/perl
use strict;
use warnings;
use File::Find::Rule;
use MP3::Info;
use MP3::Tag;
use URI::file;
use POSIX;
use XML::Writer;
use utf8;
use Unicode::String qw(utf8 latin1);
if (@ARGV ne 3) {
print "USAGE:\n";
print $0." <directory with MP3 files> <feed title> <url to directory with MP3 files>\n\n";
print "Example:\n";
print $0.' /data/public/yoga-sessions-2017/ "My Yoga Podcast 2017" https://example.com/yoga-sessions-2017/'."\n";
print 'This would create a file "/data/public/yoga-sessions-2017/podcast.xml", which should also be available via https://example.com/yoga-sessions-2017/podcast.xml to get things going.'."\n";
exit;
}
my $DIR=$ARGV[0];
my $FEEDTITLE=$ARGV[1];
my $URLBASE=$ARGV[2];
my $LINK=$URLBASE."/podcast.xml";
my $NOW=POSIX::strftime("%a, %d %b %Y %T %z",localtime);
open my $RSS, '>', $DIR."/podcast.xml" or die($!);
my $xml=XML::Writer->new(OUTPUT=>$RSS,DATA_MODE=>1,DATA_INDENT=>1);
$xml->xmlDecl('utf-8');
$xml->startTag('rss','version'=>'2.0','xmlns:itunes'=>"http://www.itunes.com/dtds/podcast-1.0.dtd",'xmlns:atom'=>"http://www.w3.org/2005/Atom");
$xml->startTag('channel');
$xml->dataElement('link'=>$URLBASE);
$xml->emptyTag('atom:link', 'href'=>$LINK, 'rel'=>'self', 'type'=>'application/rss+xml');
$xml->dataElement('language'=>'de');
$xml->dataElement('pubDate'=>$NOW);
$xml->dataElement('title'=>$FEEDTITLE);
$xml->dataElement('description'=>$FEEDTITLE);
$xml->dataElement('itunes:summary'=>$FEEDTITLE);
my @files;
foreach (File::Find::Rule->file()->name('*.mp3')->in($DIR)) {
my %fileinfo;
$fileinfo{'name'}=$_;
$fileinfo{'mtime'}=(stat($fileinfo{'name'}))[10] or die($!);
$fileinfo{'mdatetime'}=POSIX::strftime("%a, %d %b %Y %T %z",localtime((stat($fileinfo{'name'}))[10]));
$fileinfo{'size'}=(stat($fileinfo{'name'}))[7] or die($!);
$fileinfo{'url'}=$URLBASE."/".URI::file->new(File::Spec->abs2rel($fileinfo{'name'},$DIR));
my $mp3=MP3::Tag->new($fileinfo{'name'}) or die($!);
$mp3->get_tags;
$fileinfo{'desc'}=latin1($mp3->comment())->utf8;
$fileinfo{'title'}=latin1($mp3->title())->utf8;
$fileinfo{'duration'}=$mp3->total_millisecs_int();
$fileinfo{'itunes-duration'}=POSIX::strftime("%H:%M:%S",gmtime(int($mp3->total_millisecs_int()/1000)));
push(@files,\%fileinfo);
}
foreach my $file (reverse(sort { $a->{'mtime'} cmp $b->{'mtime'} } @files )) {
$xml->startTag('item');
$xml->dataElement('title'=>$file->{'title'});
$xml->dataElement('description'=>$file->{'desc'});
$xml->dataElement('itunes:summary'=>$file->{'desc'});
$xml->emptyTag('enclosure','url'=>$file->{'url'}, 'type'=>'audio/mpeg', 'length'=>$file->{'size'});
$xml->dataElement('guid'=>$file->{'url'});
$xml->dataElement('pubDate'=>$file->{'mdatetime'});
$xml->dataElement('itunes:duration'=>$file->{'itunes-duration'});
$xml->endTag('item');
}
$xml->endTag('channel');
$xml->endTag('rss');
close $RSS;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment