#!/usr/bin/perl
#
# This script retrieves all locations and names
# from the BBC's online RSS weather service and
# saves these into an XML file, which can for
# instance be loaded by the @Pel.Net Weathermon
# utility.
#
# PelliX 2009
#
use strict;

use XML::RSS::Parser::Lite;
use LWP::Simple;

my $startnum = 1;
my $stopnum = 5017;

open(FILE, '>>', 'out.csv');

while($startnum < $stopnum)
	{
	if(my $xml = get("http://newsrss.bbc.co.uk/weather/forecast/".$startnum."/Next3DaysRSS.xml"))
		{
		my $rp = new XML::RSS::Parser::Lite;
		if($rp->parse($xml))
			{
			print $rp->get('title');
			my $tmp = $rp->get('title');
			$tmp =~ s/BBC\s\-\sWeather\sCentre\s\-\sForecast\sfor//gi;
			print FILE $startnum.", ".$tmp.", http://newsrss.bbc.co.uk/weather/forecast/".$startnum."/Next3DaysRSS.xml\n";
			print $startnum."\n";
			}
		}
	$startnum++;
	}
close(FILE);

# parse the plaintext CSV list into XML

open(FILE, '<', "out.csv");
open(OUT, '>>', "test.xml");

print OUT "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>
<!-- This is an autogenerated WeatherService config file-->\n";
print OUT "<locations type=\"s:complextype\">\n";

while(<FILE>)
        {
        my $item = $_;
        my @values = split(/,/, $item);
        my $itemnum = $values[0];
        my $itemloc = $values[1].",".$values[2];
        my $itemurl = $values[3];
        $itemloc =~ s/  //gi;
        chomp($itemloc);
        chomp($itemurl);
        $itemurl = substr($itemurl, 1, (length($itemurl)-1));
        print OUT "<item>\n";
        print OUT "<number type=\"s:integer\">".$itemnum."</number>\n";
        print OUT "<location type=\"s:string\">".$itemloc."</location>\n";
        print OUT "<rssurl type=\"s:string\">".$itemurl."</rssurl>\n";
        print OUT "</item>\n";
        }
print OUT "</locations>\n";

close(FILE);
close(OUT);

exit;
