#!/usr/bin/python EMAIL = 'example@cybersmart.co.za' #Your email address at cybersmart PASSWORD = 'password' #Your cybersmart password import urllib import pynotify import datetime import time import sys sys.path.append('/usr/lib/python%s/site-packages/oldxml' % sys.version[:3]) from xml.dom.ext.reader import HtmlLib def collect_text(node): "A function which collects text inside 'node', returning that text." s = "" for child_node in node.childNodes: if child_node.nodeType == child_node.TEXT_NODE: s += child_node.nodeValue else: s += collect_text(child_node) return s def mkDateTime(dateString,strFormat="%Y-%m-%d"): # Expects "YYYY-MM-DD" string # returns a datetime object eSeconds = time.mktime(time.strptime(dateString,strFormat)) return datetime.datetime.fromtimestamp(eSeconds) def mkLastOfMonth(dtDateTime): dYear = dtDateTime.strftime("%Y") #get the year dMonth = str(int(dtDateTime.strftime("%m"))%12+1)#get next month, watch rollover dDay = "1" #first day of next month nextMonth = mkDateTime("%s-%s-%s"%(dYear,dMonth,dDay))#make a datetime obj for 1st of next month delta = datetime.timedelta(seconds=1) #create a delta of 1 second return nextMonth - delta #subtract from nextMonth and return print("Downloading HTML") data = urllib.urlencode({"accountName":EMAIL,"password":PASSWORD,"login":"login"}) f = urllib.urlopen("http://www.cybersmart.co.za/getAccountDetails.cgi",data) doc = f.read() f.close() reader= HtmlLib.Reader() print("- Parsing Document") dom = reader.fromString(doc) print("- Walking the tree") p_elements = dom.getElementsByTagName("p") warning=False for p in p_elements: temp = p.attributes.getNamedItem('class') if temp.value=='warning': warning = collect_text(p) print("\n Warning: %s" % warning) exit() td_elements = dom.getElementsByTagName("td") remaining='' used='' for td in td_elements: temp = td.attributes.getNamedItem('class') if temp: if temp.value == "savedTotal": remaining=collect_text(td) if temp.value == "usedTotal": used=collect_text(td) print('-- Got values: %s used, %s remaining' % (used,remaining)) print("Caluclating averages") daysleft = mkLastOfMonth(datetime.datetime.now())-datetime.datetime.now() daysleft = daysleft.days+1 remaining_float = float(remaining) used_float = -float(used) days = int(datetime.datetime.now().strftime("%d")) print("Displaying data") usage="%.2f Gb used in %s days\nUsage average of %.2f Mb" % (used_float,days,(used_float*1000)/days) projected="\n\n%s days left to use %s Gb\ndaily allowance of %.2f Mb" % (daysleft,remaining,(remaining_float*1000)/daysleft) content = ''.join([usage,projected]) pynotify.init("cybersmart") n = pynotify.Notification("Cybersmart Remaining Cap",content) n.show()