nbn.py
· 1.4 KiB · Python
Raw
#!/usr/bin/env python
import requests # pip install requests, apt install python-requests
from time import time
from argparse import ArgumentParser
from sys import exit
headers = {"Referer": "https://www.nbnco.com.au/"}
parser = ArgumentParser()
parser.add_argument("address", nargs="*")
parser.add_argument("--opts", help="e.g addressDetail.techType,location.id,servingArea.serviceStatus")
args = parser.parse_args()
locations = requests.get("https://places.nbnco.net.au/places/v1/autocomplete", params={"query": args.address, "timestamp": int(time()*1000)}, headers=headers).json()
for n,location in enumerate(locations["suggestions"]):
print("{}: {}".format(n,location["formattedAddress"]))
if len(locations["suggestions"]) != 1:
try:
choice = raw_input("Pick a number> ")
except NameError:
choice = input("Pick a number> ")
else:
choice = 0
location = locations["suggestions"][int(choice)]
nbninfo = requests.get("https://places.nbnco.net.au/places/v1/details/{}".format(location["id"]), headers=headers).json()
if args.opts:
info = {}
for opt in args.opts.split(","):
_opt = opt
while "." in opt:
c,opt = opt.split(".",1)
current = nbninfo[c]
info[_opt] = current[opt]
nbninfo = info
for k,v in nbninfo.items():
if type(v) is dict:
print("{}: {}".format(k,"\n\t".join("{}: {}".format(m,f) for m,f in v.items())))
else:
print("{}: {}".format(k,v))
| 1 | #!/usr/bin/env python |
| 2 | import requests # pip install requests, apt install python-requests |
| 3 | from time import time |
| 4 | from argparse import ArgumentParser |
| 5 | from sys import exit |
| 6 | |
| 7 | headers = {"Referer": "https://www.nbnco.com.au/"} |
| 8 | |
| 9 | parser = ArgumentParser() |
| 10 | parser.add_argument("address", nargs="*") |
| 11 | parser.add_argument("--opts", help="e.g addressDetail.techType,location.id,servingArea.serviceStatus") |
| 12 | args = parser.parse_args() |
| 13 | locations = requests.get("https://places.nbnco.net.au/places/v1/autocomplete", params={"query": args.address, "timestamp": int(time()*1000)}, headers=headers).json() |
| 14 | for n,location in enumerate(locations["suggestions"]): |
| 15 | print("{}: {}".format(n,location["formattedAddress"])) |
| 16 | if len(locations["suggestions"]) != 1: |
| 17 | try: |
| 18 | choice = raw_input("Pick a number> ") |
| 19 | except NameError: |
| 20 | choice = input("Pick a number> ") |
| 21 | else: |
| 22 | choice = 0 |
| 23 | location = locations["suggestions"][int(choice)] |
| 24 | |
| 25 | nbninfo = requests.get("https://places.nbnco.net.au/places/v1/details/{}".format(location["id"]), headers=headers).json() |
| 26 | if args.opts: |
| 27 | info = {} |
| 28 | for opt in args.opts.split(","): |
| 29 | _opt = opt |
| 30 | while "." in opt: |
| 31 | c,opt = opt.split(".",1) |
| 32 | current = nbninfo[c] |
| 33 | info[_opt] = current[opt] |
| 34 | nbninfo = info |
| 35 | for k,v in nbninfo.items(): |
| 36 | if type(v) is dict: |
| 37 | print("{}: {}".format(k,"\n\t".join("{}: {}".format(m,f) for m,f in v.items()))) |
| 38 | else: |
| 39 | print("{}: {}".format(k,v)) |