| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Net::Google::Drive::Simple::Mirror; | ||
| 4 | use Net::Google::Drive::Simple; | ||
| 5 | use DateTime::Format::RFC3339; | ||
| 6 | use DateTime; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | our $VERSION = '0.53'; | ||
| 10 | |||
| 11 | sub new{ | ||
| 12 | my ($class, %options) = @_; | ||
| 13 | |||
| 14 | croak "Local folder '$options{local_root}' not found" | ||
| 15 | unless -d $options{local_root}; | ||
| 16 | $options{local_root} .= '/' | ||
| 17 | unless $options{local_root} =~ m{/$}; | ||
| 18 | |||
| 19 | my $gd = Net::Google::Drive::Simple->new(); | ||
| 20 | $options{remote_root} = '/'.$options{remote_root} | ||
| 21 | unless $options{remote_root} =~ m{^/}; | ||
| 22 | # XXX To support slashes in folder names in remote_root, I would have | ||
| 23 | # to implement a different remote_root lookup mechanism here: | ||
| 24 | my (undef, $remote_root_ID) = $gd->children( $options{remote_root}); | ||
| 25 | |||
| 26 | my $self = { | ||
| 27 | remote_root_ID => $remote_root_ID, | ||
| 28 | export_format => ['opendocument', 'html'], | ||
| 29 | download_condition => \&_should_download, | ||
| 30 | force => undef, # XXX move this to mirror() | ||
| 31 | net_google_drive_simple => $gd, | ||
| 32 | |||
| 33 | %options | ||
| 34 | }; | ||
| 35 | |||
| 36 | bless $self, $class; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub mirror{ | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | _process_folder( | ||
| 43 | $self, | ||
| 44 | $self->{remote_root_ID}, | ||
| 45 | $self->{local_root} | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _process_folder{ | ||
| 50 | my ($self, $folder_id, $path) = @_; | ||
| 51 | my $gd = $self->{net_google_drive_simple}; | ||
| 52 | my $children = $gd->children_by_folder_id($folder_id); | ||
| 53 | |||
| 54 | for my $child (@$children){ | ||
| 55 | my $file_name = $child->title(); | ||
| 56 | $file_name =~ s{/}{_}; | ||
| 57 | my $local_file = $path.$file_name; | ||
| 58 | |||
| 59 | # a google document: export to preferred format | ||
| 60 | if ($child->can( "exportLinks" )){ | ||
| 61 | next unless $self->{download_condition} | ||
| 62 | ->($self, $child, $local_file); | ||
| 63 | print "$path$file_name ..exporting\n"; | ||
| 64 | |||
| 65 | my $type; | ||
| 66 | FOUND: | ||
| 67 | foreach my $preferred_type (@{$self->{export_format}}){ | ||
| 68 | foreach my $t (keys %{$child->exportLinks()}){ | ||
| 69 | $type = $t; | ||
| 70 | last FOUND if $t =~ /$preferred_type/; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | my $url = $child->exportLinks()->{$type}; | ||
| 75 | $gd->download($url, $local_file); | ||
| 76 | next; | ||
| 77 | } | ||
| 78 | |||
| 79 | # pdfs and the like get downloaded directly | ||
| 80 | if ($child->can( "downloadUrl" )){ | ||
| 81 | next unless $self->{download_condition} | ||
| 82 | ->($self, $child, $local_file); | ||
| 83 | print "$path$file_name ..downloading\n"; | ||
| 84 | $gd->download( $child, $local_file); | ||
| 85 | next; | ||
| 86 | } | ||
| 87 | |||
| 88 | # if we reach this, we could not "fetch" the file. A dir, then.. | ||
| 89 | mkdir ($path.$file_name) | ||
| 90 | unless -d $path.$file_name; | ||
| 91 | |||
| 92 | _process_folder($self, $child->id(), $path.$file_name.'/'); | ||
| 93 | |||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | sub _should_download{ | ||
| 99 | my ($self, $remote_file, $local_file) = @_; | ||
| 100 | |||
| 101 | return 1 if $self->{force}; | ||
| 102 | |||
| 103 | my $date_time_parser = DateTime::Format::RFC3339->new(); | ||
| 104 | |||
| 105 | my $local_epoch = (stat($local_file))[9]; | ||
| 106 | my $remote_epoch = $date_time_parser | ||
| 107 | ->parse_datetime($remote_file->modifiedDate()) | ||
| 108 | ->epoch(); | ||
| 109 | |||
| 110 | if (-f $local_file and $remote_epoch < $local_epoch ){ | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | return 1; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ |