-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFolderDownloader.py
116 lines (91 loc) · 3.7 KB
/
FolderDownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from dropbox import client, rest, session
import os
import sys
BASE_PATH = os.getcwd()
CHUNK_SIZE = 128 * 1024
APP_KEY = 'APP_KEY'
APP_SECRET = 'APP_SECRET'
ACCESS_TYPE = 'app_folder'
def init_session():
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()
url = sess.build_authorize_url(request_token)
print "Please visit this website and press the 'Allow' button, "
print "then hit 'Enter' here:\n"
print url
raw_input()
access_token = sess.obtain_access_token(request_token)
c = client.DropboxClient(sess)
print "linked account:", c.account_info()
return c
def recursive_get_folder(client, folder):
folder_metadata = client.metadata(folder)
for c in folder_metadata["contents"]:
if c["is_dir"]:
dirname = BASE_PATH + c["path"]
sys.stdout.write("Found directory %s ... " % dirname)
sys.stdout.flush()
if os.path.exists(dirname):
sys.stdout.write("Exists local.\n")
else:
sys.stdout.write("Creating local...")
sys.stdout.flush()
os.makedirs(dirname)
sys.stdout.write("Done.\n")
sys.stdout.flush()
previous_cwd = os.getcwd()
os.chdir(dirname)
recursive_get_folder(client, c["path"])
os.chdir(previous_cwd)
else:
# Check if the file exists
download = True
local_filename = BASE_PATH + c["path"]
sys.stdout.write("Checking %s ... " % c["path"])
sys.stdout.flush()
if os.path.exists(local_filename):
sys.stdout.write("Exists ")
sys.stdout.flush()
# Compare if they are the same
if os.path.getsize(local_filename) == c["bytes"]:
# FIXME: Improve reliability
#if os.path.getmtime(filename) == c.client_mtime:
sys.stdout.write("and are the same.\n")
sys.stdout.flush()
download = False
else:
sys.stdout.write("but aren't the same. ")
else:
sys.stdout.write("Does not exist. ")
sys.stdout.flush()
if download:
# Download the file
sys.stdout.write("\nDownloading.. ")
sys.stdout.flush()
out = open(local_filename, "wb")
f, metadata = client.get_file_and_metadata(c["path"])
total_size = metadata["bytes"]
written = 0
while True:
data = f.read(CHUNK_SIZE)
if data == '':
break
else:
out.write(data)
written = written + len(data)
percent = written * 30 / total_size
#percent_line = "[" + ("*" * percent) +
# (" " * (30 - percent)) +
# "] " + str(written) + "/" +
# str(total_size)
percent_line = "[%s%s] %d/%d" % (("*" * percent),
(" " * (30 - percent)),
written, total_size)
sys.stderr.write(percent_line)
sys.stderr.write("\b" * (len(percent_line)))
sys.stderr.flush()
out.close()
sys.stdout.write("Done. \n")
if __name__ == "__main__":
client = init_session()
recursive_get_folder(client, "/")