Skip to content

Instantly share code, notes, and snippets.

@laranea
Forked from debuti/dbxcli-getr
Created April 6, 2020 14:56
Show Gist options
  • Save laranea/d49ab12258cd7f0c6462ea1fd30cdf03 to your computer and use it in GitHub Desktop.
Save laranea/d49ab12258cd7f0c6462ea1fd30cdf03 to your computer and use it in GitHub Desktop.
Wrap "dbxcli get" command to download files from a dropbox dir recursively
#!/usr/bin/env python3
import os
import sys
import subprocess
import re
import argparse
import hashlib
args = None
def md5(f):
BLOCKSIZE=65536
hasher = hashlib.md5()
with open(f, 'rb') as afile:
buf = afile.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(BLOCKSIZE)
return(hasher.hexdigest())
def get(remote):
dlproc = subprocess.run(["dbxcli", "get", remote], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if args.verbose:
try:
count, order = re.compile('/(\S+)\s(\S+)').match(dlproc.stderr.decode('utf-8')).group(1, 2)
print("Downloaded " + remote +" "+ count + " " + order)
except Exception:
print("Downloaded " + remote)
def getr(remote, local):
localcwd = os.getcwd()
os.chdir(local)
#print("cwd: " + os.getcwd())
regex = re.compile('^(\S+).*/(.+?)\s*$')
proc = subprocess.run(["dbxcli", "ls", "-l", remote], stdout=subprocess.PIPE)
lines = proc.stdout.decode('utf-8').splitlines()
for line in lines[1:]:
obj_id, obj_name = regex.match(line).group(1, 2)
if obj_id == "-":
os.mkdir(obj_name)
if args.verbose: print("Created " + remote+'/'+obj_name)
getr(remote+'/'+obj_name, obj_name)
else:
if args.verify:
hash=None
while True:
get(remote+'/'+obj_name)
chash = md5(obj_name)
if hash == chash:
break
else:
hash=chash
else:
get(remote+'/'+obj_name)
os.chdir(localcwd)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-V', dest='verify', action='store_true', help='Verify downloads (download it several times)')
parser.add_argument('-v', dest='verbose', action='store_true', help='Be verbose')
parser.add_argument('remote', type=str, help='remote_dir')
parser.add_argument('local', type=str, help='local_dir')
args = parser.parse_args()
if args.remote is None or args.local is None:
parser.print_help()
sys.exit(-1)
getr(args.remote, args.local)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment