-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdocs_backup.pl
executable file
·65 lines (56 loc) · 1.46 KB
/
gdocs_backup.pl
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
#! /usr/bin/perl
use uni::perl;
use autodie;
use Getopt::Long;
use Net::Google::DocumentsList;
use JSON;
use IO::All -utf8;
my ($output_dir, $login, $password);
GetOptions('login=s' => \$login, 'password=s' => \$password,
'output=s' => \$output_dir);
unless ($login && $password) {
die "Usage: $0 --login <login> --password <password> [--output <output dir>]\n";
}
$output_dir //= 'docs/';
-d $output_dir or mkdir $output_dir;
my %save_as = (
document => 'odt',
spreadsheet => 'ods',
presentation=> 'ppt',
pdf => 'pdf',
drawing => 'svg',
'image/png' => 'png',
);
my $gdocs = Net::Google::DocumentsList->new(
# XXX OAuthify
username => $login,
password => $password,
);
my @items = $gdocs->items;
for (@items) {
say "Downloading " . $_->title . " as " . $_->kind;
(my $filename = $_->title) =~ y{/}{_};
if (my $ext = $save_as{$_->kind}) {
$_->export({
format => $ext,
file => "$output_dir/$filename.$ext",
});
}
else {
next if $_->kind ~~ ['map', 'form'];
$_->export({
file => "$output_dir/$filename",
});
}
}
say "Saving metadata.json";
io("$output_dir/metadata.json")->print(to_json([
map {
my %hash;
for my $attr
(qw/title kind published alternate updated edited
resource_id deleted parent/)
{ $hash{$attr} = $_->$attr }
\%hash;
} @items
], { pretty => 1 }));