-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_table.py
executable file
·243 lines (218 loc) · 8.39 KB
/
get_table.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
import csv
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import requests
import sys
HEADERS = {'Authorization' : 'Bearer key1agtUnabRLb2LS', 'accept' : 'text/plain'}
BASE_URL = 'https://api.airtable.com/v0/appj3UWymNh6FgtGR/'
VIEW = 'view=Grid%20view'
# Values related to the place table
PLACE_TABLE_NAME = 'Help%20Services'
PLACE_TABLE_VAR = 'placesTableCached'
PLACE_TABLE_MAP = {
'name' : 'Name',
'name_es' : 'Name-ES',
'catSubcatId' : 'CatSubcat',
'city' : 'City',
'category' : 'Category',
'subcategory' : 'Subcategory',
'phone' : 'Phone Number',
'address' : 'Physical Address',
'latitude' : 'Latitude',
'longitude' : 'Longitude',
'url' : 'Web address',
'email' : 'Email Address',
'hours' : 'Hours of operation',
'hours_es' : 'Hours of operation-ES',
'description' : 'Description',
'description_es' : 'Description-ES',
'wheelchair' : 'Wheelchair access (y)',
'languageHelp' : 'Language Help (y)',
'schedule' : 'schedule',
}
# Values related to the Category table
CATEGORY_TABLE_NAME = 'Categories'
CATEGORY_TABLE_VAR = 'categoryTableCached'
CATEGORY_TABLE_MAP = {
'name' : 'Name',
'name_es' : 'Name-ES',
'subcategories' : 'Subcategories',
}
# Values related to the Subcategory table
SUBCATEGORY_TABLE_NAME = 'Subcategories'
SUBCATEGORY_TABLE_VAR = 'subcategoryTableCached'
SUBCATEGORY_TABLE_MAP = {
'categoryId' : 'Category',
'name' : 'Name',
'name_es' : 'Name-ES',
}
# Values related to the CatSubcat table
CATSUBCAT_TABLE_NAME = 'CatSubcats'
CATSUBCAT_TABLE_VAR = 'catSubcatTableCached'
# Cat Subcat handled specially, so doesn't use table map.
CATSUBCAT_TABLE_MAP = {}
# Values related to the Cities table
CITY_TABLE_NAME = 'Cities'
CITY_TABLE_VAR = 'cityTableCached'
CITY_TABLE_MAP = {
'name' : 'Name',
}
ALERT_TABLE_NAME = 'Alerts'
ALERT_TABLE_VAR = 'alertTableCached'
ALERT_TABLE_MAP = {
'title' : 'Title',
'displayDate' : 'Display Date',
'startDate' : 'StartDate',
'endDate' : 'EndDate',
'note' : 'Notes',
}
SCHEDULE_TABLE_NAME = 'schedule'
SCHEDULE_TABLE_VAR = 'scheduleTableCached'
SCHEDULE_TABLE_MAP = {
'byDay' : 'byday',
'opensAt' : 'opens_at',
'closesAt' : 'closes_at',
'byMonthDay' : 'bymonthday',
'validFrom' : 'valid_from',
'validTo' : 'valid_to',
}
# Make a record in our desired output format
def make_record(record_in, key_pairs):
record_out = {}
record_out["id"] = record_in["id"]
for key in key_pairs:
new_key = key
old_key = key_pairs[new_key]
record_out[new_key] = ''
if old_key in record_in['fields']:
record_out[new_key] = record_in['fields'][old_key]
return record_out
# Make a record in our desired output format -- special handling for
# special CatSubcat features
def make_record_catsubcat(record_in):
catSubcatId = record_in["id"]
catSubcatName = record_in['fields']['Name']
categoryId = record_in['fields']['Category'][0]
subcategoryId = ''
subcategoryName = ''
subcategoryNameSpanish = ''
if 'Subcategory' in record_in['fields']:
subcategoryId = record_in['fields']['Subcategory'][0]
subcategoryName = record_in['fields']['SubcategoryString'][0]
subcategoryNameSpanish = record_in['fields']['Subcategory-ES'][0]
record_out = {
'catSubcatId': catSubcatId,
'catSubcatName': catSubcatName,
'categoryId' : categoryId,
'subcategoryId' : subcategoryId,
'name' : subcategoryName,
'name_es' : subcategoryNameSpanish,
'places' : []
}
return record_out
# Use map to convert a table to the form we want. Note the special code for the
# CatSubcat table because it is structured a little different from the others.
def table_map(table_name, table_raw, key_pairs):
if table_name == CATSUBCAT_TABLE_NAME:
table = map(lambda record: make_record_catsubcat(record), table_raw)
else:
table = map(lambda record: make_record(record, key_pairs), table_raw)
table = list(table)
return table
# Air table only allows pages of up to 100 records at a time. This gets a page
def get_page(url):
r = requests.get(url, headers=HEADERS)
data = r.json()
records = data['records']
offset = None
if 'offset' in data:
offset = data['offset']
return records, offset
def get_table(table_name, mapping):
# Get the table data from air table
table_url = BASE_URL+table_name
page, offset = get_page(table_url+'?'+VIEW)
table_raw = page
# Air table only allows 100 records at a time, so loop to get them all
while offset:
page, offset = get_page(table_url+'?offset='+offset+'&'+VIEW)
table_raw.extend(page)
# Map it into the form we need
table = table_map(table_name, table_raw, mapping)
return table
def do_table(table_name, mapping, var_name, f):
table = get_table(table_name, mapping)
# Write it into the javascript file
print('const', var_name, '=', table, ';', file=f)
def do_mailmerge(dir, place_table, category_table, catsubcat_table, language_str):
mailmerge_table = []
for record in place_table:
catsubcats = record['catSubcatId']
for cat_subcat_id in catsubcats:
# Wrangle the cats and subcats from the catSubcat table
catsubcat_record = list(filter(lambda catsubcat_rec: catsubcat_rec['catSubcatId'] == cat_subcat_id, catsubcat_table))
# Assuming the above returns exactly one record
category_id = catsubcat_record[0]['categoryId']
category_record = list(filter(lambda cat_rec: cat_rec['id'] == category_id, category_table))
# Assuming the above returns exactly one record
category_str = category_record[0]['name'+language_str]
subcategory_str = catsubcat_record[0]['name'+language_str]
mailmerge_table.append({
'Category' : category_str,
'Subcategory' : subcategory_str,
'Service Name' : record['name'],
'Phone Number' : record['phone'],
'Physical Address' : record['address'],
'Hours of operation' : record['hours'],
'Description' : record['description'+language_str],
'Wheelchair access (y)' : record['wheelchair'],
'Language Help (y)' : record['languageHelp'],
'Web address' : record['url'],
'Email Address' : record['email'],
})
mailmerge_table = sorted(mailmerge_table, key = lambda i: (i['Category'], i['Subcategory'], i['Service Name']))
# Save as csv file
csv_columns = list(mailmerge_table[0].keys())
filename = dir+'final_book'+language_str
csv_file = filename+'.csv'
xls_file = filename+'.xls'
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in mailmerge_table:
writer.writerow(data)
except IOError:
print("I/O error")
exit
# Convert the csv file to an Excel file
f = open(csv_file, 'r')
reader = csv.reader(f)
wb = Workbook()
ws = wb.worksheets[0]
ws.title = "Sheet1"
for row_index, row in enumerate(reader):
for column_index, cell in enumerate(row):
column_letter = get_column_letter((column_index + 1))
ws[column_letter+str(row_index+1)] = cell
wb.save(filename = xls_file)
dir = './'
if len(sys.argv) > 1:
dir = sys.argv[1]
if dir[-1] != '/':
dir += '/'
# Process each table
f = open(dir+'cachedInlineTables.js', 'w')
do_table(PLACE_TABLE_NAME, PLACE_TABLE_MAP, PLACE_TABLE_VAR, f)
do_table(CATEGORY_TABLE_NAME, CATEGORY_TABLE_MAP, CATEGORY_TABLE_VAR, f)
do_table(SUBCATEGORY_TABLE_NAME, SUBCATEGORY_TABLE_MAP, SUBCATEGORY_TABLE_VAR, f)
do_table(CATSUBCAT_TABLE_NAME, CATSUBCAT_TABLE_MAP, CATSUBCAT_TABLE_VAR, f)
do_table(CITY_TABLE_NAME, CITY_TABLE_MAP, CITY_TABLE_VAR, f)
do_table(ALERT_TABLE_NAME, ALERT_TABLE_MAP, ALERT_TABLE_VAR, f)
do_table(SCHEDULE_TABLE_NAME, SCHEDULE_TABLE_MAP, SCHEDULE_TABLE_VAR, f)
place_table = get_table(PLACE_TABLE_NAME, PLACE_TABLE_MAP)
category_table = get_table(CATEGORY_TABLE_NAME, CATEGORY_TABLE_MAP)
catsubcat_table = get_table(CATSUBCAT_TABLE_NAME, CATSUBCAT_TABLE_MAP)
do_mailmerge(dir, place_table, category_table, catsubcat_table, '')
do_mailmerge(dir, place_table, category_table, catsubcat_table, '_es')