diff options
Diffstat (limited to 'rhimport.pm')
-rw-r--r-- | rhimport.pm | 37 |
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") { |