Ticket #7999: BBCLocation.patch

File BBCLocation.patch, 2.3 KB (added by Mike Parker <michael.parker@…>, 10 years ago)

BBCLocation.pm patch file

  • BBCLocation.

    old new  
    3939    my $response = shift;
    4040    my $isresults = 0;
    4141    my $resultline = "";
     42   
     43    # Initialise a hash for the $locid & $locname results.
     44     
     45    # Use of a hash indexed by $locid ensures that more informative results
     46    # (e.g. "Sale, Australia" vs. "Sale") coming from <p class="response">
     47    # section will overwrite less informative results coming from
     48    # <span id="printbutton_Forecast"> section
     49   
     50    my %loc_hash = ();
    4251
    4352    foreach (split("\n", $response)) {
    44         if (/<p class=\"response\">/) {
     53   
     54        # Declare a result if either the '<p class="response"> OR <span id="printbutton_Forecast"> strings are found
     55        # This ensures that single and multiple matches are caught
     56   
     57        if (/<p class=\"response\">/ || /<span id=\"printbutton_Forecast\">/) {
    4558            $isresults = 1;
    4659        }
    4760
    4861        my $locname;
    4962        my $locid;
    5063        my $url;
    51 
     64       
    5265        if ($isresults) {
    5366            last if (/There are no forecasts matching/);
    5467
     
    7083                $locname =~ s/.*<a id=\"result_\d*\".*>(.*)<\/a>.*/$1/s;
    7184
    7285                $resultline = $locid . "::" . $locname;
    73                 if (! grep(/^$locid/, @searchresults)) {
    74                     push (@searchresults, $resultline);
    75                 }
     86               
     87                $loc_hash{$locid} = $locname;
    7688            }
     89           
     90            # Extract location ID and name from "Print <location>" link
     91           
     92            # This string is always present (provided valid search result - invalid results are caught above)
     93            # irresepective of whether single or multiple matches are returned
     94           
     95            elsif (/<span id=\"printbutton_Forecast\"><a title=\"Print (.+)\" href=\"\/weather\/forecast\/(\d{0,5})?/o)
     96            {
     97                $locid   = $2;
     98                $locname = $1;
     99               
     100                $loc_hash{$locid} = $locname;               
     101            }
    77102        }
    78103    }
     104   
     105    # Loop through contents of %loc_hash, check for existence within @searchresults, and add as necessary
     106   
     107    foreach my $key (keys %loc_hash)
     108    {
     109        my $resultline = $key."::".$loc_hash{$key};
     110   
     111        if (! grep(/^$key/, @searchresults))
     112        {
     113                push (@searchresults, $resultline);
     114        }
     115    }
    79116
    80117    return @searchresults;
    81118}