Permalink
Browse files

Update MetOffice weather grabbers for changes to API

  • Loading branch information...
stuartm committed Aug 27, 2012
1 parent 56b3ef2 commit fc5bb99d443257f4d5bfe0260e12db66b0b33abb
@@ -25,9 +25,13 @@ our $VERSION = 0.1;
# Free keys can be requested from
# https://register.metoffice.gov.uk/register/ddc
our $api_key = '40af3680-8fd5-4c68-a762-4a6fe107f4e2';
-our $copyright_str = '© Crown copyright 2012, the Met Office';
+# August 2012 - Terms changed the attribution string that must be displayed
+#our $copyright_str = '"© Crown copyright 2012, the Met Office";
+our $copyright_str = 'Contains public sector information licensed under the Open Government Licence';
+# August 2012 - Metoffice changed terms of use to forbid use of their logos in
+# applications using their data, therefore this should not be used!
our $copyright_logo = 'http://www.metoffice.gov.uk/lib/template/logos/MO_Landscape_W.jpg';
-our $forecast_url = 'http://partner.metoffice.gov.uk/public/val/wxfcs/all/xml/';
+our $forecast_url = 'http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/xml/';
my @searchresults;
my @resulturl;
@@ -20,6 +20,7 @@
use File::Path;
use Switch;
+use DateTime;
use Date::Parse;
use Date::Calc qw(Day_of_Week);
use File::Basename;
@@ -105,11 +106,26 @@
}
my $base_args = '?res=daily&key=' . $MetOffCommon::api_key;
+
+# BEGIN workaround for API bug, may be removed when fixed, although it does no
+# harm and possibly some benefits to it such as 6 days instead of the default
+# 5
+my $start_date = DateTime->now()->set_hour(0)->set_minute(0)->set_second(0);
+if (DateTime->now()->hour() >= 18) {
+ $start_date->add( days => 1 );
+}
+my $end_date = $start_date->clone();
+$end_date->add( days => 5 );
+$base_args = $base_args . '&valid-from=' . $start_date . '&valid-to=' . $end_date;
+print $base_args . "\n";
+# END workaround
+
my $url = "";
if ($locid =~ s/^(\d*)/$1/)
{
$url = $MetOffCommon::forecast_url . $1 . $base_args;
+ print $url . "\n";
}
else
{
@@ -127,31 +143,31 @@
}
printf "copyright::" . $MetOffCommon::copyright_str . "\n";
-printf "copyrightlogo::" . $MetOffCommon::copyright_logo . "\n";
+#printf "copyrightlogo::" . $MetOffCommon::copyright_logo . "\n";
printf "station_id::" . $locid . "\n";
my $location = $xml->{DV}->{Location}->{name};
printf "3dlocation::" . $location . "\n";
printf "6dlocation::" . $location . "\n";
printf "updatetime::" . localtime() . "\n";
my $i = 0;
-my $item;
+my $period;
-foreach $item (@{$xml->{DV}->{Location}->{Period}}) {
+foreach $period (@{$xml->{DV}->{Location}->{Period}}) {
- if (ref($item->{Rep}) ne 'ARRAY') {
+ if (ref($period->{Rep}) ne 'ARRAY') {
next;
}
- my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($item->{val});
+ my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($period->{value});
$year += 1900; # Returns year as offset from 1900
$month += 1; # Returns month starting at zero
my $dow = Day_of_Week($year,$month,$day);
$dow = $dow == 7 ? 0 : $dow;
printf "date-" . $i . "::" . $dow . "\n";
- my $daytime = $item->{Rep}[0];
- my $nighttime = $item->{Rep}[1];
+ my $daytime = $period->{Rep}[0];
+ my $nighttime = $period->{Rep}[1];
my $iconname = "unknown";
switch ($daytime->{W}) { # Weather Type
@@ -128,24 +128,24 @@
}
printf "copyright::" . $MetOffCommon::copyright_str . "\n";
-printf "copyrightlogo::" . $MetOffCommon::copyright_logo . "\n";
+#printf "copyrightlogo::" . $MetOffCommon::copyright_logo . "\n";
printf "station_id::" . $locid . "\n";
my $location = $xml->{DV}->{Location}->{name};
printf "18hrlocation::" . $location . "\n";
printf "updatetime::" . localtime() . "\n";
-my $item;
+my $period;
my $i = 0;
-foreach $item (@{$xml->{DV}->{Location}->{Period}}) {
+foreach $period (@{$xml->{DV}->{Location}->{Period}}) {
- if (ref($item->{Rep}) ne 'ARRAY') {
+ if (ref($period->{Rep}) ne 'ARRAY') {
next;
}
- foreach my $rep (@{$item->{Rep}}) {
+ foreach my $rep (@{$period->{Rep}}) {
- my $datetime = DateTime::Format::ISO8601->parse_datetime(substr($item->{val}, 0, -1));
+ my $datetime = DateTime::Format::ISO8601->parse_datetime(substr($period->{value}, 0, -1));
$datetime->add( minutes => $rep->{content} );
if ($datetime <= DateTime->now()->subtract( hours => 3 )) # Ignore periods in past

0 comments on commit fc5bb99

Please sign in to comment.