Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2012/08/28

What do I do with my weather stuff now?

It started with me wanting to know how to code with R. R is about handling and graphing data, and I didn't really have data of my own. So, what I did was find a source I liked for weather and started putting it into a MySQL database.

That source was Google, specifically the XML used for weather in the soon-to-be-closed iGoogle.

Here's that code.

  1. #!/usr/bin/perl  
  2.   
  3. # $Id: hpsetdisp.pl 11 2006-03-22 01:21:03Z yaakov $  
  4.   
  5. # hpsetdisp.pl  
  6. # Connects to a JetDirect equipped HP printer and uses  
  7. # HP's control language to set the ready message on the  
  8. # LCD display.  Takes an IP address and message on the  
  9. # command line. My favorite message is "INSERT COIN".  
  10. # Keep in mind the limitations of the display when composing  
  11. # your clever verbiage.  
  12. #  
  13. # THIS PROGRAM IS PROVIDED WITH NO WARRANTY OF ANY KIND EXPRESSED OR IMPLIED  
  14. # THE AUTHOR CANNOT BE RESPONSIBLE FOR THE EFFECTS OF THIS PROGRAM  
  15. # IF YOU ARE UNCERTAIN ABOUT THE ADVISABILITY OF USING IT, DO NOT!  
  16. #  
  17. # Yaakov (http://kovaya.com/ )  
  18. #  
  19. # Modified by Garrett Hoofman  
  20. # 10/18/2007  
  21. # http://www.visionsofafar.com  
  22.   
  23. # Modified by David Jacoby  
  24. # 12/10/2008  
  25. # http://varlogrant.blogspot.com/  
  26.   
  27. # Modified by David Jacoby, using notify-send and tweaking Unicode  
  28. # 09/08/2008  
  29. # http://varlogrant.blogspot.com/  
  30.   
  31. # Modified by David Jacoby, moving from Google to NOAA  
  32. # 08/28/2012  
  33. # http://varlogrant.blogspot.com/  
  34.   
  35.   
  36. use strict ;  
  37. use warnings ;  
  38. use Modern::Perl ;  
  39. use DBI ;  
  40. use Getopt::Long ;  
  41. use IO::Interactive qw{ interactive } ;  
  42. use IO::Socket ;  
  43. use XML::DOM ;  
  44. use Data::Dumper ;  
  45.   
  46. use lib '/home/jacoby/lib' ;  
  47. use MyDB 'db_connect' ;  
  48.   
  49. use subs qw{  get_weather notify db_upload } ;  
  50.   
  51. my $zip         = '' ;  
  52. my $interactive = '' ;  
  53. my $degree = '°' ; #the unicode, straight  
  54.    $degree = "\x{00b0}" ; #the unicode, specified  
  55. my $rdymsg      = "Ready" ;  
  56. my $curr_cond ;  
  57.   
  58. GetOptions(  
  59.     'zipcode=s'   => \$zip ,  
  60.     ) ;  
  61.   
  62. $zip !~ /\d/ and exit 0 ;  
  63. my %weather = get_weather ;  
  64. db_upload %weather ;  
  65.   
  66. exit ;  
  67.   
  68. ########## ########## ########## ########## ########## ########## ##########  
  69. sub get_weather {  
  70.     my %output ;  
  71.     my $parser = XML::DOM::Parser->new() ;  
  72.     my $file   = 'http://www.google.com/ig/api?weather=XXXXX&hl=en' ;  
  73.     #my $zip    = shift ;  
  74.     $zip =~ s{(\d{5})}{}mx ;  
  75.     $zip = $1 ;  
  76.     $file =~ s/XXXXX/$zip/mx ;  
  77.     my $doc = $parser->parsefile( $file ) ;  
  78.     exit if ! defined $doc ;  
  79.     $output{zip}        = $zip ;  
  80.     $output{city}       = $doc->getElementsByTagName( 'city' )->item( 0 )  
  81.                         ->getAttribute('data');  
  82.     $output{temp_c}     = $doc->getElementsByTagName( 'temp_c' )->item( 0 )  
  83.                         ->getAttribute('data');  
  84.     $output{temp_f}     = $doc->getElementsByTagName( 'temp_f' )->item( 0 )  
  85.                         ->getAttribute('data');  
  86.     $output{humidity}   = $doc->getElementsByTagName( 'humidity' )->item( 0 )  
  87.                         ->getAttribute('data');  
  88.     $output{wind}       = $doc->getElementsByTagName( 'wind_condition' )->item( 0 )  
  89.                         ->getAttribute('data');  
  90.     $output{conditions} = $doc->getElementsByTagName( 'condition' )->item( 0 )  
  91.                         ->getAttribute('data') ;  
  92.   
  93.     return %output ;  
  94.     }  
  95. ########## ########## ########## ########## ########## ########## ##########  
  96.   
  97. ########## ########## ########## ########## ########## ########## ##########  
  98. sub db_upload {  
  99.     my %cond = @_ ;  
  100.     my $dbh = db_connect() ;  
  101.     for my $k ( sort keys %cond ) {  
  102.         $cond{$k} = $dbh->quote( $cond{$k} ) ;  
  103.         }  
  104.     my $fields = join ',' , qw{  
  105.         city        conditions  
  106.         humidity    temp_c  
  107.         temp_f      wind  
  108.         zip  
  109.         } ;  
  110.   
  111.     my $sql ;  
  112.     my $rows ;  
  113.   
  114.     $sql = qq{  
  115.     INSERT INTO weather_data ( $fields ) VALUES (  
  116.         $cond{ city } ,  
  117.         $cond{ conditions } ,  
  118.         $cond{ humidity } ,  
  119.         $cond{ temp_c } ,  
  120.         $cond{ temp_f } ,  
  121.         $cond{ wind } ,  
  122.         $cond{ zip }  
  123.         ) } ;  
  124.     #$rows = $dbh->do( $sql ) or croak $dbh->errstr;  
  125.   
  126.     $sql = qq{  
  127.     INSERT INTO weather ( $fields ) VALUES (  
  128.         $cond{ city } ,  
  129.         $cond{ conditions } ,  
  130.         $cond{ humidity } ,  
  131.         $cond{ temp_c } ,  
  132.         $cond{ temp_f } ,  
  133.         $cond{ wind } ,  
  134.         $cond{ zip }  
  135.         ) } ;  
  136.     $rows = $dbh->do$sql ) or croak $dbh->errstr;  
  137.   
  138.     return ;  
  139.     }  
  140. ########## ########## ########## ########## ########## ########## ##########  
Problem is, Google knows I've been grabbing weather data every 10 minutes and has blocked me.

While I fill in all these, I am not pulling in weather data from multiple cities, so I can hardcode city and zip, and I couldjust drop conditions and wind. All I'm really watching is temp_f, and it's a simple conversion to get temp_c if I wanted it.

I have code that pulls from NOAA. Think I'll try to bring that in here.

No comments:

Post a Comment