maps.chandlerswift.com/layers/chains/kwik-trip/get_data.py
Chandler Swift 7957523c3c
Distribute data with layers instead of data/ dir
This was originally done to make the gitignoring easier, but ended up
being somewhat more complex when trying to include files, so they're
moving out closer to the point of use.
2023-07-25 19:14:05 -05:00

48 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import requests
import json
from bs4 import BeautifulSoup
response = requests.get('https://www.kwiktrip.com/Maps-Downloads/Store-List')
# HACK HACK HACK
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table') # there's only one, currently; no identifying ID or anything
headers = [th.get_text() for th in table.find('thead').find_all('th')]
# turn it into a reasonable dict
raw_stores = []
for row in table.find('tbody').find_all('tr'):
store = {}
for (header, cell) in zip(headers, row.find_all('td')):
store[header] = cell.get_text()
raw_stores.append(store)
print(f"""{len(raw_stores)} locations found""")
# turn _that_ into GeoJSON Features
stores = []
for store in raw_stores:
stores.append({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(store['Longitude']), float(store['Latitude'])], # yes, [lon, lat] since it's [x, y]
},
"properties": {
'address': store['Address'].title(),
'city': store['City'].title(),
'state': store['State'],
'zip': store['Zip'],
'website': f"https://www.kwiktrip.com/locator/store?id={store['Store Number']}",
},
})
geojson = {
"type": "FeatureCollection",
"features": stores,
}
with open("./data.geojson", "w") as f:
f.write(json.dumps(geojson))