Ticket #11623: 2013-06-24-envcanparse.diff

File 2013-06-24-envcanparse.diff, 5.8 KB (added by jripley@…, 11 years ago)

Patch to fix ENVCANParser.pm for Environment Canada weather parsing

  • mythplugins/mythweather/mythweather/scripts/ca_envcan/ENVCANParser.pm

    diff --git mythplugins/mythweather/mythweather/scripts/ca_envcan/ENVCANParser.pm mythplugins/mythweather/mythweather/scripts/ca_envcan/ENVCANParser.pm
    index 7420a66..66677da 100644
    use strict; 
    1111use POSIX;
    1212use XML::Simple;
    1313
    14 our $VERSION = 0.5;
     14our $VERSION = 0.6;
    1515
    1616my %results;
    1717my %directions = (  N  => "North",      NNE => "North Northeast",
    my %directions = ( N => "North", NNE => "North Northeast", 
    2323                    SE => "Southeast",  WNW => "West Northwest",
    2424                    SW => "Southwest",  NNW => "North Northwest");
    2525
     26my %degrees = (     N  => 0,   NNE => 22.5,
     27                    S  => 180, ENE => 67.5,
     28                    E  => 90,  ESE => 112.5,
     29                    W  => 270, SSE => 157.5,
     30                    NE => 45,  SSW => 202.5,
     31                    NW => 315, WSW => 247.5,
     32                    SE => 135, WNW => 292.5,
     33                    SW => 225, NNW => 337.5);
     34
    2635sub getIcon {
    2736    my $condition = shift;
    2837    my $icon;
    sub getIcon { 
    3544    elsif ($condition =~ /fog/i) {
    3645        $icon = 'fog.png';
    3746    }
     47    elsif ($condition =~ /precip/i) {
     48        $icon = 'lshowers.png';
     49        $icon = 'rainsnow.png'  if ($condition =~ /freezing/i);
     50    }
    3851    elsif ($condition =~ /drizzle/i) {
    3952        $icon = 'lshowers.png';
    4053        $icon = 'rainsnow.png'  if ($condition =~ /freezing/i);
    sub getIcon { 
    4760                                     ($condition =~ /flurries/i));
    4861        $icon = 'thunshowers.png' if ($condition =~ /thunder/i);
    4962    }
     63    elsif ($condition =~ /overcast/i) {
     64        $icon = 'cloudy.png';
     65    }
    5066    elsif ($condition =~ /cloud/i) {
    5167        $icon = 'cloudy.png';
    5268        $icon = 'mcloudy.png' if ($condition =~ /mostly/i);
    sub getIcon { 
    5773    }
    5874    elsif ($condition =~ /clear/i) {
    5975        $icon = 'fair.png';
     76        $icon = 'pcloudy.png' if ($condition =~ /clearing/i);
    6077    }
    6178    elsif ($condition =~ /sun/i) {
    6279        $icon = 'sunny.png';
    sub doParse { 
    7592    # Initialize results hash
    7693    foreach my $type (@types) { $results{$type} = ""; }
    7794
    78     my $xml = XMLin($data);
     95    my $xml = XMLin($data, ForceArray => [ 'id' ] );
    7996    die if (!$xml);
    8097
    81     $results{'copyright'}  = $xml->{channel}->{copyright};
    82     if ($xml->{channel}->{title} =~ /^(.*) - Weather/) {
     98    $results{'copyright'}  = $xml->{rights};
     99    if ($xml->{title} =~ /^(.*) - Weather/) {
    83100        $results{'cclocation'} = ucfirst($1);
    84101        $results{'3dlocation'} = ucfirst($1);
    85102        $results{'6dlocation'} = ucfirst($1);
    86103    }
    87104   
    88105    my $i = 0;
    89     foreach my $item (@{$xml->{channel}->{item}}) {
     106
     107    foreach my $item (@{$xml->{entry}}) {
    90108        if ($item->{title} =~ /Current Conditions/) {
    91             if ($item->{description} =~ /Condition:\<\/b\>\s*([\w ]*)\s*\<br\/\>/s) {
     109            if ($item->{summary}->{content} =~ /Condition:\<\/b\>\s*([\w ]*)\s*\<br\/\>/s) {
    92110                $results{'weather'} = $1;
    93111                $results{'weather_icon'} = getIcon($1);
    94112            }
    95113            $results{'temp'}     = sprintf("%.0f", $1)
    96                 if ($item->{description} =~ /Temperature:\<\/b\>\s*(-?\d*\.?\d*)\&deg\;\C\s*\<br\/\>/s);
     114                if ($item->{summary}->{content} =~ /Temperature:\<\/b\>\s*(-?\d*\.?\d*)\&deg\;\C\s*\<br\/\>/s);
    97115            $results{'pressure'} = sprintf("%d", $1 * 10)
    98                 if ($item->{description} =~ /Pressure \/ Tendency:\<\/b\>\s*(\d*\.?\d*) kPa\s*.*\<br\/\>/s);
     116                if ($item->{summary}->{content} =~ /Pressure.*:\<\/b\>\s*(\d*\.?\d*) kPa\s*.*\<br\/\>/s);
    99117            $results{'visibility'} = sprintf("%.1f", $1)
    100                 if ($item->{description} =~ /Visibility:\<\/b\>\s*(\d*\.?\d*) km\s*.*\<br\/\>/s);
     118                if ($item->{summary}->{content} =~ /Visibility:\<\/b\>\s*(\d*\.?\d*) km\s*.*\<br\/\>/s);
    101119            $results{'relative_humidity'} = $1
    102                 if ($item->{description} =~ /Humidity:\<\/b\>\s*(\d*) \%\<br\/\>/s);
    103             if ($item->{description} =~ /Wind Chill:\<\/b\>\s*(-?\d*\.?\d*)\s*\<br\/\>/s) {
     120                if ($item->{summary}->{content} =~ /Humidity:\<\/b\>\s*(\d*) \%\<br\/\>/s);
     121            if ($item->{summary}->{content} =~ /Wind Chill:\<\/b\>\s*(-?\d*\.?\d*)\s*\<br\/\>/s) {
    104122                $results{'appt'} = $1;
    105123                $results{'windchill'} = $1;
    106124            }
    107125            $results{'dewpoint'} = sprintf("%.0f", $1)
    108                 if ($item->{description} =~ /Dewpoint:\<\/b\>\s*(-?\d*\.?\d*)\&deg\;\C\s*\<br\/\>/s);
    109             if ($item->{description} =~ /(\d*\:\d*[\w ]*\d*[\w *]\d*)\s*\<br\/\>/s) {
     126                if ($item->{summary}->{content} =~ /Dewpoint:\<\/b\>\s*(-?\d*\.?\d*)\&deg\;\C\s*\<br\/\>/s);
     127            if ($item->{summary}->{content} =~ /(\d*\:\d*[\w ]*\d*[\w *]\d*)\s*\<br\/\>/s) {
    110128                $results{'observation_time'} = "Last updated at ". $1;
    111129                $results{'updatetime'} = "Last updated at ". $1;
    112130            }
    113             if ($item->{description} =~ /Wind:\<\/b\>(.*)\<br\/\>/s) {
     131            if ($item->{summary}->{content} =~ /Wind:\<\/b\>(.*)\<br\/\>/s) {
    114132                my $wind = $1;
    115133                if ($wind =~ /\s*(\d*)\s*km\/h\s*/i) {
    116134                    $results{'wind_dir'} = 'Calm';
    sub doParse { 
    118136                    $results{'wind_gust'} = 0;
    119137                }
    120138                if ($wind =~ /\s*(\w*)\s*(\d*)\s*km\/h\s*/i) {
    121                     $results{'wind_dir'}   = $directions{$1};
     139                    $results{'wind_dir'}     = $directions{$1};
     140                    $results{'wind_degrees'} = $degrees{$1};
    122141                    $results{'wind_speed'} = $2;
    123142                }
    124143                if ($wind =~ /\s*(\w*)\s*(\d*)\s*km\/h\s*gust\s*(\d*)\s*km\/h/i) {