maps.chandlerswift.com/layers/chains/kwik-trip/get_data.py

49 lines
1.4 KiB
Python
Raw Normal View History

2023-07-04 01:18:14 -05:00
#!/usr/bin/env python3
import requests
import json
from bs4 import BeautifulSoup
print("Searching for Kwik Trip locations")
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("kwik-trip-data.geojson", "w") as f:
f.write(json.dumps(geojson))