Steven Smith revised this gist . Go to revision
2 files changed, 13 insertions, 4 deletions
iinet_usage.py
| @@ -28,8 +28,16 @@ def prompt(p=None): | |||
| 28 | 28 | ||
| 29 | 29 | def format_usage(data): | |
| 30 | 30 | try: | |
| 31 | - | data = data["response"]["usage"]["traffic_types"][0] | |
| 32 | - | return "{} remaining ({} used)".format(sizeof_fmt(data["allocation"] - data["used"]), sizeof_fmt(data["used"])) | |
| 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 | + | ) | |
| 33 | 41 | except KeyError: | |
| 34 | 42 | return False | |
| 35 | 43 | ||
| @@ -44,7 +52,8 @@ def refresh_token(): | |||
| 44 | 52 | if westnet and westnet[0].lower() == "y": | |
| 45 | 53 | user += "@westnet.com.au" | |
| 46 | 54 | password = getpass() | |
| 47 | - | data = get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", params={"_USERNAME": user, "_PASSWORD": password}).json() | |
| 55 | + | data = get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", | |
| 56 | + | params={"_USERNAME": user, "_PASSWORD": password}).json() | |
| 48 | 57 | if "token" in data: | |
| 49 | 58 | print("export IINET_TOKEN=" + data["token"]) | |
| 50 | 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) | |
zExample.txt
| @@ -15,5 +15,5 @@ export IINET_TOKEN=RiceTYp{redacted} | |||
| 15 | 15 | export IINET_SERVICE=90b41e3{redacted} | |
| 16 | 16 | $ source .bash_profile | |
| 17 | 17 | $ usage | |
| 18 | - | 3.4GiB remaining (286.8MiB used) | |
| 18 | + | 2.8GiB remaining (282.7MiB per day), 988.0MiB used. Resets on the 19th | |
| 19 | 19 | $ | |
Steven Smith revised this gist . Go to revision
2 files changed, 42 insertions, 14 deletions
iinet_usage.py
| @@ -13,15 +13,19 @@ def sizeof_fmt(num, suffix='B'): | |||
| 13 | 13 | num /= 1024.0 | |
| 14 | 14 | return "%.1f%s%s" % (num, 'Yi', suffix) | |
| 15 | 15 | ||
| 16 | - | def get_service_data(token=environ["IINET_TOKEN"], service=environ["IINET_SERVICE"]): | |
| 16 | + | def get_service_data(token=None, service=None): | |
| 17 | 17 | url = "https://toolbox.iinet.net.au/cgi-bin/api.cgi?Usage&_TOKEN={}&_SERVICE={}" | |
| 18 | 18 | try: | |
| 19 | 19 | data = get(url.format(environ["IINET_TOKEN"], environ["IINET_SERVICE"])).json() | |
| 20 | 20 | return data | |
| 21 | 21 | except KeyError: | |
| 22 | - | print("set environ vars pls", file=stderr) | |
| 23 | 22 | return False | |
| 24 | 23 | ||
| 24 | + | def prompt(p=None): | |
| 25 | + | if p: | |
| 26 | + | stderr.write(str(p)) | |
| 27 | + | return input() | |
| 28 | + | ||
| 25 | 29 | def format_usage(data): | |
| 26 | 30 | try: | |
| 27 | 31 | data = data["response"]["usage"]["traffic_types"][0] | |
| @@ -30,24 +34,31 @@ def format_usage(data): | |||
| 30 | 34 | return False | |
| 31 | 35 | ||
| 32 | 36 | def refresh_token(): | |
| 33 | - | print("token needs refreshing", file=stderr) | |
| 37 | + | print("Tokens need refreshing", file=stderr) | |
| 34 | 38 | from getpass import getpass | |
| 35 | 39 | try: | |
| 36 | - | stdout = stderr | |
| 37 | - | yn = input("Want to do that now? [y] ") or "y" | |
| 40 | + | yn = prompt("Want to do that now? [y] ") or "y" | |
| 38 | 41 | if yn and yn[0].lower() == "y": | |
| 39 | - | user = input("Username: ") | |
| 40 | - | westnet = input("Westnet account? [n] ") or "n" | |
| 42 | + | user = prompt("Username: ") | |
| 43 | + | westnet = prompt("Westnet account? [n] ") or "n" | |
| 41 | 44 | if westnet and westnet[0].lower() == "y": | |
| 42 | 45 | user += "@westnet.com.au" | |
| 43 | - | stdout = STDOUT | |
| 44 | 46 | password = getpass() | |
| 45 | - | print("Token:", file=stderr) | |
| 46 | - | return get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", params={"_USERNAME": user, "_PASSWORD": password}).json()["token"], 0 | |
| 47 | + | data = get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", params={"_USERNAME": user, "_PASSWORD": password}).json() | |
| 48 | + | if "token" in data: | |
| 49 | + | print("export IINET_TOKEN=" + data["token"]) | |
| 50 | + | 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) | |
| 51 | + | id_num = prompt("Please select a service (using the ID number): ") or "" | |
| 52 | + | if id_num and id_num.isdigit(): | |
| 53 | + | print("export IINET_SERVICE=" + data["response"]["service_list"][int(id_num)]["s_token"]) | |
| 54 | + | else: | |
| 55 | + | print("Invalid ID number, please try again", file=stderr) | |
| 56 | + | return 1 | |
| 57 | + | return 0 | |
| 47 | 58 | else: | |
| 48 | 59 | raise KeyboardInterrupt | |
| 49 | 60 | except KeyboardInterrupt: | |
| 50 | - | return "", 130 | |
| 61 | + | return 130 | |
| 51 | 62 | ||
| 52 | 63 | def main(): | |
| 53 | 64 | data = format_usage(get_service_data()) | |
| @@ -55,9 +66,7 @@ def main(): | |||
| 55 | 66 | print(data, file=stderr) | |
| 56 | 67 | exit(0) | |
| 57 | 68 | else: | |
| 58 | - | pl, retcode = refresh_token() | |
| 59 | - | print(pl) | |
| 60 | - | exit(retcode) | |
| 69 | + | exit(refresh_token()) | |
| 61 | 70 | ||
| 62 | 71 | if __name__ == "__main__": | |
| 63 | 72 | main() | |
zExample.txt(file created)
| @@ -0,0 +1,19 @@ | |||
| 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 | + | 3.4GiB remaining (286.8MiB used) | |
| 19 | + | $ | |
Steven Smith revised this gist . Go to revision
1 file changed, 63 insertions
iinet_usage.py(file created)
| @@ -0,0 +1,63 @@ | |||
| 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=environ["IINET_TOKEN"], service=environ["IINET_SERVICE"]): | |
| 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 | + | print("set environ vars pls", file=stderr) | |
| 23 | + | return False | |
| 24 | + | ||
| 25 | + | def format_usage(data): | |
| 26 | + | try: | |
| 27 | + | data = data["response"]["usage"]["traffic_types"][0] | |
| 28 | + | return "{} remaining ({} used)".format(sizeof_fmt(data["allocation"] - data["used"]), sizeof_fmt(data["used"])) | |
| 29 | + | except KeyError: | |
| 30 | + | return False | |
| 31 | + | ||
| 32 | + | def refresh_token(): | |
| 33 | + | print("token needs refreshing", file=stderr) | |
| 34 | + | from getpass import getpass | |
| 35 | + | try: | |
| 36 | + | stdout = stderr | |
| 37 | + | yn = input("Want to do that now? [y] ") or "y" | |
| 38 | + | if yn and yn[0].lower() == "y": | |
| 39 | + | user = input("Username: ") | |
| 40 | + | westnet = input("Westnet account? [n] ") or "n" | |
| 41 | + | if westnet and westnet[0].lower() == "y": | |
| 42 | + | user += "@westnet.com.au" | |
| 43 | + | stdout = STDOUT | |
| 44 | + | password = getpass() | |
| 45 | + | print("Token:", file=stderr) | |
| 46 | + | return get("https://toolbox.iinet.net.au/cgi-bin/api.cgi", params={"_USERNAME": user, "_PASSWORD": password}).json()["token"], 0 | |
| 47 | + | else: | |
| 48 | + | raise KeyboardInterrupt | |
| 49 | + | except KeyboardInterrupt: | |
| 50 | + | return "", 130 | |
| 51 | + | ||
| 52 | + | def main(): | |
| 53 | + | data = format_usage(get_service_data()) | |
| 54 | + | if data: | |
| 55 | + | print(data, file=stderr) | |
| 56 | + | exit(0) | |
| 57 | + | else: | |
| 58 | + | pl, retcode = refresh_token() | |
| 59 | + | print(pl) | |
| 60 | + | exit(retcode) | |
| 61 | + | ||
| 62 | + | if __name__ == "__main__": | |
| 63 | + | main() | |