-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairtable-dl-beta2.py
56 lines (48 loc) · 1.63 KB
/
airtable-dl-beta2.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
import requests
import os
import sys
import json
from datetime import datetime
from dateutil import parser as date_parser
from urllib.parse import urlparse
with open(sys.argv[1]) as json_data:
data = json.load(json_data)
cs_dir = 'Consent Forms'
if not os.path.isdir(cs_dir):
os.mkdir(cs_dir)
sub_dir = 'Submissions'
if not os.path.isdir(sub_dir):
os.mkdir(sub_dir)
# Download file from URL
def download_files(url, filename):
response = requests.get(url)
modified_header = response.headers.get('Last-Modified')
# Maintain date stamps from original upload to AirTable
if modified_header:
modified_datetime = date_parser.parse(modified_header)
modified_timestamp = modified_datetime.timestamp()
else:
modified_timestamp = None
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filename, "wb") as file:
file.write(response.content)
os.utime(filename, (modified_timestamp, modified_timestamp))
for entry in data:
if 'export' in entry:
continue
consent_forms = entry.get("Consent Forms", [])
identifier = entry["Identifier"]
for form in consent_forms:
filename = form.get("filename")
url = form.get("url")
if entry.get("Export") == True:
download_files(url, "Consent Forms/" + identifier + "_" + filename)
print(f"Downloaded: {filename}")
submission = entry.get("Submission", [])
for form in submission:
filename = form.get("filename")
url = form.get("url")
if entry.get("Export") == True:
download_files(url, "Submissions/" + identifier + "_" + filename)
print(f"Downloaded: {filename}")