App-dropboxapi

 view release on metacpan or  search on metacpan

script/dropbox-api  view on Meta::CPAN

        $help = qq{
        Usage: dropbox-api <command> [args] [options]

        Available commands:
            setup get access_key and access_secret
            ls    list directory contents
            find  walk a file hierarchy
            du    disk usage statistics
            cp    copy file or directory
            mv    move file or directory
            mkdir make directory (Create intermediate directories as required)
            rm    remove file or directory (Attempt to remove the file hierarchy rooted in each file argument)
            put   upload file
            get   download file
            sync  sync directory (local => dropbox or dropbox => local)

        Common Options
            -e enable env_proxy ( HTTP_PROXY, NO_PROXY )
            -D enable debug
            -v verbose

        See 'dropbox-api help <command>' for more information on a specific command.
        };
    }
    $help =~ s|^ {8}||mg;
    $help =~ s|^\s*\n||;
    print "\n$help\n";
}

sub setup {
    my $config = {};

    print "Please Input API Key: ";
    chomp( my $key = <STDIN> );
    die 'Get API Key from https://www.dropbox.com/developers' unless $key;
    $config->{key} = $key;

    print "Please Input API Secret: ";
    chomp( my $secret = <STDIN> );
    die 'Get API Secret from https://www.dropbox.com/developers' unless $secret;
    $config->{secret} = $secret;

    my $box = WebService::Dropbox->new($config);
    $box->env_proxy if $env_proxy;
    my $login_link = $box->authorize;
    die $box->error if $box->error;
    print "1. Open the Login URL: $login_link\n";
    print "2. Input code and press Enter: ";
    chomp( my $code = <STDIN> );
    unless ($box->token($code)) {
        die $box->error;
    }

    $config->{access_token} = $box->access_token;
    print "success! try\n";
    print "> dropbox-api ls\n";
    print "> dropbox-api find /\n";

    $config_file->openw->print(encode_json($config));

    chmod 0600, $config_file;

    exit(0);
}

sub du {
    my $remote_base = decode('locale_fs', slash(shift));
    $remote_base =~ s|/$||;
    my $entries = _find($remote_base);
    my $dir_map = {};
    for my $content (@{ $entries }) {
        if ($content->{'.tag'} eq 'folder') {
            next;
        }
        my @paths = _paths($remote_base, $content->{path_lower});
        for my $path (@paths) {
            $dir_map->{ lc $path } ||= 0;
            $dir_map->{ lc $path } += $content->{size};
        }
    }
    if (!$remote_base) {
        my $size = $dir_map->{'/'} || 0;
        if ($human) {
            $size = format_bytes($size);
        }
        printf("%s\t%s\n", $size, '/');
    }
    for my $content (@{ $entries }) {
        if ($content->{'.tag'} ne 'folder') {
            next;
        }
        if (defined $max_depth) {
            my $path = $content->{path_lower};
            $path =~ s|^\Q$remote_base\E/?||i;
            my $depth = $path ? scalar(split('/', $path)) : 0;
            if ($depth > $max_depth) {
                next;
            }
        }
        my $size = $dir_map->{ lc $content->{path_lower} } || 0;
        if ($human) {
            $size = format_bytes($size);
        }
        printf("%s\t%s\n", $size, $content->{path_display});
    }
}

sub _paths ($$) {
    my ($base_path, $path) = @_;
    $path =~ s|^\Q$base_path\E/?||i;
    my @paths;
    my $dir = $base_path || '/';
    push @paths, $dir;
    my @names = split '/', $path;
    pop @names;
    for my $name (@names) {
        if ($dir ne '/') {
            $dir .= '/';
        }
        $dir .= $name;
        push @paths, $dir;



( run in 1.029 second using v1.01-cache-2.11-cpan-39bf76dae61 )