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-maps';
00023 my $version = 0.2;
00024 my $author = 'Gavin Hurlbut';
00025 my $email = 'gjhurlbu@gmail.com';
00026 my $updateTimeout = 15*60;
00027 my $retrieveTimeout = 30;
00028 my @types = ( 'smdesc', 'updatetime', 'map', '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{"smdesc"} = join( " / ", @names) . " Static Radar Map";
00103
00104 $attrib{"map"} = "http://radblast-mi.wunderground.com/cgi-bin/radar/".
00105 "WUNIDS_map?station=$loc&type=N0R&noclutter=0&showlabels=1&".
00106 "rainsnow=1&num=1";
00107
00108 $attrib{"copyright"} = "Weather data courtesy of Weather Underground, Inc.";
00109
00110 my $now = time;
00111 $attrib{"updatetime"} = format_date($now);
00112
00113 for my $attr ( sort keys %attrib ) {
00114 print $attr . "::" . $attrib{$attr} . "\n";
00115 }
00116 exit 0;
00117
00118 #
00119 # Subroutines
00120 #
00121 sub nodeToHash {
00122 my ($node, $prefix, $hashref) = @_;
00123
00124 my $nodename = $node->getName;
00125 my @subnodelist = $node->getChildNodes;
00126
00127 if ( not defined $prefix or $prefix eq "" ) {
00128 $prefix = $nodename;
00129 } elsif ( defined $nodename ) {
00130 $prefix = $prefix . "::" . $nodename;
00131 }
00132
00133 foreach my $attr ( $node->getAttributes ) {
00134 $prefix .= "::".$attr->getName."=".$attr->getData;
00135 }
00136
00137 if ( $#subnodelist == 0 ) {
00138 $hashref->{$prefix} = $node->string_value;
00139 } else {
00140 foreach my $subnode ( @subnodelist ) {
00141 nodeToHash( $subnode, $prefix, $hashref );
00142 }
00143 }
00144 }
00145
00146 sub getCachedFile {
00147 my ($url, $dir, $file, $timeout, $logdir) = @_;
00148
00149 my $cachefile = "$dir/$file";
00150
00151 my $now = time();
00152
00153 if( (-e $cachefile) and ((stat($cachefile))[9] >= ($now - $timeout)) ) {
00154 # File cache is still recent.
00155 log_print( $logdir, "cached in $cachefile\n" );
00156 } else {
00157 log_print( $logdir, "$url\ncaching to $cachefile\n" );
00158 my $ua = LWP::UserAgent->new;
00159 $ua->timeout(30);
00160 $ua->env_proxy;
00161 $ua->default_header('Accept-Language' => "en");
00162
00163 my $response = $ua->get($url, ":content_file" => $cachefile);
00164 if ( !$response->is_success ) {
00165 die $response->status_line;
00166 }
00167 }
00168 }
00169
00170 sub format_date {
00171 my ($time) = @_;
00172
00173 return strftime '%a %b %e, %Y %H:%M:%S', localtime($time);
00174 }
00175
00176 sub log_print {
00177 return if not defined $opt_D;
00178 my $dir = shift;
00179
00180 open OF, ">>$dir/wunderground.log";
00181 print OF @_;
00182 close OF;
00183 }