From 15241d289910aed8981bbe3975c996fa21b411b8 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Thu, 28 Dec 2017 16:12:07 +0100
Subject: renewed x/xy importer


diff --git a/INFO b/INFO
index 7127f00..416cb37 100644
--- a/INFO
+++ b/INFO
@@ -85,3 +85,12 @@
          gefunden wird -> last attempt und nix importieren.
    RH: 4wöchentlich, Woche 3, Dienstag, 16:30-17:00
        4wöchentlich, Woche 2, Mittwoch, 14:30-15:00, Wiederholung
+
+- (xy) X_XY (Un)gelöst und (Un)erhört!:
+   https://cba.fro.at/series/x_xy-ungeloest-und-unerhoert/feed
+
+   Original: 2wöchentlich, Woche 2/4, Freitag
+   Upload: oft ein paar Tage davor, manchmal erst 1 Tag später
+   Such-Kriterium: Dateiname bgeinnt mit 'dd-mm-YYYY'
+   RH: 4wöchentlich, Woche 4, Dienstag, 13:00-14:00
+          (wir übernehmen die aktuellste Sendung)
diff --git a/TODO b/TODO
index 92626a9..ecabcd5 100644
--- a/TODO
+++ b/TODO
@@ -1,12 +1,3 @@
-- (xy) X_XY (Un)gelöst und (Un)erhört!:
-   https://cba.fro.at/series/x_xy-ungeloest-und-unerhoert/feed
-
-   Original: 2wöchentlich, Woche 2/4, Freitag
-   Upload: oft ein paar Tage davor, manchmal erst 1 Tag später
-   Such-Kriterium: Dateiname bgeinnt mit 'dd-mm-YYYY'
-   RH: 4wöchentlich, Woche 4, Dienstag, 13:00-14:00
-          (wir übernehmen die aktuellste Sendung)
-
 - (dm) das mensch. gender_queer on air:
    https://cba.fro.at/series/das-mensch-gender_queer-on-air/feed
 
diff --git a/rhautoimport-xy b/rhautoimport-xy
index b5e046c..36722c3 100755
--- a/rhautoimport-xy
+++ b/rhautoimport-xy
@@ -42,6 +42,7 @@ my $PV_ID = '608';
 my $TITLE="X_XY (Un)gelöst und (Un)erhört!";
 my $LAST_RUN = 0;
 my $upload_delay_days = 1;   # files are usally there at least 1 day after original broadcast
+my $last_resort_threshold = 2; # also consider older file when there are only 2 days until the RH broadcast
 
 binmode(STDIN, ":utf8");
 binmode(STDOUT, ":utf8");
