22 | | my $scratch; |
23 | | my %directions = ( N => "North", S => "South", E => "East", W => "West", |
24 | | NE => "Northeast", NW => "Northwest", SE => "Southeast", SW => "Southwest"); |
| 20 | my $scratch = 0; |
| 21 | my %directions = ( N => "North", S => "South", E => "East", W => "West", |
| 22 | NE => "Northeast", NW => "Northwest", SE => "Southeast", SW => "Southwest"); |
30 | | # Copy attributes |
31 | | $inside{"$tag-id"} = $attr{id} if defined $attr{id}; |
32 | | $inside{"$tag-class"} = $attr{class} if defined $attr{class}; |
33 | | $inside{"$tag-src"} = $attr{src} if defined $attr{src}; |
34 | | $inside{"$tag-longdesc"} = $attr{longdesc} if defined $attr{longdesc}; |
| 28 | # Copy attributes |
| 29 | $inside{"$tag-id"} = $attr{id} if defined $attr{id}; |
| 30 | $inside{"$tag-class"} = $attr{class} if defined $attr{class}; |
| 31 | $inside{"$tag-src"} = $attr{src} if defined $attr{src}; |
| 32 | $inside{"$tag-longdesc"} = $attr{longdesc} if defined $attr{longdesc}; |
49 | | if (defined $inside{title}) { |
50 | | $_[0] =~ s/\n//sg; |
51 | | $_[0] =~ /(.*),\s*(.*)- 5 Day Weather/; |
52 | | $results{'cclocation'} = "$1, $2"; |
53 | | $results{'3dlocation'} = "$1, $2"; |
54 | | $results{'6dlocation'} = "$1, $2"; |
55 | | } |
| 47 | if (defined($inside{'title'}) && $inside{'title'}) { |
| 48 | $_[0] =~ s/\n//sg; |
| 49 | $_[0] =~ /(.*),\s*(.*)- Forecast - Environment Canada/; |
| 50 | $results{'cclocation'} = "$1, $2"; |
| 51 | $results{'3dlocation'} = "$1, $2"; |
| 52 | $results{'6dlocation'} = "$1, $2"; |
| 53 | } |
57 | | if ($inside{h3}) { |
58 | | if ($inside{'div-id'} eq "f1") { $results{'date-0'} = $_[0]; } |
59 | | if ($inside{'div-id'} eq "f2") { $results{'date-1'} = $_[0]; } |
60 | | if ($inside{'div-id'} eq "f3") { $results{'date-2'} = $_[0]; } |
61 | | if ($inside{'div-id'} eq "f4") { $results{'date-3'} = $_[0]; } |
62 | | if ($inside{'div-id'} eq "f5") { $results{'date-4'} = $_[0]; } |
63 | | if ($inside{'div-id'} eq "f6") { $results{'date-5'} = $_[0]; } |
64 | | } |
| 55 | if (defined($inside{'h3'}) && $inside{'h3'}) { |
| 56 | if ($inside{'div-class'} =~ /^period/) { |
| 57 | if ($results{'date-0'} eq "NA") { $results{'date-0'} = "Today"; } |
| 58 | if ($results{'date-1'} eq "NA") { $results{'date-1'} = $_[0]; } |
| 59 | elsif ($results{'date-2'} eq "NA") { $results{'date-2'} = $_[0]; } |
| 60 | elsif ($results{'date-3'} eq "NA") { $results{'date-3'} = $_[0]; } |
| 61 | elsif ($results{'date-4'} eq "NA") { $results{'date-4'} = $_[0]; } |
| 62 | elsif ($results{'date-5'} eq "NA") { $results{'date-5'} = $_[0]; } |
| 63 | } |
| 64 | } |
66 | | if ($inside{li}) { |
67 | | if ($inside{'li-class'} eq "low") { |
68 | | if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'low-0'} = $1; } |
69 | | if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'low-1'} = $1; } |
70 | | if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'low-2'} = $1; } |
71 | | if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'low-3'} = $1; } |
72 | | if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'low-4'} = $1; } |
73 | | if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'low-5'} = $1; } |
74 | | } |
| 66 | if ($inside{li}) { |
| 67 | if (defined($inside{'li-class'})) { |
| 68 | if ($inside{'li-class'} eq "low") { |
| 69 | if ($results{'low-1'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'low-1'} = $1; } |
| 70 | elsif ($results{'low-2'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'low-2'} = $1; } |
| 71 | elsif ($results{'low-3'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'low-3'} = $1; } |
| 72 | elsif ($results{'low-4'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'low-4'} = $1; } |
| 73 | elsif ($results{'low-5'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'low-5'} = $1; } |
| 74 | } |
76 | | if ($inside{'li-class'} eq "high") { |
77 | | if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'high-0'} = $1; } |
78 | | if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'high-1'} = $1; } |
79 | | if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'high-2'} = $1; } |
80 | | if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'high-3'} = $1; } |
81 | | if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'high-4'} = $1; } |
82 | | if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'high-5'} = $1; } |
83 | | } |
84 | | } |
85 | | |
86 | | if ($inside{div}) { |
87 | | if ($inside{'div-class'} eq "citycondition") { $results{'weather'} = $_[0]; } |
| 76 | if ($inside{'li-class'} eq "high") { |
| 77 | if ($results{'high-1'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'high-1'} = $1; } |
| 78 | elsif ($results{'high-2'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'high-2'} = $1; } |
| 79 | elsif ($results{'high-3'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'high-3'} = $1; } |
| 80 | elsif ($results{'high-4'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'high-4'} = $1; } |
| 81 | elsif ($results{'high-5'} eq "NA") { $_[0] =~ /(-?\d*)/; $results{'high-5'} = $1; } |
| 82 | } |
| 83 | } |
| 84 | } |
97 | | if ($inside{dt}) { |
98 | | if ($_[0] =~ /(Temperature)/) { $scratch = 1; return; } |
99 | | if ($_[0] =~ /(Pressure)\/ Tendency/) { $scratch = 2; return; } |
100 | | if ($_[0] =~ /(Visibility)/) { $scratch = 3; return; } |
101 | | if ($_[0] =~ /(Humidity)/) { $scratch = 4; return; } |
102 | | if ($_[0] =~ /(Dewpoint)/) { $scratch = 5; return; } |
103 | | if ($_[0] =~ /(Wind Chill)/ ) { $scratch = 6; return; } |
104 | | if ($_[0] =~ /(Wind)/) { $scratch = 7; return; } |
105 | | } |
| 92 | if (defined($inside{'dt'}) && $inside{'dt'}) { |
| 93 | if ($_[0] =~ /(Temperature)/) { $scratch = 1; return; } |
| 94 | if ($_[0] =~ /(Pressure)/) { $scratch = 2; return; } |
| 95 | if ($_[0] =~ /(Visibility)/) { $scratch = 3; return; } |
| 96 | if ($_[0] =~ /(Humidity)/) { $scratch = 4; return; } |
| 97 | if ($_[0] =~ /(Dewpoint)/) { $scratch = 5; return; } |
| 98 | if ($_[0] =~ /(Wind Chill)/ ) { $scratch = 6; return; } |
| 99 | if ($_[0] =~ /(Wind)/) { $scratch = 7; return; } |
| 100 | if ($_[0] =~ /(Condition)/) { $scratch = 8; return; } |
| 101 | if ($_[0] =~ /(Date)/) { $scratch = 9; return; } |
| 102 | } |
107 | | if ($inside{dd}) { |
108 | | if ($scratch == 1) { $_[0] =~ /(-?\d*).*/; $results{'temp'} = $1; } |
109 | | if ($scratch == 2) { $_[0] =~ /(\d*\.\d+) kPa.*/; $results{'pressure'} = $1 * 10; } |
110 | | if ($scratch == 3) { $_[0] =~ /(\d*) km/; $results{'visibility'} = $1; } |
111 | | if ($scratch == 4) { $_[0] =~ /(\d*) \%/; $results{'relative_humidity'} = $1; } |
112 | | if ($scratch == 5) { $_[0] =~ /(-?\d*).*/; $results{'dewpoint'} = $1; } |
113 | | if ($scratch == 6) { $_[0] =~ /(-?\d*).*/; $results{'appt'} = $1; $results{'windchill'} = $1; } |
114 | | if ($scratch == 7) { |
115 | | $_[0] =~ /.?(\w+) (\d+) km\/h/; |
116 | | $results{'wind_dir'} = $directions{$1}; |
117 | | $results{'wind_speed'} = $2; |
| 104 | if (defined($inside{'dd'}) && $inside{'dd'}) { |
| 105 | if ($scratch == 1) { |
| 106 | $_[0] =~ /(-?\d*\.?\d*).*/; |
| 107 | $results{'temp'} = sprintf("%.0f", $1); |
| 108 | $results{'high-0'} = sprintf("%.0f", $1); |
| 109 | $results{'low-0'} = sprintf("%.0f", $1); |
| 110 | } |
| 111 | if ($scratch == 2) { $_[0] =~ /(\d*\.?\d*).*kPa.*/; $results{'pressure'} = $1 * 10; } |
| 112 | if ($scratch == 3) { $_[0] =~ /(\d*) km/; $results{'visibility'} = $1; } |
| 113 | if ($scratch == 4) { $_[0] =~ /(\d*) \%/; $results{'relative_humidity'} = $1; } |
| 114 | if ($scratch == 5) { $_[0] =~ /(-?\d*\.?\d*).*/; $results{'dewpoint'} = sprintf("%.0f", $1); } |
| 115 | if ($scratch == 6) { $_[0] =~ /(-?\d*).*/; $results{'appt'} = $1; $results{'windchill'} = $1; } |
| 116 | if ($scratch == 7) { |
| 117 | $_[0] =~ /.?(\w+) (\d+) km\/h/; |
| 118 | $results{'wind_dir'} = $directions{$1}; |
| 119 | $results{'wind_speed'} = $2; |
134 | | # Get Icon |
135 | | if ($inside{'img-src'} =~ /\/weathericons\/(\d*\.gif)/) { |
136 | | $icon = $1; |
137 | | open(FH, "ENVCAN_icons") or die "Cannot open icons"; |
138 | | while (my $line = <FH>) { |
139 | | chomp $line; |
140 | | if ($line =~ /$icon\:\:(.*)/) { |
141 | | $icon = $1; |
142 | | last; |
143 | | } |
144 | | } |
145 | | close (FH); |
146 | | } |
147 | | |
148 | | # Current conditions |
149 | | $results{"weather_icon"} = $icon if ($inside{'img-class'} eq "currentimg"); |
150 | | $results{"icon-0"} = $icon if ($inside{'img-longdesc'} eq "#f1"); |
151 | | $results{"icon-1"} = $icon if ($inside{'img-longdesc'} eq "#f2"); |
152 | | $results{"icon-2"} = $icon if ($inside{'img-longdesc'} eq "#f3"); |
153 | | $results{"icon-3"} = $icon if ($inside{'img-longdesc'} eq "#f4"); |
154 | | $results{"icon-4"} = $icon if ($inside{'img-longdesc'} eq "#f5"); |
155 | | $results{"icon-5"} = $icon if ($inside{'img-longdesc'} eq "#f6"); |
| 144 | # Get Icon |
| 145 | if (defined($inside{'img-src'}) && ($inside{'img-src'} =~ /\/weathericons\/(\d*\.gif)/)) { |
| 146 | $icon = $1; |
| 147 | open(FH, "ENVCAN_icons") or die "Cannot open icons"; |
| 148 | while (my $line = <FH>) { |
| 149 | chomp $line; |
| 150 | if ($line =~ /$icon\:\:(.*)/) { |
| 151 | $icon = $1; |
| 152 | last; |
| 153 | } |
| 154 | } |
| 155 | close (FH); |
| 156 | } |
| 157 | |
| 158 | # Current conditions |
| 159 | $results{"weather_icon"} = $icon if (defined($inside{'img-id'}) && ($inside{'img-id'} eq "currentimg")); |
| 160 | $results{"icon-0"} = $icon if (defined($inside{'img-id'}) && ($inside{'img-id'} eq "currentimg")); |
157 | | undef ($inside{'img-class'}); |
158 | | undef ($inside{'img-src'}); |
159 | | undef ($inside{'img-longdesc'}); |
160 | | undef ($inside{'img'}); |
| 162 | $results{"icon-1"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f1")); |
| 163 | $results{"icon-2"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f2")); |
| 164 | $results{"icon-3"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f4")); |
| 165 | $results{"icon-4"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f5")); |
| 166 | $results{"icon-5"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f6")); |
| 167 | |
| 168 | undef ($inside{'img-class'}); |
| 169 | undef ($inside{'img-id'}); |
| 170 | undef ($inside{'img-src'}); |
| 171 | undef ($inside{'img-longdesc'}); |
| 172 | undef ($inside{'img'}); |
175 | | if ($ampm eq "PM") { if (int($hour) < 11) { $hour += 12; } } |
176 | | $month = 0 if $month eq "January"; |
177 | | $month = 1 if $month eq "February"; |
178 | | $month = 2 if $month eq "March"; |
179 | | $month = 3 if $month eq "April"; |
180 | | $month = 4 if $month eq "May"; |
181 | | $month = 5 if $month eq "June"; |
182 | | $month = 6 if $month eq "July"; |
183 | | $month = 7 if $month eq "August"; |
184 | | $month = 8 if $month eq "September"; |
185 | | $month = 9 if $month eq "October"; |
186 | | $month = 10 if $month eq "November"; |
187 | | $month = 11 if $month eq "December"; |
188 | | $year = int($year) - 1900; |
| 187 | if ($ampm eq "PM") { if (int($hour) < 11) { $hour += 12; } } |
| 188 | $month = 0 if $month eq "January"; |
| 189 | $month = 1 if $month eq "February"; |
| 190 | $month = 2 if $month eq "March"; |
| 191 | $month = 3 if $month eq "April"; |
| 192 | $month = 4 if $month eq "May"; |
| 193 | $month = 5 if $month eq "June"; |
| 194 | $month = 6 if $month eq "July"; |
| 195 | $month = 7 if $month eq "August"; |
| 196 | $month = 8 if $month eq "September"; |
| 197 | $month = 9 if $month eq "October"; |
| 198 | $month = 10 if $month eq "November"; |
| 199 | $month = 11 if $month eq "December"; |
| 200 | $year = int($year) - 1900; |
190 | | my $time_t = POSIX::mktime(0, $min, $hour, $day, $month, $year); |
191 | | my $now_string = localtime($time_t); |
| 202 | my $time_t = POSIX::mktime(0, $min, $hour, $day, $month, $year); |
| 203 | my $now_string = localtime($time_t); |
240 | | my $p = HTML::Parser->new(api_version => 3); |
241 | | $p->unbroken_text(1); |
242 | | $p->report_tags(qw(div dd dt h2 h3 img li p title)); |
243 | | $p->ignore_elements(qw(style script)); |
244 | | $p->handler( start => \&start_h, 'tagname, @attr'); |
245 | | $p->handler( end => \&end_h, "tagname"); |
246 | | $p->handler( text => \&text, "dtext"); |
247 | | $p->parse($data) || die $!; |
| 237 | my $p = HTML::Parser->new(api_version => 3); |
| 238 | $p->unbroken_text(1); |
| 239 | $p->report_tags(qw(div dd dt h2 h3 img li p title)); |
| 240 | $p->ignore_elements(qw(style script)); |
| 241 | $p->handler( start => \&start_h, 'tagname, @attr'); |
| 242 | $p->handler( end => \&end_h, "tagname"); |
| 243 | $p->handler( text => \&text, "dtext"); |
| 244 | $p->parse($data) || die $!; |