-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
executable file
·317 lines (256 loc) · 11 KB
/
fetch.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
import requests
import argparse
import yaml
import json
import sys
import re
from pathlib import Path
from bs4 import BeautifulSoup
page_cache = {}
def debug(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
def fetch_cache(url, *, mode, force=False, cache_dir: Path):
if mode == 'lxml':
dest = cache_dir / 'html' / (url.replace('/', '_') + '.html')
elif mode == 'json':
dest = cache_dir / 'json' / (url.replace('/', '_') + '.json')
dest.parent.mkdir(exist_ok=True)
if url + mode in page_cache:
debug('using memory cache for', url)
return page_cache[url + mode]
try:
if force:
raise Exception('trigger catch')
with open(dest, 'r', encoding='utf-8') as infile:
body = infile.read()
debug('using cache for', url)
except:
debug('fetching', url)
r = requests.get(url)
body = r.text
if mode == 'lxml':
soup = BeautifulSoup(body, 'lxml')
[t.decompose() for t in soup.find_all('script')]
[t.decompose() for t in soup.find_all('style')]
[t.decompose() for t in soup.find_all('link')]
[t.decompose() for t in soup.find_all('img') if t.attrs['src'].startswith('data:')]
[t.decompose() for t in soup.find_all('input')]
[t.decompose() for t in soup.select('#mapData')]
[t.decompose() for t in soup.select('#footer')]
[t.decompose() for t in soup.select('#carletonBanner')]
body = soup.prettify()
with open(dest, 'w', encoding='utf-8') as outfile:
outfile.write(body)
if mode == 'lxml':
body = BeautifulSoup(body, 'lxml')
elif mode == 'json':
body = json.loads(body)
page_cache[url + mode] = body
return body
def fetch_cache_img(url, *, name, force=False, cache_dir: Path):
dest = cache_dir / 'img' / name
dest.parent.mkdir(exist_ok=True)
try:
if force:
raise Exception('trigger catch')
with open(dest, 'r', encoding='utf-8') as infile:
debug('using cache for', url)
except:
debug('fetching', url)
r = requests.get(url)
with open(dest, 'wb') as outfile:
outfile.write(r.content)
return name
def parse_classes(classes):
categories = set()
for c in classes:
if c == "academicTypeLocation":
categories.add('academic')
elif c == "administrativeTypeLocation":
categories.add('administrative')
elif c == "employeeHousingTypeLocation":
categories.add('employee-housing')
elif c == "studentHousingTypeLocation":
categories.add('student-housing')
return categories
def parse_location_attrs(locationAttributes):
attrs = {
'address': None,
'accessibility': 'unknown',
'floors': [],
'offices': [],
'departments': [],
'description': '',
'nickname': '',
}
for i, thing in enumerate(locationAttributes):
label = None
if i is 0:
label = 'address'
elif i is 1:
label = 'description'
label_el = thing.select_one('.label')
if label_el:
label = label_el.get_text().strip().lower().strip(':')
value = None
if label == 'address':
bits = thing.select('.buildingAttributes li')
if len(bits) >= 1:
value = bits[0].get_text().strip()
if len(bits) == 2:
access_level = 'unknown'
text = bits[1].get_text().strip()
if text == 'Wheelchair Access':
access_level = 'wheelchair'
elif text == 'No Handicap Access':
access_level = 'none'
elif text == 'Unkown':
access_level = 'unknown'
attrs['accessibility'] = access_level
elif label == 'floors':
floors = thing.select('.buildingFloors a')
value = [{'href': f.attrs['href'], 'label': f.get_text().strip()} for f in floors]
value = [f'{v["label"]} <{v["href"]}>' for v in value]
elif label == 'offices':
offices = thing.select('.buildingAttributes a')
value = [{'href': o.attrs['href'], 'label': o.get_text().strip()} for o in offices]
value = [f'{v["label"]} <{v["href"]}>' for v in value]
elif label == 'departments':
depts = thing.select('.buildingAttributes a')
value = [{'href': d.attrs['href'], 'label': d.get_text().strip()} for d in depts]
value = [f'{v["label"]} <{v["href"]}>' for v in value]
elif label == 'description':
description_bits = thing.select('p')
value = "\n\n".join([bit.get_text().strip() for bit in description_bits])
attrs[label] = value
if not attrs['address'] and not attrs['description']:
description_bits = [thing.select('p') for thing in locationAttributes]
value = "\n\n".join([bit.get_text().strip() for thing in description_bits for bit in thing])
attrs['description'] = value
return attrs
def get_features(*, force=False, cache_dir: Path, overrides={}):
urls = [
['building', 'https://apps.carleton.edu/map/types/buildings/',],
['outdoors', 'https://apps.carleton.edu/map/types/outdoors/',],
['athletics', 'https://apps.carleton.edu/map/types/athletics/',],
['parking', 'https://apps.carleton.edu/map/types/parking/',],
]
locations = {}
house_regex = re.compile(r'\bHouse\b')
hall_regex = re.compile(r'\bHall\b')
for category, url in urls:
listing_soup = fetch_cache(url, mode='lxml', force=force, cache_dir=cache_dir)
for location in listing_soup.select('.currentList .locationListing li'):
ident = location.select_one('a').attrs['href'].split('/')[-2]
if ident in locations:
debug('already processed', ident)
locations[ident]['properties']['categories'].add(category)
continue
# check if the entry is schedule for removal
deleted = next((x for x in overrides['removals'] if x['id'] == ident), False) is not False
if deleted:
debug('removing', ident)
continue
# grab the override, if it exists
override = next((x for x in overrides['changes'] if x['id'] == ident), None)
classes = location.attrs.get('class', [])
name = location.get_text().strip()
if override and override.get('name', ''):
name = override['name']
soup = fetch_cache(f'https://apps.carleton.edu/map/{ident}/', force=force, mode='lxml', cache_dir=cache_dir)
categories = parse_classes(classes)
categories.add(category)
if house_regex.search(name):
categories.add('house')
elif hall_regex.search(name):
categories.add('hall')
location_attrs = soup.select('.locationAttribute')
attributes = parse_location_attrs(location_attrs)
if override and override.get('nickname', ''):
nickname = override['nickname']
img = None
img_el = soup.select_one('#locationRepresentativeImage')
if img_el:
link = img_el.select_one('img').attrs['src']
link = link.replace('_tn', '')
img = fetch_cache_img('https://apps.carleton.edu' + link, force=force, name=ident + '.jpg', cache_dir=cache_dir)
json_detail = fetch_cache(
f'https://apps.carleton.edu/map/api/static/?size=1x1&context=1&buildings={ident}&format=json',
mode='json',
force=force,
cache_dir=cache_dir,
)
outline = []
centerpoint = None
if not json_detail.get('error', False):
outline = json_detail['all_building_coords']
outline = [[[p['lon'], p['lat']] for shape in outline for p in shape]]
centerpoint = [json_detail['center_lon'], json_detail['center_lat']]
feature = {
'type': 'Feature',
'id': ident,
'properties': {
'name': name,
'categories': categories,
**attributes,
},
'geometry': {
'type': 'GeometryCollection',
'geometries': []
}
}
if img:
feature['properties']['photos'] = [img]
if override and override.get('outline', []):
outline = override['outline']
if override and override.get('centerpoint', []):
centerpoint = override['centerpoint']
if override and override.get('departments', []):
debug('has override depts', ident)
feature['properties']['departments'] = override['departments']
if override and override.get('offices', []):
feature['properties']['offices'] = override['offices']
if len(outline):
# the first and last positions of at ring of coordinates
# must be the same
for ring in outline:
if ring[0] != ring[-1]:
debug(f'editing ring for {ident}')
ring.append(ring[0])
feature['geometry']['geometries'].append({
'type': 'Polygon',
'coordinates': outline,
})
if centerpoint:
feature['geometry']['geometries'].append({
'type': 'Point',
'coordinates': centerpoint,
})
if not centerpoint and not len(outline):
debug(f'warning: {ident} has no geometry!')
del feature['geometry']
locations[ident] = feature
for location in locations.values():
location['properties']['categories'] = sorted(list(location['properties']['categories']))
return locations
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--force', action='store_true',
help='Force a re-download of all files')
parser.add_argument('--root-dir', action='store', default='./', metavar='DIR',
help='Where to cache the downloaded files')
args = parser.parse_args()
cache_dir = Path(args.root_dir) / 'cache'
overrides_file = Path(args.root_dir) / 'overrides.yaml'
with open(overrides_file, 'r', encoding='utf-8') as infile:
overrides = yaml.safe_load(infile)
features = get_features(force=args.force, cache_dir=cache_dir, overrides=overrides)
feature_collection = {
'type': 'FeatureCollection',
'features': list(features.values()),
}
dump = json.dumps(feature_collection, indent='\t', sort_keys=True, ensure_ascii=False)
print(dump)
if __name__ == '__main__':
main()