00001 package BBCLocation;
00002 use strict;
00003 require Exporter;
00004 use LWP::Simple;
00005
00006 our @EXPORT = qw(Search);
00007 our $VERSION = 0.1;
00008
00009 my @searchresults;
00010
00011 sub Search {
00012
00013 my $base_url = 'http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl?search_query=';
00014
00015 my $search_string = shift;
00016
00017 my $response = get $base_url . $search_string;
00018 die unless defined $response;
00019
00020 my $isresults = 0;
00021 my $isredirect = 0;
00022 my $isworld = 0;
00023
00024 my $havename = 0;
00025 my $haveid = 0;
00026
00027 my $resultline = "";
00028
00029 foreach (split("\n", $response)) {
00030 if (/<title>.*?Search Results.*?<\/title>/) {
00031 $isresults = 1;
00032 }
00033 else {
00034 $isredirect = 1;
00035 }
00036
00037 my $locname;
00038 my $locid;
00039
00040 if ($isresults) {
00041
00042 if (/No locations were found/) {
00043 last;
00044 }
00045 elsif (/if query returns results close/) {
00046 last;
00047 }
00048 elsif (/non UK towns results open/) {
00049 $isworld = 1;
00050 }
00051 elsif (/\/weather\/5day\.shtml\?id=/) {
00052 $locid = $_;
00053 $locid =~ s/.*?\?id=(\d{0,4}).*/$1/s;
00054
00055 $locname = $_;
00056 $locname =~ s/.*?<strong>(.*?)<.*/$1/s;
00057
00058 $resultline = "L" . $locid . "::" . $locname . ", United Kingdom";
00059 push (@searchresults, $resultline);
00060 }
00061 elsif (/\/weather\/5day\.shtml\?world=/) {
00062 $locid = $_;
00063 $locid =~ s/.*?\?world=(\d{4}).*/$1/s;
00064
00065 $locname = $_;
00066 $locname =~ s/.*?<strong>(.*?)<.*/$1/s;
00067
00068 $resultline = "W" . $locid . "::" . $locname;
00069
00070 unless ($isworld) {
00071 $resultline = $resultline . ", United Kingdom";
00072 push (@searchresults, $resultline);
00073 }
00074 }
00075 elsif ($isworld && /<\/a><\/strong>/) {
00076 my $country = $_;
00077 $country =~ s/.*?>([a-zA-Z ,']*)<\/a><\/strong>.*/$1/;
00078 $resultline = $resultline . ", " . $country;
00079 push (@searchresults, $resultline);
00080 }
00081
00082 }
00083 elsif ($isredirect) {
00084 if (/name : \"/) {
00085 my $locname = $_;
00086 $locname =~ s/^.*name : \"(.*?)\".*$/$1/;
00087 $resultline = $resultline . "::" . $locname . ", United Kingdom";
00088 $havename = 1;
00089 }
00090 if (/rssloc :/) {
00091 my $id = 0;
00092 $id = s/.*rssloc : (\d{4}),.*/$1/;
00093 $resultline = "W" . $_ . $resultline;
00094 $haveid = 1;
00095 }
00096 }
00097
00098 if ($havename && $haveid) {
00099 push (@searchresults, $resultline);
00100 last;
00101 }
00102 }
00103
00104 return @searchresults;
00105 }
00106
00107 1;