| 1 | *** yrnoxml.pl.orig 2012-01-01 13:39:35.352683073 +0100
|
|---|
| 2 | --- yrnoxml.pl 2012-01-01 13:44:21.415674460 +0100
|
|---|
| 3 | ***************
|
|---|
| 4 | *** 11,16 ****
|
|---|
| 5 | --- 11,17 ----
|
|---|
| 6 | use URI::Escape;
|
|---|
| 7 | use XML::XPath;
|
|---|
| 8 | use XML::XPath::XMLParser;
|
|---|
| 9 | + use JSON;
|
|---|
| 10 | use DateTime::Format::ISO8601;
|
|---|
| 11 | use POSIX qw(strftime);
|
|---|
| 12 | use File::Path;
|
|---|
| 13 | ***************
|
|---|
| 14 | *** 54,60 ****
|
|---|
| 15 | mkpath( $logdir, {mode => 0755} );
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | ! getopts('Tvtlu:d:');
|
|---|
| 19 |
|
|---|
| 20 | if (defined $opt_v) {
|
|---|
| 21 | print "$name,$version,$author,$email\n";
|
|---|
| 22 | --- 55,61 ----
|
|---|
| 23 | mkpath( $logdir, {mode => 0755} );
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | ! getopts('Tvtlu:d:D');
|
|---|
| 27 |
|
|---|
| 28 | if (defined $opt_v) {
|
|---|
| 29 | print "$name,$version,$author,$email\n";
|
|---|
| 30 | ***************
|
|---|
| 31 | *** 79,96 ****
|
|---|
| 32 | if (defined $opt_l) {
|
|---|
| 33 | my $search = uri_escape(shift);
|
|---|
| 34 | log_print( $logdir, "-l $search\n" );
|
|---|
| 35 | ! my $base_url = 'http://www.yr.no/soek.aspx?sted=';
|
|---|
| 36 |
|
|---|
| 37 | ! my $xp = getCachedXML($base_url . $search, $dir, $search . ".html",
|
|---|
| 38 | $updateTimeout, $logdir);
|
|---|
| 39 |
|
|---|
| 40 | ! my $nodeset = $xp->find('//td[@class="place"]/a');
|
|---|
| 41 | ! foreach my $node ($nodeset->get_nodelist) {
|
|---|
| 42 | ! my $loc = $node->getAttribute("href");
|
|---|
| 43 | ! $loc =~ s/^\/place\///;
|
|---|
| 44 | ! $loc =~ s/\/$//;
|
|---|
| 45 | ! print $loc . "::" . $node->string_value . "\n";
|
|---|
| 46 | }
|
|---|
| 47 | exit 0;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | --- 80,100 ----
|
|---|
| 51 | if (defined $opt_l) {
|
|---|
| 52 | my $search = uri_escape(shift);
|
|---|
| 53 | log_print( $logdir, "-l $search\n" );
|
|---|
| 54 | ! my $base_url = 'http://www.yr.no/_/websvc/jsonforslagsboks.aspx?s1t=&s1i=&s2t=&s2i=&s=';
|
|---|
| 55 |
|
|---|
| 56 | ! my $response = getCachedJSON($base_url . $search, $dir, $search . ".json",
|
|---|
| 57 | $updateTimeout, $logdir);
|
|---|
| 58 |
|
|---|
| 59 | ! my @cities=@{$$response[1]};
|
|---|
| 60 | ! if (@cities)
|
|---|
| 61 | ! { foreach my $city (@cities) {
|
|---|
| 62 | ! my ($cityName,$url,$location,$country)=@{$city};
|
|---|
| 63 | !
|
|---|
| 64 | ! $url=~s/^\/place//;
|
|---|
| 65 | ! printf("%s::%s, %s, %s\n",$url,$cityName,$location,$country);
|
|---|
| 66 | ! }
|
|---|
| 67 | }
|
|---|
| 68 | +
|
|---|
| 69 | exit 0;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | ***************
|
|---|
| 73 | *** 258,263 ****
|
|---|
| 74 | --- 262,303 ----
|
|---|
| 75 | return $xp;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | + sub getCachedJSON {
|
|---|
| 79 | + my ($url, $dir, $file, $timeout, $logdir) = @_;
|
|---|
| 80 | +
|
|---|
| 81 | + my $cachefile = "$dir/$file";
|
|---|
| 82 | + my $xp;
|
|---|
| 83 | +
|
|---|
| 84 | + my $now = time();
|
|---|
| 85 | +
|
|---|
| 86 | + if( (-e $cachefile) and ((stat($cachefile))[9] >= ($now - $timeout)) ) {
|
|---|
| 87 | + # File cache is still recent.
|
|---|
| 88 | + log_print( $logdir, "cached in $cachefile\n" );
|
|---|
| 89 | + } else {
|
|---|
| 90 | + log_print( $logdir, "$url\ncaching to $cachefile\n" );
|
|---|
| 91 | + my $ua = LWP::UserAgent->new;
|
|---|
| 92 | + $ua->timeout(30);
|
|---|
| 93 | + $ua->env_proxy;
|
|---|
| 94 | + $ua->default_header('Accept' => "application/json, text/javascript, */*; q=0.01");
|
|---|
| 95 | + $ua->default_header('Accept-Language' => "en");
|
|---|
| 96 | +
|
|---|
| 97 | + my $response = $ua->get($url);
|
|---|
| 98 | + if ( !$response->is_success ) {
|
|---|
| 99 | + die $response->status_line;
|
|---|
| 100 | + }
|
|---|
| 101 | +
|
|---|
| 102 | + open OF, ">$cachefile" or die "Can't open $cachefile: $!\n";
|
|---|
| 103 | + print OF $response->content;
|
|---|
| 104 | + close OF;
|
|---|
| 105 | + }
|
|---|
| 106 | +
|
|---|
| 107 | + open IF, "<$cachefile" or die "Can't open $cachefile: $!\n";
|
|---|
| 108 | + my $content=<IF>;
|
|---|
| 109 | + close IF;
|
|---|
| 110 | +
|
|---|
| 111 | + return decode_json($content);
|
|---|
| 112 | + }
|
|---|
| 113 | +
|
|---|
| 114 | sub convert_temp {
|
|---|
| 115 | my ( $degC, $units ) = @_;
|
|---|
| 116 | my $deg;
|
|---|