comparison 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
comparison
equal deleted inserted replaced
6:9f3eb9a07966 7:bf896507faa9
1 import mechanize, ClientForm, re, sys, logging
2
3 class BadLogin(Exception):
4 pass
5
6 class SMSVodaAu:
7 MSGPAGE = 'https://www.myvodafone.com.au/yrweb2txt/enter.do'
8
9 def __init__(self, user, password):
10 self.br = mechanize.Browser()
11 self.cj = mechanize.CookieJar()
12 self.br.set_cookiejar(self.cj)
13 self.user = user
14 self.password = password
15
16 def dodebug(self):
17 # logging.DEBUG covers masses of debugging information,
18 # logging.INFO just shows the output from HTTPRedirectDebugProcessor,
19 logger = logging.getLogger("mechanize")
20 logger.addHandler(logging.StreamHandler(sys.stdout))
21 logger.setLevel(logging.DEBUG)
22
23 def sendamsg(self, recipient, msg):
24 assert(len(msg) < 160)
25
26 self.br.open(self.MSGPAGE)
27 for f in self.br.forms():
28 if (f.name == 'loginForm'):
29 #print "Need to login"
30 self.br.select_form("loginForm")
31 self.br['txtUserID'] = self.user
32 self.br['txtPassword'] = self.password
33 response = self.br.submit()
34 if (re.match('Sorry you must enter a valid username and password', response.read(), re.IGNORECASE) != None):
35 print "Unable to login"
36 raise BadLogin()
37
38 self.br.select_form("sendMessageForm")
39
40 self.br.form.find_control("action").readonly = False
41 self.br.form.find_control("action").value = 'send'
42
43 self.br.form.find_control("totalMsgs").readonly = False
44 self.br.form.find_control("totalMsgs").value = '1'
45
46 self.br.form.find_control("msg_counter").readonly = False
47 self.br.form.find_control("msg_counter").value = '1'
48
49 #c = self.br.form.find_control(name = "recipients")
50 #ClientForm.Item(c, {'contents' : 'adhoc' + recipient, 'value' : recipient})
51
52 nc = ClientForm.TextControl('text', 'recipients', {})
53 nc.add_to_form(self.br.form)
54 self.br.form.find_control(name = "recipients", type = "text").value = 'adhoc' + recipient
55
56 self.br.form.find_control("messageBody").value = msg
57 self.br.form.find_control("counter").readonly = False
58 self.br.form.find_control("counter").value = str(160 - len(msg))
59
60 #return(self.br)
61 #f = open('out.html', 'w')
62 r = self.br.submit()
63 #f.write(r.read())
64 #r.seek(0)
65 return(r)
66