annotate fetch_weather.pl @ 698:6ba9f961e463 default tip

quotedb: Bump version and copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 18 Sep 2023 11:38:41 +0300
parents fd45c52d4297
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
381
846a0a95ad5a fetch_weather: Add back -w
Matti Hamalainen <ccr@tnsp.org>
parents: 380
diff changeset
1 #!/usr/bin/perl -w
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
2 ##########################################################################
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
3 #
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
4 # Fetch Weather v1.3.0 by Matti 'ccr' Hamalainen <ccr@tnsp.org>
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
5 # (C) Copyright 2014-2023 Tecnic Software productions (TNSP)
206
2ac661d551b3 Update documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
6 # This script is freely distributable under GNU GPL (version 2) license.
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
7 #
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
8 # Should be ran as a cronjob, and configured properly.
206
2ac661d551b3 Update documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
9 # */10 * * * * perl -w /absolute/path/to/fetch_weather.pl /path/to/configfile
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
10 #
475
4075cf3e094c fetch_weather: Update 'documentation'.
Matti Hamalainen <ccr@tnsp.org>
parents: 463
diff changeset
11 # Configuration file example is in "config.fetch_weather.example"
477
6773d97a3de2 fetch_weather: Add note about commandline options.
Matti Hamalainen <ccr@tnsp.org>
parents: 476
diff changeset
12 # For debugging/testing, try ./fetch_weather.pl to see commandline options.
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
13 #
206
2ac661d551b3 Update documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 201
diff changeset
14 # Requires various Perl modules, in Debian the packages should be:
475
4075cf3e094c fetch_weather: Update 'documentation'.
Matti Hamalainen <ccr@tnsp.org>
parents: 463
diff changeset
15 # libwww-perl libxml-simple-perl libtimedate-perl libjson-perl
4075cf3e094c fetch_weather: Update 'documentation'.
Matti Hamalainen <ccr@tnsp.org>
parents: 463
diff changeset
16 # libfile-slurper-perl
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
17 #
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
18 ##########################################################################
417
3c42ad35e157 fetch_weather: Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 416
diff changeset
19 use 5.18.0;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 use strict;
380
729eb88d1c63 fetch_weather: "use warnings".
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
21 use warnings;
243
aef2cf1dc3be Use UTF8 source.
Matti Hamalainen <ccr@tnsp.org>
parents: 242
diff changeset
22 use utf8;
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
23 use Encode;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 use LWP::UserAgent;
399
a80de0130e45 fetch_weather: Support gzip encoded content in HTTP requests.
Matti Hamalainen <ccr@tnsp.org>
parents: 398
diff changeset
25 use HTTP::Message;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 use HTML::Entities;
399
a80de0130e45 fetch_weather: Support gzip encoded content in HTTP requests.
Matti Hamalainen <ccr@tnsp.org>
parents: 398
diff changeset
27 use Compress::Zlib;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 use XML::Simple;
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
29 use Date::Format;
199
95012f8f687a Another round of timestamp parsing changes, urgh.
Matti Hamalainen <ccr@tnsp.org>
parents: 198
diff changeset
30 use Date::Parse;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 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
32 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
33 use JSON;
166
c8d5f3a7c4b7 Add debug option.
Matti Hamalainen <ccr@tnsp.org>
parents: 164
diff changeset
34
400
f69aff75d1e2 fetch_weather: Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 399
diff changeset
35
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
36 ###
182
24f9f38210fe Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 178
diff changeset
37 ### Configuration settings
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
38 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
39 my %settings = (
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
40 "force_update" => 0,
166
c8d5f3a7c4b7 Add debug option.
Matti Hamalainen <ccr@tnsp.org>
parents: 164
diff changeset
41 "debug" => 0,
680
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
42 "dump" => 0,
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
43 "opt_fmi" => 0,
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
44 "opt_tiehallinto" => 0,
374
26b7963e7fbd fetch_weather: Oops, forgot to define the configuration setting
Matti Hamalainen <ccr@tnsp.org>
parents: 373
diff changeset
45 "purge_threshold" => 60,
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
46 "outfile" => "",
185
64e06ee172c7 Add HTTP user agent setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 182
diff changeset
47 "http_user_agent" => "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 6.0) Opera 10.63 [en]",
446
ba559d5d44db fetch_weather: Rename the Tiehallinto static metadata cache filename setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
48 "tiehallinto_meta" => "tiehallinto.meta",
447
11124f9bf994 fetch_weather: Instead of checking the latest static metadata update timestamp against the weather data XML, have a setting of days until the metadata is considered stale.
Matti Hamalainen <ccr@tnsp.org>
parents: 446
diff changeset
49 "tiehallinto_meta_period" => 7,
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
50 "tiehallinto_rw_url" => "https://tie.digitraffic.fi/api/weather/v1/stations/data",
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
51 "tiehallinto_meta_url" => "https://tie.digitraffic.fi/api/weather/v1/stations",
495
ec5f0ed99a28 fetch_weather: Move base "hardcoded" FMI url and certain parameters into
Matti Hamalainen <ccr@tnsp.org>
parents: 491
diff changeset
52 "fmi_weather_base_url" => "https://opendata.fmi.fi/wfs",
ec5f0ed99a28 fetch_weather: Move base "hardcoded" FMI url and certain parameters into
Matti Hamalainen <ccr@tnsp.org>
parents: 491
diff changeset
53 "fmi_weather_extra_params" => "&maxlocations=300&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
54 );
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
55
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
56
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
57 ###
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
58 ### Helper functions
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
59 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
60 sub mlog($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
61 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
62 print STDERR $_[0];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
63 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
64
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
65
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
66 sub fetch_http($)
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
67 {
198
60a4c69c7f25 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 197
diff changeset
68 my $agent = LWP::UserAgent->new;
461
721a56cd62e3 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
69 $agent->agent(opt_get("http_user_agent"));
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
70 $agent->timeout(20);
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
71
198
60a4c69c7f25 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 197
diff changeset
72 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
73 $req->header('Accept-Encoding' => scalar HTTP::Message::decodable());
339
6ea410acbe74 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 338
diff changeset
74
411
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
75 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
76
406
4c3abf052d01 fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 405
diff changeset
77 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
78
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
79 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
80 {
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
81 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
82 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
83 {
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
84 print STDERR
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
85 "# 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
86 "# 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
87 "# 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
88 }
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
89 }
fe47617e7251 fetch_weather: Add some debugging output to fetch_http().
Matti Hamalainen <ccr@tnsp.org>
parents: 410
diff changeset
90
406
4c3abf052d01 fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 405
diff changeset
91 return $res;
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
92 }
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
93
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
94
382
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
95 sub str_trim($)
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
96 {
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
97 my $tmp = $_[0];
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
98 $tmp =~ s/^\s*//;
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
99 $tmp =~ s/\s*$//;
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
100 return $tmp;
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
101 }
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
102
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
103
194
55ed3db51ee7 Rename a function.
Matti Hamalainen <ccr@tnsp.org>
parents: 193
diff changeset
104 sub format_time_gmt($)
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
105 {
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
106 # 2012-02-27T00:00:00Z
199
95012f8f687a Another round of timestamp parsing changes, urgh.
Matti Hamalainen <ccr@tnsp.org>
parents: 198
diff changeset
107 return time2str("%Y-%m-%dT%TZ", $_[0], "UTC");
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
108 }
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
109
193
93ec73deebc5 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 192
diff changeset
110
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
111 sub force_decode_utf8($)
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
112 {
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
113 if (!utf8::is_utf8($_[0]))
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
114 {
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
115 return decode("utf8", $_[0]);
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
116 }
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
117 else
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
118 {
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
119 return $_[0];
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
120 }
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
121 }
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
122
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
123
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
124 sub plonk_data_var($$$)
228
fe420576f284 The kludges intensify.
Matti Hamalainen <ccr@tnsp.org>
parents: 213
diff changeset
125 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
126 return defined($_[0]->{$_[1]}) ? $_[0]->{$_[1]} : $_[2];
228
fe420576f284 The kludges intensify.
Matti Hamalainen <ccr@tnsp.org>
parents: 213
diff changeset
127 }
fe420576f284 The kludges intensify.
Matti Hamalainen <ccr@tnsp.org>
parents: 213
diff changeset
128
fe420576f284 The kludges intensify.
Matti Hamalainen <ccr@tnsp.org>
parents: 213
diff changeset
129
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
130 sub plonk_data_value($$)
237
56af82fb7a6d Use lower case.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
131 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
132 if (defined($_[0]) && defined($_[0]{$_[1]}) && defined($_[0]{$_[1]}{"value"}))
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
133 {
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
134 return $_[0]{$_[1]}{"value"};
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
135 }
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
136 else
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
137 {
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
138 return "";
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
139 }
237
56af82fb7a6d Use lower case.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
140 }
56af82fb7a6d Use lower case.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
141
56af82fb7a6d Use lower case.
Matti Hamalainen <ccr@tnsp.org>
parents: 236
diff changeset
142
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 ###
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
144 ### Configuration handling
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
145 ###
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
146 sub opt_chk_bool($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
147 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
148 if (defined($settings{$_[0]}))
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
149 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
150 my $val = $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
151 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
152 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
153 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
154 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
155 return 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
156 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
157 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
158
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
159
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
160 sub opt_chk_valid($$)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
161 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
162 if (defined($settings{$_[0]}))
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
163 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
164 my $val = $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
165 return length($val) >= $_[1];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
166 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
167 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
168 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
169 return 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
170 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
171 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
172
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
173
324
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
174 sub opt_get_int($)
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
175 {
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
176 if (defined($settings{$_[0]}))
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
177 {
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
178 return int($settings{$_[0]});
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
179 }
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
180 else
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
181 {
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
182 return -1;
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
183 }
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
184 }
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
185
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
186
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
187 sub opt_get($)
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 if (defined($settings{$_[0]}))
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 return $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
192 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
193 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
194 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
195 return undef;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
196 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
197 }
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
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
200 sub opt_read_config($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
201 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
202 my $filename = $_[0];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
203 my $errors = 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
204 my $line = 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
205
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
206 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
207 while (<CONFFILE>)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
208 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
209 $line++;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
210 chomp;
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
211 if (/(^\s*#|^\s*$)/)
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
212 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
213 # Ignore comments and empty lines
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
214 }
386
299744a6c592 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 385
diff changeset
215 elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*(\d+),?\s*$/)
299744a6c592 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 385
diff changeset
216 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
217 my $key = lc($1);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
218 my $value = $2;
461
721a56cd62e3 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
219 if (defined($settings{$key}))
721a56cd62e3 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
220 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
221 $settings{$key} = $value;
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
222 }
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
223 else
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
224 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
225 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
226 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
227 }
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
228 }
386
299744a6c592 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 385
diff changeset
229 elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*\"(.*?)\",?\s*$/)
299744a6c592 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 385
diff changeset
230 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
231 my $key = lc($1);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
232 my $value = $2;
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
233 if (defined($settings{$key}))
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
234 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
235 $settings{$key} = $value;
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
236 }
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
237 else
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
238 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
239 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
240 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
241 }
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
242 }
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
243 else
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
244 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
245 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
246 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
247 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
248 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
249 close(CONFFILE);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
250 return $errors;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
251 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
252
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
253
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
254 ###
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 ### Main program begins
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 ###
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 my $weatherdata = {};
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
258 my $opt_cfgfile;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
260 while (scalar(@ARGV) > 0)
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
261 {
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
262 my $arg = shift;
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
263 if ($arg eq "-force")
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
264 {
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
265 $settings{"force_update"} = 1;
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
266 }
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
267 elsif ($arg eq "-debug")
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
268 {
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
269 $settings{"debug"} = 1;
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
270 }
680
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
271 elsif ($arg eq "-dump")
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
272 {
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
273 $settings{"dump"} = 1;
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
274 }
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
275 else
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
276 {
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
277 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
278 $opt_cfgfile = $arg;
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
279 }
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
280 }
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
281
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
282 if (!defined($opt_cfgfile))
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
283 {
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
284 die(
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
285 "Weather Fetch v1.3.0 by ccr/TNSP <ccr\@tnsp.org>\n".
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
286 "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
287 "\n".
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
288 " -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
289 " -debug : Enable debug\n".
680
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
290 " -dump : Dump received raw data\n".
442
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
291 "\n"
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 }
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
294
19fe8ef0a902 fetch_weather: Improve commandline handling, add -force and -debug options.
Matti Hamalainen <ccr@tnsp.org>
parents: 438
diff changeset
295 opt_read_config($opt_cfgfile) == 0 or die("Errors while parsing configuration file '".$opt_cfgfile."'.\n");
461
721a56cd62e3 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 452
diff changeset
296 print STDERR "Forcing update of all data.\n" if opt_chk_bool("force_update");
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
297
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
298
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
299 ###
354
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
300 ### Load already cached data
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
301 ###
462
9b4b1e4ce313 fetch_weather: When force_update is set, do not load the old data cache.
Matti Hamalainen <ccr@tnsp.org>
parents: 461
diff changeset
302 if (opt_chk_valid("outfile", 1) && !opt_chk_bool("force_update"))
354
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
303 {
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
304 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
305 if (-e "$filename")
354
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
306 {
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
307 my $str = force_decode_utf8(read_text($filename));
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
308 if (defined($str))
354
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
309 {
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
310 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
311 {
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
312 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
313 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
314 {
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
315 $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
316 }
354
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
317 }
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
318 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
319 }
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
320 }
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
321 }
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
322
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
323
de411e7834aa fetch_weather: Reload old data from stored data.
Matti Hamalainen <ccr@tnsp.org>
parents: 353
diff changeset
324 ###
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
325 ### Fetch Tiehallinto data
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
326 ###
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
327 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
328 {
443
1f16d27c571d fetch_weather: Make tiehallinto API urls configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
329 my $uri = opt_get("tiehallinto_rw_url");
406
4c3abf052d01 fetch_weather: "use 5.018;" and some cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 405
diff changeset
330 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
331 my $res = fetch_http($uri);
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
332 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
333 {
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
334 my $data_json = JSON->new->decode(force_decode_utf8($res->decoded_content));
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
335
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
336 print Dumper($data_json) if opt_chk_bool("dump");
487
b4be881a9908 fetch_weather, weather: Add primitive support for wind direction data.
Matti Hamalainen <ccr@tnsp.org>
parents: 486
diff changeset
337
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
338 if (!defined($data_json->{"dataUpdatedTime"}) || !defined($data_json->{"stations"}))
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
339 {
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
340 print STDERR "ERROR: REST/JSON call result did not contain required data.\n";
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
341 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
342 else
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
343 {
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
344 # Check if we need to update the static meta data
446
ba559d5d44db fetch_weather: Rename the Tiehallinto static metadata cache filename setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 445
diff changeset
345 my $meta_file = opt_get("tiehallinto_meta");
447
11124f9bf994 fetch_weather: Instead of checking the latest static metadata update timestamp against the weather data XML, have a setting of days until the metadata is considered stale.
Matti Hamalainen <ccr@tnsp.org>
parents: 446
diff changeset
346 my $meta_stamp = (-e $meta_file) ? (stat($meta_file))[9] : -1;
11124f9bf994 fetch_weather: Instead of checking the latest static metadata update timestamp against the weather data XML, have a setting of days until the metadata is considered stale.
Matti Hamalainen <ccr@tnsp.org>
parents: 446
diff changeset
347 my $fetch_meta = ($meta_stamp + 60*60*24 * opt_get_int("tiehallinto_meta_period")) < time();
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
348
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
349 # Fetch or read the cache
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
350 my $meta_str;
444
fe671cd76ecc Oops, a typo.
Matti Hamalainen <ccr@tnsp.org>
parents: 443
diff changeset
351 if ($fetch_meta || opt_chk_bool("force_update"))
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
352 {
443
1f16d27c571d fetch_weather: Make tiehallinto API urls configurable.
Matti Hamalainen <ccr@tnsp.org>
parents: 442
diff changeset
353 my $uri = opt_get("tiehallinto_meta_url");
445
dbe9e233f564 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 444
diff changeset
354 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
355 my $res = fetch_http($uri);
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
356 die("Failed to fetch $uri data.\n") unless ($res->code <= 200 && $res->code <= 201);
339
6ea410acbe74 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 338
diff changeset
357
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
358 $meta_str = force_decode_utf8($res->decoded_content);
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
359 $fetch_meta = 1;
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
360 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
361 else
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
362 {
445
dbe9e233f564 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 444
diff changeset
363 print STDERR "Using CACHED Tiehallinto static meta data from '".$meta_file."'.\n" if (opt_get_int("debug") > 0);
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
364 $meta_str = force_decode_utf8(read_text($meta_file));
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
365 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
366
416
c88f7449b526 fetch_weather: Possibly another UTF8 fix :S
Matti Hamalainen <ccr@tnsp.org>
parents: 414
diff changeset
367 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
368
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
369 # Parse the data ..
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
370 my $meta_data = {};
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
371 my $meta_json = JSON->new->decode($meta_str);
398
6419327df9e7 fetch_weather: Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 397
diff changeset
372
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
373 print Dumper($meta_json) if opt_chk_bool("dump");
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
374
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
375 if ($fetch_meta)
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
376 {
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
377 # Save new cache, in more optimal form, if needed.
445
dbe9e233f564 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 444
diff changeset
378 print STDERR "Storing to cache '".$meta_file."'.\n" if (opt_get_int("debug") > 0);
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
379 write_binary($meta_file, JSON->new->utf8->encode($meta_json));
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
380 }
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
381
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
382 foreach my $ms (@{$meta_json->{"features"}})
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
383 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
384 # Filter functional stations?
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
385 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
386 defined($ms->{"geometry"}{"coordinates"}) &&
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
387 $ms->{"properties"}{"collectionStatus"} eq "GATHERING" &&
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
388 $ms->{"properties"}{"name"} !~ /^TEST_/
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
389 )
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
390 {
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
391 $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
392 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
393 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
394
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
395 my $nrecords = 0;
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
396 foreach my $wdata (@{$data_json->{"stations"}})
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
397 {
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
398 my $wid = $wdata->{"id"};
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
399 if (defined($meta_data->{$wid}) &&
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
400 defined($wdata->{"sensorValues"}))
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
401 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
402 my $sensors = {};
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
403 foreach my $sensor (@{$wdata->{"sensorValues"}})
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
404 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
405 $sensors->{$sensor->{"name"}} = $sensor;
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
406 }
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
407
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
408 my $wqname = $meta_data->{$wid}{"properties"}{"name"};
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
409 $wqname =~ s#_# #g;
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
410 $wqname =~ s/(^|\s)(vt|yt|st|kt)(\d+)/"Tie ".$3/ge;
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
411 $wqname .= " (TH)";
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
412
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
413 $nrecords++;
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
414 $weatherdata->{$wqname} =
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
415 [
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
416 # Basic data
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
417 $meta_data->{$wid}{"geometry"}{"coordinates"}[1],
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
418 $meta_data->{$wid}{"geometry"}{"coordinates"}[0],
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
419
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
420 # This is kind of wrong, because the actual measuredTime exists
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
421 # for each sensor separately ... but .. whatever.
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
422 str2time(plonk_data_var($wdata, "dataUpdatedTime", "1970-01-01T14:15:16")),
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
423
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
424 plonk_data_value($sensors, "ILMA"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
425 plonk_data_value($sensors, "ILMAN_KOSTEUS"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
426 plonk_data_value($sensors, "KESKITUULI"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
427 plonk_data_value($sensors, "TUULENSUUNTA"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
428
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
429 "", # total cloud cover
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
430 plonk_data_value($sensors, "TIE_1"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
431 plonk_data_value($sensors, "SADE"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
432 plonk_data_value($sensors, "NÄKYVYYS_KM"),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
433 plonk_data_value($sensors, "SADE_INTENSITEETTI"),
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
434 ];
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
435 }
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
436 else
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
437 {
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
438 print STDERR "Station ID #".$wid." not defined?\n" if (opt_get_int("debug") > 0);
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
439 #.Dumper($meta_data->{$wid});
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
440 }
338
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
441 }
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
442 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
443 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
444 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
445 }
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
446
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
447
afb8c0e2d513 fetch_weather: Implement support for new Tiehallinto data API.
Matti Hamalainen <ccr@tnsp.org>
parents: 332
diff changeset
448 ###
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 ### Fetch FMI data
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
450 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
451 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
452 {
680
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
453 my @fmitems = (
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
454 "temperature", "humidity", "windspeedms",
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
455 "winddirection", "totalcloudcover",
053d5662bb16 fetch_weather: Add commandline option to dump the received raw data in stdout.
Matti Hamalainen <ccr@tnsp.org>
parents: 578
diff changeset
456 );
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457
495
ec5f0ed99a28 fetch_weather: Move base "hardcoded" FMI url and certain parameters into
Matti Hamalainen <ccr@tnsp.org>
parents: 491
diff changeset
458 my $uri = opt_get("fmi_weather_base_url").
486
b352722e8717 fetch_weather: FMI open data no longer requires an API key. Also the data
Matti Hamalainen <ccr@tnsp.org>
parents: 477
diff changeset
459 "?request=getFeature".
487
b4be881a9908 fetch_weather, weather: Add primitive support for wind direction data.
Matti Hamalainen <ccr@tnsp.org>
parents: 486
diff changeset
460 "&storedquery_id=fmi::observations::weather::multipointcoverage".
194
55ed3db51ee7 Rename a function.
Matti Hamalainen <ccr@tnsp.org>
parents: 193
diff changeset
461 "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()).
487
b4be881a9908 fetch_weather, weather: Add primitive support for wind direction data.
Matti Hamalainen <ccr@tnsp.org>
parents: 486
diff changeset
462 "&parameters=".join(",", @fmitems).
495
ec5f0ed99a28 fetch_weather: Move base "hardcoded" FMI url and certain parameters into
Matti Hamalainen <ccr@tnsp.org>
parents: 491
diff changeset
463 opt_get("fmi_weather_extra_params");
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
464
324
19c64798de91 weather: Add some better optional debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 247
diff changeset
465 print STDERR "FMI URI: ".$uri."\n" if (opt_get_int("debug") > 0);
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
466
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
467 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
468 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
469 {
463
fe478b7bd80e fetch_weather: Perhaps, possibly, fix the UTF8 issues. Maybe.
Matti Hamalainen <ccr@tnsp.org>
parents: 462
diff changeset
470 my $xml = XMLin(force_decode_utf8($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
471 my $time_base = time();
339
6ea410acbe74 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 338
diff changeset
472
693
fd45c52d4297 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 690
diff changeset
473 print Dumper($xml) if opt_chk_bool("dump");
487
b4be881a9908 fetch_weather, weather: Add primitive support for wind direction data.
Matti Hamalainen <ccr@tnsp.org>
parents: 486
diff changeset
474
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
475 if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"}))
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
476 {
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
477 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
478 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
479
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
480 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
481 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
482 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
483
378
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
484 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
485 {
378
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
486 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
487 {
382
5573c1681da9 fetch_weather: Trim split input data lines in FMI XML.
Matti Hamalainen <ccr@tnsp.org>
parents: 381
diff changeset
488 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
489 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
490
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
491 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
492 if (scalar(@fmatches) != scalar(@fmitems))
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
493 {
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
494 print STDERR "Not enough items in scalar line #".$nline." (".
452
ec97c29768e7 fetch_weather: Improve some debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 450
diff changeset
495 scalar(@fmatches). " vs ".scalar(@fmitems)."): ".$dline."\n"
ec97c29768e7 fetch_weather: Improve some debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 450
diff changeset
496 if (opt_get_int("debug") > 0);
378
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
497 }
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
498 else
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
499 {
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
500 my $vtmp = {};
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
501 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
502 {
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
503 $$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
504 }
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
505 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
506 {
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
507 $$vtmp{"lat"} = $1;
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
508 $$vtmp{"long"} = $2;
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
509 $$vtmp{"time"} = $3;
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
510 push(@farray, $vtmp);
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
511 }
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
512 else
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
513 {
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
514 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
515 }
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
516 }
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
517 }
169
c12e422a543a FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
518 }
378
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
519 else
169
c12e422a543a FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
520 {
452
ec97c29768e7 fetch_weather: Improve some debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 450
diff changeset
521 print STDERR "Position and data line counts do not match ".
ec97c29768e7 fetch_weather: Improve some debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 450
diff changeset
522 scalar(@position_lines)." <> ".scalar(@data_lines)."\n";
378
b706e8ebecf8 fetch_weather: Possibly improve FMI XML mess parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 377
diff changeset
523 goto skip_it;
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
524 }
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
525 # XXX Hashify the array into lat/long keys
339
6ea410acbe74 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 338
diff changeset
526
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
527 # This is horrible :S
352
19b363586b40 fetch_weather: Fix debug prints.
Matti Hamalainen <ccr@tnsp.org>
parents: 349
diff changeset
528 my $nrecords = 0;
410
5c97006c89e6 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 407
diff changeset
529 foreach my $xnode (@{$fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"}})
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
530 {
228
fe420576f284 The kludges intensify.
Matti Hamalainen <ccr@tnsp.org>
parents: 213
diff changeset
531 my $floc = $xnode->{"gml:Point"};
687
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
532 if ($floc->{"gml:name"} ne "" &&
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
533 $floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
534 {
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
535 my ($flat, $flong) = ($1, $2);
349
f7793ef7c925 fetch_weather: Improve debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 343
diff changeset
536
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
537 # Should use a hash -
410
5c97006c89e6 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 407
diff changeset
538 foreach my $frec (@farray)
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
539 {
687
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
540 # If lat/long matches, and location is not yet defined, or
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
541 # if timestamp is newer, store to location data
410
5c97006c89e6 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 407
diff changeset
542 if ($frec->{"lat"} == $flat && $frec->{"long"} == $flong &&
687
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
543 (!defined($weatherdata->{$floc->{"gml:name"}}) ||
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
544 $frec->{"time"} >= $weatherdata->{$floc->{"gml:name"}}[2])
687
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
545 )
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
546 {
687
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
547 $nrecords++ unless defined($weatherdata->{$floc->{"gml:name"}});
7f1a0d25aa40 fetch_weather: Cleanups, fix counting of FMI stations.
Matti Hamalainen <ccr@tnsp.org>
parents: 680
diff changeset
548
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
549 $weatherdata->{$floc->{"gml:name"}} =
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
550 [
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
551 # Basic data
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
552 plonk_data_var($frec, "lat", 0),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
553 plonk_data_var($frec, "long", 0),
410
5c97006c89e6 fetch_weather: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 407
diff changeset
554
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
555 plonk_data_var($frec, "time", ""),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
556 plonk_data_var($frec, "temperature", ""),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
557 plonk_data_var($frec, "humidity", ""),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
558 plonk_data_var($frec, "windspeedms", ""),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
559 plonk_data_var($frec, "winddirection", ""),
448
ac3b79eca0ca fetch_weather: Change to use the new REST/JSON API for fetching Tiehallinto digitraffic weather data.
Matti Hamalainen <ccr@tnsp.org>
parents: 447
diff changeset
560
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
561 plonk_data_var($frec, "totalcloudcover", ""),
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
562
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
563 ];
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
564 }
189
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
565 }
3af8ea19a3e2 Improved weather fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 186
diff changeset
566 }
169
c12e422a543a FMI data parsing kinda almost works. It is horrible, though. XML ... :Q
Matti Hamalainen <ccr@tnsp.org>
parents: 168
diff changeset
567 }
352
19b363586b40 fetch_weather: Fix debug prints.
Matti Hamalainen <ccr@tnsp.org>
parents: 349
diff changeset
568 print STDERR $nrecords." records from FMI.\n" if (opt_get_int("debug") > 0);
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
569 }
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
570 else
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
571 {
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
572 # defined
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
573 print STDERR "Invalid XML received:\n";
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
574 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
575 }
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
576 }
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
577 else
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
578 {
208
c6bbf69afc57 Add some error handling and debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 206
diff changeset
579 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
580 }
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
583
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
584 ### 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
585 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
586
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
587
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
588 ###
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
589 ### Purge too old entries
371
36869897f21d fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
590 ###
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
591 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
592 {
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
593 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
594 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
595 {
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
596 my $wqtime = time();
395
33b5e7adc30c fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
597 my $nold = scalar(keys %$weatherdata);
373
bc7746ca622e fetch_weather: Implement purge_threshold setting.
Matti Hamalainen <ccr@tnsp.org>
parents: 371
diff changeset
598
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
599 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
600 {
690
8a6cca58e853 fetch_weather, weather: Update to match with Digitraffic API changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 687
diff changeset
601 if ($wqtime - $weatherdata->{$key}[2] > (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
602 {
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
603 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
604 }
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 }
395
33b5e7adc30c fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
606
33b5e7adc30c fetch_weather: Make purge_threshold output some information when debugging.
Matti Hamalainen <ccr@tnsp.org>
parents: 394
diff changeset
607 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
608 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
609 }
36869897f21d fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
610 }
36869897f21d fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
611
36869897f21d fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
612
36869897f21d fetch_weather: Purge sufficiently old data.
Matti Hamalainen <ccr@tnsp.org>
parents: 370
diff changeset
613 ###
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 ### Output
178
9b8ec700ede4 Clean up the weather data parser backend a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 170
diff changeset
615 ###
353
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
616 if (opt_chk_valid("outfile", 1))
a52a983c0c14 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 352
diff changeset
617 {
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
618 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
619 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
620 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
621 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
622 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 binmode STDOUT, ':encoding(utf-8)';
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 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
627 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 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
629 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 close(STDOUT);