"""
This file hits the geolocation API, gets the json response and then saves it 
to a file.
"""

import json
import os
import requests

station_names = [
    "Allawah",
    "Arncliffe",
    "Artarmon",
    "Ashfield",
    "Asquith",
    "Auburn",
    "Banksia",
    "Bankstown",
    "Bardwell Park",
    "Beecroft",
    "Belmore",
    "Berala",
    "Berowra",
    "Beverly Hills",
    "Bexley North",
    "Birrong",
    "Blacktown",
    "Bondi Junction",
    "Burwood",
    "Cabramatta",
    "Campbelltown",
    "Campsie",
    "Canley Vale",
    "Canterbury",
    "Caringbah",
    "Carlton",
    "Carramar",
    "Casula",
    "Central",
    "Chatswood",
    "Cheltenham",
    "Chester Hill",
    "Circular Quay",
    "Clarendon",
    "Clyde",
    "Como",
    "Concord West",
    "Cronulla",
    "Croydon",
    "Denistone",
    "Domestic Airport",
    "Doonside",
    "Dulwich Hill",
    "East Hills",
    "East Richmond",
    "Eastwood",
    "Edgecliff",
    "Edmondson Park",
    "Emu Plains",
    "Engadine",
    "Epping",
    "Erskineville",
    "Fairfield",
    "Flemington",
    "Glenfield",
    "Gordon",
    "Granville",
    "Green Square",
    "Guildford",
    "Gymea",
    "Harris Park",
    "Heathcote",
    "Helensburgh",
    "Holsworthy",
    "Homebush",
    "Hornsby",
    "Hurlstone Park",
    "Hurstville",
    "Ingleburn",
    "International Airport",
    "Jannali",
    "Killara",
    "Kings Cross",
    "Kingsgrove",
    "Kingswood",
    "Kirrawee",
    "Kogarah",
    "Lakemba",
    "Leightonfield",
    "Leppington",
    "Leumeah",
    "Lewisham",
    "Lidcombe",
    "Lindfield",
    "Liverpool",
    "Loftus",
    "Macarthur",
    "Macdonaldtown",
    "Macquarie Fields",
    "Marayong",
    "Marrickville",
    "Martin Place",
    "Mascot",
    "Meadowbank",
    "Merrylands",
    "Milsons Point",
    "Minto",
    "Miranda",
    "Mortdale",
    "Mount Colah",
    "Mount Druitt",
    "Mount Kuring-gai",
    "Mulgrave",
    "Museum",
    "Narwee",
    "Newtown",
    "Normanhurst",
    "North Strathfield",
    "North Sydney",
    "Oatley",
    "Olympic Park",
    "Padstow",
    "Panania",
    "Parramatta",
    "Pendle Hill",
    "Pennant Hills",
    "Penrith",
    "Penshurst",
    "Petersham",
    "Punchbowl",
    "Pymble",
    "Quakers Hill",
    "Redfern",
    "Regents Park",
    "Revesby",
    "Rhodes",
    "Richmond",
    "Riverstone",
    "Riverwood",
    "Rockdale",
    "Rooty Hill",
    "Roseville",
    "Schofields",
    "Sefton",
    "Seven Hills",
    "St James",
    "St Leonards",
    "St Marys",
    "St Peters",
    "Stanmore",
    "Strathfield",
    "Summer Hill",
    "Sutherland",
    "Sydenham",
    "Tempe",
    "Thornleigh",
    "Toongabbie",
    "Town Hall",
    "Turramurra",
    "Turrella",
    "Villawood",
    "Vineyard",
    "Wahroonga",
    "Waitara",
    "Warrawee",
    "Warwick Farm",
    "Waterfall",
    "Waverton",
    "Wentworthville",
    "Werrington",
    "West Ryde",
    "Westmead",
    "Wiley Park",
    "Windsor",
    "Wolli Creek",
    "Wollstonecraft",
    "Woolooware",
    "Wynyard",
    "Yagoona",
    "Yennora",
]

problem_station_names = [
    "Artarmon",
    "Auburn",
    "Banksia",
    "Bardwell Park",
    "Bexley North",
    "Birrong",
    "Caringbah",
    "Carramar",
    "Central",
    "Cheltenham",
    "Denistone",
    "East Richmond",
    "Eastwood",
    "Edgecliff",
    "Engadine",
    "Green Square",
    "Hornsby",
    "Hurlstone Park",
    "Hurstville",
    "Kings Cross",
    "Kirrawee",
    "Macarthur",
    "Martin Place",
    "Milsons Point",
    "Miranda",
    "Mortdale",
    "Mount Colah",
    "Mount Druitt",
    "Narwee",
    "Penrith",
    "Richmond",
    "Riverwood",
    "Schofields",
    "St James",
    "Sutherland",
    "Tempe",
    "Turramurra",
    "Vineyard",
    "Waverton",
    "Wentworthville",
    "Windsor",
    "Wolli Creek",
]

key = "AIzaSyCCNNwCQCx4yG60KJIFR8xzggoBCCNCnqw"
base = "https://maps.googleapis.com/maps/api/geocode"
response = "json"
data = []
for name in problem_station_names:
    address = f"{name} Train Station, NSW".replace(" ", "+")
    geo_url = f"{base}/{response}?address={address}&key={key}"
    print(geo_url)
    r = requests.get(geo_url)
    json_data = r.json()
    if not json_data.get("error_message"):
        data.append(json_data)
    else:
        json_data["url"] = geo_url
        json_data["name"] = name
        data.append({json_data})


with open("problem_train_station_data.json", "w") as ts:
    ts.write(json.dumps(data, indent=2))
