forked from USGCRP/gcis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-files.pl
More file actions
executable file
·130 lines (91 loc) · 2.7 KB
/
get-files.pl
File metadata and controls
executable file
·130 lines (91 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env perl
use Getopt::Long qw/GetOptions/;
use Pod::Usage qw/pod2usage/;
use Gcis::Client;
use Gcis::Exim;
use YAML;
use Data::Dumper;
use strict;
use v5.14;
# local $YAML::Indent = 2;
GetOptions(
'log_file=s' => \(my $log_file = '/tmp/gcis-get-files.log'),
'log_level=s' => \(my $log_level = "info"),
'input=s' => \(my $input),
'local=s' => \(my $local = '.'),
'dry_run|n' => \(my $dry_run),
'help|?' => sub { pod2usage(verbose => 2) },
) or die pod2usage(verbose => 1);
my $n = 0;
&main;
sub main {
say " importing files for a report";
say " log file : $log_file";
say " log level : $log_level";
say " input : $input" if $input;
say " local : $local";
say " dry run" if $dry_run;
my $a = Exim->new("no url");
$a->load($input);
my $b = Mojo::UserAgent->new;
$b = $b->max_redirects(3);
my $base = $a->{base};
say " base : $base";
my $logger = Mojo::Log->new($log_file eq '-' ? () : (path => $log_file));
$logger->level($log_level);
$a->logger($logger);
$a->logger_info("starting: ".$base);
for (keys %{ $a->{files} }) {
my $obj = $a->{files}->{$_};
my $url = $obj->{url};
my $name = ($obj->{file} =~ s[.*/][]r);
my $loc = "$local/$name";
if ($url =~ m[^http:]) {
say " warning - external file not read : $url";
next;
}
my $org = "$base$url";
if (-f $loc) {
say " warning - file already exists : $loc";
next;
}
if ($dry_run) {
say " would download file : $org";
say " to : $loc";
next;
}
say " downloading file : $org";
say " to : $loc";
my $tx = $b->get($org);
$tx->res->content->asset->move_to($loc);
}
$a->logger_info("done");
return;
}
1;
=head1 NAME
get-files.pl -- get files for a report from gcis instance
=head1 DESCRIPTION
get-files.pl -- gets the files for an entire report from the original gcis
instance. The report source is a yaml file (see export-report.pl). The gcis
instance is the one specified in the report. The files are stored in a local
directory.
If the file already exists in the local directory, it is skipped.
=head1 SYNOPSIS
./get-files.pl [OPTIONS]
=head1 OPTIONS
=over
=item B<--log_file>
Log file (/tmp/gcis-get-files.log)
=item B<--log_level>
Log level (see Mojo::Log)
=item B<--input>
Input (source) report (yaml file, defaults to STDIN)
=item B<--local>
Directory to store file (defaults to ".")
=item B<--dry_run>
Set to perform dry run (no actual download)
=back
=head1 EXAMPLES
./get-files.pl --file=report.yaml --local=./tmp
=cut