]> git.donarmstrong.com Git - bin.git/blob - dropbox_recursive
add reset usb bus command
[bin.git] / dropbox_recursive
1 #!/usr/bin/perl
2 # dropbox_recursive recursively downloads files from dropbox
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
5 # more information.
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 dropbox_recursive - recursively downloads files from dropbox
18
19 =head1 SYNOPSIS
20
21 dropbox_recursive [options] dropboxurl1 [additional dropbox urls]
22
23  Options:
24    --debug, -d debugging level (Default 0)
25    --help, -h display this help
26    --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48 dropbox_recursive https://www.dropbox.com/sh/foo/bar
49
50 =cut
51
52
53 use vars qw($DEBUG);
54
55 my %options = (debug           => 0,
56                help            => 0,
57                man             => 0,
58               );
59
60 GetOptions(\%options,
61            'debug|d+','help|h|?','man|m');
62
63 pod2usage() if $options{help};
64 pod2usage({verbose=>2}) if $options{man};
65
66 $DEBUG = $options{debug};
67
68 use URI::Escape;
69 use WWW::Mechanize;
70
71 my @USAGE_ERRORS;
72 if (not @ARGV) {
73     push @USAGE_ERRORS,"You must give at least one dropbox url";
74 }
75
76 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
77
78
79
80 my $mech = WWW::Mechanize->new();
81 for my $url (@ARGV) {
82     $mech->get($url);
83     download_url($mech);
84 }
85
86 sub download_url {
87     my ($m) = @_;
88
89
90     my @links = $m->find_all_links(class=>"filename-link");
91     print STDERR map {$_->url()."\n"} @links;
92     for my $link (@links) {
93         $m->get($link->url());
94         my ($name) = $m->uri() =~ m{/([^/]+)$};
95         $name = uri_unescape($name);
96         # see if there is a download button
97         my @download_button = $m->find_all_links(id=>'default_content_download_button');
98         # If there's a download button, then we've visted the link for a
99         # file; save it.
100         if (@download_button) {
101             print STDERR "getting file $name\n";
102             $m->get($download_button[0]->url(),':content_file'=>$name);
103             print STDERR "got file $name\n";
104         } else {
105             # there isn't a download button, so we must have visited
106             # the link for a directory
107             mkdir $name unless -d $name;
108             print STDERR "made directory $name\n";
109             chdir $name or die "Unable to chdir to $name";
110             download_url($m);
111             chdir '..';
112         }
113     }
114 }
115
116
117 __END__