Ticket #4519: 4519_diff.4.patch

File 4519_diff.4.patch, 25.3 KB (added by Joe Ripley <vitaminjoe@…>, 16 years ago)

Update ENVCAN grabber to use RSS feed from Environment Canada, also correct some whitespace issues in other modules

  • mythplugins/mythweather/mythweather/scripts/envcan.pl

     
    2424my $email   = 'vitaminjoe@gmail.com';
    2525my $updateTimeout = 15*60;
    2626my $retrieveTimeout = 30;
    27 my @types = ('cclocation', 'station_id',
    28                         'observation_time', 'observation_time_rfc822', 'weather',
    29                         'temp', 'relative_humidity',
    30                         'wind_dir', 'wind_degrees', 'wind_speed', 'wind_gust',
    31                         'pressure', 'dewpoint', 'heat_index', 'windchill',
    32                         'visibility', 'weather_icon', 'appt', 'wind_spdgst',
    33                         '3dlocation', '6dlocation', 'date-0', 'icon-0', 'low-0', 'high-0',
    34                         'date-1', 'icon-1', 'low-1', 'high-1',
    35                         'date-2', 'icon-2', 'low-2', 'high-2', 'updatetime',
    36                         'date-3', 'icon-3', 'low-3', 'high-3',
    37                         'date-4', 'icon-4', 'low-4', 'high-4',
    38                         'date-5', 'icon-5', 'low-5', 'high-5' );
     27my @types = ('cclocation', 'station_id', 'copyright',
     28            'observation_time', 'observation_time_rfc822', 'weather',
     29            'temp', 'relative_humidity',
     30            'wind_dir', 'wind_degrees', 'wind_speed', 'wind_gust',
     31            'pressure', 'dewpoint', 'heat_index', 'windchill',
     32            'visibility', 'weather_icon', 'appt', 'wind_spdgst',
     33            '3dlocation', '6dlocation', 'date-0', 'icon-0', 'low-0', 'high-0',
     34            'date-1', 'icon-1', 'low-1', 'high-1',
     35            'date-2', 'icon-2', 'low-2', 'high-2', 'updatetime',
     36            'date-3', 'icon-3', 'low-3', 'high-3',
     37            'date-4', 'icon-4', 'low-4', 'high-4',
     38            'date-5', 'icon-5', 'low-5', 'high-5' );
    3939
    4040my $dir = "./";
    4141
    4242getopts('Tvtlu:d:');
    4343
    4444if (defined $opt_v) {
    45         print "$name,$version,$author,$email\n";
    46         exit 0;
     45    print "$name,$version,$author,$email\n";
     46    exit 0;
    4747}
    4848
    4949if (defined $opt_T) {
    50         print "$updateTimeout,$retrieveTimeout\n";
    51         exit 0;
     50    print "$updateTimeout,$retrieveTimeout\n";
     51    exit 0;
    5252}
    5353if (defined $opt_l) {
    54         my $search = shift;
    55         ENVCANLocation::AddStationIdSearch($search);
    56         ENVCANLocation::AddRegionIdSearch($search);
    57         ENVCANLocation::AddCitySearch($search);
    58         ENVCANLocation::AddProvinceSearch($search);
    59         my $results = doSearch();
    60         my $result;
    61         while($result = shift @$results) {
    62                 if ($result->{station_id} ne "NA" ) {
    63                         print "$result->{station_id}::";
    64                         print "$result->{city}, $result->{region_id}\n";
    65                 }
    66         }
    67 
    68 exit 0;
    69 
     54    my $search = shift;
     55    ENVCANLocation::AddStationIdSearch($search);
     56    ENVCANLocation::AddRegionIdSearch($search);
     57    ENVCANLocation::AddCitySearch($search);
     58    ENVCANLocation::AddProvinceSearch($search);
     59    my $results = doSearch();
     60    my $result;
     61    while($result = shift @$results) {
     62        if ($result->{station_id} ne "NA" ) {
     63            print "$result->{station_id}::";
     64            print "$result->{city}, $result->{region_id}\n";
     65        }
     66    }
     67    exit 0;
    7068}
    7169
    7270
    7371if (defined $opt_t) {
    74         foreach (@types) {print; print "\n";}
    75         exit 0;
     72    foreach (@types) {print; print "\n";}
     73    exit 0;
    7674}
    7775
    7876if (defined $opt_d) {
    79         $dir = $opt_d;
     77    $dir = $opt_d;
    8078}
    8179
    8280# check variables for defined status
     
    106104
    107105# no cache, grab from the web
    108106if ($getData) {
    109     my $base_url = 'http://www.weatheroffice.gc.ca/forecast/city_e.html?';
    110     my $response = get $base_url . $loc;
     107    my $base_url = 'http://www.weatheroffice.gc.ca/rss/city/';
     108    my $response = get $base_url . $loc .'_e.xml';
    111109    die unless defined $response;
    112110
    113111    %results = ENVCANParser::doParse($response, @types);
     
    129127
    130128# do some quick conversions
    131129if ($units eq "ENG") {
    132         $results{'temp'}       = int(((9/5) * $results{'temp'}) + 32);
    133         $results{'dewpoint'}   = int(((9/5) * $results{'dewpoint'}) + 32);
    134         $results{'windchill'}  = int(((9/5) * $results{'windchill'}) + 32);
    135         $results{'appt'}       = int(((9/5) * $results{'appt'}) + 32);
    136         $results{'visibility'} = sprintf("%.1f", ($results{'visibility'} * 0.621371192));
    137         $results{'pressure'}   = sprintf("%.2f", $results{'pressure'} * 0.0295301);
    138         $results{'wind_gust'}  = sprintf("%.2f", $results{'wind_gust'} * 0.621371192);
    139         $results{'wind_speed'} = sprintf("%.2f", $results{'wind_speed'} * 0.621371192);
    140         $results{'wind_spdgst'} = sprintf("%.2f (%.2f) mph", $results{'wind_speed'}, $results{'wind_gust'});
    141        
    142         for (my $i=0;$i<6;$i++) {
    143         if ($results{"high-$i"} ne "NA") {
    144                 $results{"high-$i"} = int(((9/5) * $results{"high-$i"}) + 32);
     130    $results{'temp'}       = int(((9/5) * $results{'temp'}) + 32);
     131    $results{'dewpoint'}   = int(((9/5) * $results{'dewpoint'}) + 32);
     132    $results{'windchill'}  = int(((9/5) * $results{'windchill'}) + 32);
     133    $results{'appt'}       = int(((9/5) * $results{'appt'}) + 32);
     134    $results{'visibility'} = sprintf("%.1f", ($results{'visibility'} * 0.621371192));
     135    $results{'pressure'}   = sprintf("%.2f", $results{'pressure'} * 0.0295301);
     136    $results{'wind_gust'}  = sprintf("%.2f", $results{'wind_gust'} * 0.621371192);
     137    $results{'wind_speed'} = sprintf("%.2f", $results{'wind_speed'} * 0.621371192);
     138    $results{'wind_spdgst'} = sprintf("%.2f (%.2f) mph", $results{'wind_speed'}, $results{'wind_gust'});
     139   
     140    for (my $i=0;$i<6;$i++) {
     141        if ($results{"high-$i"} =~ /\d*/) {
     142            $results{"high-$i"} = int(((9/5) * $results{"high-$i"}) + 32);
    145143        }
    146         if ($results{"low-$i"} ne "NA") {
    147                     $results{"low-$i"} = int(((9/5) * $results{"low-$i"}) + 32);
     144        if ($results{"low-$i"} =~ /\d*/) {
     145            $results{"low-$i"} = int(((9/5) * $results{"low-$i"}) + 32);
    148146        }
    149         }
     147    }
    150148} else {
    151         $results{'wind_spdgst'} = sprintf("%.2f (%.2f)", $results{'wind_speed'}, $results{'wind_gust'});
     149    $results{'wind_spdgst'} = sprintf("%.2f (%.2f)", $results{'wind_speed'}, $results{'wind_gust'});
    152150}
    153        
     151   
    154152
    155153foreach my $key (sort (keys %results)) {
    156         print "$key". "::";
    157         if (length($results{$key}) == 0) {
    158                 print "NA\n";
    159         } else {
    160                 print $results{$key} ."\n";
    161         }
     154    print "$key". "::";
     155    if (length($results{$key}) == 0) {
     156        print "NA\n";
     157    } else {
     158        print $results{$key} ."\n";
     159    }
    162160}
    163161
  • mythplugins/mythweather/mythweather/scripts/envcan_animaps.pl

     
    3737    exit 0;
    3838}
    3939if (defined $opt_l) {
    40         my $search = shift;
     40    my $search = shift;
    4141    ENVCANMapSearch::AddSatSearch($search);
    4242    ENVCANMapSearch::AddSatClassSearch($search);
    4343    ENVCANMapSearch::AddImageTypeSearch($search);
     
    7979
    8080# Get list of images (at most 10)
    8181foreach my $line (split(/\n/, $response)) {
    82         if ($line =~ /theImagesComplete\[\d*\] \= \"(.*)\"\;/) {
    83                 push (@image_list, $1);
    84                 if ($#image_list >= 10) { shift @image_list; }
    85         }
     82    if ($line =~ /theImagesComplete\[\d*\] \= \"(.*)\"\;/) {
     83        push (@image_list, $1);
     84        if ($#image_list >= 10) { shift @image_list; }
     85    }
    8686}
    8787
    8888# Download map files, if necessary (maps are stale after 15 minutes)
     
    9595    }
    9696
    9797    getstore($base_url . $image, $path . $i);
    98         $i++;
     98    $i++;
    9999}
    100100
    101101# Determine image size
    102102if (!$size) {
    103         use Image::Size;
    104         my ($x, $y) = imgsize("${path}0");
    105         $size = "${x}x$y" if ($x && $y);
     103    use Image::Size;
     104    my ($x, $y) = imgsize("${path}0");
     105    $size = "${x}x$y" if ($x && $y);
    106106}
    107107
    108108print  "amdesc::$desc\n";
  • mythplugins/mythweather/mythweather/scripts/ENVCANParser.pm

     
    1 # This script parses the HTML of an Environment Canada weather forecast
     1# This script parses the XML of an Environment Canada weather forecast
    22# page as returned from http://www.weatheroffice.gc.ca.
    33#
    4 # TODO  Environment Canada only reports 5 day forecasts.
    5 #       6 day forecast layout is used to report 5 day information.
     4# TODO  Environment Canada only reports 5 day forecasts.  6 day forecast
     5#       layout is used to report 5 day information.
    66#
    7 # TODO  Code is pretty messy right now, by no means an elegant solution
    8 #
    9 # This requires the HTML::Parser module.
     7# This requires the XML::Simple module.
    108
    119package ENVCANParser;
    1210use strict;
    1311use POSIX;
    14 use HTML::Parser ();
     12use XML::Simple;
    1513
    16 our $VERSION = 0.1;
     14our $VERSION = 0.2;
    1715
    1816my %results;
    19 my %inside;
    20 my $scratch;
    21 my %directions = ( N => "North", S => "South", E => "East", W => "West",
    22                   NE => "Northeast", NW => "Northwest",
    23                   SE => "Southeast", SW => "Southwest");
     17my %directions = (  N  => "North",      NNE => "North Northeast",
     18                    S  => "South",      ENE => "East Northeast",
     19                    E  => "East",       ESE => "East Southeast",
     20                    W  => "West",       SSE => "South Southeast",
     21                    NE => "Northeast",  SSW => "South Southwest",
     22                    NW => "Northwest",  WSW => "West Southwest",
     23                    SE => "Southeast",  WNW => "West Northwest",
     24                    SW => "Southwest",  NNW => "North Northwest");
    2425
    25 sub start_h {
    26     my ($tag, %attr) = @_;
    27     $inside{$tag}  = 1;
     26sub getIcon {
     27    my $condition = shift;
     28    my $icon;
    2829
    29     # Copy attributes
    30     $inside{"$tag-id"}       = $attr{id}       if defined $attr{id};
    31     $inside{"$tag-class"}    = $attr{class}    if defined $attr{class};
    32     $inside{"$tag-src"}      = $attr{src}      if defined $attr{src};
    33     $inside{"$tag-longdesc"} = $attr{longdesc} if defined $attr{longdesc};
    34 
    35     doIMG() if $inside{img};
    36 }
    37 
    38 # End tag, clear hash
    39 sub end_h {
    40     my ($tag) = @_;
    41     undef $inside{$tag};
    42     undef $inside{"$tag-id"};
    43     undef $inside{"$tag-class"};
    44 }
    45 
    46 sub text {
    47 
    48     if (defined($inside{'title'}) && $inside{'title'}) {
    49         $_[0] =~ s/\n//sg;
    50         $_[0] =~ /(.*),\s*(.*)- 5 Day Weather/;
    51         $results{'cclocation'} = "$1, $2";
    52         $results{'3dlocation'} = "$1, $2";
    53         $results{'6dlocation'} = "$1, $2";
     30    if ( ($condition =~ /snow/i) || ($condition =~ /flurries/i)) {
     31        $icon = 'flurries.png';
     32        $icon = 'rainsnow.png' if ($condition =~ /rain/i);
     33        $icon = 'snowshow.png' if ($condition =~ /heavy/i);
    5434    }
    55 
    56     if (defined($inside{'h3'}) && $inside{'h3'}) {
    57         if ($inside{'div-id'} eq "f1") { $results{'date-0'} = $_[0]; }
    58         if ($inside{'div-id'} eq "f2") { $results{'date-1'} = $_[0]; }
    59         if ($inside{'div-id'} eq "f3") { $results{'date-2'} = $_[0]; }
    60         if ($inside{'div-id'} eq "f4") { $results{'date-3'} = $_[0]; }
    61         if ($inside{'div-id'} eq "f5") { $results{'date-4'} = $_[0]; }
    62         if ($inside{'div-id'} eq "f6") { $results{'date-5'} = $_[0]; }
     35    elsif ($condition =~ /fog/i) {
     36        $icon = 'fog.png';
    6337    }
    64 
    65     if ($inside{li}) {
    66         if (defined($inside{'li-class'})) {
    67             if ($inside{'li-class'} eq "low") {
    68                 if ($inside{'div-id'} eq "f1") {
    69                     $_[0] =~ /\w* (-?\d*)/; $results{'low-0'} = $1; }
    70                 if ($inside{'div-id'} eq "f2") {
    71                     $_[0] =~ /\w* (-?\d*)/; $results{'low-1'} = $1; }
    72                 if ($inside{'div-id'} eq "f3") {
    73                     $_[0] =~ /\w* (-?\d*)/; $results{'low-2'} = $1; }
    74                 if ($inside{'div-id'} eq "f4") {
    75                     $_[0] =~ /\w* (-?\d*)/; $results{'low-3'} = $1; }
    76                 if ($inside{'div-id'} eq "f5") {
    77                     $_[0] =~ /\w* (-?\d*)/; $results{'low-4'} = $1; }
    78                 if ($inside{'div-id'} eq "f6") {
    79                     $_[0] =~ /\w* (-?\d*)/; $results{'low-5'} = $1; }
    80             }
    81 
    82             if ($inside{'li-class'} eq "high") {
    83                 if ($inside{'div-id'} eq "f1") {
    84                     $_[0] =~ /\w* (-?\d*)/; $results{'high-0'} = $1; }
    85                 if ($inside{'div-id'} eq "f2") {
    86                     $_[0] =~ /\w* (-?\d*)/; $results{'high-1'} = $1; }
    87                 if ($inside{'div-id'} eq "f3") {
    88                     $_[0] =~ /\w* (-?\d*)/; $results{'high-2'} = $1; }
    89                 if ($inside{'div-id'} eq "f4") {
    90                     $_[0] =~ /\w* (-?\d*)/; $results{'high-3'} = $1; }
    91                 if ($inside{'div-id'} eq "f5") {
    92                     $_[0] =~ /\w* (-?\d*)/; $results{'high-4'} = $1; }
    93                 if ($inside{'div-id'} eq "f6") {
    94                     $_[0] =~ /\w* (-?\d*)/; $results{'high-5'} = $1; }
    95             }
    96         }
     38    elsif ( ($condition =~ /rain/i) || ($condition =~ /showers/i) ) {
     39        $icon = 'showers.png';
     40        $icon = 'lshowers.png'  if ( ($condition =~ /chance/i) ||
     41                                     ($condition =~ /few/i));
     42        $icon = 'rainsnow.png'  if ( ($condition =~ /snow/i)   ||
     43                                     ($condition =~ /flurries/i));
     44        $icon = 'thunshowers.png' if ($condition =~ /thunder/i);
    9745    }
    98 
    99     if (defined($inside{'div'}) && $inside{'div'}) {
    100         if (defined($inside{'div-class'})
    101                 && ($inside{'div-class'} eq "citycondition")) {
    102             $results{'weather'} = $_[0];
    103         }
    104 
    105         if (defined($inside{'div-id'})
    106                 && ($inside{'div-id'} eq "cityobserved")) {
    107             $_[0] =~ /.* (\d*\:\d*.*)/;
    108             $results{'observation_time'}        = "Last updated at $1";
    109             $results{'updatetime'}              = "Last updated at $1";
    110             $results{'observation_time_rfc822'} = rfc822($1);
    111         }
     46    elsif ($condition =~ /cloud/i) {
     47        $icon = 'cloudy.png';
     48        $icon = 'mcloudy.png' if ($condition =~ /mostly/i);
     49        $icon = 'pcloudy.png' if ( ($condition =~ /few/i)    ||
     50                                   ($condition =~ /mix/i)    ||
     51                                   ($condition =~ /partly/i) ||
     52                                   ($condition =~ /period/i) );
    11253    }
    113 
    114     if (defined($inside{'dt'}) && $inside{'dt'}) {
    115         if ($_[0] =~ /(Temperature)/)         { $scratch = 1; return; }
    116         if ($_[0] =~ /(Pressure)\/ Tendency/) { $scratch = 2; return; }
    117         if ($_[0] =~ /(Visibility)/)          { $scratch = 3; return; }
    118         if ($_[0] =~ /(Humidity)/)            { $scratch = 4; return; }
    119         if ($_[0] =~ /(Dewpoint)/)            { $scratch = 5; return; }
    120         if ($_[0] =~ /(Wind Chill)/ )         { $scratch = 6; return; }
    121         if ($_[0] =~ /(Wind)/)                { $scratch = 7; return; }
     54    elsif ($condition =~ /clear/i) {
     55        $icon = 'fair.png';
    12256    }
    123 
    124     if (defined($inside{'dd'}) && $inside{'dd'}) {
    125         if ($scratch == 1) {
    126             $_[0] =~ /(-?\d*).*/; $results{'temp'} = $1; }
    127         if ($scratch == 2) {
    128             $_[0] =~ /(\d*\.\d+) kPa.*/; $results{'pressure'} = $1 * 10; }
    129         if ($scratch == 3) {
    130             $_[0] =~ /(\d*) km/; $results{'visibility'} = $1; }
    131         if ($scratch == 4) {
    132             $_[0] =~ /(\d*) \%/; $results{'relative_humidity'} = $1; }
    133         if ($scratch == 5) {
    134             $_[0] =~ /(-?\d*).*/; $results{'dewpoint'} = $1; }
    135         if ($scratch == 6) {
    136             $_[0] =~ /(-?\d*).*/;
    137             $results{'appt'} = $1;
    138             $results{'windchill'} = $1;
    139         }
    140         if ($scratch == 7) {
    141             $_[0] =~ /.?(\w+) (\d+) km\/h/;
    142             $results{'wind_dir'} = $directions{$1};
    143             $results{'wind_speed'} = $2;
    144 
    145             if ($_[0] =~ /gust (\d+) km\/h/m) {
    146                 $results{'wind_gust'}   = $1;
    147             } else {
    148                 $results{'wind_gust'} = 0;
    149             }
    150         }
    151    
    152         $scratch = 0;
     57    elsif ($condition =~ /sun/i) {
     58        $icon = 'sunny.png';
    15359    }
    154 
    155 }
    156 
    157 sub doIMG {
    158     my $icon;
    159 
    160     # Get Icon
    161     if (defined($inside{'img-src'})
    162             && ($inside{'img-src'} =~ /\/weathericons\/(\d*\.gif)/)) {
    163         $icon = $1;
    164         open(FH, "ENVCAN_icons") or die "Cannot open icons";
    165         while (my $line = <FH>) {
    166             chomp $line;
    167             if ($line =~ /$icon\:\:(.*)/) {
    168                  $icon = $1;
    169                 last;
    170             }
    171         }
    172         close (FH);
     60    else {
     61        $icon = 'unknown.png';
    17362    }
    174            
    175     # Current conditions
    176     $results{"weather_icon"} = $icon
    177         if (defined($inside{'img-class'})
    178             && ($inside{'img-class'} eq "currentimg"));
    179     $results{"icon-0"} = $icon
    180         if (defined($inside{'img-longdesc'})
    181             && ($inside{'img-longdesc'} eq "#f1"));
    182     $results{"icon-1"} = $icon
    183         if (defined($inside{'img-longdesc'})
    184             && ($inside{'img-longdesc'} eq "#f2"));
    185     $results{"icon-2"} = $icon
    186         if (defined($inside{'img-longdesc'})
    187             && ($inside{'img-longdesc'} eq "#f3"));
    188     $results{"icon-3"} = $icon
    189         if (defined($inside{'img-longdesc'})
    190             && ($inside{'img-longdesc'} eq "#f4"));
    191     $results{"icon-4"} = $icon
    192         if (defined($inside{'img-longdesc'})
    193             && ($inside{'img-longdesc'} eq "#f5"));
    194     $results{"icon-5"} = $icon
    195         if (defined($inside{'img-longdesc'})
    196             && ($inside{'img-longdesc'} eq "#f6"));
    19763
    198     undef ($inside{'img-class'});
    199     undef ($inside{'img-src'});
    200     undef ($inside{'img-longdesc'});
    201     undef ($inside{'img'});
     64    return $icon;
    20265}
    20366
    204 sub rfc822 {
    205     my ($string) = @_;
     67sub doParse {
    20668
    207     if ($string =~ /(\d*):(\d*) (AM|PM) (...) \w* (\d*) (\w*) (\d*)/) {
    208         my $hour  = int($1) - 1;
    209         my $min   = int($2);
    210         my $ampm  = $3;
    211         my $tzone = $4;
    212         my $day   = $5;
    213         my $month = $6;
    214         my $year  = $7;
     69    my ($data, @types) = @_;
    21570
    216         if ($ampm eq "PM") { if (int($hour) < 11) { $hour += 12; } }
    217         $month = 0  if $month eq "January";
    218         $month = 1  if $month eq "February";
    219         $month = 2  if $month eq "March";
    220         $month = 3  if $month eq "April";
    221         $month = 4  if $month eq "May";
    222         $month = 5  if $month eq "June";
    223         $month = 6  if $month eq "July";
    224         $month = 7  if $month eq "August";
    225         $month = 8  if $month eq "September";
    226         $month = 9  if $month eq "October";
    227         $month = 10 if $month eq "November";
    228         $month = 11 if $month eq "December";
    229         $year  = int($year) - 1900;
     71    # Initialize results hash
     72    foreach my $type (@types) { $results{$type} = ""; }
    23073
    231         my $time_t = POSIX::mktime(0, $min, $hour, $day, $month, $year);
    232         my $now_string = localtime($time_t);
     74    my $xml = XMLin($data);
     75    die if (!$xml);
    23376
    234         return $now_string;
     77    $results{'copyright'}  = $xml->{channel}->{copyright};
     78    if ($xml->{channel}->{title} =~ /^(.*) - Weather/) {
     79        $results{'cclocation'} = ucfirst($1);
     80        $results{'3dlocation'} = ucfirst($1);
     81        $results{'6dlocation'} = ucfirst($1);
    23582    }
     83   
     84    my $i = 0;
     85    foreach my $item (@{$xml->{channel}->{item}}) {
     86        if ($item->{title} =~ /Current Conditions/) {
     87            if ($item->{description} =~ /Condition:\<\/b\>\s*([\w ]*)\s*\<br\/\>/s) {
     88                $results{'weather'} = $1;
     89                $results{'weather_icon'} = getIcon($1);
     90            }
     91            $results{'temp'}     = sprintf("%.0f", $1)
     92                if ($item->{description} =~ /Temperature:\<\/b\>\s*(-?\d*\.?\d*)\260\C\s*\<br\/\>/s);
     93            $results{'pressure'} = sprintf("%d", $1 * 10)
     94                if ($item->{description} =~ /Pressure \/ Tendency:\<\/b\>\s*(\d*\.?\d*) kPa\s*.*\<br\/\>/s);
     95            $results{'visibility'} = sprintf("%.1f", $1)
     96                if ($item->{description} =~ /Visibility:\<\/b\>\s*(\d*\.?\d*) km\s*.*\<br\/\>/s);
     97            $results{'relative_humidity'} = $1
     98                if ($item->{description} =~ /Humidity:\<\/b\>\s*(\d*) \%\<br\/\>/s);
     99            if ($item->{description} =~ /Wind Chill:\<\/b\>\s*(-?\d*\.?\d*)\s*\<br\/\>/s) {
     100                $results{'appt'} = $1;
     101                $results{'windchill'} = $1;
     102            }
     103            $results{'dewpoint'} = sprintf("%.0f", $1)
     104                if ($item->{description} =~ /Dewpoint:\<\/b\>\s*(-?\d*\.?\d*)\260\C\s*\<br\/\>/s);
     105            if ($item->{description} =~ /(\d*\:\d*[\w ]*\d*[\w *]\d*)\s*\<br\/\>/s) {
     106                $results{'observation_time'} = "Last updated at ". $1;
     107                $results{'updatetime'} = "Last updated at ". $1;
     108            }
     109            if ($item->{description} =~ /Wind:\<\/b\>(.*)\<br\/\>/s) {
     110                my $wind = $1;
     111                if ($wind =~ /\s*(\d*)\s*km\/h\s*/i) {
     112                    $results{'wind_dir'} = 'Calm';
     113                    $results{'wind_speed'} = $1;
     114                    $results{'wind_gust'} = 0;
     115                }
     116                if ($wind =~ /\s*(\w*)\s*(\d*)\s*km\/h\s*/i) {
     117                    $results{'wind_dir'}   = $directions{$1};
     118                    $results{'wind_speed'} = $2;
     119                }
     120                if ($wind =~ /\s*(\w*)\s*(\d*)\s*km\/h\s*gust\s*(\d*)\s*km\/h/i) {
     121                    $results{'wind_gust'}  = $3;
     122                }
     123            }
     124            next;
     125        }
    236126
    237     return "";
     127        if ($item->{title} =~ /^(.*):\s*([\w ]*)\.\s*(.*)/) {
     128            my $day       = $1;
     129            my $condition = $2;
     130            my $high_low  = $3;
     131            my $temp;
    238132
    239 }
     133            $results{"date-$i"} = $day;
     134            $results{"icon-$i"} = getIcon($condition);
    240135
    241 sub postProcess {
    242     # In the morning, Environment Canada doesn't report these variables:
    243     # low-0, high-1, low-2
    244     # Here we assume what they should be...
     136            if ($high_low =~ /high (\w*) (\d*)/i) {
     137                $temp = $2;
     138                if ($1 =~ /minus/i) { $temp = ($temp * -1); }
     139                $results{"high-$i"} = $temp;
     140            }
    245141
    246     if (($results{'low-0'} eq "NA")
    247             && ($results{'low-1'} <= $results{'high-0'})) {
    248         $results{'low-0'} = $results{'low-1'};
    249     }
    250     if (($results{'high-1'} eq "NA")
    251             && ($results{'high-0'} >= $results{'low-1'})) {
    252         $results{'high-1'} = $results{'high-0'};
    253     }
    254     if (($results{'low-2'} eq "NA")
    255             && ($results{'low-1'} <= $results{'high-2'})) {
    256         $results{'low-2'} = $results{'low-1'};
    257     }
     142            if ($high_low =~ /steady near (\w*) (\d*)/i) {
     143                $temp = $2;
     144                if ($1 =~ /minus/i) { $temp = ($temp * -1); }
     145                $results{"high-$i"} = $temp;
     146            }
    258147
    259     # In the afternoon, Environment Canada doesn't report these variables:
    260     #
    261     # high-0, low-1, high-2, low-3
    262     # Here we assume what they should be...
    263     if (($results{'high-0'} eq "NA")
    264             && ($results{'temp'} >= $results{'low-0'})) {
    265         $results{'high-0'} = $results{'temp'};
     148            if ($high_low =~ /low (\w*) (\d*)/i) {
     149                $temp = $2;
     150                if ($1    =~ /minus/i) { $temp = ($temp * -1); }
     151                $results{"low-$i"} = $temp;
     152            }
     153
     154            $results{"high-$i"} = 0 if ($high_low =~ /high zero/i);
     155            $results{"low-$i"}  = 0 if ($high_low =~ /low zero/i);
     156           
     157            $i++;
     158        }
    266159    }
    267     if (($results{'low-1'} eq "NA")
    268             && ($results{'low-2'} <= $results{'high-1'})) {
    269         $results{'low-1'} = $results{'low-2'};
    270     }
    271     if (($results{'high-2'} eq "NA")
    272             && ($results{'high-1'} >= $results{'low-2'})) {
    273         $results{'high-2'} = $results{'high-1'};
    274     }
    275     if (($results{'low-3'} eq "NA")
    276             && ($results{'low-2'} <= $results{'high-3'})) {
    277         $results{'low-3'} = $results{'low-2'};
    278     }
    279 }
    280160
    281 sub doParse {
     161    # Correct for Environment Canada's temperature methods
     162    $results{'low-0'}  = $results{'low-1'}  if (!length($results{'low-0'}));
     163    $results{'low-1'}  = $results{'low-0'}  if (!length($results{'low-1'}));
     164    $results{'low-2'}  = $results{'low-1'}  if (!length($results{'low-2'}));
     165    $results{'low-3'}  = $results{'low-4'}  if (!length($results{'low-3'}));
     166    $results{'high-0'} = $results{'temp'}   if (!length($results{'high-0'}) &&
     167                                            ($results{'temp'} >= $results{'low-0'}));
     168    $results{'high-1'} = $results{'high-0'} if (!length($results{'high-1'}));
    282169
    283     my ($data, @types) = @_;
    284 
    285     # Initialize results hash
    286     foreach my $type (@types) { $results{$type} = "NA"; }
    287 
    288     my $p = HTML::Parser->new(api_version => 3);
    289     $p->unbroken_text(1);
    290     $p->report_tags(qw(div dd dt h2 h3 img li p title));
    291     $p->ignore_elements(qw(style script));
    292     $p->handler( start => \&start_h, 'tagname, @attr');
    293     $p->handler( end   => \&end_h, "tagname");
    294     $p->handler( text  => \&text, "dtext");
    295     $p->parse($data) || die $!;
    296 
    297     # Do some post-processing to handle Environment Canada weirdness
    298     postProcess();
    299 
    300170    return %results;
    301171}
    302172
  • mythplugins/mythweather/mythweather/scripts/ENVCANMapSearch.pm

     
    4040    if ($name eq 'entry') {
    4141       $expat->{CurrEntry} = {};
    4242       $expat->{MatchFound} = 0;
    43         }
     43    }
    4444}
    4545
    4646sub Text {
     
    9999
    100100    elsif ($expat->in_element('animated_url')) {
    101101        $expat->{CurrEntry}->{animated_url} = $text;
    102         }
     102    }
    103103}
    104104
    105105sub EndTag {