@@ -50,7 +51,7 @@ binmode(STDERR, ":utf8");
 my $curweek = RHRD::utils::get_rd_week();
 
 if($#ARGV >= 0 && $ARGV[0] eq 'last') {
-  if($curweek == 2 || $curweek == 4) {
+  if($curweek == 4) {
     print "!!!This is the last attempt, there won't be a retry on error!!!\n";
     $LAST_RUN = 1;
   }
@@ -76,32 +77,44 @@ my @today = Date::Calc::Today();
 my @import_date = Date::Calc::Standard_to_Business(@today);
 my $dow = $import_date[2];
 @import_date = Date::Calc::Business_to_Standard($import_date[0], $import_date[1], 2);
-if($dow <= 2) {
-  if($curweek == 1 || $curweek == 3) {
-    @import_date = Date::Calc::Add_Delta_Days(@import_date, 7);
-  }
-} else {
+if($curweek == 1) {
+  @import_date = Date::Calc::Add_Delta_Days(@import_date, 21);
+} elsif ($curweek == 2) {
+  @import_date = Date::Calc::Add_Delta_Days(@import_date, 14);
+} elsif ($curweek == 3) {
   @import_date = Date::Calc::Add_Delta_Days(@import_date, 7);
-  if($curweek == 2 || $curweek == 4) {
-    @import_date = Date::Calc::Add_Delta_Days(@import_date, 7);
-  }
+} elsif($dow > 2) {
+  @import_date = Date::Calc::Add_Delta_Days(@import_date, 28);
 }
 
-my @broadcast_date = Date::Calc::Add_Delta_Days(@import_date, -11);
+my @broadcast_dateW2 = Date::Calc::Add_Delta_Days(@import_date, -11);
 print "today: " . Date::Calc::Date_to_Text(@today) . "\n";
 print "day of next Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@import_date) . "\n";
-print "day of latest original broadcast before next Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@broadcast_date) . "\n";
+print "day of latest original broadcast before next Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@broadcast_dateW2) . "\n";
 
-if(Date::Calc::Delta_Days(@broadcast_date, @today) <= $upload_delay_days) {
+if(Date::Calc::Delta_Days(@broadcast_dateW2, @today) <= $upload_delay_days) {
   print "File won't be available by now!\n";
   exit 42;
 }
 
+my $includeW4 = 0;
+my @broadcast_dateW4 = Date::Calc::Add_Delta_Days(@broadcast_dateW2, -14);
+if(Date::Calc::Delta_Days(@import_date, @today) <= $last_resort_threshold) {
+  $includeW4 = 1;
+  print " ... it's already late, also including show from: " . Date::Calc::Date_to_Text(@broadcast_dateW4) . "\n";
+}
+
 my $id = sprintf("%04d-%02d-%02d", @import_date);
-my $bd = sprintf("%04d-%02d-%02d", @broadcast_date);
-my $bdfile = sprintf("^0?%d-?0?%d-?%04d", $broadcast_date[2], $broadcast_date[1], $broadcast_date[0]);
+my $bdW2 = sprintf("%04d-%02d-%02d", @broadcast_dateW2);
+my $bdW4 = sprintf("%04d-%02d-%02d", @broadcast_dateW4);
+my $bdfileW2 = sprintf("^0?%d-?0?%d-?%04d", $broadcast_dateW2[2], $broadcast_dateW2[1], $broadcast_dateW2[0]);
+my $bdfileW4 = sprintf("^0?%d-?0?%d-?%04d", $broadcast_dateW4[2], $broadcast_dateW4[1], $broadcast_dateW4[0]);
 
-print "looking for files like '$bdfile' in RSS Feed\n";
+if($includeW4) {
+  print "looking for files like '$bdfileW2' and '$bdfileW4' in RSS Feed\n";
+} else {
+  print "looking for files like '$bdfileW2' in RSS Feed\n";
+}
 print " -> $RSS_URL\n";
 
 my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
@@ -132,9 +145,16 @@ for my $entry ($feed->entries) {
       $uri = new URI::URL($orig_uri);
       my @path = $uri->path_components;
       $file = $path[-1];
-      unless(uc($file) =~ $bdfile) { # file not from correct day
-        $uri = "";
-        next;
+      unless(uc($file) =~ $bdfileW2) { # file not from correct day
+        if($includeW4) {
+          unless(uc($file) =~ $bdfileW4) { # file not from correct day (either from the show 2 weeks earlier
+            $uri = "";
+            next;
+          }
+        } else {
+          $uri = "";
+          next;
+        }
       }
 
       my $current_file = `cat $STAT_FILE`;
@@ -157,7 +177,11 @@ for my $entry ($feed->entries) {
   last if $uri ne "";
 }
 if($uri eq "") {
-  print "No Entry found from $bd - ";
+  if($includeW4) {
+    print "No Entry found from $bdW2 or $bdW4 - ";
+  } else {
+    print "No Entry found from $bdW2 - ";
+  }
   if($LAST_RUN) {
     print "giving up, manual import necessary!!!\n";
     print "\n\n --> https://import.helsinki.at/shows/$show_id\n";
-- 
cgit v0.10.2