#!/usr/bin/env perl use feature ':5.10'; use strict; use warnings; use Pod::Usage (); use Getopt::Long (); use File::Spec::Functions qw(catfile catdir); =head1 NAME download-osm-with-retry - Try really hard to get a bbox from hypercube and fallback to an existing dump if that fails =head1 SYNOPSIS download-osm-with-retry --out-dir PATH --fallback FALLBACK-URI PRIMARY-URI download-osm-with-retry --out-dir /var/www/osm.nix.is/dump --fallback http://download.geofabrik.de/osm/europe/iceland.osm.bz2 ['http://www.informationfreeway.org/api/0.5/*[bbox=-24.6333,63.1833,-13.1333,67]'] =head1 DESCRIPTION Downloading from hypercube would trip up F, so now I get an osm file by any means in a seperate step: Stick something like this in cron: perl /var/www/osm.nix.is/script/download-osm-with-retry --out-dir /var/www/osm.nix.is/dump --fallback http://download.geofabrik.de/osm/europe/iceland.osm.bz2 'http://www.informationfreeway.org/api/0.5/*[bbox=-24.6333,63.1833,-13.1333,67]' Around an hour or so before running F. =head1 OPTIONS =over =item -h, --help Display this help message =item --fallback Dump to fallback on =item --out-dir Directory to output stuff into =back =cut # Get command line options Getopt::Long::Parser->new( config => [ qw(bundling no_ignore_case no_require_order) ], )->getoptions( 'h|help' => \my $help, 'fallback=s' => \my $fallback, 'out-dir=s' => \my $out_dir, ) or help(); select STDERR; my $xapi_uri = shift; help() if $help; chomp(my $date = `date --iso-8601`); if (!-d $out_dir) { system qq[mkdir -p $out_dir] and die "Can't mkdir($out_dir): $!"; } my $outfile = catfile($out_dir, "$date.osm"); my $outfile_tmp = $outfile . '.tmp'; my $ua = "See http://wiki.openstreetmap.org/index.php/Garmin_map_of_Iceland"; unless (-f $outfile) { if ($xapi_uri) { download_xapi($xapi_uri, $outfile_tmp, $outfile); # If at first you don't succeed... if (!-e $outfile) { for (1..3) { sleep 60 * 10; # 10 mins download_xapi($xapi_uri, $outfile_tmp, $outfile); last if -e $outfile; } } bz2_outfile($outfile) and exit 0 if -e $outfile; } #say "Downloading fallback URL $fallback"; # Fallback to geofabrik dump my $out = download_fallback($fallback, $outfile_tmp, $outfile); if (-f $out) { # Debugging #say `du -sh $out`; #say `stat $out`; exit 0; } else { say "ERROR: Couldn't download $xapi_uri or $fallback\n"; exit 1; } } sub bz2_outfile { my $file = shift; system qq[bzip2 $file] and die "bzip2: $!"; return 1; } sub download_fallback { my ($fallback, $tmp, $out) = @_; my $already_compressed = $fallback =~ /\.bz2$/; system qq[wget -q --user-agent='$ua' -O $tmp '$fallback'] and die "Can't wget $fallback: $!"; if (!$?) { system qq[mv $tmp $out] and die "Can't mv($tmp, $out): $!"; if ($already_compressed) { system qq[mv $out $out.bz2] and die "Can't mv($out, $out.bz2): $!"; } else { bz2_outfile($out); } return "$out.bz2"; } return; } sub download_xapi { my ($url, $tmp, $out) = @_; system qq[wget -q --user-agent='$ua' '$url' -O $tmp]; if (!$?) { system qq[mv $tmp $out] and die "Can't mv($tmp, $out): $!"; return 1; } return; } sub help { my %arg = @_; Pod::Usage::pod2usage( -verbose => $arg{ verbose }, -exitval => $arg{ exitval } || 0, ); }