annotate fetch_weather.pl @ 166:c8d5f3a7c4b7

Add debug option.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Jun 2014 20:25:31 +0300
parents d5a0e4248f3e
children 7eee54638b0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #!/usr/bin/perl -w
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 use strict;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 use LWP::UserAgent;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 use HTML::Entities;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 use XML::Simple;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 use Text::Iconv;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 use Date::Parse;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 use Data::Dumper;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9
166
c8d5f3a7c4b7 Add debug option.
Matti Hamalainen <ccr@tnsp.org>
parents: 164
diff changeset
10
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
11 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
12 ### Globals
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
13 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
14 my %settings = (
166
c8d5f3a7c4b7 Add debug option.
Matti Hamalainen <ccr@tnsp.org>
parents: 164
diff changeset
15 "debug" => 0,
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
16 "opt_fmi" => 0,
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
17 "opt_tiehallinto" => 0,
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
18 "fmi_api_key" => "",
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
19 "outfile" => "",
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
20 );
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
21
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
22 ###
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
23 ### Helper functions
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
24 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
25 sub mlog($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
26 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
27 print STDERR $_[0];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
28 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
29
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
30
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 sub str_trim($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 my $str = $_[0];
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 if (defined($str)) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 $str =~ s/^\s*//;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 $str =~ s/\s*$//;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 return $str;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 sub pop_token_a($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 my $tokens = $_[0];
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 return shift(@$tokens);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 sub pop_token($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 return str_trim(pop_token_a($_[0]));
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 sub parse_html_str($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 my $tokens = $_[0];
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 my $token = pop_token($tokens);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 my $str = "";
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 $token =~ s/^\s*//;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 return undef unless (substr($token, 0, 1) eq '"');
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 $token = substr($token, 1);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 while (defined($token)) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 my $tmp = $token;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 $tmp =~ s/\s*$//;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 if (substr($tmp, -1) eq '"') {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 $str .= substr($tmp, 0, -1);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 return $str;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 $str .= $token;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 $token = shift(@$tokens);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 return undef;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 sub parse_html_tree($$);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 sub parse_html_tree($$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 my ($tokens, $tree) = @_;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 while (my $token = pop_token($tokens)) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 if ($token =~ /^<[!\/]?[a-zA-Z]+/) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 $token = lc($token);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 if ($token =~ /^<\!.*>$/) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 # Ignore comments etc.
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 } elsif ($token =~ /^<([a-z]+[1-9]*)(.*)>$/) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 my ($name, $args) = ($1, $2);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 if ($name eq "meta" || $name eq "img") {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 my $tmp = {};
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 parse_html_tree($tokens, $tree);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 return $tree;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 my $tmp = { "name" => $name, "args" => str_trim($args) };
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 parse_html_tree($tokens, $tmp);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 push(@{$$tree{"nodes"}}, $tmp);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 } elsif ($token =~ /^<\/([a-z]+[1-9]*)>$/) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 return $tree;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 die("HORROR TERROR ELITE: '$token'\n");
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 $token = str_trim(decode_entities($token));
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 push(@{$$tree{"nodes"}}, { "name" => "text", "args" => "", "text" => $token }) if length($token) > 0;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 return $tree;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 sub parse_html($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 return undef unless defined($_[0]);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 my $str = $_[0];
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 my $res = { "name" => "", "args" => "" };
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 $str =~ tr/\r/ /;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 $str =~ tr/\n/ /;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 my @tokens = grep { !($_ =~ /^\s*$/) } split(/(<\/?[a-zA-Z]+.*?>)/, $str);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 parse_html_tree(\@tokens, $res);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 return $res;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 sub html_find_node($$$);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 sub html_find_node($$$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 my ($node, $name, $args) = @_;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 if (defined($node)) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 if (ref($node) eq "ARRAY") {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 foreach my $n (@$node) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 my $tmp = html_find_node($n, $name, $args);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 # Must do it like this, in order not to break the loop
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 return $tmp if defined($tmp);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 } elsif (ref($node) eq "HASH") {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 if (defined($$node{"name"})) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 if ($$node{"name"} eq $name) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 if ($args ne "") {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 if (defined($$node{"args"}) && $$node{"args"} =~ /$args/) {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 return html_find_node($$node{"nodes"}, $name, $args);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return $node;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 } else {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 return html_find_node($$node{"nodes"}, $name, $args);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 return undef;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 sub fnode($$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 return html_find_node($_[0], $_[1], "");
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 sub fnodea($$$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 return html_find_node($_[0], $_[1], $_[2]);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 sub fetch_http($)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 my $tmpAgent = LWP::UserAgent->new;
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 $tmpAgent->agent("Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 6.0) Opera 10.63 [en]");
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 $tmpAgent->timeout(10);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 my $tmpRequest = HTTP::Request->new(GET => $_[0]);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 return $tmpAgent->request($tmpRequest);
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 sub get_node($$$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 return defined($_[0]->[$_[2]]{"nodes"}[0]{$_[1]}) ? $_[0]->[$_[2]]{"nodes"}[0]{$_[1]} : "";
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 sub parse_timestamp($$)
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 {
162
4f8a163b2bc1 Work on the backend weather data fetching.
Matti Hamalainen <ccr@tnsp.org>
parents: 158
diff changeset
194 return str2time($_[0]) - str2time("00:00") + $_[1];
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 ###
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
199 ### Configuration
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
200 ###
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
201 sub opt_chk_bool($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
202 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
203 if (defined($settings{$_[0]}))
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
204 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
205 my $val = $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
206 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
207 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
208 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
209 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
210 return 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
211 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
212 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
213
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
214
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
215 sub opt_chk_valid($$)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
216 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
217 if (defined($settings{$_[0]}))
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
218 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
219 my $val = $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
220 return length($val) >= $_[1];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
221 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
222 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
223 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
224 return 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
225 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
226 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
227
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
228
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
229 sub opt_get($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
230 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
231 if (defined($settings{$_[0]}))
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
232 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
233 return $settings{$_[0]};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
234 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
235 else
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
236 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
237 return undef;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
238 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
239 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
240
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
241
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
242 sub opt_read_config($)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
243 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
244 my $filename = $_[0];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
245 my $errors = 0;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
246 my $line = 0;
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 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
249 while (<CONFFILE>)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
250 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
251 $line++;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
252 chomp;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
253 if (/(^\s*#|^\s*$)/) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
254 # Ignore comments and empty lines
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
255 } elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*(\d+),?\s*$/) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
256 my $key = lc($1);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
257 my $value = $2;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
258 if (defined($settings{$key})) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
259 $settings{$key} = $value;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
260 } else {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
261 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
262 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
263 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
264 } elsif (/^\s*\"?([a-zA-Z0-9_]+)\"?\s*=>?\s*\"(.*?)\",?\s*$/) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
265 my $key = lc($1);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
266 my $value = $2;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
267 if (defined($settings{$key})) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
268 $settings{$key} = $value;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
269 } else {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
270 mlog("[$filename:$line] Unknown setting '$key' = '$value'\n");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
271 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
272 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
273 } else {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
274 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
275 $errors = 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
276 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
277 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
278 close(CONFFILE);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
279 return $errors;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
280 }
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
281
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
282
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
283 ###
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 ### Main program begins
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 ###
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 my $weatherdata = {};
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
288 die(
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
289 "Weather Fetch v0.1 by ccr/TNSP <ccr\@tnsp.org>\n".
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
290 "Usage: $0 <config file>\n"
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
291 ) unless scalar(@ARGV) >= 1;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
292
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
293 opt_read_config(shift) == 0 or exit;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
294
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
295
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 ### Fetch tiehallinto data
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
297 if (opt_chk_bool("opt_tiehallinto"))
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
299 for (my $i = 1; $i <= 22; $i++)
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 {
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
301 my $res = fetch_http("http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_".$i.".html");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
302 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
303 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
304 my $data = $res->decoded_content;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
305
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
306 # Filter out crap tags we don't want or need
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
307 $data =~ s/\n/§/g;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
308 $data =~ s/<!--.*?-->//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
309 $data =~ s/<map[^>]*>.*?<\/map>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
310 $data =~ s/<form[^>]*>.*?<\/form>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
311 $data =~ s/<script[^>]*>.*?<\/script>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
312 $data =~ s/<meta[^>]*>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
313 $data =~ s/<font[^>]*>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
314 $data =~ s/<\/font>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
315 $data =~ s/<span[^>]*>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
316 $data =~ s/<\/span>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
317 $data =~ s/<\/?b>//ig;
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
319 $data =~ s/<br>//ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
320 $data =~ s/&nbsp;/ /ig;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
321 $data =~ s/§/\n/g;
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
322
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
323 # Parse the HTML mess
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
324 my $otree = parse_html($data);
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
326 # Find our desired element nodes
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
327 my $odata = fnodea(fnode($otree, "body"), "div", "class=elementc");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
328 my $oupdate = fnode($odata, "p");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
329 my $time_base = str2time("00:00");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
330 if ($oupdate) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
331 my $tmp = $oupdate->{"nodes"}[0]{"text"};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
332 if ($tmp =~ /:\s+(\d\d\.\d\d\.\d\d\d\d)\s+(\d\d:\d\d)/) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
333 $time_base = str2time($1);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
334 }
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 }
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
336
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
337 my $oelems = fnode($odata, "table")->{"nodes"};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
338 for (my $n = 1; $n < scalar(@$oelems); $n++)
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
339 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
340 my $fdata = @$oelems[$n]->{"nodes"};
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
341 $weatherdata->{$fdata->[0]{"nodes"}[0]{"text"}} =
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
342 [
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
343 parse_timestamp(get_node($fdata, "text", 1), $time_base),
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
344 get_node($fdata, "text", 2),
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
345 get_node($fdata, "text", 3),
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
346 get_node($fdata, "text", 4),
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
347 get_node($fdata, "text", 5),
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
348 ];
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
349 }
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 ### Fetch FMI data
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
355 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
356 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
357 die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10);
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
359 my $res = fetch_http("http://data.fmi.fi/fmi-apikey/".opt_get("fmi_api_key").
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
360 "/wfs?request=getFeature&storedquery_id=fmi::observations::weather::cities::multipointcoverage".
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
361 "&parameters=temperature,humidity");
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
362
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
363 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
364 {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
365 my $xml = XMLin($res->decoded_content,
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
366 KeyAttr => { server => 'name' },
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
367 ForceArray => [ 'server', 'address' ]);
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
368 }
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
371
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 ### Output
164
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
373 if (opt_chk_valid("outfile", 1)) {
d5a0e4248f3e weather: Implement configuration file support for weather fetcher backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
374 open(STDOUT, '>', opt_get("outfile")) or die("Could not open output file '".opt_get("outfile")."'.\n");
158
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 binmode STDOUT, ':encoding(utf-8)';
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 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
380 {
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 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
382 }
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383
db2b1f74d994 Initial import of weather data fetching backend, written in Perl.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384 close(STDOUT);