Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
westnetusage.py
| @@ -1,5 +1,5 @@ | |||
| 1 | 1 | #!/usr/bin/env python2.7 | |
| 2 | - | version = "r5" | |
| 2 | + | version = "r7" | |
| 3 | 3 | # This script gets usage from my ISP's website, checks to see if the current average for the month | |
| 4 | 4 | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 5 | 5 | # I made this for a household of people who don't understand the consequences of their actions | |
Steven Smith revised this gist . Go to revision
1 file changed, 39 insertions, 37 deletions
westnetusage.py
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | #!/usr/bin/env python2.7 | |
| 2 | 2 | version = "r5" | |
| 3 | - | # This script gets usage from my ISP's website, checks to see if the current average for the month | |
| 3 | + | # This script gets usage from my ISP's website, checks to see if the current average for the month | |
| 4 | 4 | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 5 | 5 | # I made this for a household of people who don't understand the consequences of their actions | |
| 6 | 6 | # (namely, that if you abuse our awesome internet, we all have to suffer with being shaped :( ) | |
| @@ -12,12 +12,13 @@ version = "r5" | |||
| 12 | 12 | # password = Westnet password | |
| 13 | 13 | # routerpass = BoB2 router password | |
| 14 | 14 | # always_on_wireless_computer = A laptop or some other device that's always turned on, and connected to the wireless network. | |
| 15 | + | # and also running a web server (http://address needs to be accessible) | |
| 15 | 16 | # timezone = Timezone for logs, in format <country>/<city> | |
| 16 | 17 | # BSD license. | |
| 17 | 18 | username = "USERNAME" | |
| 18 | 19 | password = "PASSWORD" | |
| 19 | - | routerpass = "routerpass" | |
| 20 | - | always_on_wireless_computer = "stevensmith-laptop" | |
| 20 | + | routerpass = "" | |
| 21 | + | always_on_wireless_computer = "ip or hostname" | |
| 21 | 22 | timezone = "Australia/Perth" | |
| 22 | 23 | ||
| 23 | 24 | import urllib2 | |
| @@ -30,19 +31,19 @@ import os | |||
| 30 | 31 | ||
| 31 | 32 | debug = False | |
| 32 | 33 | try: | |
| 33 | - | if argv[1] == "debug": | |
| 34 | - | debug = True | |
| 34 | + | if argv[1] == "debug": | |
| 35 | + | debug = True | |
| 35 | 36 | except IndexError: | |
| 36 | - | debug = False | |
| 37 | + | debug = False | |
| 37 | 38 | ||
| 38 | 39 | def debug(msg): | |
| 39 | - | if debug: | |
| 40 | - | print msg | |
| 40 | + | if debug: | |
| 41 | + | print msg | |
| 41 | 42 | ||
| 42 | 43 | debug("Debug active") | |
| 43 | 44 | ||
| 44 | 45 | def getnumbers(inp): # For parsing "100,000MB" to an integer | |
| 45 | - | return int(inp[:-2].replace(",", "")) | |
| 46 | + | return int(inp[:-2].replace(",", "")) | |
| 46 | 47 | debug("getnumbers function set") | |
| 47 | 48 | ||
| 48 | 49 | url = "https://myaccount2.westnet.com.au/" # login page url | |
| @@ -60,10 +61,10 @@ debug("user agent set") | |||
| 60 | 61 | ||
| 61 | 62 | login = opener.open(url, logindata) # log in, save session data to cookie jar | |
| 62 | 63 | debug("logged into westnet") | |
| 63 | - | ||
| 64 | - | page = opener.open(url) # reopen same url (westnet has both login and homepage on the same url) | |
| 64 | + | ||
| 65 | + | page = opener.open(url) # reopen same url (westnet has both login and homepage on the same url) | |
| 65 | 66 | debug("loaded usage page") | |
| 66 | - | soup = BeautifulSoup(page.read()) # make soup from page | |
| 67 | + | soup = BeautifulSoup(page.read()) # make soup from page | |
| 67 | 68 | debug("made soup") | |
| 68 | 69 | usage = soup.findAll('div',{'class':'usage_text'})[0].text # get usage string "XXX,XXXMB out of XXX,XXXMB" | |
| 69 | 70 | debug("found usage string") | |
| @@ -91,51 +92,52 @@ debug("set up bob wireless urls") | |||
| 91 | 92 | os.environ['TZ'] = timezone | |
| 92 | 93 | time.tzset() | |
| 93 | 94 | if debug: | |
| 94 | - | ts = time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) # timestamp for status webpage | |
| 95 | + | ts = time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) # timestamp for status webpage | |
| 95 | 96 | else: | |
| 96 | - | ts = time.strftime("%a, %d %b %Y %H:00:00 %Z", time.localtime()) # as the script is called automatically every hour on the hour, minutes and seconds aren't needed | |
| 97 | + | ts = time.strftime("%a, %d %b %Y %H:00:00 %Z", time.localtime()) # as the script is called automatically every hour on the hour, minutes and seconds aren't needed | |
| 97 | 98 | debug("set up timestamp") | |
| 98 | 99 | ||
| 99 | 100 | def logintoBob(): | |
| 100 | - | opener.open(boblogin, bobdata) # log into router, save session to cookie jar | |
| 101 | + | opener.open(boblogin, bobdata) # log into router, save session to cookie jar | |
| 101 | 102 | debug("set up logintoBob") | |
| 102 | 103 | ||
| 103 | 104 | def enableWireless(f): | |
| 104 | - | if urllib2.urlopen("http://%s" % always_on_wireless_computer).getcode() != 200: # if this is true, it confirms that the wireless is already on | |
| 105 | - | debug("Couldn't find %s, telling the router to enable wireless" % always_on_wireless_computer) | |
| 106 | - | logintoBob() | |
| 107 | - | debug("logged into bob") | |
| 108 | - | opener.open(wenurl) # send wireless enable request using login info from cookiejar | |
| 109 | - | debug("Wireless enabled. connection may die for a few seconds") | |
| 110 | - | else: | |
| 111 | - | debug("Found %s, not re-enabling wireless (avoids annoying disconnection issue)" % always_on_wireless_computer) | |
| 112 | - | f.write("%s: Enable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 113 | - | debug("wrote to /var/www/westnet.html") | |
| 105 | + | try: | |
| 106 | + | wireless_test = urllib2.urlopen("http://%s" % always_on_wireless_computer).getcode() | |
| 107 | + | debug("Found %s, not re-enabling wireless (avoids annoying disconnection issue)" % always_on_wireless_computer) | |
| 108 | + | except urllib2.URLError: | |
| 109 | + | debug("Couldn't find %s, telling the router to enable wireless" % always_on_wireless_computer) | |
| 110 | + | logintoBob() | |
| 111 | + | debug("logged into bob") | |
| 112 | + | opener.open(wenurl) # send wireless enable request using login info from cookiejar | |
| 113 | + | debug("Wireless enabled. connection may die for a few seconds") | |
| 114 | + | f.write("%s: Enable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 115 | + | debug("wrote to /var/www/westnet.html") | |
| 114 | 116 | debug("set up enableWireless") | |
| 115 | 117 | def disableWireless(f): | |
| 116 | - | logintoBob() | |
| 117 | - | debug("logged into bob") | |
| 118 | - | opener.open(wdisurl) # send wireless disable request using login info from cookiejar | |
| 119 | - | debug("wireless disabled") | |
| 120 | - | f.write("%s: Disable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 121 | - | debug("wrote to /var/www/westnet.html") | |
| 118 | + | logintoBob() | |
| 119 | + | debug("logged into bob") | |
| 120 | + | opener.open(wdisurl) # send wireless disable request using login info from cookiejar | |
| 121 | + | debug("wireless disabled") | |
| 122 | + | f.write("%s: Disable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 123 | + | debug("wrote to /var/www/westnet.html") | |
| 122 | 124 | debug("set up disableWireless") | |
| 123 | 125 | ||
| 124 | 126 | if count > total: | |
| 125 | - | shaped = True | |
| 127 | + | shaped = True | |
| 126 | 128 | else: | |
| 127 | - | shaped = False | |
| 129 | + | shaped = False | |
| 128 | 130 | ||
| 129 | 131 | debug("shaped: " + str(shaped)) | |
| 130 | 132 | ||
| 131 | 133 | f = open("/var/www/westnet.html", "a") # Append to web-accessible file | |
| 132 | 134 | debug("/var/www/westnet.html opened for appending") | |
| 133 | 135 | if trend > suggested and not shaped: | |
| 134 | - | debug("disabling wireless") | |
| 135 | - | disableWireless(f) | |
| 136 | + | debug("disabling wireless") | |
| 137 | + | disableWireless(f) | |
| 136 | 138 | elif shaped or trend <= suggested: | |
| 137 | - | debug("enabling wireless") | |
| 138 | - | enableWireless(f) | |
| 139 | + | debug("enabling wireless") | |
| 140 | + | enableWireless(f) | |
| 139 | 141 | debug("closing file") | |
| 140 | 142 | f.close() | |
| 141 | 143 | debug("finished") | |
Steven Smith revised this gist . Go to revision
1 file changed, 19 insertions, 10 deletions
westnetusage.py
| @@ -1,4 +1,5 @@ | |||
| 1 | 1 | #!/usr/bin/env python2.7 | |
| 2 | + | version = "r5" | |
| 2 | 3 | # This script gets usage from my ISP's website, checks to see if the current average for the month | |
| 3 | 4 | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 4 | 5 | # I made this for a household of people who don't understand the consequences of their actions | |
| @@ -11,23 +12,28 @@ | |||
| 11 | 12 | # password = Westnet password | |
| 12 | 13 | # routerpass = BoB2 router password | |
| 13 | 14 | # always_on_wireless_computer = A laptop or some other device that's always turned on, and connected to the wireless network. | |
| 15 | + | # timezone = Timezone for logs, in format <country>/<city> | |
| 14 | 16 | # BSD license. | |
| 15 | 17 | username = "USERNAME" | |
| 16 | 18 | password = "PASSWORD" | |
| 17 | 19 | routerpass = "routerpass" | |
| 18 | 20 | always_on_wireless_computer = "stevensmith-laptop" | |
| 19 | - | ||
| 21 | + | timezone = "Australia/Perth" | |
| 22 | + | ||
| 20 | 23 | import urllib2 | |
| 21 | 24 | from BeautifulSoup import BeautifulSoup | |
| 22 | 25 | import urllib | |
| 23 | 26 | import cookielib | |
| 24 | 27 | import time | |
| 25 | 28 | from sys import argv | |
| 29 | + | import os | |
| 26 | 30 | ||
| 27 | 31 | debug = False | |
| 28 | - | if len(argv) > 1: | |
| 32 | + | try: | |
| 29 | 33 | if argv[1] == "debug": | |
| 30 | 34 | debug = True | |
| 35 | + | except IndexError: | |
| 36 | + | debug = False | |
| 31 | 37 | ||
| 32 | 38 | def debug(msg): | |
| 33 | 39 | if debug: | |
| @@ -40,7 +46,7 @@ def getnumbers(inp): # For parsing "100,000MB" to an integer | |||
| 40 | 46 | debug("getnumbers function set") | |
| 41 | 47 | ||
| 42 | 48 | url = "https://myaccount2.westnet.com.au/" # login page url | |
| 43 | - | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" # what the server sees as the client ID | |
| 49 | + | desc = "westnet usage alert script %s by blha303. https://gist.github.com/blha303/5788199 for source" % version # what the server sees as the client ID | |
| 44 | 50 | debug("url, desc variables set") | |
| 45 | 51 | ||
| 46 | 52 | logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data | |
| @@ -82,7 +88,12 @@ wdisurl = "http://10.1.1.1/wireless_id.wl?wlSsidIdx=0&wlEnbl=0" # router disable | |||
| 82 | 88 | wenurl = "http://10.1.1.1/wireless_id.wl?wlEnbl=1&wlSsidIdx=0" # router enable wireless url as GET data | |
| 83 | 89 | debug("set up bob wireless urls") | |
| 84 | 90 | ||
| 85 | - | ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) # timestamp for status webpage | |
| 91 | + | os.environ['TZ'] = timezone | |
| 92 | + | time.tzset() | |
| 93 | + | if debug: | |
| 94 | + | ts = time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()) # timestamp for status webpage | |
| 95 | + | else: | |
| 96 | + | ts = time.strftime("%a, %d %b %Y %H:00:00 %Z", time.localtime()) # as the script is called automatically every hour on the hour, minutes and seconds aren't needed | |
| 86 | 97 | debug("set up timestamp") | |
| 87 | 98 | ||
| 88 | 99 | def logintoBob(): | |
| @@ -98,18 +109,16 @@ def enableWireless(f): | |||
| 98 | 109 | debug("Wireless enabled. connection may die for a few seconds") | |
| 99 | 110 | else: | |
| 100 | 111 | debug("Found %s, not re-enabling wireless (avoids annoying disconnection issue)" % always_on_wireless_computer) | |
| 101 | - | if not debug: | |
| 102 | - | f.write("%s: Enable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 103 | - | debug("would have written to /var/www/westnet.html") | |
| 112 | + | f.write("%s: Enable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 113 | + | debug("wrote to /var/www/westnet.html") | |
| 104 | 114 | debug("set up enableWireless") | |
| 105 | 115 | def disableWireless(f): | |
| 106 | 116 | logintoBob() | |
| 107 | 117 | debug("logged into bob") | |
| 108 | 118 | opener.open(wdisurl) # send wireless disable request using login info from cookiejar | |
| 109 | 119 | debug("wireless disabled") | |
| 110 | - | if not debug: | |
| 111 | - | f.write("%s: Disable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 112 | - | debug("would have written to /var/www/westnet.html") | |
| 120 | + | f.write("%s: Disable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 121 | + | debug("wrote to /var/www/westnet.html") | |
| 113 | 122 | debug("set up disableWireless") | |
| 114 | 123 | ||
| 115 | 124 | if count > total: | |
Steven Smith revised this gist . Go to revision
1 file changed, 70 insertions, 21 deletions
westnetusage.py
| @@ -3,81 +3,130 @@ | |||
| 3 | 3 | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 4 | 4 | # I made this for a household of people who don't understand the consequences of their actions | |
| 5 | 5 | # (namely, that if you abuse our awesome internet, we all have to suffer with being shaped :( ) | |
| 6 | - | # Please, modify this for your own ISP/router and set it up in your own house to help with | |
| 7 | - | # your daily usage. -blha303 | |
| 6 | + | # Please, modify this for your own ISP/router and set it up in your own house to help with your daily usage. | |
| 7 | + | # -blha303 | |
| 8 | + | # Usage: python westnetusage.py [debug] | |
| 9 | + | # Options: | |
| 10 | + | # username = Westnet username | |
| 11 | + | # password = Westnet password | |
| 12 | + | # routerpass = BoB2 router password | |
| 13 | + | # always_on_wireless_computer = A laptop or some other device that's always turned on, and connected to the wireless network. | |
| 8 | 14 | # BSD license. | |
| 9 | 15 | username = "USERNAME" | |
| 10 | 16 | password = "PASSWORD" | |
| 11 | 17 | routerpass = "routerpass" | |
| 18 | + | always_on_wireless_computer = "stevensmith-laptop" | |
| 12 | 19 | ||
| 13 | 20 | import urllib2 | |
| 14 | 21 | from BeautifulSoup import BeautifulSoup | |
| 15 | 22 | import urllib | |
| 16 | 23 | import cookielib | |
| 17 | 24 | import time | |
| 25 | + | from sys import argv | |
| 26 | + | ||
| 27 | + | debug = False | |
| 28 | + | if len(argv) > 1: | |
| 29 | + | if argv[1] == "debug": | |
| 30 | + | debug = True | |
| 31 | + | ||
| 32 | + | def debug(msg): | |
| 33 | + | if debug: | |
| 34 | + | print msg | |
| 35 | + | ||
| 36 | + | debug("Debug active") | |
| 18 | 37 | ||
| 19 | 38 | def getnumbers(inp): # For parsing "100,000MB" to an integer | |
| 20 | 39 | return int(inp[:-2].replace(",", "")) | |
| 40 | + | debug("getnumbers function set") | |
| 21 | 41 | ||
| 22 | 42 | url = "https://myaccount2.westnet.com.au/" # login page url | |
| 23 | - | # what the server sees as the client ID | |
| 24 | - | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" | |
| 43 | + | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" # what the server sees as the client ID | |
| 44 | + | debug("url, desc variables set") | |
| 25 | 45 | ||
| 26 | - | logindata = urllib.urlencode({"hdnLoginType": "myaccount", # login POST data | |
| 27 | - | "action": "login", | |
| 28 | - | "username": username, | |
| 29 | - | "password": password}) | |
| 46 | + | logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data | |
| 47 | + | debug("logindata set") | |
| 30 | 48 | ||
| 31 | 49 | cj = cookielib.CookieJar() # Cookie jar, used to store login session | |
| 32 | 50 | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
| 51 | + | debug("cookie jar set") | |
| 33 | 52 | opener.addheaders = [('User-Agent', desc)] # set client ID to desc | |
| 53 | + | debug("user agent set") | |
| 34 | 54 | ||
| 35 | 55 | login = opener.open(url, logindata) # log in, save session data to cookie jar | |
| 56 | + | debug("logged into westnet") | |
| 36 | 57 | ||
| 37 | 58 | page = opener.open(url) # reopen same url (westnet has both login and homepage on the same url) | |
| 59 | + | debug("loaded usage page") | |
| 38 | 60 | soup = BeautifulSoup(page.read()) # make soup from page | |
| 61 | + | debug("made soup") | |
| 39 | 62 | usage = soup.findAll('div',{'class':'usage_text'})[0].text # get usage string "XXX,XXXMB out of XXX,XXXMB" | |
| 63 | + | debug("found usage string") | |
| 40 | 64 | count = getnumbers(usage.split(" ")[0]) # get count integer, first part of usage string above as integer | |
| 65 | + | debug("found count") | |
| 41 | 66 | total = getnumbers(usage.split(" ")[-1]) # get total integer, last part of usage string above as integer | |
| 67 | + | debug("found total") | |
| 42 | 68 | suggested = getnumbers(soup.findAll('a',{'href':'#suggested_anytime_tooltip'})[0].text) # get suggested integer | |
| 69 | + | debug("found suggested") | |
| 43 | 70 | trend = getnumbers(soup.findAll('a',{'href':'#trend_anytime_tooltip'})[0].text) # get trend integer | |
| 44 | - | #print "usage: " + usage | |
| 45 | - | #print "count: " + str(count) | |
| 46 | - | #print "total: " + str(total) | |
| 47 | - | #print "suggested: " + str(suggested) | |
| 48 | - | #print "trend: " + str(trend) | |
| 71 | + | debug("found trend") | |
| 72 | + | debug("usage: " + usage) | |
| 73 | + | debug("count: " + str(count)) | |
| 74 | + | debug("total: " + str(total)) | |
| 75 | + | debug("suggested: " + str(suggested)) | |
| 76 | + | debug("trend: " + str(trend)) | |
| 49 | 77 | ||
| 50 | 78 | boblogin = "http://10.1.1.1/login.cgi" # router login url | |
| 51 | - | # router login info as POST data | |
| 52 | - | bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) | |
| 79 | + | bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) # router login info as POST data | |
| 80 | + | debug("set up bob login data") | |
| 53 | 81 | wdisurl = "http://10.1.1.1/wireless_id.wl?wlSsidIdx=0&wlEnbl=0" # router disable wireless url as GET data | |
| 54 | 82 | wenurl = "http://10.1.1.1/wireless_id.wl?wlEnbl=1&wlSsidIdx=0" # router enable wireless url as GET data | |
| 83 | + | debug("set up bob wireless urls") | |
| 55 | 84 | ||
| 56 | 85 | ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) # timestamp for status webpage | |
| 86 | + | debug("set up timestamp") | |
| 57 | 87 | ||
| 58 | 88 | def logintoBob(): | |
| 59 | 89 | opener.open(boblogin, bobdata) # log into router, save session to cookie jar | |
| 90 | + | debug("set up logintoBob") | |
| 60 | 91 | ||
| 61 | 92 | def enableWireless(f): | |
| 62 | - | logintoBob() | |
| 63 | - | opener.open(wenurl) # send wireless enable request using login info from cookiejar | |
| 64 | - | f.write("%s: Enable wireless<br>" % ts) | |
| 65 | - | ||
| 93 | + | if urllib2.urlopen("http://%s" % always_on_wireless_computer).getcode() != 200: # if this is true, it confirms that the wireless is already on | |
| 94 | + | debug("Couldn't find %s, telling the router to enable wireless" % always_on_wireless_computer) | |
| 95 | + | logintoBob() | |
| 96 | + | debug("logged into bob") | |
| 97 | + | opener.open(wenurl) # send wireless enable request using login info from cookiejar | |
| 98 | + | debug("Wireless enabled. connection may die for a few seconds") | |
| 99 | + | else: | |
| 100 | + | debug("Found %s, not re-enabling wireless (avoids annoying disconnection issue)" % always_on_wireless_computer) | |
| 101 | + | if not debug: | |
| 102 | + | f.write("%s: Enable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 103 | + | debug("would have written to /var/www/westnet.html") | |
| 104 | + | debug("set up enableWireless") | |
| 66 | 105 | def disableWireless(f): | |
| 67 | 106 | logintoBob() | |
| 107 | + | debug("logged into bob") | |
| 68 | 108 | opener.open(wdisurl) # send wireless disable request using login info from cookiejar | |
| 69 | - | f.write("%s: Disable wireless<br>" % ts) | |
| 109 | + | debug("wireless disabled") | |
| 110 | + | if not debug: | |
| 111 | + | f.write("%s: Disable wireless %s %s/%s\n" % (ts, usage, str(trend), str(suggested))) | |
| 112 | + | debug("would have written to /var/www/westnet.html") | |
| 113 | + | debug("set up disableWireless") | |
| 70 | 114 | ||
| 71 | 115 | if count > total: | |
| 72 | 116 | shaped = True | |
| 73 | 117 | else: | |
| 74 | 118 | shaped = False | |
| 75 | 119 | ||
| 76 | - | #print "shaped: " + str(shaped) | |
| 120 | + | debug("shaped: " + str(shaped)) | |
| 77 | 121 | ||
| 78 | 122 | f = open("/var/www/westnet.html", "a") # Append to web-accessible file | |
| 123 | + | debug("/var/www/westnet.html opened for appending") | |
| 79 | 124 | if trend > suggested and not shaped: | |
| 125 | + | debug("disabling wireless") | |
| 80 | 126 | disableWireless(f) | |
| 81 | 127 | elif shaped or trend <= suggested: | |
| 128 | + | debug("enabling wireless") | |
| 82 | 129 | enableWireless(f) | |
| 130 | + | debug("closing file") | |
| 83 | 131 | f.close() | |
| 132 | + | debug("finished") | |
Steven Smith revised this gist . Go to revision
1 file changed, 10 insertions, 5 deletions
westnetusage.py
| @@ -3,8 +3,8 @@ | |||
| 3 | 3 | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 4 | 4 | # I made this for a household of people who don't understand the consequences of their actions | |
| 5 | 5 | # (namely, that if you abuse our awesome internet, we all have to suffer with being shaped :( ) | |
| 6 | - | # Please, modify this for your own ISP/router and set it up in your own house to help with your daily usage. | |
| 7 | - | # -blha303 | |
| 6 | + | # Please, modify this for your own ISP/router and set it up in your own house to help with | |
| 7 | + | # your daily usage. -blha303 | |
| 8 | 8 | # BSD license. | |
| 9 | 9 | username = "USERNAME" | |
| 10 | 10 | password = "PASSWORD" | |
| @@ -20,9 +20,13 @@ def getnumbers(inp): # For parsing "100,000MB" to an integer | |||
| 20 | 20 | return int(inp[:-2].replace(",", "")) | |
| 21 | 21 | ||
| 22 | 22 | url = "https://myaccount2.westnet.com.au/" # login page url | |
| 23 | - | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" # what the server sees as the client ID | |
| 23 | + | # what the server sees as the client ID | |
| 24 | + | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" | |
| 24 | 25 | ||
| 25 | - | logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data | |
| 26 | + | logindata = urllib.urlencode({"hdnLoginType": "myaccount", # login POST data | |
| 27 | + | "action": "login", | |
| 28 | + | "username": username, | |
| 29 | + | "password": password}) | |
| 26 | 30 | ||
| 27 | 31 | cj = cookielib.CookieJar() # Cookie jar, used to store login session | |
| 28 | 32 | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
| @@ -44,7 +48,8 @@ trend = getnumbers(soup.findAll('a',{'href':'#trend_anytime_tooltip'})[0].text) | |||
| 44 | 48 | #print "trend: " + str(trend) | |
| 45 | 49 | ||
| 46 | 50 | boblogin = "http://10.1.1.1/login.cgi" # router login url | |
| 47 | - | bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) # router login info as POST data | |
| 51 | + | # router login info as POST data | |
| 52 | + | bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) | |
| 48 | 53 | wdisurl = "http://10.1.1.1/wireless_id.wl?wlSsidIdx=0&wlEnbl=0" # router disable wireless url as GET data | |
| 49 | 54 | wenurl = "http://10.1.1.1/wireless_id.wl?wlEnbl=1&wlSsidIdx=0" # router enable wireless url as GET data | |
| 50 | 55 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
westnetusage.py
| @@ -20,7 +20,7 @@ def getnumbers(inp): # For parsing "100,000MB" to an integer | |||
| 20 | 20 | return int(inp[:-2].replace(",", "")) | |
| 21 | 21 | ||
| 22 | 22 | url = "https://myaccount2.westnet.com.au/" # login page url | |
| 23 | - | desc = "westnet usage alert script by blha303" # what the server sees as the client ID | |
| 23 | + | desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" # what the server sees as the client ID | |
| 24 | 24 | ||
| 25 | 25 | logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data | |
| 26 | 26 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 78 insertions
westnetusage.py(file created)
| @@ -0,0 +1,78 @@ | |||
| 1 | + | #!/usr/bin/env python2.7 | |
| 2 | + | # This script gets usage from my ISP's website, checks to see if the current average for the month | |
| 3 | + | # is above their recommended amount per day, then disables the wifi on my router if it is. | |
| 4 | + | # I made this for a household of people who don't understand the consequences of their actions | |
| 5 | + | # (namely, that if you abuse our awesome internet, we all have to suffer with being shaped :( ) | |
| 6 | + | # Please, modify this for your own ISP/router and set it up in your own house to help with your daily usage. | |
| 7 | + | # -blha303 | |
| 8 | + | # BSD license. | |
| 9 | + | username = "USERNAME" | |
| 10 | + | password = "PASSWORD" | |
| 11 | + | routerpass = "routerpass" | |
| 12 | + | ||
| 13 | + | import urllib2 | |
| 14 | + | from BeautifulSoup import BeautifulSoup | |
| 15 | + | import urllib | |
| 16 | + | import cookielib | |
| 17 | + | import time | |
| 18 | + | ||
| 19 | + | def getnumbers(inp): # For parsing "100,000MB" to an integer | |
| 20 | + | return int(inp[:-2].replace(",", "")) | |
| 21 | + | ||
| 22 | + | url = "https://myaccount2.westnet.com.au/" # login page url | |
| 23 | + | desc = "westnet usage alert script by blha303" # what the server sees as the client ID | |
| 24 | + | ||
| 25 | + | logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data | |
| 26 | + | ||
| 27 | + | cj = cookielib.CookieJar() # Cookie jar, used to store login session | |
| 28 | + | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
| 29 | + | opener.addheaders = [('User-Agent', desc)] # set client ID to desc | |
| 30 | + | ||
| 31 | + | login = opener.open(url, logindata) # log in, save session data to cookie jar | |
| 32 | + | ||
| 33 | + | page = opener.open(url) # reopen same url (westnet has both login and homepage on the same url) | |
| 34 | + | soup = BeautifulSoup(page.read()) # make soup from page | |
| 35 | + | usage = soup.findAll('div',{'class':'usage_text'})[0].text # get usage string "XXX,XXXMB out of XXX,XXXMB" | |
| 36 | + | count = getnumbers(usage.split(" ")[0]) # get count integer, first part of usage string above as integer | |
| 37 | + | total = getnumbers(usage.split(" ")[-1]) # get total integer, last part of usage string above as integer | |
| 38 | + | suggested = getnumbers(soup.findAll('a',{'href':'#suggested_anytime_tooltip'})[0].text) # get suggested integer | |
| 39 | + | trend = getnumbers(soup.findAll('a',{'href':'#trend_anytime_tooltip'})[0].text) # get trend integer | |
| 40 | + | #print "usage: " + usage | |
| 41 | + | #print "count: " + str(count) | |
| 42 | + | #print "total: " + str(total) | |
| 43 | + | #print "suggested: " + str(suggested) | |
| 44 | + | #print "trend: " + str(trend) | |
| 45 | + | ||
| 46 | + | boblogin = "http://10.1.1.1/login.cgi" # router login url | |
| 47 | + | bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) # router login info as POST data | |
| 48 | + | wdisurl = "http://10.1.1.1/wireless_id.wl?wlSsidIdx=0&wlEnbl=0" # router disable wireless url as GET data | |
| 49 | + | wenurl = "http://10.1.1.1/wireless_id.wl?wlEnbl=1&wlSsidIdx=0" # router enable wireless url as GET data | |
| 50 | + | ||
| 51 | + | ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) # timestamp for status webpage | |
| 52 | + | ||
| 53 | + | def logintoBob(): | |
| 54 | + | opener.open(boblogin, bobdata) # log into router, save session to cookie jar | |
| 55 | + | ||
| 56 | + | def enableWireless(f): | |
| 57 | + | logintoBob() | |
| 58 | + | opener.open(wenurl) # send wireless enable request using login info from cookiejar | |
| 59 | + | f.write("%s: Enable wireless<br>" % ts) | |
| 60 | + | ||
| 61 | + | def disableWireless(f): | |
| 62 | + | logintoBob() | |
| 63 | + | opener.open(wdisurl) # send wireless disable request using login info from cookiejar | |
| 64 | + | f.write("%s: Disable wireless<br>" % ts) | |
| 65 | + | ||
| 66 | + | if count > total: | |
| 67 | + | shaped = True | |
| 68 | + | else: | |
| 69 | + | shaped = False | |
| 70 | + | ||
| 71 | + | #print "shaped: " + str(shaped) | |
| 72 | + | ||
| 73 | + | f = open("/var/www/westnet.html", "a") # Append to web-accessible file | |
| 74 | + | if trend > suggested and not shaped: | |
| 75 | + | disableWireless(f) | |
| 76 | + | elif shaped or trend <= suggested: | |
| 77 | + | enableWireless(f) | |
| 78 | + | f.close() | |