-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_data.py
61 lines (53 loc) · 2.02 KB
/
get_data.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
import ftplib
import os
import urllib.request
from pathlib import Path
from urllib.parse import urlparse
def get_example_data():
cwd = Path(os.getcwd()).resolve()
# If in icon-vis directory, get icon-vis root path,
# otherwise data will be downloaded in data/example_data folder within current directory.
if "icon-vis" in cwd.parts:
while True:
if cwd.parts[-1] == "icon-vis":
break
cwd = cwd.parent
# Make data directory if doesn't exist
data_dir = cwd / "data"
if not data_dir.exists():
os.mkdir(data_dir)
ftp_path = "ftp://iacftp.ethz.ch/pub_read/alauber/"
url_parts = urlparse(ftp_path)
domain = url_parts.netloc
path = url_parts.path
ftp = ftplib.FTP(domain)
ftp.login()
ftp.cwd(path)
filenames = ftp.nlst()
example_data_dir = Path(data_dir, "example_data")
try:
if not example_data_dir.exists():
os.mkdir(example_data_dir)
for directory in filenames:
try:
ftp.cwd(directory)
print(" ")
print("Getting data from folder: " + str(directory))
filenames = ftp.nlst()
for file_to_retrieve in filenames:
target_dir = Path(example_data_dir, directory)
if not target_dir.exists():
os.mkdir(target_dir)
target_loc = Path(target_dir, file_to_retrieve)
if not target_loc.is_file():
source_file = ftp_path + directory + "/" + file_to_retrieve
print(str(source_file) + " --> " + str(target_loc))
urllib.request.urlretrieve(source_file, str(target_loc))
ftp.cwd(path)
except ftplib.error_perm:
continue # It's probably not a directory.
ftp.quit()
except Exception:
raise Exception("""Getting example data from FTP server failed.""")
if __name__ == "__main__":
get_example_data()