summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rwxr-xr-xlib/RHRD/utils.pm32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/RHRD/utils.pm b/lib/RHRD/utils.pm
index 2c7b7a9..d87ed70 100755
--- a/lib/RHRD/utils.pm
+++ b/lib/RHRD/utils.pm
@@ -26,6 +26,9 @@ use strict;
use POSIX;
use DateTime;
use DateTime::TimeZone;
+use LWP::Simple;
+use URI::Fetch;
+use JSON;
sub get_rd_week
{
@@ -112,4 +115,33 @@ sub dropbox_param_len_ok
return (1, 'OK', DB_PARAM_LEN_HINT);
}
+sub fetch_parse_json
+{
+ my ($url, $ua_str) = @_;
+
+ 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;
+ my $res = URI::Fetch->fetch($uri, UserAgent => $ua)
+ or return (0, URI::Fetch->errstr);
+ if($res->status == URI::Fetch::URI_GONE()) {
+ return (0, "This feed has been permanently removed");
+ }
+
+ my $json = $res->content;
+ my $data;
+ eval {
+ $data = from_json($json);
+ 1;
+ } or do {
+ return (0, "error parsing import result");
+ };
+
+ return (1, \$data);
+}
+
+
return 1;