Mercurial > hg > egg-tcls
annotate fetch_weather.pl @ 442:19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 26 Sep 2017 12:33:38 +0300 |
parents | a17b7fdd03c0 |
children | 1f16d27c571d |
rev | line source |
---|---|
381 | 1 #!/usr/bin/perl -w |
182 | 2 ########################################################################## |
3 # | |
407
b05da2077e44
fetch_weather: Bump copyright and version.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
4 # Fetch Weather v0.8 by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
b05da2077e44
fetch_weather: Bump copyright and version.
Matti Hamalainen <ccr@tnsp.org>
parents:
406
diff
changeset
|
5 # (C) Copyright 2014-2017 Tecnic Software productions (TNSP) |
206 | 6 # This script is freely distributable under GNU GPL (version 2) license. |
182 | 7 # |
8 # Should be ran as a cronjob, and configured properly. | |
206 | 9 # */10 * * * * perl -w /absolute/path/to/fetch_weather.pl /path/to/configfile |
182 | 10 # |
186
b41c525bdaf7
Add example configuration file.
Matti Hamalainen <ccr@tnsp.org>
parents:
185
diff
changeset
|
11 # Configuration file example is in fetch_weather.config |
182 | 12 # |
206 | 13 # Requires various Perl modules, in Debian the packages should be: |
14 # libwww-perl libxml-simple-perl libtimedate-perl | |
15 # | |
182 | 16 # |
17 ########################################################################## | |
417 | 18 use 5.18.0; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 use strict; |
380
729eb88d1c63
fetch_weather: "use warnings".
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
20 use warnings; |
243 | 21 use utf8; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 use LWP::UserAgent; |
399
a80de0130e45
fetch_weather: Support gzip encoded content in HTTP requests.
Matti Hamalainen <ccr@tnsp.org>
parents:
398
diff
changeset
|
23 use HTTP::Message; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 use HTML::Entities; |
399
a80de0130e45
fetch_weather: Support gzip encoded content in HTTP requests.
Matti Hamalainen <ccr@tnsp.org>
parents:
398
diff
changeset
|
25 use Compress::Zlib; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 use XML::Simple; |
189 | 27 use Date::Format; |
199
95012f8f687a
Another round of timestamp parsing changes, urgh.
Matti Hamalainen <ccr@tnsp.org>
parents:
198
diff
changeset
|
28 use Date::Parse; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 use Data::Dumper; |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
30 use File::Slurper qw(read_text write_binary); |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
31 use JSON; |
166 | 32 |
400 | 33 |
162
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
34 ### |
182 | 35 ### Configuration settings |
162
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
36 ### |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
37 my %settings = ( |
442
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
38 "force_update" => 0, |
166 | 39 "debug" => 0, |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
40 "opt_fmi" => 0, |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
41 "opt_tiehallinto" => 0, |
374
26b7963e7fbd
fetch_weather: Oops, forgot to define the configuration setting
Matti Hamalainen <ccr@tnsp.org>
parents:
373
diff
changeset
|
42 "purge_threshold" => 60, |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
43 "fmi_api_key" => "", |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
44 "outfile" => "", |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
45 "tiehallinto_static_meta" => "tiehallinto.meta", |
185
64e06ee172c7
Add HTTP user agent setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
182
diff
changeset
|
46 "http_user_agent" => "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 6.0) Opera 10.63 [en]", |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
47 ); |
162
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
48 |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
49 |
162
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
50 ### |
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
51 ### Helper functions |
4f8a163b2bc1
Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents:
158
diff
changeset
|
52 ### |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
53 sub mlog($) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
54 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
55 print STDERR $_[0]; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
56 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
57 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
58 |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
59 sub fetch_http($) |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
60 { |
198 | 61 my $agent = LWP::UserAgent->new; |
62 $agent->agent($settings{"http_user_agent"}); | |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
63 $agent->timeout(20); |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
64 |
198 | 65 my $req = HTTP::Request->new(GET => $_[0]); |
399
a80de0130e45
fetch_weather: Support gzip encoded content in HTTP requests.
Matti Hamalainen <ccr@tnsp.org>
parents:
398
diff
changeset
|
66 $req->header('Accept-Encoding' => scalar HTTP::Message::decodable()); |
339 | 67 |
411
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
68 print STDERR "# FETCHING URL: ".$_[0]."\n" if (opt_get_int("debug") > 0); |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
69 |
406
4c3abf052d01
fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
405
diff
changeset
|
70 my $res = $agent->request($req); |
411
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
71 |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
72 if (opt_get_int("debug") > 0) |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
73 { |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
74 print STDERR "# Response: ".$res->code.": ".$res->message."\n"; |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
75 if ($res->code >= 200 && $res->code <= 201) |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
76 { |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
77 print STDERR |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
78 "# Content-charset: ".$res->content_charset."\n". |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
79 "# Content-encoding: ".$res->content_encoding."\n". |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
80 "# Is decoded_content UTF8? ".(utf8::is_utf8($res->decoded_content) ? "yes" : "NO!")."\n"; |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
81 } |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
82 } |
fe47617e7251
fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents:
410
diff
changeset
|
83 |
406
4c3abf052d01
fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
405
diff
changeset
|
84 return $res; |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
85 } |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
86 |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
87 |
382
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
88 sub str_trim($) |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
89 { |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
90 my $tmp = $_[0]; |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
91 $tmp =~ s/^\s*//; |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
92 $tmp =~ s/\s*$//; |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
93 return $tmp; |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
94 } |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
95 |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
96 |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
97 sub parse_timestamp($$) |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
98 { |
196
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
99 my ($str, $offs) = @_; |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
100 if ($str =~ /^(\d+):(\d+)$/) |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
101 { |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
102 return $offs + (60 * 60 * $1) + ($2 * 60); |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
103 } |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
104 else |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
105 { |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
106 return $offs; |
a38c123921a9
Change the timestamp parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
195
diff
changeset
|
107 } |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
108 } |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
109 |
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
110 |
194 | 111 sub format_time_gmt($) |
189 | 112 { |
113 # 2012-02-27T00:00:00Z | |
199
95012f8f687a
Another round of timestamp parsing changes, urgh.
Matti Hamalainen <ccr@tnsp.org>
parents:
198
diff
changeset
|
114 return time2str("%Y-%m-%dT%TZ", $_[0], "UTC"); |
189 | 115 } |
116 | |
193 | 117 |
212
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
118 my %th_rain_states = |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
119 ( |
237 | 120 "Pouta" => "poutaa", |
121 "Heikko" => "heikkoa sadetta", | |
122 "Kohtalainen" => "kohtalaista sadetta", | |
123 "Voimakas" => "voimakasta sadetta", | |
212
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
124 ); |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
125 |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
126 my $th_rain_states_k = join("|", map {quotemeta} sort { length($b)<=>length($a) } keys %th_rain_states); |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
127 |
242 | 128 sub translate_rain($) |
212
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
129 { |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
130 my $tmp = $_[0]; |
237 | 131 $tmp =~ s/($th_rain_states_k)/$th_rain_states{$1}/igo; |
212
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
132 return $tmp; |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
133 } |
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
134 |
331
b8ae5e99341b
fetc_weather: Remove support for old HTML format parsing of Tiehallinto data, it's not provided in this format anymore.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
135 |
244 | 136 my %th_cloud_states = |
137 ( | |
138 0 => "selkeää", | |
247
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
139 1 => "melkein selkeää", |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
140 2 => "verrattain selkeää", |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
141 3 => "verrattain selkeää", |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
142 4 => "puolipilvistä", |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
143 5 => "verrattain pilvistä", |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
144 6 => "verrattain pilvistä", |
244 | 145 7 => "melkein pilvistä", |
146 8 => "pilvistä", | |
147 ); | |
148 | |
149 sub translate_clouds($) | |
150 { | |
247
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
151 return "" if ($_[0] eq "NaN" || $_[0] eq ""); |
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
152 my $tmp = int($_[0]); |
244 | 153 foreach my $n (sort { $a <=> $b } keys %th_cloud_states) |
154 { | |
247
85ad2912e720
Possibly handle cloudyness translation better.
Matti Hamalainen <ccr@tnsp.org>
parents:
245
diff
changeset
|
155 return $th_cloud_states{$n}." (".$n."/8)" if ($tmp == $n); |
244 | 156 } |
157 return $tmp; | |
158 } | |
159 | |
212
5f04ec60e8d9
weather: "Translate" rain state from Tiehallitus data.
Matti Hamalainen <ccr@tnsp.org>
parents:
208
diff
changeset
|
160 |
410 | 161 ### Return either data or if not defined, empty string |
228 | 162 sub plonk_data($) |
163 { | |
164 return defined($_[0]) ? $_[0] : ""; | |
165 } | |
166 | |
167 | |
410 | 168 ### Same as plonk_data() but also lowercase the data string |
237 | 169 sub plonk_data_lc($) |
170 { | |
171 return defined($_[0]) ? lc($_[0]) : ""; | |
172 } | |
173 | |
174 | |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 ### |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
176 ### Configuration handling |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
177 ### |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
178 sub opt_chk_bool($) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
179 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
180 if (defined($settings{$_[0]})) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
181 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
182 my $val = $settings{$_[0]}; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
183 return ($val == 1 || $val eq "true" || $val eq "on" || $val eq "1"); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
184 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
185 else |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
186 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
187 return 0; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
188 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
189 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
190 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
191 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
192 sub opt_chk_valid($$) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
193 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
194 if (defined($settings{$_[0]})) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
195 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
196 my $val = $settings{$_[0]}; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
197 return length($val) >= $_[1]; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
198 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
199 else |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
200 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
201 return 0; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
202 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
203 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
204 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
205 |
324
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
206 sub opt_get_int($) |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
207 { |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
208 if (defined($settings{$_[0]})) |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
209 { |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
210 return int($settings{$_[0]}); |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
211 } |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
212 else |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
213 { |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
214 return -1; |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
215 } |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
216 } |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
217 |
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
218 |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
219 sub opt_get($) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
220 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
221 if (defined($settings{$_[0]})) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
222 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
223 return $settings{$_[0]}; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
224 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
225 else |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
226 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
227 return undef; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
228 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
229 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
230 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
231 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
232 sub opt_read_config($) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
233 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
234 my $filename = $_[0]; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
235 my $errors = 0; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
236 my $line = 0; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
237 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
238 open(CONFFILE, "<", $filename) or die("Could not open configuration '".$filename."'!\n"); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
239 while (<CONFFILE>) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
240 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
241 $line++; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
242 chomp; |
353 | 243 if (/(^\s*#|^\s*$)/) |
244 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
245 # Ignore comments and empty lines |
353 | 246 } |
386 | 247 elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*(\d+),?\s*$/) |
248 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
249 my $key = lc($1); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
250 my $value = $2; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
251 if (defined($settings{$key})) { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
252 $settings{$key} = $value; |
353 | 253 } |
254 else | |
255 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
256 mlog("[$filename:$line] Unknown setting '$key' = $value\n"); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
257 $errors = 1; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
258 } |
353 | 259 } |
386 | 260 elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*\"(.*?)\",?\s*$/) |
261 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
262 my $key = lc($1); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
263 my $value = $2; |
353 | 264 if (defined($settings{$key})) |
265 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
266 $settings{$key} = $value; |
353 | 267 } |
268 else | |
269 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
270 mlog("[$filename:$line] Unknown setting '$key' = '$value'\n"); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
271 $errors = 1; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
272 } |
353 | 273 } |
274 else | |
275 { | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
276 mlog("[$filename:$line] Syntax error: $_\n"); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
277 $errors = 1; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
278 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
279 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
280 close(CONFFILE); |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
281 return $errors; |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
282 } |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
283 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
284 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
285 ### |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
286 ### Main program begins |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
287 ### |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
288 my $weatherdata = {}; |
442
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
289 my $opt_cfgfile; |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
290 |
442
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
291 while (scalar(@ARGV) > 0) |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
292 { |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
293 my $arg = shift; |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
294 if ($arg eq "-force") |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
295 { |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
296 $settings{"force_update"} = 1; |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
297 } |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
298 elsif ($arg eq "-debug") |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
299 { |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
300 $settings{"debug"} = 1; |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
301 } |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
302 else |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
303 { |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
304 die("Configuration file already specified!\n") if defined($opt_cfgfile); |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
305 $opt_cfgfile = $arg; |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
306 } |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
307 } |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
308 |
442
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
309 if (!defined($opt_cfgfile)) |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
310 { |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
311 die( |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
312 "Weather Fetch v0.8 by ccr/TNSP <ccr\@tnsp.org>\n". |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
313 "Usage: $0 <config file> [options]\n". |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
314 "\n". |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
315 " -force : Force updating of all data\n". |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
316 " -debug : Enable debug\n". |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
317 "\n" |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
318 ); |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
319 } |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
320 |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
321 opt_read_config($opt_cfgfile) == 0 or die("Errors while parsing configuration file '".$opt_cfgfile."'.\n"); |
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
322 print STDERR "Forcing update of all data.\n" if $settings{"force_update"}; |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
323 |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
324 |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
325 ### |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
326 ### Load already cached data |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
327 ### |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
328 if (opt_chk_valid("outfile", 1)) |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
329 { |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
330 my $filename = opt_get("outfile"); |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
331 if (-e "$filename") |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
332 { |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
333 my $str = read_text($filename); |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
334 if (defined($str)) |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
335 { |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
336 foreach my $line (split(/\s*\n\s*/, $str)) |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
337 { |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
338 my @mtmp = split(/\|/, $line, -1); |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
339 if (scalar(\@mtmp) >= 3) |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
340 { |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
341 $weatherdata->{shift @mtmp} = \@mtmp; |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
342 } |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
343 } |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
344 print STDERR scalar(keys %$weatherdata)." old records reloaded.\n" if (opt_get_int("debug") > 0); |
354
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
345 } |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
346 } |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
347 } |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
348 |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
349 |
de411e7834aa
fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents:
353
diff
changeset
|
350 ### |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
351 ### Fetch Tiehallinto data |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
352 ### |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
353 if (opt_chk_bool("opt_tiehallinto")) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
354 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
355 my $uri = "http://tie.digitraffic.fi/sujuvuus/ws/roadWeather"; |
406
4c3abf052d01
fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
405
diff
changeset
|
356 print STDERR "Fetching Tiehallinto road weather data from ".$uri."\n" if (opt_get_int("debug") > 0); |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
357 my $res = fetch_http($uri); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
358 if ($res->code >= 200 && $res->code <= 201) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
359 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
360 my $xml = XMLin($res->decoded_content); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
361 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
362 if (!defined($xml->{"soap:Body"}) || !defined($xml->{"soap:Body"}{"RoadWeatherResponse"})) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
363 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
364 print STDERR "ERROR: SOAP call result did not contain required data.\n"; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
365 print STDERR $res->decoded_content."\n\n"; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
366 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
367 else |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
368 { |
406
4c3abf052d01
fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
405
diff
changeset
|
369 # Parse the XML |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
370 my $data = $xml->{"soap:Body"}{"RoadWeatherResponse"}; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
371 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
372 # Check if we need to update the static meta data |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
373 my $meta_file = opt_get("tiehallinto_static_meta"); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
374 my $fetch_meta = (-e $meta_file) ? 0 : 1; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
375 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
376 if (defined($data->{"laststaticdataupdate"})) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
377 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
378 # Compare metadata cache file modification timestamp to info in XML |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
379 my $tmp1 = str2time($data->{"laststaticdataupdate"}); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
380 my $tmp2 = (-e $meta_file) ? (stat($meta_file))[9] : -1; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
381 $fetch_meta = 1 unless ($tmp1 < $tmp2); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
382 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
383 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
384 # Fetch or read the cache |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
385 my $meta_str; |
442
19fe8ef0a902
fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents:
438
diff
changeset
|
386 if ($fetch_meta || opt_chk_book("force_update")) |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
387 { |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
388 my $uri = "http://tie.digitraffic.fi/api/v1/metadata/weather-stations"; |
406
4c3abf052d01
fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
405
diff
changeset
|
389 print STDERR "Fetching Tiehallinto static meta data from $uri\n" if (opt_get_int("debug") > 1); |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
390 my $res = fetch_http($uri); |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
391 die("Failed to fetch $uri data.\n") unless ($res->code <= 200 && $res->code <= 201); |
339 | 392 |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
393 $meta_str = $res->decoded_content; |
413
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
394 |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
395 # XXX: This is a hack. For some reason the data does not get utf8 flagged internally. |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
396 if (!utf8::is_utf8($meta_str)) |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
397 { |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
398 printf STDERR "Upgrading meta_str to UTF-8.\n" if (opt_get_int("debug") > 0); |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
399 utf8::upgrade($meta_str); |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
400 } |
b63525aed190
fetch_weather: Unfortunately we have to deal with some UTF-8 brokenness here.
Matti Hamalainen <ccr@tnsp.org>
parents:
412
diff
changeset
|
401 |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
402 $fetch_meta = 1; |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
403 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
404 else |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
405 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
406 print STDERR "Using CACHED Tiehallinto static meta data from '$meta_file'.\n" if (opt_get_int("debug") > 0); |
416
c88f7449b526
fetch_weather: Possibly another UTF8 fix :S
Matti Hamalainen <ccr@tnsp.org>
parents:
414
diff
changeset
|
407 $meta_str = read_text($meta_file); |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
408 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
409 |
416
c88f7449b526
fetch_weather: Possibly another UTF8 fix :S
Matti Hamalainen <ccr@tnsp.org>
parents:
414
diff
changeset
|
410 print STDERR "Is meta_str UTF8? ".(utf8::is_utf8($meta_str) ? "yes" : "NO!")."\n" if (opt_get_int("debug") > 0); |
c88f7449b526
fetch_weather: Possibly another UTF8 fix :S
Matti Hamalainen <ccr@tnsp.org>
parents:
414
diff
changeset
|
411 |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
412 # Parse the data .. |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
413 my $meta_data = {}; |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
414 my $json = JSON->new->decode($meta_str); |
398 | 415 |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
416 if ($fetch_meta) |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
417 { |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
418 # Save new cache, in more optimal form, if needed. |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
419 print STDERR "Storing to cache '$meta_file'.\n" if (opt_get_int("debug") > 0); |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
420 write_binary($meta_file, JSON->new->encode($json)); |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
421 } |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
422 |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
423 foreach my $ms (@{$json->{"features"}}) |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
424 { |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
425 if (defined($ms->{"properties"}) && |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
426 defined($ms->{"geometry"}{"coordinates"}) && |
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
427 defined($ms->{"properties"}{"names"}{"fi"})) |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
428 { |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
429 $meta_data->{$ms->{"id"}} = $ms; |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
430 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
431 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
432 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
433 # Parse XML and combine with the station meta data |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
434 if (defined($data->{"roadweatherdata"})) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
435 { |
349
f7793ef7c925
fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
343
diff
changeset
|
436 my $nrecords = 0; |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
437 foreach my $wdata (@{$data->{"roadweatherdata"}{"roadweather"}}) |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
438 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
439 my $wid = $wdata->{"stationid"}; |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
440 if (defined($meta_data->{$wid})) |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
441 { |
349
f7793ef7c925
fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
343
diff
changeset
|
442 $nrecords++; |
412
e0d86ccef873
fetch_weather: Change to using JSON static weather station metadata from digitraffic instead of the "old" API's CSV format data.
Matti Hamalainen <ccr@tnsp.org>
parents:
411
diff
changeset
|
443 $weatherdata->{$meta_data->{$wid}{"properties"}{"names"}{"fi"}} = |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
444 [ |
343
3fff571a904b
fetch_weather: Some fixes to make this actually work.
Matti Hamalainen <ccr@tnsp.org>
parents:
340
diff
changeset
|
445 1, |
414
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
446 |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
447 $meta_data->{$wid}{"geometry"}{"coordinates"}[1], |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
448 $meta_data->{$wid}{"geometry"}{"coordinates"}[0], |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
449 $meta_data->{$wid}{"geometry"}{"coordinates"}[2], |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
450 |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
451 str2time(plonk_data($wdata->{"measurementtime"}{"utc"})), |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
452 plonk_data($wdata->{"airtemperature1"}), |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
453 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
454 plonk_data($wdata->{"humidity"}), |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
455 plonk_data($wdata->{"averagewindspeed"}), |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
456 ]; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
457 } |
377
0a6d9f0f0a57
fetch_weather: Fix Tiehallinto metadata handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
375
diff
changeset
|
458 else |
0a6d9f0f0a57
fetch_weather: Fix Tiehallinto metadata handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
375
diff
changeset
|
459 { |
397
799320c3386e
Change "Station ID #" not defined?" to a debug message.
Matti Hamalainen <ccr@tnsp.org>
parents:
396
diff
changeset
|
460 print STDERR "Station ID #".$wid." not defined?\n" if (opt_get_int("debug") > 0); |
400 | 461 #.Dumper($meta_data->{$wid}); |
377
0a6d9f0f0a57
fetch_weather: Fix Tiehallinto metadata handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
375
diff
changeset
|
462 } |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
463 } |
349
f7793ef7c925
fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
343
diff
changeset
|
464 print STDERR $nrecords." records from Tiehallinto.\n" if (opt_get_int("debug") > 0); |
338
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
465 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
466 else |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
467 { |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
468 print STDERR "ERROR: Invalid (or unsupported) road weather data blob.\n"; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
469 print STDERR $res->decoded_content."\n\n"; |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
470 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
471 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
472 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
473 } |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
474 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
475 |
afb8c0e2d513
fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
476 ### |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
477 ### Fetch FMI data |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
478 ### |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
479 if (opt_chk_bool("opt_fmi")) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
480 { |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
481 die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10); |
238
a1d6e2d8789e
Add new data from FMI service and change datafile format.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
482 my @fmitems = ("temperature", "humidity", "windspeedms", "totalcloudcover"); |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
483 |
189 | 484 my $uri = "http://data.fmi.fi/fmi-apikey/".opt_get("fmi_api_key"). |
485 "/wfs?request=getFeature&storedquery_id=fmi::observations::weather::". | |
486 "multipointcoverage". | |
487 # "timevaluepair". | |
194 | 488 "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()). |
238
a1d6e2d8789e
Add new data from FMI service and change datafile format.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
489 "¶meters=".join(",", @fmitems)."&maxlocations=100&bbox=19,59,32,75"; |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
490 |
324
19c64798de91
weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
247
diff
changeset
|
491 print STDERR "FMI URI: ".$uri."\n" if (opt_get_int("debug") > 0); |
189 | 492 |
493 my $res = fetch_http($uri); | |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
494 if ($res->code >= 200 && $res->code <= 201) |
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
495 { |
170
e4faa3746869
Remove useless parameters from XMLin().
Matti Hamalainen <ccr@tnsp.org>
parents:
169
diff
changeset
|
496 my $xml = XMLin($res->decoded_content); |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
497 my $time_base = time(); |
339 | 498 |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
499 if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"})) |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
500 { |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
501 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"}; |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
502 my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"}; |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
503 |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
504 my @position_lines = split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}); |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
505 my @data_lines = split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}); |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
506 my @farray = (); |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
507 |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
508 if (scalar(@position_lines) == scalar(@data_lines)) |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
509 { |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
510 for (my $nline = 0; $nline < scalar(@position_lines); $nline++) |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
511 { |
382
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
512 my $dline = str_trim($data_lines[$nline]); |
5573c1681da9
fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents:
381
diff
changeset
|
513 my $pline = str_trim($position_lines[$nline]); |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
514 |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
515 my @fmatches = ($dline =~ /\s*([\+\-]?\d+\.\d*|NaN)\s*/ig); |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
516 if (scalar(@fmatches) != scalar(@fmitems)) |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
517 { |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
518 print STDERR "Not enough items in scalar line #".$nline." (". |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
519 scalar(@fmatches). " vs ".scalar(@fmitems)."): ".$dline."\n"; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
520 } |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
521 else |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
522 { |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
523 my $vtmp = {}; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
524 for (my $fni = 0; $fni < scalar(@fmitems); $fni++) |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
525 { |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
526 $$vtmp{$fmitems[$fni]} = $fmatches[$fni] if (lc($fmatches[$fni]) ne "nan"); |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
527 } |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
528 if ($pline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/) |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
529 { |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
530 $$vtmp{"lat"} = $1; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
531 $$vtmp{"long"} = $2; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
532 $$vtmp{"time"} = $3; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
533 push(@farray, $vtmp); |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
534 } |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
535 else |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
536 { |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
537 print STDERR "Data mismatch #".$nline.": ".$pline."\n"; |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
538 } |
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
539 } |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
540 } |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
541 } |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
542 else |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
543 { |
384 | 544 print STDERR "Position and data line counts do not match.\n"; |
378
b706e8ebecf8
fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
377
diff
changeset
|
545 goto skip_it; |
189 | 546 } |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
547 # XXX Hashify the array into lat/long keys |
339 | 548 |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
549 # This is horrible :S |
352
19b363586b40
fetch_weather: Fix debug prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
550 my $nrecords = 0; |
410 | 551 foreach my $xnode (@{$fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"}}) |
189 | 552 { |
228 | 553 my $floc = $xnode->{"gml:Point"}; |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
554 if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/) |
189 | 555 { |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
556 my ($flat, $flong) = ($1, $2); |
349
f7793ef7c925
fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
343
diff
changeset
|
557 |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
558 # Should use a hash - |
410 | 559 foreach my $frec (@farray) |
189 | 560 { |
410 | 561 if ($frec->{"lat"} == $flat && $frec->{"long"} == $flong && |
383
1ba1191da15d
fetch_weather: Check for empty station name.
Matti Hamalainen <ccr@tnsp.org>
parents:
382
diff
changeset
|
562 $floc->{"gml:name"} ne "") |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
563 { |
349
f7793ef7c925
fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
343
diff
changeset
|
564 $nrecords++; |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
565 $weatherdata->{$floc->{"gml:name"}} = |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
566 [ |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
567 1, |
410 | 568 |
414
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
569 $frec->{"lat"}, |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
570 $frec->{"long"}, |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
571 0, |
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
572 |
410 | 573 plonk_data($frec->{"time"}), |
574 plonk_data($frec->{"temperature"}), | |
238
a1d6e2d8789e
Add new data from FMI service and change datafile format.
Matti Hamalainen <ccr@tnsp.org>
parents:
237
diff
changeset
|
575 |
410 | 576 plonk_data($frec->{"humidity"}), |
577 plonk_data($frec->{"windspeedms"}), | |
578 translate_clouds(plonk_data($frec->{"totalcloudcover"})), | |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
579 ]; |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
580 } |
189 | 581 } |
582 } | |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
583 } |
352
19b363586b40
fetch_weather: Fix debug prints.
Matti Hamalainen <ccr@tnsp.org>
parents:
349
diff
changeset
|
584 print STDERR $nrecords." records from FMI.\n" if (opt_get_int("debug") > 0); |
353 | 585 } |
586 else | |
587 { | |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
588 # defined |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
589 print STDERR "Invalid XML received:\n"; |
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
590 print STDERR $res->decoded_content."\n\n"; |
169
c12e422a543a
FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents:
168
diff
changeset
|
591 } |
353 | 592 } |
593 else | |
594 { | |
208
c6bbf69afc57
Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
206
diff
changeset
|
595 print STDERR "Error fetching FMI XML: ".$res->status_line."\n"; |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
596 } |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
597 } |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
598 |
164
d5a0e4248f3e
weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
599 |
375
c303ddc0a683
fetch_weather: Don't die() in case of bad XML from FMI, just skip parsing
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
600 ### Skip here if the FMI shit fails due to broken data |
c303ddc0a683
fetch_weather: Don't die() in case of bad XML from FMI, just skip parsing
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
601 skip_it: |
c303ddc0a683
fetch_weather: Don't die() in case of bad XML from FMI, just skip parsing
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
602 |
c303ddc0a683
fetch_weather: Don't die() in case of bad XML from FMI, just skip parsing
Matti Hamalainen <ccr@tnsp.org>
parents:
374
diff
changeset
|
603 |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
604 ### |
394
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
605 ### Purge too old entries |
371
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
606 ### |
394
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
607 if (opt_chk_valid("purge_threshold", 1)) |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
608 { |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
609 my $purge = opt_get_int("purge_threshold"); |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
610 if ($purge > 0) |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
611 { |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
612 my $wqtime = time(); |
395
33b5e7adc30c
fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
394
diff
changeset
|
613 my $nold = scalar(keys %$weatherdata); |
373
bc7746ca622e
fetch_weather: Implement purge_threshold setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
371
diff
changeset
|
614 |
394
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
615 foreach my $key (keys %$weatherdata) |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
616 { |
414
d623652df6b5
fetch_weather / weather: Add weather measurement station location data (GPS WGS-84 lat/long/height) to weather cache. Not used in weather.tcl yet, tho.
Matti Hamalainen <ccr@tnsp.org>
parents:
413
diff
changeset
|
617 if ($wqtime - $weatherdata->{$key}[4] > (60 * $purge)) |
394
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
618 { |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
619 delete $$weatherdata{$key}; |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
620 } |
315f31e4f964
fetch_weather: Make purge_threshold option entirely optional, e.g. not set or 0 will disable purging.
Matti Hamalainen <ccr@tnsp.org>
parents:
392
diff
changeset
|
621 } |
395
33b5e7adc30c
fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
394
diff
changeset
|
622 |
33b5e7adc30c
fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
394
diff
changeset
|
623 my $nnew = scalar(keys %$weatherdata); |
33b5e7adc30c
fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents:
394
diff
changeset
|
624 print STDERR "Purged data older than ".$purge." minutes, ".$nold." -> ".$nnew." = ".($nold - $nnew)." removed.\n" if (opt_get_int("debug") > 0); |
371
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
625 } |
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
626 } |
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
627 |
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
628 |
36869897f21d
fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents:
370
diff
changeset
|
629 ### |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
630 ### Output |
178
9b8ec700ede4
Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
170
diff
changeset
|
631 ### |
353 | 632 if (opt_chk_valid("outfile", 1)) |
633 { | |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
634 my $filename = opt_get("outfile"); |
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
635 print STDERR "Dumping data to output file '".$filename."'\n" if (opt_get_int("debug") > 0); |
368
ed1c7f2d81df
fetch_weather: Close STDOUT filehandle before reopening it, to make sure we
Matti Hamalainen <ccr@tnsp.org>
parents:
367
diff
changeset
|
636 close(STDOUT); |
438
a17b7fdd03c0
fetch_weather: Simplify and check for existence of output file before trying to read the old one.
Matti Hamalainen <ccr@tnsp.org>
parents:
417
diff
changeset
|
637 open(STDOUT, '>', $filename) or die("Could not open output file '".$filename."': $!\n"); |
158
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
638 } |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
639 |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
640 binmode STDOUT, ':encoding(utf-8)'; |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
641 |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
642 foreach my $key (sort { $a cmp $b } keys %$weatherdata) |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
643 { |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
644 print STDOUT $key."|".join("|", @{$weatherdata->{$key}})."\n"; |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
645 } |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
646 |
db2b1f74d994
Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
647 close(STDOUT); |