temp.cgi


Code Index:

temp

  Report the current temperature for city $city from
  http://m.wund.com/.

#!/usr/bin/env perl

use strict;
use warnings;
use CGI qw(:all);
use LWP::UserAgent;

print header('text/html'),
      start_html(-title => 'Cities Basic Weather',
	  style => { -src => "/main.css",
	  -media => "all"}),
      h1('Cities Basic Weather'),
      "\n",
      start_form,
      "Enter city: ", textfield('city'),
      "\n",
      submit,
      end_form,
      "\n",
      hr;

if(param('city')) {
  my $city = param('city');
  print temp($city);
  print "\n";
} else {
  print "\n<p class=\"note_m\">\n\nUsage: ", url(-path_info => 1),
	"?city=\"your city\"\n(without quotes)\n<p/>\n"
}

print hr,
      "\n",
      '<div id="footer">',
      "\n",
      '<center>',
      "\n",
      '<p>',
      "\n",
      '<a href="http://validator.w3.org/check?uri=referer">',
      "\n",
      '<img align="left" class="valid" src="/images/valid-xhtml10.png" ',
      'alt="Valid XHTML 1.0!" title="Valid XHTML 1.0!" />',
      "\n",
      '</a>',
      "\n",
      '</p>',
      "\n",
      '<p>',
      '<a href="http://www.apache.org/">',
      "\n",
      '<img align="right" class="apache" src="/images/apache_pb.gif" ',
      "alt=\"Powered by Apache\" title=\"Powered by Apache\" />\n",
      '</a>',
      "\n",
      '</p>',
      "\n",
      '<br/>',
      "\n",
      '<p>',
      '<a href="http://jigsaw.w3.org/css-validator/validator/check/referer">',
      "\n",
      '<img align="left" class="valid" src="/images/vcss.png" ',
      'alt="Valid CSS!" title="Valid CSS!" />',
      "\n",
      '</a>',
      '</p>',
      "\n",
      '<div>',
      "\n",
      '</center>';

print end_html;

sub temp {
  my $city = shift;
  my $content;
  my $retstr;
  my $agent = "LWP";
  my $int_err = "Couldn't get temperature report :( <br/>\n";
  my $inf_err = "The response was malformed <br/>";
  my $empty_err = "The response was empty <br/>";
  my $na = "not available";
  my $cnf = "City not found! <br/>\n";

  # Create a new useragent
  my $ua = LWP::UserAgent->new();
  $ua->agent($agent);

  # Create a request
  my $req = HTTP::Request->new(
	  POST => 'http://m.wund.com/' .
	  'cgi-bin/findweather/getForecast?brand=mobile&query=' . $city
	  );

  $req->content_type('application/x-www-form-urlencoded');

  # Pass request to the user agent and get a response back
  my $res = $ua->request($req);

  # Check the outcome of the response
  if ($res->is_success) {
	if ($res->content =~ /City Not Found/) {
	  return $cnf;
	}
	$content = $res->content;
  }
  else {
	return $int_err;
  }

  my $cur_temp = my $windchill = my $sky = $content;
  $cur_temp =~ s/^.+?Temperature.+?<b>(-?\d+)<\/b>&deg;C.+$/$1°C/ms;
  
  if ($windchill =~ /Windchill/) {
	$windchill =~ s/^.+?Windchill.+?<b>(-?\d+)<\/b>&deg;C.+$/$1°C/ms;
  }
  else {
	$windchill = $na;
  }
  
  if ($sky =~ />Conditions</) {
	$sky =~ s/^.+?Conditions.+?<b>([\w\s]+)<\/b>.+$/$1/ms;
  }
  else {
	$sky = $na;
  }
  
  if ($cur_temp =~ /^[\s]$/) {
	return "${empty_err}\n";
  }
  elsif ($cur_temp !~ /^-?\d+°C$/ ||
		 ($windchill !~ /^-?\d+°C$/ && $windchill !~ /^$na$/) ||
		 ($sky !~ /^[\w\s]+$/ && $sky !~ /^$na$/)
		) {
	return "${inf_err}\n";
  }
  else {
    $retstr = "${city}: <br/><br/>\nTemperature: ${cur_temp} <br/>\n";
    $retstr .= "Windchill: ${windchill} <br/>\n";
    $retstr .= "Sky: ${sky} <br/>\n";
    $retstr .= " <br/>\n";
    return $retstr;
  }
}

1;