Commit 2423c8c5 authored by Bernhard Geier's avatar Bernhard Geier

first commit

This diff is collapsed.
# FM4 7-Tage Backup
The Austrian radio station FM4 publishes MP3 recordings of all of their shows during the last 7 days on its website.
This script is a simple command line interface for their player API, and allows you to download all currently available recordings for a specific show.
```./ "morning show" Downloads/Morning-Show-Recordings```
This would download all available recordings of "Morning Show" and save them in the "Downloads/Morning-Show-Recordings" directory.
Recordings get correct id3 tags, files aready downloaded are skipped, so it should be well suited for cron jobs.
use strict;
use warnings;
use WWW::Mechanize;
use URI::Encode;
use JSON;
use POSIX;
use HTML::Strip;
use MP3::Tag;
use Unicode::String qw(utf8 latin1);
if (@ARGV ne 2) {
print "USAGE:\n";
print $0." <ShowTitle> <downloadDir>\n\n";
print "Example:\n";
print $0.' "Morning Show" "Downloads/Morning Show Recordings"'."\n";
die("Directory ".$DESTDIR." does not exist!\n") unless (-d $DESTDIR);
my $searchUrl="";
my $shoutcastBaseUrl="";
my $browser=WWW::Mechanize->new(timeout=>5);
my $removeHtml=HTML::Strip->new();
my $result=JSON->new()->utf8->decode($browser->content());
foreach (@{$result->{'hits'}}) {
my $data=JSON->new()->utf8->decode($browser->content());
my $title=$data->{'title'};
next unless $title=~/$SENDUNG/i; # Filter out results not containing the query string
my $broadcastDate=POSIX::strftime("%Y-%m-%d",localtime($data->{'start'}/1000));
my $description=$removeHtml->parse(($data->{'description'}) or $broadcastDate);
my @streams=(sort { $a->{'start'} cmp $b->{'start'}} @{$data->{'streams'}}); # for multi-part shows sort them by start time...
for (my $i=0; $i<@streams; $i++) {
my $tagTitle="FM4 ".$title." ".$broadcastDate;
$tagTitle.=" [".($i+1)."/".@streams."]" if @streams>1; # ...and add "[currentPartNo/totalParts]" to title
print $tagTitle;
my $filename=$tagTitle.".mp3";
if (-f $DESTDIR."/".$filename) {
print " already exists, skipping.\n";
print " downloading...";
my $tag=MP3::Tag->new($DESTDIR."/".$filename);
$tag->new_tag("ID3v2") unless (exists $tag->{ID3v2});
print " done.\n";
Markdown is supported
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