summaryrefslogtreecommitdiff
path: root/lib/radio.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/radio.pm')
-rw-r--r--lib/radio.pm100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/radio.pm b/lib/radio.pm
new file mode 100644
index 0000000..306d904
--- /dev/null
+++ b/lib/radio.pm
@@ -0,0 +1,100 @@
+#######################################
+# self-enclosed polling mechanism for radio area
+
+use LWP;
+
+sub get_radio_info
+ {
+ my $radio;
+ my $raw_radio;
+
+ print "<small>" if ($DEBUG);
+
+ $RADIO_STATUS_ENABLED = 1;
+ if ( ! $RADIO_STATUS_ENABLED || poll_radio_status() == -1)
+ { return -1; }
+
+ print "\nRadio is up!<br>\n" if ($DEBUG);
+
+ if (poll_cache_age($RADIO_INFO_PATH) > 60)
+ {
+ print "Polling radio...<br>\n" if ($DEBUG);
+ $raw_radio = go_slurp($RADIO_INFO_URL, $RADIO_INFO_PATH);
+ }
+
+ $raw_radio ||= quickread($RADIO_INFO_PATH);
+ $radio = parse_colons($raw_radio);
+
+ print "</small>" if ($DEBUG);
+
+ return $radio;
+ }
+
+sub poll_cache_age
+ {
+ my $file = shift;
+ my $ret = time - (stat($file))[9];
+ print "Age of $file: $ret seconds...<br>\n" if ($DEBUG);
+ return $ret;
+ }
+
+sub poll_radio_status
+ {
+ my $status;
+ print "Polling radio status...<br>\n" if ($DEBUG);
+# go_slurp($RADIO_STATUS_URL, $RADIO_STATUS_PATH);
+# $status = quickread($RADIO_STATUS_PATH);
+
+ my $r = HTTP::Request->new(GET => $RADIO_STATUS_URL);
+ my $re = LWP::UserAgent->new->request($r);
+ my $data = $re->content();
+
+ chomp($data);
+ if ($data eq "1")
+ { return 1; }
+ else
+ { return -1; }
+ }
+
+sub parse_colons
+ {
+ my $filedata = shift;
+ my %parsed;
+
+ foreach $l (@$filedata)
+ {
+ chomp $l;
+ my ($k, $v) = split(/: /, $l, 2);
+ $k = lc($k);
+ $k =~ s/ //;
+ $parsed{lc($k)} = $v;
+ }
+
+ return \%parsed;
+ }
+
+sub go_slurp
+ {
+ my ($url, $file) = @_;
+ my $r = HTTP::Request->new(GET => $url);
+ my $re = LWP::UserAgent->new->request($r);
+ my $data = $re->content();
+ open F, ">$file" or return $data;
+ print F $data;
+ close F;
+ my @lines = split "\n", $data;
+ return \@lines;
+ }
+
+sub quickread
+ {
+ my $file = shift;
+ my @out;
+ open F, $file or die "problem with $file $!";
+ @out=<F>;
+ close F;
+ return \@out;
+ }
+
+1;
+