From c10b740439ddcdeb4fd475e113cc439f374fc96b Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Tue, 19 Jul 2016 23:44:38 +0200
Subject: make usage of fetch_parse_(rss|json) easier


diff --git a/rhautoimport-er b/rhautoimport-er
index 31033db..bca53d8 100755
--- a/rhautoimport-er
+++ b/rhautoimport-er
@@ -36,7 +36,7 @@ use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-er.stat";
 my $LAST_FILE = $ENV{'HOME'} . "/rhautoimport-er.last";
-my $RSS_URL = "https://cba.fro.at/series/ergo/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/ergo/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "ergo";
 my $PV_ID = '569';
@@ -103,7 +103,7 @@ print " -> $RSS_URL\n";
 
 my $bdnumexp = "^Ergo\\s+#(\\d+)\\s*:";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-eu b/rhautoimport-eu
index 5aad6f0..72509c3 100755
--- a/rhautoimport-eu
+++ b/rhautoimport-eu
@@ -84,8 +84,7 @@ my $bd = sprintf("%04d-%02d-%02d", @broadcast_date);
 print "looking for files after $bd in Audioboom Clips\n";
 print " -> $API_URL\n";
 
-my %h = ( 'Accept' => 'application/json; version=1' );
-my ($result, $data) = RHRD::utils::fetch_parse_json($API_URL, "Radio Helsinki - Automatic Import", \%h);
+my ($result, $data) = rhautoimport::fetch_parse_json_audioboom($API_URL);
 unless ($result) {
   print "Error fetching data: $data\n";
   exit 1;
diff --git a/rhautoimport-fl b/rhautoimport-fl
index 368dc8f..441a51a 100755
--- a/rhautoimport-fl
+++ b/rhautoimport-fl
@@ -38,7 +38,7 @@ use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-fl.stat";
 my $LAST_FILE = $ENV{'HOME'} . "/rhautoimport-fl.last";
-my $RSS_URL = "https://cba.fro.at/series/frequenz-leitwolf/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/frequenz-leitwolf/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "leitwolf";
 my $PV_ID = '571';
@@ -107,7 +107,7 @@ if($current_id eq $id) {
 print "looking for file after $last_broadcast in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-nw b/rhautoimport-nw
index 356c2a8..c8ea341 100755
--- a/rhautoimport-nw
+++ b/rhautoimport-nw
@@ -34,7 +34,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-nw.stat";
-my $RSS_URL = "https://cba.fro.at/series/radio-netwatcher/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/radio-netwatcher/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "netwatcher";
 my $PV_ID = '73';
@@ -91,7 +91,7 @@ my $bdfile = sprintf("NETWATCHER[-_]?%04d%02d%02d(.*)\.MP3", @broadcast_day);
 print "looking for files from $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-po b/rhautoimport-po
index 5f9372b..8d3caeb 100755
--- a/rhautoimport-po
+++ b/rhautoimport-po
@@ -36,7 +36,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-po.stat";
-my $RSS_URL = "https://cba.fro.at/series/panoptikum-bildung/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/panoptikum-bildung/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "panoptikum";
 my $PV_ID = '570';
@@ -109,7 +109,7 @@ my $bd = sprintf("%04d-%02d-%02d", @broadcast_date);
 print "looking for files for broadcast date $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-ra b/rhautoimport-ra
index 3544d10..164797f 100755
--- a/rhautoimport-ra
+++ b/rhautoimport-ra
@@ -34,7 +34,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-ra.stat";
-my $RSS_URL = "https://cba.fro.at/series/radioattac/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/radioattac/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "RadioATTAC";
 my $PV_ID = '76';
@@ -82,7 +82,7 @@ my $bd = sprintf("%04d-%02d-%02d", @broadcast_day);
 print "looking for files from $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-rs b/rhautoimport-rs
index fbcae5e..32a57b6 100755
--- a/rhautoimport-rs
+++ b/rhautoimport-rs
@@ -35,7 +35,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-rs.stat";
-my $RSS_URL = "https://cba.fro.at/series/radio-stimme/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/radio-stimme/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "radioStimm";
 my $PV_ID = '75';
@@ -129,7 +129,7 @@ my $bdfile = sprintf("RAST%04d%02d%02d(CBA)?(.*)\.MP3", @broadcast_date);
 print "looking for files from $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-tr b/rhautoimport-tr
index 7b88c6d..095402c 100755
--- a/rhautoimport-tr
+++ b/rhautoimport-tr
@@ -34,7 +34,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-tr.stat";
-my $RSS_URL = "https://cba.fro.at/series/tierrechtsradio/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/tierrechtsradio/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "tierrecht";
 my $PV_ID = '506';
@@ -84,7 +84,7 @@ my $bdfile = sprintf("^%04d-?%02d-?%02d", @broadcast_day);
 print "looking for files from $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport-wr b/rhautoimport-wr
index 155d428..97b38b3 100755
--- a/rhautoimport-wr
+++ b/rhautoimport-wr
@@ -36,7 +36,7 @@ use lib '/usr/local/share/rhautoimport/';
 use rhautoimport;
 
 my $STAT_FILE = $ENV{'HOME'} . "/rhautoimport-wr.stat";
-my $RSS_URL = "https://cba.fro.at/series/werkstatt-radio/feed?c=Kfs2IoV2Wmd";
+my $RSS_URL = "https://cba.fro.at/series/werkstatt-radio/feed";
 $XML::Feed::MULTIPLE_ENCLOSURES=1;
 my $RD_GROUP = "werkstatt";
 my $PV_ID = '567';
@@ -105,7 +105,7 @@ my $bd = sprintf("%04d-%02d-%02d", @broadcast_date);
 print "looking for files for broadcast date $bd in RSS Feed\n";
 print " -> $RSS_URL\n";
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
diff --git a/rhautoimport.pm b/rhautoimport.pm
index 83bf17b..a2b4c50 100644
--- a/rhautoimport.pm
+++ b/rhautoimport.pm
@@ -34,6 +34,7 @@ use LWP::Simple;
 use XML::Feed;
 use RHRD::rddb;
 use JSON::MaybeXS;
+use URI::QueryParam;
 
 
 use constant {
@@ -59,11 +60,16 @@ sub get_dropboxes
 
 sub fetch_parse_rss
 {
-  my ($url, $ua_str, $headers) = @_;
+  my ($url, $ua_str, $headers, $queries) = @_;
 
   my $uri = URI->new($url);
-  $ua_str = "Radio Helsinki - Automatic Import" unless $ua_str;
+  if (defined $queries) {
+    while(my ($name, $value) = each %{$queries}) {
+      $uri->query_param($name => $value);
+    }
+  }
 
+  $ua_str = "Radio Helsinki - Automatic Import" unless $ua_str;
   my $ua  = LWP::UserAgent->new;
   $ua->agent($ua_str);
   $ua->env_proxy;
@@ -82,6 +88,41 @@ sub fetch_parse_rss
   return (1, XML::Feed->parse(\$xml));
 }
 
+sub fetch_parse_rss_cba
+{
+  my ($url, $ua_str, $headers, $queries) = @_;
+
+  my $api_key = 'Kfs2IoV2Wmd'; # TODO: read this from config!!!
+  if (defined $api_key) {
+    if (defined $queries) {
+      $queries->{'c'} = $api_key;
+    } else {
+      $queries = { 'c' => $api_key };
+    }
+  }
+  return fetch_parse_rss($url, $ua_str, $headers, $queries);
+}
+
+sub fetch_parse_json
+{
+  my ($url, $ua_str, $headers, $queries) = @_;
+
+  $ua_str = "Radio Helsinki - Automatic Import" unless $ua_str;
+  return RHRD::utils::fetch_parse_json($url, $ua_str, $headers, $queries);
+}
+
+sub fetch_parse_json_audioboom
+{
+  my ($url, $ua_str, $headers, $queries) = @_;
+
+  if (defined $headers) {
+    $headers->{'Accept'} = 'application/json; version=1';
+  } else {
+    $headers = { 'Accept' => 'application/json; version=1' };
+  }
+  return fetch_parse_json($url, $ua_str, $headers, $queries);
+}
+
 sub check_file_extension
 {
   my ($file) = @_;
@@ -95,6 +136,9 @@ sub check_file_extension
   return 0;
 }
 
+#########################################
+## rhimportd - watchDir Control Interface
+
 sub parse_result
 {
   my ($donefile) = @_;
@@ -201,6 +245,9 @@ sub import_uri
   return $ret, $log . $tmplog, $keptfile_uri;
 }
 
+#########################################
+## PV
+
 sub pv_add_note
 {
   my ( $title, $text, $id, $date, $type, $index ) = @_;
diff --git a/test/audioboom-inspect b/test/audioboom-inspect
index aab1b2b..7f4c6fc 100755
--- a/test/audioboom-inspect
+++ b/test/audioboom-inspect
@@ -42,8 +42,7 @@ binmode(STDIN, ":utf8");
 binmode(STDOUT, ":utf8");
 binmode(STDERR, ":utf8");
 
-my %h = ( 'Accept' => 'application/json; version=1' );
-my ($result, $data) = RHRD::utils::fetch_parse_json($API_URL, "Radio Helsinki - Automatic Import", \%h);
+my ($result, $data) = rhautoimport::fetch_parse_json_audioboom($API_URL);
 unless ($result) {
   print "Error fetching data: $data\n";
   exit 1;
diff --git a/test/cba-inspect b/test/cba-inspect
index f625cff..bc03ac3 100755
--- a/test/cba-inspect
+++ b/test/cba-inspect
@@ -43,13 +43,13 @@ if($#ARGV != 0) {
   print "Usage: cba-inspect <rss url>\n";
   exit 1;
 }
-my $RSS_URL = $ARGV[0] . "?c=Kfs2IoV2Wmd";
+my $RSS_URL = $ARGV[0];
 
 binmode(STDIN, ":utf8");
 binmode(STDOUT, ":utf8");
 binmode(STDERR, ":utf8");
 
-my ($result, $feed) = rhautoimport::fetch_parse_rss($RSS_URL);
+my ($result, $feed) = rhautoimport::fetch_parse_rss_cba($RSS_URL);
 unless ($result) {
   print "Error fetching feed: $feed\n";
   exit 1;
-- 
cgit v0.10.2