iinet_usage.py
· 2.8 KiB · Python
Raw
#!/usr/bin/env python3
from requests import get
from os import environ
from sys import exit, stderr, stdout
STDOUT = stdout
# http://stackoverflow.com/a/1094933
def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
def get_service_data(token=None, service=None):
url = "https://toolbox.iinet.net.au/cgi-bin/api.cgi?Usage&_TOKEN={}&_SERVICE={}"
try:
data = get(url.format(environ["IINET_TOKEN"], environ["IINET_SERVICE"])).json()
return data
except KeyError:
return False
def prompt(p=None):
if p:
stderr.write(str(p))
return input()
def format_usage(data):
try:
quota = data["response"]["usage"]["traffic_types"][0]
qr = data["response"]["quota_reset"]
return "{} remaining ({} per day), {} used. Resets on the {}{}".format(
sizeof_fmt(quota["allocation"] - quota["used"]),
sizeof_fmt((quota["allocation"] - quota["used"]) /
qr["days_remaining"]),
sizeof_fmt(quota["used"]),
qr["anniversary"],
"st" if qr["anniversary"] == 1 else "nd" if qr["anniversary"] == 2 else "rd" if qr["anniversary"] == 3 else "th"
)
except KeyError:
return False
def refresh_token():
print("Tokens need refreshing", file=stderr)
from getpass import getpass
try:
yn = prompt("Want to do that now? [y] ") or "y"
if yn and yn[0].lower() == "y":
user = prompt("Username: ")
westnet = prompt("Westnet account? [n] ") or "n"
if westnet and westnet[0].lower() == "y":
user += "@westnet.com.au"
password = getpass()
data = get("https://toolbox.iinet.net.au/cgi-bin/api.cgi",
params={"_USERNAME": user, "_PASSWORD": password}).json()
if "token" in data:
print("export IINET_TOKEN=" + data["token"])
print("\n".join([ "{}\t{}".format(i, sv["pk_v"]) for i, sv in enumerate(data["response"]["service_list"]) if "Usage" in sv["actions"] ]), file=stderr)
id_num = prompt("Please select a service (using the ID number): ") or ""
if id_num and id_num.isdigit():
print("export IINET_SERVICE=" + data["response"]["service_list"][int(id_num)]["s_token"])
else:
print("Invalid ID number, please try again", file=stderr)
return 1
return 0
else:
raise KeyboardInterrupt
except KeyboardInterrupt:
return 130
def main():
data = format_usage(get_service_data())
if data:
print(data, file=stderr)
exit(0)
else:
exit(refresh_token())
if __name__ == "__main__":
main()
| 1 | #!/usr/bin/env python3 |
| 2 | from requests import get |
| 3 | from os import environ |
| 4 | from sys import exit, stderr, stdout |
| 5 | |
| 6 | STDOUT = stdout |
| 7 | |
| 8 | # http://stackoverflow.com/a/1094933 |
| 9 | def sizeof_fmt(num, suffix='B'): |
| 10 | for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: |
| 11 | if abs(num) < 1024.0: |
| 12 | return "%3.1f%s%s" % (num, unit, suffix) |
| 13 | num /= 1024.0 |
| 14 | return "%.1f%s%s" % (num, 'Yi', suffix) |
| 15 | |
| 16 | def get_service_data(token=None, service=None): |
| 17 | url = "https://toolbox.iinet.net.au/cgi-bin/api.cgi?Usage&_TOKEN={}&_SERVICE={}" |
| 18 | try: |
| 19 | data = get(url.format(environ["IINET_TOKEN"], environ["IINET_SERVICE"])).json() |
| 20 | return data |
| 21 | except KeyError: |
| 22 | return False |
| 23 | |
| 24 | def prompt(p=None): |
| 25 | if p: |
| 26 | stderr.write(str(p)) |
| 27 | return input() |
| 28 | |
| 29 | def format_usage(data): |
| 30 | try: |
| 31 | quota = data["response"]["usage"]["traffic_types"][0] |
| 32 | qr = data["response"]["quota_reset"] |
| 33 | return "{} remaining ({} per day), {} used. Resets on the {}{}".format( |
| 34 | sizeof_fmt(quota["allocation"] - quota["used"]), |
| 35 | sizeof_fmt((quota["allocation"] - quota["used"]) / |
| 36 | qr["days_remaining"]), |
| 37 | sizeof_fmt(quota["used"]), |
| 38 | qr["anniversary"], |
| 39 | "st" if qr["anniversary"] == 1 else "nd" if qr["anniversary"] == 2 else "rd" if qr["anniversary"] == 3 else "th" |
| 40 | ) |
| 41 | except KeyError: |
| 42 | return False |
| 43 | |
| 44 | def refresh_token(): |
| 45 | print("Tokens need refreshing", file=stderr) |
| 46 | from getpass import getpass |
| 47 | try: |
| 48 | yn = prompt("Want to do that now? [y] ") or "y" |
| 49 | if yn and yn[0].lower() == "y": |
| 50 | user = prompt("Username: ") |
| 51 | westnet = prompt("Westnet account? [n] ") or "n" |
| 52 | if westnet and westnet[0].lower() == "y": |
| 53 | user += "@westnet.com.au" |
| 54 | password = getpass() |
| 55 | data = get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", |
| 56 | params={"_USERNAME": user, "_PASSWORD": password}).json() |
| 57 | if "token" in data: |
| 58 | print("export IINET_TOKEN=" + data["token"]) |
| 59 | print("\n".join([ "{}\t{}".format(i, sv["pk_v"]) for i, sv in enumerate(data["response"]["service_list"]) if "Usage" in sv["actions"] ]), file=stderr) |
| 60 | id_num = prompt("Please select a service (using the ID number): ") or "" |
| 61 | if id_num and id_num.isdigit(): |
| 62 | print("export IINET_SERVICE=" + data["response"]["service_list"][int(id_num)]["s_token"]) |
| 63 | else: |
| 64 | print("Invalid ID number, please try again", file=stderr) |
| 65 | return 1 |
| 66 | return 0 |
| 67 | else: |
| 68 | raise KeyboardInterrupt |
| 69 | except KeyboardInterrupt: |
| 70 | return 130 |
| 71 | |
| 72 | def main(): |
| 73 | data = format_usage(get_service_data()) |
| 74 | if data: |
| 75 | print(data, file=stderr) |
| 76 | exit(0) |
| 77 | else: |
| 78 | exit(refresh_token()) |
| 79 | |
| 80 | if __name__ == "__main__": |
| 81 | main() |
zExample.txt
· 480 B · Text
Raw
$ usage >> .bash_profile
Tokens need refreshing
Want to do that now? [y] y
Username: {redacted}
Westnet account? [n] y
Password:
1 {my phone number}
4 {other number 1}
10 {other number 2}
11 {other number 3}
13 {email address}
Please select a service (using the ID number): 1
$ tail -n2 .bash_profile
export IINET_TOKEN=RiceTYp{redacted}
export IINET_SERVICE=90b41e3{redacted}
$ source .bash_profile
$ usage
2.8GiB remaining (282.7MiB per day), 988.0MiB used. Resets on the 19th
$
| 1 | $ usage >> .bash_profile |
| 2 | Tokens need refreshing |
| 3 | Want to do that now? [y] y |
| 4 | Username: {redacted} |
| 5 | Westnet account? [n] y |
| 6 | Password: |
| 7 | 1 {my phone number} |
| 8 | 4 {other number 1} |
| 9 | 10 {other number 2} |
| 10 | 11 {other number 3} |
| 11 | 13 {email address} |
| 12 | Please select a service (using the ID number): 1 |
| 13 | $ tail -n2 .bash_profile |
| 14 | export IINET_TOKEN=RiceTYp{redacted} |
| 15 | export IINET_SERVICE=90b41e3{redacted} |
| 16 | $ source .bash_profile |
| 17 | $ usage |
| 18 | 2.8GiB remaining (282.7MiB per day), 988.0MiB used. Resets on the 19th |
| 19 | $ |