weather script

Devin Whalen devin-Gq53QDLGkWIleAitJ8REmdBPR1lH4CV8 at public.gmane.org
Thu Sep 16 18:48:03 UTC 2004


On Thu, 2004-09-16 at 13:59, David J Patrick wrote:
> I know this should be dead easy, but I have been unable to cook up a
> simple script that would get tomorrows weather forcast, so that I can
> dump it into a text file.
> 
> I've been looking at 
> http://www.schwarzvogel.de/software-pymetar.shtml
> but haven't managed to get the desired output.
> 
> any suggestions ?
> 
> ps, I'd also like to include sun rise/set times in the script.
> thanks,
> djp

Hey,

I use xfce4 and you can get a weather panel plugin that is really cool. 
So I wanted to add something similar to the web application that I work
on.  I looked at the code the for panel app and basically it just gets
the info from xoap.weather.com sending a location code and parses
through the returned xml data and does what it wants with it.  The thing
is this was almost a year ago and I was just messing around with
it....so the code is not very pretty :).  I was just basically seeing
what xoap.weather.com returns.  This script has no functionality, it
just prints stuff to the screen.  But it will just give you an idea of
how to get the weather in xml format.  Then you can do whatever you want
with it.  If you want it let me know and I can send it to you off list.

Also, you can get sunrise and sunset from it.


Actually I will just put the code in the email.  This is just getting
the xml back from the website.  You should look up  HTML::Parser on cpan
for more info.  Again, I will reinterate that this code is sloppy and I
was just fooling around to see what can be done with HTML::Parser and an
xml file.  But it is a start that you can work with :).

Later

code:


#!/usr/bin/perl -w
use LWP::Simple;
use HTML::Parser ();
use HTTP::Request;
use LWP::UserAgent;

use strict;

my $tagName = "";
#location code for toronto
my $location="CAXX0504";
#my $location="NLXX0019";
#metric or imperial?
my $unit = "m";
my %myHash = ();

my $url =
"http://xoap.weather.com/weather/local/$location?cc=&prod=xoap&unit=$unit&par=1003832479&key=bb12936706a2d601";
my $ua = LWP::UserAgent->new; #instantiate a user agent
my $request = HTTP::Request->new(GET => $url);
my $response = $ua->request($request);


#now we attach events for the parser to act on
#for example when the parser comes across <start_tag> it will fire the
#event or function start passing it the tagname and a reference to
#itself
my $p=HTML::Parser->new(start_h => [\&start,"tagname, self"],);



#setting xml mode
my $bool="true";
$p->xml_mode($bool);
$p->eof;

#get our xml info
my $xml = $response->content;

 
#make sure we actually got something
if( $response->is_success ) {
 #request succeeded
	#this will print out the actual xml file that was returned
	#uncomment this to see what you are parsing
        #print $response->content;
	
	#start the parser parsing the xml
        $p->parse($xml);

} else {
 #request failed
        print $response->error_as_HTML;
}


#this is called when we meet a start tag
sub start {
my($self, $tagname,$origtext) = @_;
 
$tagName = $self;
#print the tagname
print("$self\n");
#set a handler for dealing with the text for this tag
$tagname->handler(text => \&text, "dtext");

}
#prints out the html text for the tag
sub text {
 my($self, $origtext, $is_cdata) = @_;
                       #...
#print($self." ".$origtext."\n");
print($self."\n");

}




-- 
Devin Whalen
Programmer
Synaptic Vision Inc
Phone-(416) 539-0801
Fax- (416) 539-8280
1179A King St. West
Toronto, Ontario
Suite 309 M6K 3C5
Home-(416) 653-3982
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list