#!/usr/bin/env python3

import requests
import re
import json

res = requests.get('https://locations.wafflehouse.com/')

content = res.text

locations = re.search(r'<script id="__NEXT_DATA__" type="application/json">(.*)</script>', content)[1]

locations = json.loads(locations)['props']['pageProps']['locations']

stores = []

for location in locations:
    stores.append({
        "type": "Feature",
        "geometry": {
            "type": "Point",
            "coordinates": [float(location['longitude']), float(location['latitude'])], # yes, [lon, lat] since it's [x, y]
        },
        "properties": {
            'address': location['addressLines'][0].title(),
            'city': location['city'].title(),
            'state': location['state'],
            'zip': location['postalCode'],
            'website': location['websiteURL'],
        },
    })

geojson = {
    "type": "FeatureCollection",
    "features": stores,
}

with open("data.geojson", "w") as f:
    f.write(json.dumps(geojson))

print(f"{len(locations)} locations found")