comparison fetch_weather.pl @ 442:19fe8ef0a902

fetch_weather: Improve commandline handling, add -force and -debug options.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Sep 2017 12:33:38 +0300
parents a17b7fdd03c0
children 1f16d27c571d
comparison
equal deleted inserted replaced
441:9c2cc740ec76 442:19fe8ef0a902
33 33
34 ### 34 ###
35 ### Configuration settings 35 ### Configuration settings
36 ### 36 ###
37 my %settings = ( 37 my %settings = (
38 "force_update" => 0,
38 "debug" => 0, 39 "debug" => 0,
39 "opt_fmi" => 0, 40 "opt_fmi" => 0,
40 "opt_tiehallinto" => 0, 41 "opt_tiehallinto" => 0,
41 "purge_threshold" => 60, 42 "purge_threshold" => 60,
42 "fmi_api_key" => "", 43 "fmi_api_key" => "",
283 284
284 ### 285 ###
285 ### Main program begins 286 ### Main program begins
286 ### 287 ###
287 my $weatherdata = {}; 288 my $weatherdata = {};
288 289 my $opt_cfgfile;
289 die( 290
290 "Weather Fetch v0.8 by ccr/TNSP <ccr\@tnsp.org>\n". 291 while (scalar(@ARGV) > 0)
291 "Usage: $0 <config file> [force]\n" 292 {
292 ) unless scalar(@ARGV) >= 1; 293 my $arg = shift;
293 294 if ($arg eq "-force")
294 my $cfgfile = shift; 295 {
295 opt_read_config($cfgfile) == 0 or die("Errors while parsing configuration file '".$cfgfile."'.\n"); 296 $settings{"force_update"} = 1;
296 my $force_update = scalar(@ARGV) >= 1 && (shift eq "force"); 297 }
298 elsif ($arg eq "-debug")
299 {
300 $settings{"debug"} = 1;
301 }
302 else
303 {
304 die("Configuration file already specified!\n") if defined($opt_cfgfile);
305 $opt_cfgfile = $arg;
306 }
307 }
308
309 if (!defined($opt_cfgfile))
310 {
311 die(
312 "Weather Fetch v0.8 by ccr/TNSP <ccr\@tnsp.org>\n".
313 "Usage: $0 <config file> [options]\n".
314 "\n".
315 " -force : Force updating of all data\n".
316 " -debug : Enable debug\n".
317 "\n"
318 );
319 }
320
321 opt_read_config($opt_cfgfile) == 0 or die("Errors while parsing configuration file '".$opt_cfgfile."'.\n");
322 print STDERR "Forcing update of all data.\n" if $settings{"force_update"};
297 323
298 324
299 ### 325 ###
300 ### Load already cached data 326 ### Load already cached data
301 ### 327 ###
355 $fetch_meta = 1 unless ($tmp1 < $tmp2); 381 $fetch_meta = 1 unless ($tmp1 < $tmp2);
356 } 382 }
357 383
358 # Fetch or read the cache 384 # Fetch or read the cache
359 my $meta_str; 385 my $meta_str;
360 if ($fetch_meta || $force_update) 386 if ($fetch_meta || opt_chk_book("force_update"))
361 { 387 {
362 my $uri = "http://tie.digitraffic.fi/api/v1/metadata/weather-stations"; 388 my $uri = "http://tie.digitraffic.fi/api/v1/metadata/weather-stations";
363 print STDERR "Fetching Tiehallinto static meta data from $uri\n" if (opt_get_int("debug") > 1); 389 print STDERR "Fetching Tiehallinto static meta data from $uri\n" if (opt_get_int("debug") > 1);
364 my $res = fetch_http($uri); 390 my $res = fetch_http($uri);
365 die("Failed to fetch $uri data.\n") unless ($res->code <= 200 && $res->code <= 201); 391 die("Failed to fetch $uri data.\n") unless ($res->code <= 200 && $res->code <= 201);