00001 #! /usr/bin/perl
00002 # vim:ts=4:sw=4:ai:et:si:sts=4
00003
00004 use English;
00005 use strict;
00006 use warnings;
00007
00008 use File::Path;
00009 use File::Basename;
00010 use Cwd 'abs_path';
00011 use lib dirname(abs_path($0 or $PROGRAM_NAME)),
00012 '/usr/share/mythtv/mythweather/scripts/wunderground',
00013 '/usr/local/share/mythtv/mythweather/scripts/wunderground';
00014
00015 use utf8;
00016 use encoding 'utf8';
00017 use Getopt::Std;
00018 use POSIX qw(strftime);
00019
00020 our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d, $opt_D);
00021
00022 my $name = 'wunderground-animaps';
00023 my $version = 0.1;
00024 my $author = 'Gavin Hurlbut';
00025 my $email = 'gjhurlbu@gmail.com';
00026 my $updateTimeout = 15*60;
00027 my $retrieveTimeout = 30;
00028 my @types = ( 'amdesc', 'updatetime', 'animatedimage', 'copyright' );
00029 my $dir = "/tmp/wunderground";
00030 my $logdir = "/tmp/wunderground";
00031 my $config_file = dirname(abs_path($0 or $PROGRAM_NAME)) . "/maps.csv";
00032
00033 binmode(STDOUT, ":utf8");
00034
00035 if (!-d $logdir) {
00036 mkpath( $logdir, {mode => 0755} );
00037 }
00038
00039 getopts('Tvtlu:d:D');
00040
00041 if (defined $opt_v) {
00042 print "$name,$version,$author,$email\n";
00043 log_print( $logdir, "-v\n" );
00044 exit 0;
00045 }
00046
00047 if (defined $opt_T) {
00048 print "$updateTimeout,$retrieveTimeout\n";
00049 log_print( $logdir, "-t\n" );
00050 exit 0;
00051 }
00052
00053 if (defined $opt_d) {
00054 $dir = $opt_d;
00055 }
00056
00057 if (!-d $dir) {
00058 mkpath( $dir, {mode => 0755} );
00059 }
00060
00061 if (defined $opt_l) {
00062 my $search = shift;
00063 $search = qr{(?i)^(.*?),(.*$search.*)$};
00064 log_print( $logdir, "-l $search\n" );
00065
00066 open my $fh, "<", $config_file or die "Couldn't open config file: $!\n";
00067 while (<$fh>) {
00068 if ( /$search/ ) {
00069 my $code = uc $1;
00070 print "${code}::$2\n";
00071 }
00072 }
00073 close $fh;
00074
00075 exit 0;
00076 }
00077
00078 if (defined $opt_t) {
00079 foreach (@types) {print; print "\n";}
00080 exit 0;
00081 }
00082
00083 # we get here, we're doing an actual retrieval, everything must be defined
00084 my $loc = uc shift;
00085 if ( not defined $loc or $loc eq "" ) {
00086 die "Invalid usage";
00087 }
00088
00089 my %attrib;
00090
00091 log_print( $logdir, "-d $dir $loc\n" );
00092
00093 my $search = qr{(?i)^$loc,(.*?)$};
00094 my @names;
00095
00096 open my $fh, "<", $config_file or die "Couldn't open config file: $!\n";
00097 while (<$fh>) {
00098 push @names, $1 if ( /$search/ );
00099 }
00100 close $fh;
00101
00102 $attrib{"amdesc"} = join( " / ", @names) . " Animated Radar Map";
00103
00104 $attrib{"animatedimage"} = "http://radblast-mi.wunderground.com/cgi-bin/radar/".
00105 "WUNIDS_map?station=$loc&type=N0R&noclutter=0&".
00106 "showlabels=1&rainsnow=1&num=6&delay=200";
00107
00108
00109 $attrib{"copyright"} = "Weather data courtesy of Weather Underground, Inc.";
00110
00111 my $now = time;
00112 $attrib{"updatetime"} = format_date($now);
00113
00114 for my $attr ( sort keys %attrib ) {
00115 print $attr . "::" . $attrib{$attr} . "\n";
00116 }
00117 exit 0;
00118
00119 #
00120 # Subroutines
00121 #
00122 sub nodeToHash {
00123 my ($node, $prefix, $hashref) = @_;
00124
00125 my $nodename = $node->getName;
00126 my @subnodelist = $node->getChildNodes;
00127
00128 if ( not defined $prefix or $prefix eq "" ) {
00129 $prefix = $nodename;
00130 } elsif ( defined $nodename ) {
00131 $prefix = $prefix . "::" . $nodename;
00132 }
00133
00134 foreach my $attr ( $node->getAttributes ) {
00135 $prefix .= "::".$attr->getName."=".$attr->getData;
00136 }
00137
00138 if ( $#subnodelist == 0 ) {
00139 $hashref->{$prefix} = $node->string_value;
00140 } else {
00141 foreach my $subnode ( @subnodelist ) {
00142 nodeToHash( $subnode, $prefix, $hashref );
00143 }
00144 }
00145 }
00146
00147 sub getCachedFile {
00148 my ($url, $dir, $file, $timeout, $logdir) = @_;
00149
00150 my $cachefile = "$dir/$file";
00151
00152 my $now = time();
00153
00154 if( (-e $cachefile) and ((stat($cachefile))[9] >= ($now - $timeout)) ) {
00155 # File cache is still recent.
00156 log_print( $logdir, "cached in $cachefile\n" );
00157 } else {
00158 log_print( $logdir, "$url\ncaching to $cachefile\n" );
00159 my $ua = LWP::UserAgent->new;
00160 $ua->timeout(30);
00161 $ua->env_proxy;
00162 $ua->default_header('Accept-Language' => "en");
00163
00164 my $response = $ua->get($url, ":content_file" => $cachefile);
00165 if ( !$response->is_success ) {
00166 die $response->status_line;
00167 }
00168 }
00169 }
00170
00171 sub format_date {
00172 my ($time) = @_;
00173
00174 return strftime '%a %b %e, %Y %H:%M:%S', localtime($time);
00175 }
00176
00177 sub log_print {
00178 return if not defined $opt_D;
00179 my $dir = shift;
00180
00181 open OF, ">>$dir/wunderground.log";
00182 print OF @_;
00183 close OF;
00184 }