-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrimmDL.py
30 lines (21 loc) · 962 Bytes
/
grimmDL.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
import os
import requests
def download(url: str, dest_folder: str):
if not os.path.exists(dest_folder):
os.makedirs(dest_folder) # create folder if it does not exist
for i in range(1, 101):
num = f"{i:03}"
filename = url + str(num) + ".txt"
save_location = dest_folder + str(num) + ".txt"
r = requests.get(filename, stream=True)
if r.ok:
print("saving to", os.path.abspath(save_location))
with open(save_location, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 8):
if chunk:
f.write(chunk)
f.flush()
os.fsync(f.fileno())
else: # HTTP status code 4XX/5XX
print("Download failed: status code {}\n{}".format(r.status_code, r.text))
download("https://www.cs.cmu.edu/~spok/grimmtmp/", dest_folder="stories/")