summaryrefslogtreecommitdiff
path: root/rhimport.pm
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-06-03 16:31:27 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-06-03 16:31:27 (GMT)
commit6ee43bf6f2825bfdeb1a0aeebddf082caad76e54 (patch)
treecc1a5ffb641d1699059d518200a8b92ec423bfb9 /rhimport.pm
parentd0e4e4384362dfe976e49b93ad01bfb6d4b4039b (diff)
added importer for economic update
Diffstat (limited to 'rhimport.pm')
-rw-r--r--rhimport.pm37
1 files changed, 37 insertions, 0 deletions
diff --git a/rhimport.pm b/rhimport.pm
index dafaf83..0e8b7f6 100644
--- a/rhimport.pm
+++ b/rhimport.pm
@@ -36,6 +36,8 @@ use DateTime;
use DateTime::TimeZone;
use LWP::Simple;
use XML::Feed;
+use URI::Fetch;
+use JSON::MaybeXS;
my $ssh_host = "airplay";
my $ssh_user = "rhimport";
@@ -90,6 +92,41 @@ sub fetch_parse_rss
return XML::Feed->parse(\$xml);
}
+sub fetch_parse_json
+{
+ my ($url, $ua_str, $headers) = @_;
+
+ my $uri = URI->new($url);
+ $ua_str = "Radio Helsinki Rivendell Utilities" unless $ua_str;
+
+ my $ua = LWP::UserAgent->new;
+ $ua->agent($ua_str);
+ $ua->env_proxy;
+ if (defined $headers) {
+ while(my ($field, $value) = each %{$headers}) {
+ $ua->default_header($field, $value);
+ }
+ }
+
+ my $res = URI::Fetch->fetch($uri, UserAgent => $ua)
+ or return (0, URI::Fetch->errstr);
+ if($res->status == URI::Fetch::URI_GONE()) {
+ return (0, "This resource has been permanently removed");
+ }
+
+ my $json = $res->content;
+ my $data;
+ eval {
+ my $j = JSON::MaybeXS->new(utf8 => 0);
+ $data = $j->decode($json);
+ 1;
+ } or do {
+ return (0, "error parsing import result");
+ };
+
+ return (1, $data);
+}
+
sub check_key_file
{
if(-e "$ssh_key_file") {