-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbnchecker.py
executable file
·125 lines (112 loc) · 5.18 KB
/
nbnchecker.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/env python3
from requests import get
import sys
import os
def main():
checkAddress = input("Enter address to check: ")
# Run the nbnCheck function with the address as an input
addressQueryResult = nbnQueryAddress(checkAddress)
# Print an error if no valid suggestions are returned
if addressQueryResult["validResult"] is False:
print(
"\nThere are no matches for this address. Please check your input and try again.\n"
)
return
else:
# Print the formatted address for the selected address
print("\nYour selected address is: ", addressQueryResult["selectedAddress"])
# Get the details for the returned location ID
locQueryResult = nbnLocDetails(addressQueryResult["locID"])
if locQueryResult["exactMatch"] is True:
# Exact match to an NBN LOC ID, print the specifics for that match
print("\nLOC ID: ", locQueryResult["locID"])
print("Service Status: ", locQueryResult["serviceStatus"])
if locQueryResult["statusMessage"] == "connected-true":
print("An AVC is active at this LOC ID!")
if locQueryResult["statusMessage"] == "connected":
print("This LOC ID is ready for remote AVC provisioning!")
if locQueryResult["coatChangeReason"] == "on-demand":
print(
"On-Demand Fibre Upgrade is available for this LOC ID from",
locQueryResult["patChangeDate"],
"!",
)
else:
# No exact match to an NBN LOC ID, print the serving area details
print(
"\nThere is no exact match in the NBN database for your selected address."
)
print("Serving Area details are as follows.")
print("\nCSA ID: ", locQueryResult["csaID"])
print("Technology Type: ", locQueryResult["techType"])
print("\n")
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()
if len(apiResponse["suggestions"]) > 0:
# We have at least one valid address suggestion
results["validResult"] = True
if len(apiResponse["suggestions"]) != 1:
# More than one suggestion is given, display a list and ask for selection
suggestionsList = apiResponse["suggestions"]
# Generate a list of suggested addresses
print("\nPlease choose your address from the list below: ")
for n, address in enumerate(suggestionsList):
print(f"{n}: ", address["formattedAddress"])
selectedAddressNum = int(input("\nSelection: "))
# Once a suggestion is selected, return the address and LOC ID
results["selectedAddress"] = suggestionsList[selectedAddressNum][
"formattedAddress"
]
results["locID"] = suggestionsList[selectedAddressNum]["id"]
else:
# Only one suggestion is given, return the address and LOC ID immediately
results["selectedAddress"] = apiResponse["suggestions"][0][
"formattedAddress"
]
results["locID"] = apiResponse["suggestions"][0]["id"]
else:
# No suggestions are given, return False for validResult
results["validResult"] = False
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
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nBye!")
try:
sys.exit(1)
except SystemExit:
os._exit(1)