view SMSVodaAu.py @ 9:3e03facad74b default tip

New example files for latest layout.
author darius
date Thu, 18 Oct 2007 06:58:00 +0000
parents bf896507faa9
children
line wrap: on
line source

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)