westnetusage.py
· 3.4 KiB · Python
Raw
#!/usr/bin/env python2.7
# This script gets usage from my ISP's website, checks to see if the current average for the month
# is above their recommended amount per day, then disables the wifi on my router if it is.
# I made this for a household of people who don't understand the consequences of their actions
# (namely, that if you abuse our awesome internet, we all have to suffer with being shaped :( )
# Please, modify this for your own ISP/router and set it up in your own house to help with your daily usage.
# -blha303
# BSD license.
username = "USERNAME"
password = "PASSWORD"
routerpass = "routerpass"
import urllib2
from BeautifulSoup import BeautifulSoup
import urllib
import cookielib
import time
def getnumbers(inp): # For parsing "100,000MB" to an integer
return int(inp[:-2].replace(",", ""))
url = "https://myaccount2.westnet.com.au/" # login page url
desc = "westnet usage alert script by blha303. https://gist.github.com/blha303/5788199 for source" # what the server sees as the client ID
logindata = urllib.urlencode({"hdnLoginType": "myaccount", "action": "login", "username": username, "password": password}) # login POST data
cj = cookielib.CookieJar() # Cookie jar, used to store login session
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-Agent', desc)] # set client ID to desc
login = opener.open(url, logindata) # log in, save session data to cookie jar
page = opener.open(url) # reopen same url (westnet has both login and homepage on the same url)
soup = BeautifulSoup(page.read()) # make soup from page
usage = soup.findAll('div',{'class':'usage_text'})[0].text # get usage string "XXX,XXXMB out of XXX,XXXMB"
count = getnumbers(usage.split(" ")[0]) # get count integer, first part of usage string above as integer
total = getnumbers(usage.split(" ")[-1]) # get total integer, last part of usage string above as integer
suggested = getnumbers(soup.findAll('a',{'href':'#suggested_anytime_tooltip'})[0].text) # get suggested integer
trend = getnumbers(soup.findAll('a',{'href':'#trend_anytime_tooltip'})[0].text) # get trend integer
#print "usage: " + usage
#print "count: " + str(count)
#print "total: " + str(total)
#print "suggested: " + str(suggested)
#print "trend: " + str(trend)
boblogin = "http://10.1.1.1/login.cgi" # router login url
bobdata = urllib.urlencode({"login_option": "0", "password": routerpass, "passwordtemp": ""}) # router login info as POST data
wdisurl = "http://10.1.1.1/wireless_id.wl?wlSsidIdx=0&wlEnbl=0" # router disable wireless url as GET data
wenurl = "http://10.1.1.1/wireless_id.wl?wlEnbl=1&wlSsidIdx=0" # router enable wireless url as GET data
ts = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) # timestamp for status webpage
def logintoBob():
opener.open(boblogin, bobdata) # log into router, save session to cookie jar
def enableWireless(f):
logintoBob()
opener.open(wenurl) # send wireless enable request using login info from cookiejar
f.write("%s: Enable wireless<br>" % ts)
def disableWireless(f):
logintoBob()
opener.open(wdisurl) # send wireless disable request using login info from cookiejar
f.write("%s: Disable wireless<br>" % ts)
if count > total:
shaped = True
else:
shaped = False
#print "shaped: " + str(shaped)
f = open("/var/www/westnet.html", "a") # Append to web-accessible file
if trend > suggested and not shaped:
disableWireless(f)
elif shaped or trend <= suggested:
enableWireless(f)
f.close()
| 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. https://gist.github.com/blha303/5788199 for source" # 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() |
| 79 |