Permalink
Browse files

Redid wunderground scripts to use the REST API

NOTE:  All users who wish to use the wunderground weather source **MUST**
sign up for an API key, which should be put into either
$HOME/.mythtv/MythWeather/wunderground.key
or
$MYTHCONFDIR/MythWeather/wunderground.key

One can sign up for an API key at:
http://www.wunderground.com/weather/api/

You will need your key to be at the
"Cumulus Developer" level for static maps or weather forecasts and
"Anvil Developer" level for animated maps.
  • Loading branch information...
1 parent 114d4cd commit e4c181a6b04eed771851098015d1e6be86dfdec6 @Beirdo Beirdo committed Jul 12, 2012
@@ -1,4 +1,4 @@
-DATA = maps.csv
+DATA =
SCRIPTS = wunderground-animaps.pl wunderground-maps.pl wunderground.pl
include ../Makefile.inc
@@ -1,178 +0,0 @@
-abc,Bethel
-abr,Aberdeen
-abx,Albuquerque
-acg,Biorka Is.
-acg,Sitka
-aec,Nome
-ahg,Kenai
-aih,Middleton Is.
-akc,King Salmon
-akq,Norfolk
-akq,Richmond
-akq,Wakefield
-ama,Amarillo
-amx,Miami
-apd,Fairbanks
-apd,Pedro Dome
-apx,Gaylord
-arx,La Crosse
-atx,Seattle
-atx,Tacoma
-bbx,Beale AFB
-bgm,Binghamton
-bhx,Eureka
-bis,Bismarck
-blx,Billings
-bmx,Birmingham
-box,Boston
-bro,Brownsville
-buf,Buffalo
-byx,Key West
-cae,Columbia
-cbw,Caribou
-cbx,Boise
-ccx,State College
-cle,Cleveland
-clx,Charleston
-crp,Corpus Christi
-cxx,Burlington
-cys,Cheyenne
-dax,Sacramento
-ddc,Dodge City
-dfx,Laughlin AFB
-dgx,Jackson/Brandon
-dix,Mt. Holly
-dix,Philadelphia
-dlh,Duluth
-dmx,Des Moines
-dox,Dover AFB
-dtx,Detroit
-dvn,Quad Cities
-dyx,Dyess AFB
-eax,Kansas City
-emx,Tucson
-enx,Albany
-eox,Fort Rucker
-epz,El Paso
-esx,Las Vegas
-evx,Eglin AFB
-evx,NW Florida
-ewx,Austin
-ewx,San Antonio
-eyx,Edwards AFB
-fcx,Blacksburg
-fcx,Roanoke
-fdr,Frederick
-fdx,Cannon AFB
-ffc,Atlanta
-ffc,Peachtree City
-fsd,Sioux falls
-fsx,Flagstaff
-ftg,Denver/Boulder
-fws,Dallas
-fws,Fort Worth
-ggw,Glasgow
-gjx,Grand Junction
-gld,Goodland
-grb,Green Bay
-grk,Central Texas
-grr,Grand Rapids
-gsp,Greenville
-gsp,Greer
-gsp,Spartanburg
-gua,Andersen AFB
-gwx,Columbus AFB
-gyx,Portland
-hdx,Holloman AFB
-hgx,Galveston
-hgx,Houston
-hki,Kauai
-hkm,Kohala
-hmo,Molokai
-hnx,Hanford
-hnx,San Joaquin Vly.
-hpx,Fort Cambell
-htx,Nrn. Alabama
-hwa,South Shore
-ict,Wichita
-icx,Cedar City
-iln,Cincinnati
-iln,Dayton
-iln,Wilmington
-ilx,Central IL
-ind,Indianapolis
-inx,Tulsa
-iwa,Phoenix
-iwx,Nrn. Indiana
-jax,Jacksonville
-jgx,Robins AFB
-jkl,Jackson
-jua,Puerto Rico/V.I.
-lbb,Lubbock
-lch,Lake Charles
-lix,New Orleans
-lnx,North Platte
-lot,Chicago
-lrx,Elko
-lsx,St. Louis
-ltx,Wilmington
-lvx,Louisville
-lwx,Baltimore
-lwx,Sterling
-lwx,Washington
-lzk,Little Rock
-maf,Midland/Odessa
-max,Medford
-mbx,Minot AFB
-mhx,Morehead City
-mkx,Milwaukee
-mlb,Melbourne
-mob,Mobile
-mpx,Minneapolis
-mqt,Marquette
-mrx,Knoxville
-mrx,Morristown
-mrx,Tri Cities
-msx,Missoula
-mtx,Salt Lake City
-mux,San Francisco
-mvx,Grand Forks
-mxx,E. Alabama
-nkx,San Diego
-nqa,Memphis
-oax,Omaha
-ohx,Nashville
-okx,New York City
-okx,Upton
-otx,Spokane
-pah,Paducah
-pbz,Pittsburgh
-pdt,Pendleton
-poe,Fort Polk
-pux,Pueblo
-rax,Durham
-rax,Raleigh
-rgx,Reno
-riw,Riverton
-rlx,Charleston
-rtx,Portland
-sfx,Pocatello
-sgf,Springfield
-shv,Shreveport
-sjt,San Angelo
-sox,Santa Ana Mtns
-srx,W. Ark./Ft. Smith
-tbw,Tampa Bay Area
-tfx,Great Falls
-tlh,Tallahassee
-tlx,Oklahoma City
-twx,Topeka
-tyx,Montague
-udx,Rapid City
-uex,Hastings
-vax,Moody AFB
-vbx,Vandenberg AFB
-vnx,Vance AFB
-vtx,Los Angeles
-vwx,Evansville
-yux,Yuma
@@ -14,21 +14,48 @@
use utf8;
use encoding 'utf8';
+use LWP::UserAgent;
use Getopt::Std;
+use URI::Escape;
use POSIX qw(strftime);
+use JSON;
+use Env qw(HOME MYTHCONFDIR);
our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d, $opt_D);
my $name = 'wunderground-animaps';
-my $version = 0.1;
+my $version = 0.2;
my $author = 'Gavin Hurlbut';
my $email = 'gjhurlbu@gmail.com';
my $updateTimeout = 15*60;
my $retrieveTimeout = 30;
-my @types = ( 'amdesc', 'updatetime', 'animatedimage', 'copyright' );
+my @types = ( 'amdesc', 'updatetime', 'animatedimage', 'copyright',
+ 'copyrightlogo' );
my $dir = "/tmp/wunderground";
my $logdir = "/tmp/wunderground";
-my $config_file = dirname(abs_path($0 or $PROGRAM_NAME)) . "/maps.csv";
+
+my $conffile;
+
+if (defined $MYTHCONFDIR) {
+ $conffile = "$MYTHCONFDIR/MythWeather/wunderground.key";
+}
+
+if ((!defined $MYTHCONFDIR) || ($conffile && !-f $conffile)) {
+ $conffile = "$HOME/.mythtv/MythWeather/wunderground.key";
+}
+
+if (!-f $conffile) {
+ print STDERR "You need to sign up for an API key from wunderground and " .
+ "put it into\n" .
+ $conffile . "\n\n" .
+ "Visit: http://www.wunderground.com/weather/api/\n\n";
+ exit 1;
+}
+
+open IF, "<", $conffile or die "Couldn't read $conffile: $!\n";
+my $apikey = <IF>;
+close IF;
+chomp $apikey;
binmode(STDOUT, ":utf8");
@@ -59,18 +86,22 @@
}
if (defined $opt_l) {
- my $search = shift;
- $search = qr{(?i)^(.*?),(.*$search.*)$};
+ my $search = uri_escape(shift);
log_print( $logdir, "-l $search\n" );
-
- open my $fh, "<", $config_file or die "Couldn't open config file: $!\n";
- while (<$fh>) {
- if ( /$search/ ) {
- my $code = uc $1;
- print "${code}::$2\n";
+ my $base_url = "http://api.wunderground.com/api/$apikey/geolookup/q/";
+
+ my $hash = getCachedJSON($base_url . $search . ".json", $dir,
+ $search . ".json", $updateTimeout, $logdir);
+
+ if (exists $hash->{'location'}) {
+ # Single match
+ my $loc = $hash->{'location'};
+ print_location($loc);
+ } elsif (exists $hash->{'response'}->{'results'}) {
+ for my $loc (@{$hash->{'response'}->{'results'}}) {
+ print_location($loc);
}
}
- close $fh;
exit 0;
}
@@ -81,32 +112,30 @@
}
# we get here, we're doing an actual retrieval, everything must be defined
-my $loc = uc shift;
-if ( not defined $loc or $loc eq "" ) {
+my $rawloc = shift;
+if ( not defined $rawloc or $rawloc eq "" ) {
die "Invalid usage";
}
+my $loc = uri_escape($rawloc);
my %attrib;
log_print( $logdir, "-d $dir $loc\n" );
-my $search = qr{(?i)^$loc,(.*?)$};
-my @names;
+my $base_url = "http://api.wunderground.com/api/$apikey/geolookup/q/";
-open my $fh, "<", $config_file or die "Couldn't open config file: $!\n";
-while (<$fh>) {
- push @names, $1 if ( /$search/ );
-}
-close $fh;
-
-$attrib{"amdesc"} = join( " / ", @names) . " Animated Radar Map";
-
-$attrib{"animatedimage"} = "http://radblast-mi.wunderground.com/cgi-bin/radar/".
- "WUNIDS_map?station=$loc&type=N0R&noclutter=0&".
- "showlabels=1&rainsnow=1&num=6&delay=200";
+my $hash = getCachedJSON($base_url . $loc . ".json", $dir,
+ $loc . ".json", $updateTimeout, $logdir);
+my $location = location($hash->{'location'});
+my $url = "http://api.wunderground.com/api/$apikey/animatedradar/q/$loc.gif?" .
+ "width=1024&height=768&newmaps=1&num=6&timelabel=1&timelabel.x=10&" .
+ "timelabel.y=758";
+$attrib{"amdesc"} = "$location Animated Radar Map";
+$attrib{"animatedimage"} = $url;
$attrib{"copyright"} = "Weather data courtesy of Weather Underground, Inc.";
+$attrib{"copyrightlogo"} = "http://icons.wxug.com/logos/images/wundergroundLogo_4c.jpg";
my $now = time;
$attrib{"updatetime"} = format_date($now);
@@ -119,32 +148,7 @@
#
# Subroutines
#
-sub nodeToHash {
- my ($node, $prefix, $hashref) = @_;
-
- my $nodename = $node->getName;
- my @subnodelist = $node->getChildNodes;
-
- if ( not defined $prefix or $prefix eq "" ) {
- $prefix = $nodename;
- } elsif ( defined $nodename ) {
- $prefix = $prefix . "::" . $nodename;
- }
-
- foreach my $attr ( $node->getAttributes ) {
- $prefix .= "::".$attr->getName."=".$attr->getData;
- }
-
- if ( $#subnodelist == 0 ) {
- $hashref->{$prefix} = $node->string_value;
- } else {
- foreach my $subnode ( @subnodelist ) {
- nodeToHash( $subnode, $prefix, $hashref );
- }
- }
-}
-
-sub getCachedFile {
+sub getCachedJSON {
my ($url, $dir, $file, $timeout, $logdir) = @_;
my $cachefile = "$dir/$file";
@@ -161,11 +165,21 @@ sub getCachedFile {
$ua->env_proxy;
$ua->default_header('Accept-Language' => "en");
- my $response = $ua->get($url, ":content_file" => $cachefile);
+ my $response = $ua->get($url);
if ( !$response->is_success ) {
die $response->status_line;
}
+
+ open OF, ">", $cachefile or die "Can't open $cachefile: $!\n";
+ print OF $response->content;
+ close OF;
}
+
+ open OF, "<", $cachefile or die "Can't open $cachefile: $!\n";
+ my $json = do { local $/; <OF> };
+ my $hash = from_json($json);
+
+ return $hash;
}
sub format_date {
@@ -182,3 +196,21 @@ sub log_print {
print OF @_;
close OF;
}
+
+sub location {
+ my $loc = shift;
+ my $location = $loc->{'city'};
+ $location .= ", " . $loc->{'state'} if $loc->{'state'};
+ $location .= ", " . $loc->{'country_name'} if $loc->{'country_name'};
+
+ return $location;
+}
+
+sub print_location {
+ my $loc = shift;
+
+ my $ident = $loc->{'l'};
+ $ident =~ s/^\/q\///;
+ my $location = location($loc);
+ print $ident."::$location\n";
+}
Oops, something went wrong.

0 comments on commit e4c181a

Please sign in to comment.