Skip to content

Instantly share code, notes, and snippets.

@debuti
Forked from shadiakiki1986/README.md
Last active December 20, 2022 02:18
Show Gist options
  • Save debuti/5887c126811eeae1bf9451e73a7b8fd8 to your computer and use it in GitHub Desktop.
Save debuti/5887c126811eeae1bf9451e73a7b8fd8 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)
@aharwood2
Copy link

This is great, thanks!

As this uses subprocess.run this will only work in Python 3.5+. I used this workaround for anyone interested. In addition, you need to change the line proc.stdout.decode to proc.[1].decode as the workaround returns a tuple.

@shadiakiki1986
Copy link

shadiakiki1986 commented Jul 27, 2021

Nice code! I just integrated it into https://github.com/shadiakiki1986/dbxcli-extras

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment