diff options
author | Christian Pointner <equinox@spreadspace.org> | 2015-09-27 04:15:37 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2015-09-27 04:15:37 (GMT) |
commit | 6f540322a1a3db6fdcdf309b998c0c858696afac (patch) | |
tree | f891d8e7fdff8c50a83c17c4ed092c5a58ed6aa0 /rhautoimport-dn | |
parent | 81531b51fe70fee446d22d3505d04709cd7b5b8a (diff) |
democracy now importer almost done
Diffstat (limited to 'rhautoimport-dn')
-rwxr-xr-x | rhautoimport-dn | 113 |
1 files changed, 33 insertions, 80 deletions
diff --git a/rhautoimport-dn b/rhautoimport-dn index 9e54460..de41f37 100755 --- a/rhautoimport-dn +++ b/rhautoimport-dn @@ -31,7 +31,6 @@ use XML::Feed::Enclosure; use URI::URL; use IO::Handle; use IPC::Open3; -use RHRD::rddb; use lib '/usr/local/share/rhautoimport/'; use rhautoimport; @@ -40,12 +39,9 @@ my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-dn.stat"; my $FILES_RSS_URL = "http://www.democracynow.org/podcast-stations.xml"; my $HEADLINES_RSS_URL = "http://www.democracynow.org/podcast.xml"; my $IGNORE_HEADLINE = 0; +my $RD_GROUP = "democracyn"; my $PV_ID = '111'; -binmode(STDIN, ":utf8"); -binmode(STDOUT, ":utf8"); -binmode(STDERR, ":utf8"); - if($#ARGV >= 0 && $ARGV[0] eq 'last') { print "!!!This is the last attempt, there won't be a retry on error!!!\n" } @@ -55,24 +51,6 @@ if($#ARGV >= 0 && $ARGV[0] eq 'noheadline') { $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-dn-noheadline.stat"; } -my $user = `/usr/bin/id -un`; -$user =~ s/\n//; -my $group = "democracyn"; - -my ($dbh, $errorstring) = RHRD::rddb::opendb(); -if(!defined $dbh) { - print "$errorstring\n"; - exit 1; -} -my @allowed_dbs = RHRD::rddb::get_dropboxes($dbh, $user, $group); -if(!defined $allowed_dbs[0] && defined $allowed_dbs[2]) { - print "$allowed_dbs[2]\n"; - exit 1; -} - -my $dropbox = ""; -my $to_cart = 0; - my @today = Date::Calc::Today(); my @yesterday = Date::Calc::Add_Delta_Days($today[0], $today[1], $today[2], -1); my @tomorrow = Date::Calc::Add_Delta_Days($today[0], $today[1], $today[2], 1); @@ -84,21 +62,30 @@ if(localtime->hour >= 12) { @broadcast_day = @today; @import_day = @tomorrow; } +my $dow = Date::Calc::Day_of_Week(@import_day); +$dow = 0 unless $dow < 7; print "day of original broadcast: " . Date::Calc::Date_to_Text(@broadcast_day) . "\n"; print "day of Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@import_day) . "\n"; +my $show_id = -1; +my $show_title = ""; + +my @allowed_dbs = rhautoimport::get_dropboxes($RD_GROUP); +if(!defined $allowed_dbs[0] && defined $allowed_dbs[1]) { + print "$allowed_dbs[1]\n"; + exit 1; +} + for my $href ( @allowed_dbs ) { - my $dow = Date::Calc::Day_of_Week(@import_day); - if ($dow == int(substr($href->{'NAME'},0,2))) { - $dropbox = $href->{'PATH'}; - $to_cart = $href->{'TO_CART'}; + if($href->{'TYPE'} eq "show" && $dow == $href->{'SHOWDOW'}) { + $show_title = $href->{'SHOWTITLE'}; + $show_id = $href->{'SHOWID'}; } } -if($dropbox eq "") { +if($show_id < 0) { print "no dropbox for day in question\n"; - RHRD::rddb::closedb($dbh); exit 0; } @@ -111,44 +98,33 @@ print " -> $FILES_RSS_URL\n"; my $files_feed = XML::Feed->parse(URI->new($FILES_RSS_URL)) or die "Error fetching feed: " . XML::Feed->errstr; +my $uri = ""; my $file = ""; -my $out_file = ""; for my $entry ($files_feed->entries) { if($entry->enclosure && $entry->enclosure->type eq "audio/mpeg") { if($entry->enclosure->url =~ /^https?:\/\/.*\.democracynow\.org\/dn([0-9-]+)-1\.mp3$/) { next if($yc ne $1); - my $url = new URI::URL($entry->enclosure->url); - my @path = $url->path_components; + $uri = new URI::URL($entry->enclosure->url); + my @path = $uri->path_components; my $current_file = `cat $STAT_FILE`; if($current_file eq $path[-1]) { print "Already downloaded file of day in question\n"; - RHRD::rddb::closedb($dbh); exit 0; } - $out_file = $path[-1]; - if(!rhautoimport::check_file_extension($out_file)) { - print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n"; - RHRD::rddb::closedb($dbh); + $file = $path[-1]; + if(!rhautoimport::check_file_extension($file)) { + print "\n\nThe extension of the matching file '". $file . "' seems to be wrong - manual import necessary!!!\n"; exit 1; } - print $1 . ": downloading " . $entry->enclosure->url . " (" . $entry->enclosure->length . " Bytes) .. "; - - $file = "/tmp/" . $out_file; - system("wget", "--quiet", "--no-check-certificate", $url, "-O", $file); - if( $! != 0) { - die "wget returned with error: " . $!; - } - print "ok\n"; - last; } } } -if($file eq "" || !(-e "$file")) { - print "No Entry found from day in qeustion or download error - "; +if($uri eq "") { + print "No Entry found from day in question or download error - "; if($#ARGV >= 0 && $ARGV[0] eq 'last') { print "giving up, manual import necessary!!!\n"; } else { @@ -193,39 +169,16 @@ unless($IGNORE_HEADLINE) { print "\n"; } -print "converting $file ... "; -my $infile = $file; -$file =~ s/\.mp3$/.wav/; -system("ffmpeg", "-i", $infile, "-acodec", "pcm_s16le", $file); -if( $! != 0) { - die "ffmpeg returned with error: " . $!; -} -print "ok\n"; -unlink $infile; - -print "will import $file to dropbox $dropbox (cart=$to_cart)\n\n"; - -my $error_cb = sub { - my ($text) = @_; +print "will import '$uri' to show $show_id, $show_title\n\n"; - print "\n$text"; - return 0; -}; - -rhautoimport::check_key_file() or die "Import Key not found, use rhautoimport-create-id to create one\n"; - -my $ret; -my $log; -$log = rhautoimport::clear_carts($dbh, $group, $to_cart); -my $import_log; -($ret, $import_log) = rhautoimport::import_single($file, $dropbox, $user, 0, $error_cb); -$log .= $import_log; - -RHRD::rddb::closedb($dbh); - -unlink $file; +my ($ret, $log) = rhautoimport::clear_carts($show_id); +$log = '' unless defined $log; +if($ret == 0) { + ($ret, my $import_log) = rhautoimport::import_uri($show_id, $uri); + $log .= $import_log if defined $import_log; +} -if(!$ret) { +if($ret) { print "\nImport Error:\n"; print $log; exit 1; @@ -233,7 +186,7 @@ if(!$ret) { unlink($STAT_FILE); open(my $fhs, '>', $STAT_FILE); -print $fhs $out_file; +print $fhs $file; close($fhs); exit 0; |