diff SMSVodaAu.py @ 7:bf896507faa9

Add code to send an SMS if configured to do so. Rearrange the output stage a bit to make it clearer.
author darius
date Fri, 07 Sep 2007 01:31:47 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SMSVodaAu.py	Fri Sep 07 01:31:47 2007 +0000
@@ -0,0 +1,66 @@
+import mechanize, ClientForm, re, sys, logging
+
+class BadLogin(Exception):
+    pass
+
+class SMSVodaAu:
+    MSGPAGE = 'https://www.myvodafone.com.au/yrweb2txt/enter.do'
+
+    def __init__(self, user, password):
+        self.br = mechanize.Browser()
+        self.cj = mechanize.CookieJar()
+        self.br.set_cookiejar(self.cj)
+        self.user = user
+        self.password = password
+
+    def dodebug(self):
+        # logging.DEBUG covers masses of debugging information,
+        # logging.INFO just shows the output from HTTPRedirectDebugProcessor,
+        logger = logging.getLogger("mechanize")
+        logger.addHandler(logging.StreamHandler(sys.stdout))
+        logger.setLevel(logging.DEBUG)
+        
+    def sendamsg(self, recipient, msg):
+        assert(len(msg) < 160)
+
+        self.br.open(self.MSGPAGE)
+        for f in self.br.forms():
+            if (f.name == 'loginForm'):
+                #print "Need to login"
+                self.br.select_form("loginForm")
+                self.br['txtUserID'] = self.user
+                self.br['txtPassword'] = self.password
+                response = self.br.submit()
+                if (re.match('Sorry you must enter a valid username and password', response.read(), re.IGNORECASE) != None):
+                    print "Unable to login"
+                    raise BadLogin()
+
+        self.br.select_form("sendMessageForm")
+
+        self.br.form.find_control("action").readonly = False
+        self.br.form.find_control("action").value = 'send'
+
+        self.br.form.find_control("totalMsgs").readonly = False
+        self.br.form.find_control("totalMsgs").value = '1'
+        
+        self.br.form.find_control("msg_counter").readonly = False
+        self.br.form.find_control("msg_counter").value = '1'
+
+        #c = self.br.form.find_control(name = "recipients")
+        #ClientForm.Item(c, {'contents' : 'adhoc' + recipient, 'value' : recipient})
+
+        nc = ClientForm.TextControl('text', 'recipients', {})
+        nc.add_to_form(self.br.form)
+        self.br.form.find_control(name = "recipients", type = "text").value = 'adhoc' + recipient
+        
+        self.br.form.find_control("messageBody").value = msg
+        self.br.form.find_control("counter").readonly = False
+        self.br.form.find_control("counter").value = str(160 - len(msg))
+
+        #return(self.br)
+        #f = open('out.html', 'w')
+        r = self.br.submit()
+        #f.write(r.read())
+        #r.seek(0)
+        return(r)
+