*** yrnoxml.pl.orig	2012-01-01 13:39:35.352683073 +0100
--- yrnoxml.pl	2012-01-01 13:44:21.415674460 +0100
***************
*** 11,16 ****
--- 11,17 ----
  use URI::Escape;
  use XML::XPath;
  use XML::XPath::XMLParser;
+ use JSON;
  use DateTime::Format::ISO8601;
  use POSIX qw(strftime);
  use File::Path;
***************
*** 54,60 ****
      mkpath( $logdir, {mode => 0755} );
  }
  
! getopts('Tvtlu:d:');
  
  if (defined $opt_v) {
      print "$name,$version,$author,$email\n";
--- 55,61 ----
      mkpath( $logdir, {mode => 0755} );
  }
  
! getopts('Tvtlu:d:D');
  
  if (defined $opt_v) {
      print "$name,$version,$author,$email\n";
***************
*** 79,96 ****
  if (defined $opt_l) {
      my $search = uri_escape(shift);
      log_print( $logdir, "-l $search\n" ); 
!     my $base_url = 'http://www.yr.no/soek.aspx?sted=';
  
!     my $xp = getCachedXML($base_url . $search, $dir, $search . ".html",
                            $updateTimeout, $logdir);
  
!     my $nodeset = $xp->find('//td[@class="place"]/a');
!     foreach my $node ($nodeset->get_nodelist) {
!         my $loc = $node->getAttribute("href");
! 	$loc =~ s/^\/place\///;
! 	$loc =~ s/\/$//;
!         print $loc . "::" . $node->string_value . "\n";
      }
      exit 0;
  }
  
--- 80,100 ----
  if (defined $opt_l) {
      my $search = uri_escape(shift);
      log_print( $logdir, "-l $search\n" ); 
!     my $base_url = 'http://www.yr.no/_/websvc/jsonforslagsboks.aspx?s1t=&s1i=&s2t=&s2i=&s=';
  
!     my $response = getCachedJSON($base_url . $search, $dir, $search . ".json",
                            $updateTimeout, $logdir);
  
!     my @cities=@{$$response[1]};
!     if (@cities)
!     { foreach my $city (@cities) {
!         my ($cityName,$url,$location,$country)=@{$city};
! 
!         $url=~s/^\/place//;
!         printf("%s::%s, %s, %s\n",$url,$cityName,$location,$country);
!       }
      }
+ 
      exit 0;
  }
  
***************
*** 258,263 ****
--- 262,303 ----
      return $xp;
  }
  
+ sub getCachedJSON {
+     my ($url, $dir, $file, $timeout, $logdir) = @_;
+ 
+     my $cachefile = "$dir/$file";
+     my $xp;
+ 
+     my $now = time();
+ 
+     if( (-e $cachefile) and ((stat($cachefile))[9] >= ($now - $timeout)) ) {
+         # File cache is still recent.
+         log_print( $logdir, "cached in $cachefile\n" );
+     } else {
+         log_print( $logdir, "$url\ncaching to $cachefile\n" );
+         my $ua = LWP::UserAgent->new;
+         $ua->timeout(30);
+         $ua->env_proxy;
+         $ua->default_header('Accept' => "application/json, text/javascript, */*; q=0.01");
+         $ua->default_header('Accept-Language' => "en");
+ 
+         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 IF, "<$cachefile" or die "Can't open $cachefile: $!\n";
+     my $content=<IF>;
+     close IF;
+ 
+     return decode_json($content);
+ }
+ 
  sub convert_temp {
      my ( $degC, $units ) = @_;
      my $deg;
