Created
December 22, 2017 11:34
-
-
Save teckl/3004572c53ff612145825f6a2bc2b486 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl -w | |
use 5.20.0; | |
use utf8; | |
use Data::Dumper; | |
use IO::File; | |
use JSON; | |
use Net::Google::Drive::Simple; | |
use Text::CSV_XS; | |
# requires a ~/.google-drive.yml file with an access token, | |
# see description below. | |
use Log::Log4perl qw(:easy); | |
Log::Log4perl->easy_init($DEBUG); | |
my $gd = Net::Google::Drive::Simple->new(); | |
my $save_csv = 0; | |
my $csv = Text::CSV_XS->new ({ binary => 1 }); | |
my $path_to = $ENV{GOOGLE_DRIVE_PATH_TO} || '0BwTyDSiQofqST3dXRUVaYnJ5Rms/'; | |
my $children = $gd->children( $path_to ); | |
my @list; | |
my @fields = qw(id title createdDate modifiedDate ownerNames alternateLink); | |
$csv->combine(@fields); | |
for my $child ( @$children ) { | |
if ($save_csv) { | |
$csv->combine($child->title, $child->createdDate, $child->modifiedDate, join(',', @{$child->ownerNames}), $child->alternateLink); | |
push @list, $csv->string; | |
} else { | |
my $obj; | |
my $skip_obj; | |
for my $item ( @fields) { | |
# 2017-08-03T05:15:31.718Z | |
$skip_obj = 1 if $child->createdDate !~ /2017/; | |
$obj->{$item} = $child->$item; | |
} | |
next if $skip_obj; | |
push @list, $obj; | |
} | |
} | |
if ($save_csv) { | |
my $fh = new IO::File "> drive.csv"; | |
my ($output) = join("\n", @list) . "\n"; | |
print $fh $output; | |
$fh->close; | |
} else { | |
my $json = to_json(\@list); | |
my $fh = new IO::File "> drive.json"; | |
if (defined $fh) { | |
warn "save."; | |
print $fh $json; | |
$fh->close; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment