-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
executable file
·67 lines (58 loc) · 2.81 KB
/
api.py
File metadata and controls
executable file
·67 lines (58 loc) · 2.81 KB
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
#!/usr/bin/env python3
from requests import get
def nbnQueryAddress(address: str) -> dict:
# Empty dict to store results
results = {}
# Poke the NBN autocomplete API with the supplied address to check
apiUrl = f"https://places.nbnco.net.au/places/v1/autocomplete?query={address}"
apiResponse = get(apiUrl, headers={"Referer": "https://www.nbnco.com.au"}).json()
# Check if 'suggestions' key exists and is not empty
if "suggestions" in apiResponse and len(apiResponse["suggestions"]) > 0:
# Always take the first suggestion for simplicity in the web UI
first_suggestion = apiResponse["suggestions"][0]
loc_id = first_suggestion["id"]
# Check if the locID starts with "LOC"
if loc_id.startswith("LOC"):
results["validResult"] = True
results["selectedAddress"] = first_suggestion["formattedAddress"]
results["locID"] = loc_id
else:
# Invalid locID format
results["validResult"] = False
results["selectedAddress"] = None
results["locID"] = None
else:
# No suggestions are given, return False for validResult
results["validResult"] = False
results["selectedAddress"] = None
results["locID"] = None
return results
def nbnLocDetails(locID: str) -> dict:
# Empty dict to store results
results = {}
# Poke the NBN details API with the retrieved location ID
apiUrl = f"https://places.nbnco.net.au/places/v2/details/{locID}"
apiResponse = get(apiUrl, headers={"Referer": "https://www.nbnco.com.au"}).json()
# Check for an NBN LOC ID in the response
if "id" in apiResponse["addressDetail"]:
# An NBN LOC ID is present, return the address details
results["exactMatch"] = True
results["locID"] = apiResponse["addressDetail"]["id"]
results["techType"] = apiResponse["addressDetail"]["techType"]
results["serviceStatus"] = apiResponse["addressDetail"]["serviceStatus"]
# This API sucks and only returns this field sometimes, so we have to check for it
if "statusMessage" in apiResponse["addressDetail"]:
results["statusMessage"] = apiResponse["addressDetail"]["statusMessage"]
else:
results["statusMessage"] = ""
results["coatChangeReason"] = apiResponse["addressDetail"]["coatChangeReason"]
if results["coatChangeReason"] != "":
results["patChangeDate"] = apiResponse["addressDetail"]["patChangeDate"]
else:
results["patChangeDate"] = ""
else:
# No NBN LOC ID is present, return a warning and the serving area details
results["exactMatch"] = False
results["csaID"] = apiResponse["servingArea"]["csaId"]
results["techType"] = apiResponse["servingArea"]["techType"]
return results