Created
December 27, 2010 00:58
-
-
Save ahiscox/755771 to your computer and use it in GitHub Desktop.
Check Northwestel cable Internet usage from Linux or Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check your NWT account balance from Linux. | |
# This code borrows from Clopin's Gaia bots code | |
# which can be found here: http://ubuntuforums.org/showthread.php?t=1250601 | |
# Requires: | |
# Linux: BeautifulSoup installed (http://www.crummy.com/software/BeautifulSoup/) | |
# Android: android-scripting engine, BeautifulSoup.py and nwt.py in /sdcard/sl4a | |
MAC_ADDRESS = '000000000000' # Enter your 12 digit MAC address from your modem | |
import urllib | |
import urllib2 | |
import sys | |
import cookielib | |
from BeautifulSoup import BeautifulSoup | |
cookieJar = cookielib.LWPCookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar)) | |
opener.addheaders = [('User-agent', "Mozilla/5.0")] | |
url = "https://apps.nwtel.ca/cable_usage/j_security_check" | |
form = { "j_username" : MAC_ADDRESS, | |
"j_password" : '123456', | |
"j_target_url" : "secured/index.jsp", | |
"MAC" : MAC_ADDRESS } | |
encodedForm = urllib.urlencode(form) | |
request = urllib2.Request(url, encodedForm) | |
print "Contacting NWT..." | |
page = opener.open(request) # Note, we must open the page a second time, | |
page = opener.open(request) # or we will be redirected back to login! | |
print "Parsing Response..." | |
contents = page.read() | |
soup = BeautifulSoup(contents) | |
all_elements = soup.find('table').findAll('td')[-3:] | |
current_usage = all_elements[0].find('a').contents[0] | |
free_usage = str(all_elements[1]).strip('<td>').strip('</') | |
over_charge = str(all_elements[2]).strip('<td>').strip('</') | |
message = "You have used %s of %s this month, costing %s" % (current_usage, free_usage, over_charge) | |
# Comment (remove) the following line for Android: | |
print message | |
# Uncomment (add) the remaining lines for Android: | |
# import android | |
# droid = android.Android() | |
# droid.makeToast(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment