#!/usr/bin/env python # hardcoded username and password - un-comment these to enable #myUsername = "something" #myPassword = "reallysecret" """ jsusage A utility for doing the boring interaction with the Telecom Jetstream website, passing in your username and password, and retrieving your usage for the current month You can run this as a shell script with the arguments: - username - password Alternatively, you can hard-code the username and password by editing and uncommenting the declarations above. Alternatively again, you can put your username and password (separated by a newline or space) into the file .jetstreamrc in your home directory. Save this file, put it into your execution path, and chmod it executable (I recommend mode 700). (Or alternatively again, you can rename this file to jsusage.py, and import the JSUsage class into your own Python programs). """ import sys, os, re from urllib import urlencode from urllib2 import Request, HTTPSHandler, urlopen, build_opener, install_opener # URL from which we're retrieving usage info reportUrl = "https://jetstream-usage.telecom.co.nz/cgi-bin/iaf/iafReports" # file into which this util will try to read your jetstream username/passwd jspath = os.path.expanduser("~/.jetstreamrc") reSpaces = re.compile("\\s+") class JSUsageFetcher: """ Class for mining one's jetstream usage info from Telecom website this class interacts with the Telecom website, pretending to be a web browser, logs in, and clicks through to the page containing one's usage info. Once this page is retrieved, we mine it for the current usage figure. """ def __init__(self, *args, **kw): self.verbose = kw.get('verbose', 0) def getusage(self, username, password): """ Retrieve the Telecom Jetstream usage for a given username/password Arguments: - username - password Return: - current month's usage in megabytes (float) """ # set up for HTTPS access o = build_opener(HTTPSHandler) install_opener(o) # build a Request object req = self.req = Request(reportUrl) # whack in the form fields form = {"formEvent": "generate", "formName": "cscUserUsageBasic.html", "IAF_type": "csc_usage.sh", "IAF_usePrinter": "S", "IAF_printerName": "", "IAF_startAccount": "0", "IAF_endAccount": "0", "IAF_rangeType": "O", "domain": "null", "IAF_SERVER_SERVICE": "iafserver", "IAF_SERVER": "se259", "HGE_LOCALE": "hgeTemplates/csc/", "IAF_LogonType": "USER", "IAF_customerNumber": "", "IAF_userID": username, "IAF_LogonUserID": username, "IAF_LogonPassword": password, } req.add_data(urlencode(form.items())) # hit the Telecom https server if self.verbose: print "opening %s" % reportUrl u = self.u = urlopen(req) # get the full raw response raw = self.raw = u.read() # lop off the bit we're interested in raw1 = self.raw1 = raw.split("Total:", 1)[1].strip() # and just get the current month total usage usage = reSpaces.split(raw1, 1)[0] # got it if self.verbose: print "usage = %s" % usage return float(usage) def getusage(username, password): """ Creates a JSUsage class and runs it Arguments: - username - password Return: - current month's usage in megabytes (float) """ return JSUsageFetcher().getusage(username, password) def help(): print "usage: %s username password" % sys.argv[0] print "retrieves current usage stat from Telecom Jetstream website" print "You can store your username and password, separated by a newline," print "in the file '%s' instead" % jspath sys.exit(1) def main(): # any args on command line? if len(sys.argv) == 3: username, password = sys.argv[1:] else: # see if user has hard-coded these try: username, password = myUsername, myPassword except: # no - try the user file if os.path.isfile(jspath): username, password = reSpaces.split(open(jspath).read().strip())[:2] else: # spit dummy help() # ok - do the business print getusage(username, password) if __name__ == '__main__': main()