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