2 # MythWeather-revamp script to retreive weather information from Environment
5 # Most of this code was taken directly from Lucien Dunning's
6 # (ldunning@gmail.com) PERL scripts. Kudos to Lucien for doing all of the
7 # hard work that I shamelessly stole.
17 '/usr/share/mythtv/mythweather/scripts/ca_envcan',
18 '/usr/local/share/mythtv/mythweather/scripts/ca_envcan';
27 our ($opt_v, $opt_t, $opt_T, $opt_l, $opt_u, $opt_d);
32 my $email =
'vitaminjoe@gmail.com / gjhurlbu@gmail.com';
35 my @
types = (
'cclocation',
'station_id',
'copyright',
36 'observation_time',
'observation_time_rfc822',
'weather',
37 'temp',
'relative_humidity',
38 'wind_dir',
'wind_degrees',
'wind_speed',
'wind_gust',
39 'pressure',
'dewpoint',
'heat_index',
'windchill',
40 'visibility',
'weather_icon',
'appt',
'wind_spdgst',
41 '3dlocation',
'6dlocation',
'date-0',
'icon-0',
'low-0',
'high-0',
42 'date-1',
'icon-1',
'low-1',
'high-1',
43 'date-2',
'icon-2',
'low-2',
'high-2',
'updatetime',
44 'date-3',
'icon-3',
'low-3',
'high-3',
45 'date-4',
'icon-4',
'low-4',
'high-4',
46 'date-5',
'icon-5',
'low-5',
'high-5',
'copyrightlogo' );
53 print "$name,$version,$author,$email\n";
58 print "$updateTimeout,$retrieveTimeout\n";
63 ENVCANLocation::AddStationIdSearch($search);
64 ENVCANLocation::AddRegionIdSearch($search);
65 ENVCANLocation::AddCitySearch($search);
66 ENVCANLocation::AddProvinceSearch($search);
69 while($result = shift @$results) {
70 if ($result->{station_id} ne
"NA" ) {
71 print "$result->{station_id}::";
72 print "$result->{city}, $result->{region_id}\n";
88 # check variables for defined status
90 if (!(defined $opt_u && defined $loc && !$loc eq
"")) {
96 # check for cached data
101 if (
open(CACHE,
"$dir/envcan_$loc")) {
103 if (Date_Cmp($nextupdate,
"now") > 0) { # use cache
107 if (%
results) { $getData = 0; }
108 else {
print STDERR
"Error parsing cache $@\n"; }
113 # no cache, grab from the web
115 my $base_url =
'http://www.weatheroffice.gc.ca/rss/city/';
119 %
results = ENVCANParser::doParse($response, @types);
120 $results{
'station_id'} =
$loc;
123 open (CACHE,
">$dir/envcan_$loc") or
124 die (
"Cannot open cache ($dir/envcan_$loc) for writing.");
125 $Data::Dumper::Purity = 1;
126 $Data::Dumper::Indent = 0;
128 # cache is good for 15 minutes
131 $nextupdate = DateCalc(
"now",
"+ $newmin minutes");
133 print CACHE Data::Dumper->Dump([\%
results], [
'*results']);
136 $results{
'copyrightlogo'} =
"none";
138 # do some quick conversions
139 if ($units eq
"ENG") {
140 $results{
'temp'} =
int(((9/5) * $results{
'temp'}) + 32);
141 $results{
'dewpoint'} =
int(((9/5) * $results{
'dewpoint'}) + 32);
142 $results{
'windchill'} =
int(((9/5) * $results{
'windchill'}) + 32);
143 $results{
'appt'} =
int(((9/5) * $results{
'appt'}) + 32);
144 $results{
'visibility'} = sprintf(
"%.1f", ($results{
'visibility'} * 0.621371192));
145 $results{
'pressure'} = sprintf(
"%.2f", $results{
'pressure'} * 0.0295301);
146 $results{
'wind_gust'} = sprintf(
"%.2f", $results{
'wind_gust'} * 0.621371192);
147 $results{
'wind_speed'} = sprintf(
"%.2f", $results{
'wind_speed'} * 0.621371192);
148 $results{
'wind_spdgst'} = sprintf(
"%.2f (%.2f)", $results{
'wind_speed'}, $results{
'wind_gust'});
151 if ($results{
"high-$i"} =~ /\d*/) {
152 $results{
"high-$i"} =
int(((9/5) * $results{
"high-$i"}) + 32);
154 if ($results{
"low-$i"} =~ /\d*/) {
155 $results{
"low-$i"} =
int(((9/5) * $results{
"low-$i"}) + 32);
159 $results{
'wind_spdgst'} = sprintf(
"%.2f (%.2f)", $results{
'wind_speed'}, $results{
'wind_gust'});
164 if (length($results{$key}) == 0) {
167 print $results{$key} .
"\n";