annotate scrape-vb.py @ 9:3e03facad74b default tip

New example files for latest layout.
author darius
date Thu, 18 Oct 2007 06:58:00 +0000
parents d17fd6f3a492
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
8045db05180b Initial revision
darius
parents:
diff changeset
1 #!/usr/bin/env python
8045db05180b Initial revision
darius
parents:
diff changeset
2
8045db05180b Initial revision
darius
parents:
diff changeset
3 ############################################################################
8045db05180b Initial revision
darius
parents:
diff changeset
4 # Screen scraper for Virgin Blue to look for happy hour deals
8045db05180b Initial revision
darius
parents:
diff changeset
5 #
8045db05180b Initial revision
darius
parents:
diff changeset
6 # Prints out (and emails) when criteria match based on cost,
8045db05180b Initial revision
darius
parents:
diff changeset
7 # destination, etc
8045db05180b Initial revision
darius
parents:
diff changeset
8 #
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
9 # $Id: scrape-vb.py,v 1.7 2007/10/18 06:57:35 darius Exp $
1
8045db05180b Initial revision
darius
parents:
diff changeset
10 ############################################################################
8045db05180b Initial revision
darius
parents:
diff changeset
11 #
8045db05180b Initial revision
darius
parents:
diff changeset
12 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
8045db05180b Initial revision
darius
parents:
diff changeset
13 #
8045db05180b Initial revision
darius
parents:
diff changeset
14 # Redistribution and use in source and binary forms, with or without
8045db05180b Initial revision
darius
parents:
diff changeset
15 # modification, are permitted provided that the following conditions
8045db05180b Initial revision
darius
parents:
diff changeset
16 # are met:
8045db05180b Initial revision
darius
parents:
diff changeset
17 # 1. Redistributions of source code must retain the above copyright
8045db05180b Initial revision
darius
parents:
diff changeset
18 # notice, this list of conditions and the following disclaimer.
8045db05180b Initial revision
darius
parents:
diff changeset
19 # 2. Redistributions in binary form must reproduce the above copyright
8045db05180b Initial revision
darius
parents:
diff changeset
20 # notice, this list of conditions and the following disclaimer in the
8045db05180b Initial revision
darius
parents:
diff changeset
21 # documentation and/or other materials provided with the distribution.
8045db05180b Initial revision
darius
parents:
diff changeset
22 #
8045db05180b Initial revision
darius
parents:
diff changeset
23 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
8045db05180b Initial revision
darius
parents:
diff changeset
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8045db05180b Initial revision
darius
parents:
diff changeset
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8045db05180b Initial revision
darius
parents:
diff changeset
26 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
8045db05180b Initial revision
darius
parents:
diff changeset
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8045db05180b Initial revision
darius
parents:
diff changeset
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8045db05180b Initial revision
darius
parents:
diff changeset
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8045db05180b Initial revision
darius
parents:
diff changeset
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8045db05180b Initial revision
darius
parents:
diff changeset
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8045db05180b Initial revision
darius
parents:
diff changeset
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8045db05180b Initial revision
darius
parents:
diff changeset
33 # SUCH DAMAGE.
8045db05180b Initial revision
darius
parents:
diff changeset
34 #
8045db05180b Initial revision
darius
parents:
diff changeset
35 ############################################################################
8045db05180b Initial revision
darius
parents:
diff changeset
36
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
37 import os, re, BeautifulSoup, datetime, time, smtplib, sys, urllib
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
38 import ConfigParser, optparse, SMSVodaAu
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
39
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
40 usage = '''%prog [options]
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
41 Reads configuration from ./scrape-vb.ini and ~/.scrape-vb.ini'''
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
42
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
43 optparse = optparse.OptionParser(usage, version="$Id: scrape-vb.py,v 1.7 2007/10/18 06:57:35 darius Exp $")
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
44 optparse.add_option('-d', '--debug', action="store_true", default=False,
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
45 help="Disable mail & SMS sending, prints message to stdout")
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
46 optparse.add_option('-f', '--file', help="Do not fetch the page, use this file instead")
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
47 optparse.add_option('-e', '--example', action="store_true", default=False,
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
48 help="Print an example configuration file to stdout and exit")
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
49 (options, args) = optparse.parse_args()
1
8045db05180b Initial revision
darius
parents:
diff changeset
50
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
51 if (options.example):
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
52 print '''[global]
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
53 mailsubj="Subject line for emails"
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
54 # The following 3 options are necessary before email will be sent
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
55 mailfrom=user@host.com
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
56 mailsend=True
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
57 mailhost=mail.server.com
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
58 smsuser=0412312312
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
59 smspass=mys3krit
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
60 smssend=True
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
61
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
62 [user@host.com]
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
63 # All fields are optional
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
64 city1=Foo
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
65 city2=Bar
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
66 when=dd/mm/yy
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
67 maxcost=123
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
68 phone=0498765432
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
69 '''
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
70 sys.exit(0)
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
71
1
8045db05180b Initial revision
darius
parents:
diff changeset
72 parsetper = re.compile('Travel Period: ([0-9]+/[0-9]+/[0-9]+) - ([0-9]+/[0-9]+/[0-9]+)', re.IGNORECASE)
8045db05180b Initial revision
darius
parents:
diff changeset
73
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
74 conf = ConfigParser.ConfigParser()
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
75 conf.add_section('global')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
76 conf.set('global', 'mailsubj', 'Virgin Blue Happy Hour Deals')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
77 conf.set('global', 'vburl', 'http://virginblue.com.au')
5
275603a8e2ae Read config file from $HOME as well as CWD (use dotfile for $HOME)
darius
parents: 4
diff changeset
78
275603a8e2ae Read config file from $HOME as well as CWD (use dotfile for $HOME)
darius
parents: 4
diff changeset
79 conflist = ['scrape-vb.ini']
275603a8e2ae Read config file from $HOME as well as CWD (use dotfile for $HOME)
darius
parents: 4
diff changeset
80 if ('HOME' in os.environ):
275603a8e2ae Read config file from $HOME as well as CWD (use dotfile for $HOME)
darius
parents: 4
diff changeset
81 conflist.append(os.path.expanduser('~/.scrape-vb.ini'))
275603a8e2ae Read config file from $HOME as well as CWD (use dotfile for $HOME)
darius
parents: 4
diff changeset
82 conf.read(conflist)
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
83
1
8045db05180b Initial revision
darius
parents:
diff changeset
84 try:
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
85 if (options.file != None):
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
86 f = open(options.file)
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
87 else:
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
88 f = urllib.urlopen(conf.get('global', 'vburl'))
1
8045db05180b Initial revision
darius
parents:
diff changeset
89 except IOError, e:
8045db05180b Initial revision
darius
parents:
diff changeset
90 print "Unable to fetch page - " + str(e)
8045db05180b Initial revision
darius
parents:
diff changeset
91 sys.exit(1)
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
92
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
93 # Test if we have been configured to send SMSs
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
94 try:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
95 smsuser = conf.get('global', 'smsuser')
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
96 smspass = conf.get('global', 'smspass')
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
97 smssend = conf.getboolean('global', 'smssend')
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
98 except ConfigParser.NoOptionError:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
99 smssend = False
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
100
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
101 if (options.debug == True and smssend):
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
102 print "smssend overridden due to debugging"
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
103 smssend = False
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
104
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
105 if (smssend):
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
106 smshndl = SMSVodaAu.SMSVodaAu(smsuser, smspass)
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
107
1
8045db05180b Initial revision
darius
parents:
diff changeset
108 s = BeautifulSoup.BeautifulSoup(f)
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
109 citypairs = s.findAll("td", "city-pair")
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
110 if (citypairs == []):
1
8045db05180b Initial revision
darius
parents:
diff changeset
111 print "No happy hour details found"
8045db05180b Initial revision
darius
parents:
diff changeset
112 sys.exit(0)
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
113
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
114 prices = s.findAll("td", "dash-r price")
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
115 if (prices == []):
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
116 print "Couldn't find prices"
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
117 sys.exit(0)
1
8045db05180b Initial revision
darius
parents:
diff changeset
118
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
119 if (len(citypairs) != len(prices)):
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
120 print "City pair & price tables don't have equal size"
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
121 sys.exit(0)
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
122
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
123 times = parsetper.search(s.find('p', 'tandc').string)
1
8045db05180b Initial revision
darius
parents:
diff changeset
124 if (times == None):
8045db05180b Initial revision
darius
parents:
diff changeset
125 print "Unable to parse travel period " + parsetper.match(s.findAll('ul')[11].find('li'))
8045db05180b Initial revision
darius
parents:
diff changeset
126 sys.exit(0)
8045db05180b Initial revision
darius
parents:
diff changeset
127
8045db05180b Initial revision
darius
parents:
diff changeset
128 frtime = datetime.datetime(*time.strptime(times.group(1), "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
129 totime = datetime.datetime(*time.strptime(times.group(2), "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
130
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
131 #
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
132 # Go through the HTML and work out who wants to be notified of what
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
133 #
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
134 # Store in output, a dictionary keyed by email adddress which holds a
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
135 # list of each matching flight (city1, city2, cost, url)
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
136 #
1
8045db05180b Initial revision
darius
parents:
diff changeset
137 output = {}
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
138 for i, p in zip(citypairs, prices):
1
8045db05180b Initial revision
darius
parents:
diff changeset
139 href = i.find('a')
8045db05180b Initial revision
darius
parents:
diff changeset
140
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
141 city1 = href.next.strip()
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
142 city2 = href.next.next.next.next.next.strip()
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
143 cost = int(p.find('a').string.strip('$^ '))
1
8045db05180b Initial revision
darius
parents:
diff changeset
144 url = href['href']
8045db05180b Initial revision
darius
parents:
diff changeset
145
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
146 for email in conf.sections():
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
147 if (email == 'global'):
1
8045db05180b Initial revision
darius
parents:
diff changeset
148 continue
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
149 # Stuff configuration into a dictionary for our convenience
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
150 t = {'email' : email}
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
151 for i in conf.items(email):
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
152 t[i[0]] = i[1]
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
153
1
8045db05180b Initial revision
darius
parents:
diff changeset
154 citymatch = True
8045db05180b Initial revision
darius
parents:
diff changeset
155 if ('city1' in t and 'city2' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
156 if((t['city1'] != city1 or t['city2'] != city2) and
8045db05180b Initial revision
darius
parents:
diff changeset
157 (t['city1'] != city2 or t['city2'] != city1)):
8045db05180b Initial revision
darius
parents:
diff changeset
158 citymatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
159 elif ('city1' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
160 if (t['city1'] != city1 and t['city1'] != city2):
8045db05180b Initial revision
darius
parents:
diff changeset
161 citymatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
162
8045db05180b Initial revision
darius
parents:
diff changeset
163 datematch = True
8045db05180b Initial revision
darius
parents:
diff changeset
164 if ('when' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
165 travtime = datetime.datetime(*time.strptime(t['when'], "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
166 if (travtime < frtime or travtime > totime):
8045db05180b Initial revision
darius
parents:
diff changeset
167 datematch = False
8045db05180b Initial revision
darius
parents:
diff changeset
168
8045db05180b Initial revision
darius
parents:
diff changeset
169 costmatch = True
8045db05180b Initial revision
darius
parents:
diff changeset
170 if ('maxcost' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
171 if (cost > int(t['maxcost'])):
8045db05180b Initial revision
darius
parents:
diff changeset
172 costmatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
173
8045db05180b Initial revision
darius
parents:
diff changeset
174 if (citymatch and datematch and costmatch):
8045db05180b Initial revision
darius
parents:
diff changeset
175 if (t['email'] not in output):
8045db05180b Initial revision
darius
parents:
diff changeset
176
8045db05180b Initial revision
darius
parents:
diff changeset
177 output[t['email']] = []
8045db05180b Initial revision
darius
parents:
diff changeset
178 output[t['email']].append([city1, city2, cost, url])
8045db05180b Initial revision
darius
parents:
diff changeset
179
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
180 # Test if we have been configured to send email
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
181 try:
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
182 mailsubj = conf.get('global', 'mailsubj')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
183 mailhost = conf.get('global', 'mailhost')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
184 mailsend = conf.getboolean('global', 'mailsend')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
185 mailfrom = conf.get('global', 'mailfrom')
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
186 except ConfigParser.NoOptionError:
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
187 mailsend = False
6
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
188
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
189 if (options.debug == True and mailsend):
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
190 print "mailsend overridden due to debugging"
9f3eb9a07966 Add config parser.
darius
parents: 5
diff changeset
191 mailsend = False
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
192
1
8045db05180b Initial revision
darius
parents:
diff changeset
193 if (mailsend):
8045db05180b Initial revision
darius
parents:
diff changeset
194 server = smtplib.SMTP(mailhost)
8045db05180b Initial revision
darius
parents:
diff changeset
195 #server.set_debuglevel(1)
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
196
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
197 #
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
198 # Output the various notifications
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
199 #
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
200 ttimestr = "Note: travel period is from %s to %s" % \
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
201 (frtime.strftime("%A %e %B %Y"), totime.strftime("%A %e %B %Y"))
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
202
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
203 # Email each person about their flights
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
204 if (mailsend):
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
205 for o in output:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
206 msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (mailfrom, o, mailsubj)
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
207 msg = msg + "Your criteria for flights have been matched\r\n\r\n"
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
208 for i in output[o]:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
209 msg = msg + "%s <-> %s costs $%d - %s\r\n" % (i[0], i[1], i[2], i[3])
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
210
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
211 msg = msg + "\r\n" + ttimestr + "\r\n"
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
212 server.sendmail(mailfrom, o, msg)
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
213
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
214 else:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
215 # If not emailing print to stdout
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
216 for o in output:
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
217 print "Match for " + o
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
218 for i in output[o]:
3
89232ea0c3d4 Read configuration from an ini file rather than hard coding it in the
darius
parents: 1
diff changeset
219 print "%s <-> %s costs $%d" % (i[0], i[1], i[2])
1
8045db05180b Initial revision
darius
parents:
diff changeset
220
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
221 # SMS each person about their flights
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
222 if (smssend):
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
223 for o in output:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
224 if (conf.has_option(o, 'phone')):
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
225 pnum = conf.get(o, 'phone')
7
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
226 msg = ""
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
227 for i in output[o]:
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
228 msg = msg + "%s <-> %s $%d, " % (i[0], i[1], i[2])
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
229 # Chop off the last , & make sure the whole message is not
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
230 # too large.
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
231 msgend = min(len(msg) - 2, 160)
bf896507faa9 Add code to send an SMS if configured to do so.
darius
parents: 6
diff changeset
232 print msg[0:msgend]
8
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
233 try:
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
234 smshndl.sendamsg(pnum, msg[0:msgend])
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
235 print "Sent SMS to " + pnum
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
236 except:
d17fd6f3a492 - Catch up with new VB page layout.
darius
parents: 7
diff changeset
237 print "Unable to send SMS to " + pnum