From: Don Armstrong Date: Thu, 29 May 2014 21:22:43 +0000 (-0700) Subject: add script to recursively download from dropbox X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c82b0d9020aa465404d8e1bcaac2004336b1909b;hp=feefa951517b118d6455f52e20ae1001116c2c60;p=bin.git add script to recursively download from dropbox --- diff --git a/dropbox_recursive b/dropbox_recursive new file mode 100755 index 0000000..673b212 --- /dev/null +++ b/dropbox_recursive @@ -0,0 +1,117 @@ +#!/usr/bin/perl +# dropbox_recursive recursively downloads files from dropbox +# and is released under the terms of the GNU GPL version 3, or any +# later version, at your option. See the file README and COPYING for +# more information. +# Copyright 2014 by Don Armstrong . + + +use warnings; +use strict; + +use Getopt::Long; +use Pod::Usage; + +=head1 NAME + +dropbox_recursive - recursively downloads files from dropbox + +=head1 SYNOPSIS + +dropbox_recursive [options] dropboxurl1 [additional dropbox urls] + + Options: + --debug, -d debugging level (Default 0) + --help, -h display this help + --man, -m display manual + +=head1 OPTIONS + +=over + +=item B<--debug, -d> + +Debug verbosity. (Default 0) + +=item B<--help, -h> + +Display brief usage information. + +=item B<--man, -m> + +Display this manual. + +=back + +=head1 EXAMPLES + +dropbox_recursive https://www.dropbox.com/sh/foo/bar + +=cut + + +use vars qw($DEBUG); + +my %options = (debug => 0, + help => 0, + man => 0, + ); + +GetOptions(\%options, + 'debug|d+','help|h|?','man|m'); + +pod2usage() if $options{help}; +pod2usage({verbose=>2}) if $options{man}; + +$DEBUG = $options{debug}; + +use URI::Escape; +use WWW::Mechanize; + +my @USAGE_ERRORS; +if (not @ARGV) { + push @USAGE_ERRORS,"You must give at least one dropbox url"; +} + +pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; + + + +my $mech = WWW::Mechanize->new(); +for my $url (@ARGV) { + $mech->get($url); + download_url($mech); +} + +sub download_url { + my ($m) = @_; + + + my @links = $m->find_all_links(class=>"filename-link"); + print STDERR map {$_->url()."\n"} @links; + for my $link (@links) { + $m->get($link->url()); + my ($name) = $m->uri() =~ m{/([^/]+)$}; + $name = uri_unescape($name); + # see if there is a download button + my @download_button = $m->find_all_links(id=>'default_content_download_button'); + # If there's a download button, then we've visted the link for a + # file; save it. + if (@download_button) { + print STDERR "getting file $name\n"; + $m->get($download_button[0]->url(),':content_file'=>$name); + print STDERR "got file $name\n"; + } else { + # there isn't a download button, so we must have visited + # the link for a directory + mkdir $name unless -d $name; + print STDERR "made directory $name\n"; + chdir $name or die "Unable to chdir to $name"; + download_url($m); + chdir '..'; + } + } +} + + +__END__