annotate scrape-vb.py @ 1:8045db05180b SCRAPEVB_1_0

Initial revision
author darius
date Sat, 25 Aug 2007 05:17:29 +0000
parents
children 89232ea0c3d4
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 #
8045db05180b Initial revision
darius
parents:
diff changeset
9 # $Id: scrape-vb.py,v 1.1.1.1 2007/08/25 05:17:29 darius Exp $
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
8045db05180b Initial revision
darius
parents:
diff changeset
37 import re, BeautifulSoup, datetime, time, smtplib, sys, urllib
8045db05180b Initial revision
darius
parents:
diff changeset
38
8045db05180b Initial revision
darius
parents:
diff changeset
39 #### Configuration
8045db05180b Initial revision
darius
parents:
diff changeset
40
8045db05180b Initial revision
darius
parents:
diff changeset
41 ### Travel criteria
8045db05180b Initial revision
darius
parents:
diff changeset
42 # Supported keys are email, when, city1, city2, maxcost
8045db05180b Initial revision
darius
parents:
diff changeset
43 # email is mandatory. If city2 is not present either city will be
8045db05180b Initial revision
darius
parents:
diff changeset
44 # matched. when and maxcost are optional (will match for any date or
8045db05180b Initial revision
darius
parents:
diff changeset
45 # cost)
8045db05180b Initial revision
darius
parents:
diff changeset
46 travellers = [
8045db05180b Initial revision
darius
parents:
diff changeset
47 { 'email' : 'darius@dons.net.au', 'city1' : 'Sydney' },
8045db05180b Initial revision
darius
parents:
diff changeset
48 { 'email' : 'sarah.mahoney@nehta.gov.au', 'city1' : 'Adelaide', 'city2' : 'Brisbane' },
8045db05180b Initial revision
darius
parents:
diff changeset
49 ]
8045db05180b Initial revision
darius
parents:
diff changeset
50
8045db05180b Initial revision
darius
parents:
diff changeset
51 ### Mail host
8045db05180b Initial revision
darius
parents:
diff changeset
52 mailhost = 'mail.dons.net.au'
8045db05180b Initial revision
darius
parents:
diff changeset
53
8045db05180b Initial revision
darius
parents:
diff changeset
54 ### Who the email is from
8045db05180b Initial revision
darius
parents:
diff changeset
55 mailfrom = 'darius@dons.net.au'
8045db05180b Initial revision
darius
parents:
diff changeset
56
8045db05180b Initial revision
darius
parents:
diff changeset
57 ### What's onn the subject linee
8045db05180b Initial revision
darius
parents:
diff changeset
58 mailsubj = 'Virgin Blue Happy Hour Deals'
8045db05180b Initial revision
darius
parents:
diff changeset
59
8045db05180b Initial revision
darius
parents:
diff changeset
60 ### Actually send email?
8045db05180b Initial revision
darius
parents:
diff changeset
61 mailsend = False
8045db05180b Initial revision
darius
parents:
diff changeset
62
8045db05180b Initial revision
darius
parents:
diff changeset
63 ### URL to parse
8045db05180b Initial revision
darius
parents:
diff changeset
64 vburl = 'http://virginblue.com.au'
8045db05180b Initial revision
darius
parents:
diff changeset
65
8045db05180b Initial revision
darius
parents:
diff changeset
66 parsetitle = re.compile('([a-z ]+) - ([a-z ]+) \$([0-9]+)', re.IGNORECASE)
8045db05180b Initial revision
darius
parents:
diff changeset
67 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
68
8045db05180b Initial revision
darius
parents:
diff changeset
69 try:
8045db05180b Initial revision
darius
parents:
diff changeset
70 #f = open("vb-happyhour.html")
8045db05180b Initial revision
darius
parents:
diff changeset
71 f = urllib.urlopen(vburl)
8045db05180b Initial revision
darius
parents:
diff changeset
72 except IOError, e:
8045db05180b Initial revision
darius
parents:
diff changeset
73 print "Unable to fetch page - " + str(e)
8045db05180b Initial revision
darius
parents:
diff changeset
74 sys.exit(1)
8045db05180b Initial revision
darius
parents:
diff changeset
75
8045db05180b Initial revision
darius
parents:
diff changeset
76 s = BeautifulSoup.BeautifulSoup(f)
8045db05180b Initial revision
darius
parents:
diff changeset
77 hrr = s.find("ul", "happyhr-rows")
8045db05180b Initial revision
darius
parents:
diff changeset
78 if (hrr == None):
8045db05180b Initial revision
darius
parents:
diff changeset
79 print "No happy hour details found"
8045db05180b Initial revision
darius
parents:
diff changeset
80 sys.exit(0)
8045db05180b Initial revision
darius
parents:
diff changeset
81
8045db05180b Initial revision
darius
parents:
diff changeset
82 hrlist = hrr.findAll("li")
8045db05180b Initial revision
darius
parents:
diff changeset
83
8045db05180b Initial revision
darius
parents:
diff changeset
84 # XXX: I wanted to use findAll('ul', 'happyhr-conditions') but it
8045db05180b Initial revision
darius
parents:
diff changeset
85 # doesn't work
8045db05180b Initial revision
darius
parents:
diff changeset
86 times = parsetper.match(s.findAll('ul')[11].find('li').string)
8045db05180b Initial revision
darius
parents:
diff changeset
87 if (times == None):
8045db05180b Initial revision
darius
parents:
diff changeset
88 print "Unable to parse travel period " + parsetper.match(s.findAll('ul')[11].find('li'))
8045db05180b Initial revision
darius
parents:
diff changeset
89 sys.exit(0)
8045db05180b Initial revision
darius
parents:
diff changeset
90
8045db05180b Initial revision
darius
parents:
diff changeset
91 frtime = datetime.datetime(*time.strptime(times.group(1), "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
92 totime = datetime.datetime(*time.strptime(times.group(2), "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
93
8045db05180b Initial revision
darius
parents:
diff changeset
94 #print "Travel from %s to %s" % (str(frtime), str(totime))
8045db05180b Initial revision
darius
parents:
diff changeset
95
8045db05180b Initial revision
darius
parents:
diff changeset
96 output = {}
8045db05180b Initial revision
darius
parents:
diff changeset
97 for i in hrlist:
8045db05180b Initial revision
darius
parents:
diff changeset
98 href = i.find('a')
8045db05180b Initial revision
darius
parents:
diff changeset
99 match = parsetitle.match(href['title'])
8045db05180b Initial revision
darius
parents:
diff changeset
100 if (match == None):
8045db05180b Initial revision
darius
parents:
diff changeset
101 print "Unable to match " + str(s)
8045db05180b Initial revision
darius
parents:
diff changeset
102 continue
8045db05180b Initial revision
darius
parents:
diff changeset
103
8045db05180b Initial revision
darius
parents:
diff changeset
104 city1 = match.group(1)
8045db05180b Initial revision
darius
parents:
diff changeset
105 city2 = match.group(2)
8045db05180b Initial revision
darius
parents:
diff changeset
106 cost = int(match.group(3))
8045db05180b Initial revision
darius
parents:
diff changeset
107 url = href['href']
8045db05180b Initial revision
darius
parents:
diff changeset
108
8045db05180b Initial revision
darius
parents:
diff changeset
109 for t in travellers:
8045db05180b Initial revision
darius
parents:
diff changeset
110 if ('email' not in t):
8045db05180b Initial revision
darius
parents:
diff changeset
111 print "No email key found, configuration error?"
8045db05180b Initial revision
darius
parents:
diff changeset
112 continue
8045db05180b Initial revision
darius
parents:
diff changeset
113
8045db05180b Initial revision
darius
parents:
diff changeset
114 citymatch = True
8045db05180b Initial revision
darius
parents:
diff changeset
115 if ('city1' in t and 'city2' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
116 if((t['city1'] != city1 or t['city2'] != city2) and
8045db05180b Initial revision
darius
parents:
diff changeset
117 (t['city1'] != city2 or t['city2'] != city1)):
8045db05180b Initial revision
darius
parents:
diff changeset
118 citymatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
119 elif ('city1' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
120 if (t['city1'] != city1 and t['city1'] != city2):
8045db05180b Initial revision
darius
parents:
diff changeset
121 citymatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
122
8045db05180b Initial revision
darius
parents:
diff changeset
123 datematch = True
8045db05180b Initial revision
darius
parents:
diff changeset
124 if ('when' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
125 travtime = datetime.datetime(*time.strptime(t['when'], "%d/%m/%y")[0:3])
8045db05180b Initial revision
darius
parents:
diff changeset
126 if (travtime < frtime or travtime > totime):
8045db05180b Initial revision
darius
parents:
diff changeset
127 datematch = False
8045db05180b Initial revision
darius
parents:
diff changeset
128
8045db05180b Initial revision
darius
parents:
diff changeset
129 costmatch = True
8045db05180b Initial revision
darius
parents:
diff changeset
130 if ('maxcost' in t):
8045db05180b Initial revision
darius
parents:
diff changeset
131 if (cost > int(t['maxcost'])):
8045db05180b Initial revision
darius
parents:
diff changeset
132 costmatch = False
8045db05180b Initial revision
darius
parents:
diff changeset
133
8045db05180b Initial revision
darius
parents:
diff changeset
134 if (citymatch and datematch and costmatch):
8045db05180b Initial revision
darius
parents:
diff changeset
135 if (t['email'] not in output):
8045db05180b Initial revision
darius
parents:
diff changeset
136
8045db05180b Initial revision
darius
parents:
diff changeset
137 output[t['email']] = []
8045db05180b Initial revision
darius
parents:
diff changeset
138 output[t['email']].append([city1, city2, cost, url])
8045db05180b Initial revision
darius
parents:
diff changeset
139
8045db05180b Initial revision
darius
parents:
diff changeset
140 if (mailsend):
8045db05180b Initial revision
darius
parents:
diff changeset
141 server = smtplib.SMTP(mailhost)
8045db05180b Initial revision
darius
parents:
diff changeset
142 #server.set_debuglevel(1)
8045db05180b Initial revision
darius
parents:
diff changeset
143
8045db05180b Initial revision
darius
parents:
diff changeset
144 for o in output:
8045db05180b Initial revision
darius
parents:
diff changeset
145 msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (mailfrom, o, mailsubj))
8045db05180b Initial revision
darius
parents:
diff changeset
146 msg = msg + "Your criteria for flights have been matched\r\n\r\n"
8045db05180b Initial revision
darius
parents:
diff changeset
147 print "Sending email to " + o
8045db05180b Initial revision
darius
parents:
diff changeset
148 for i in output[o]:
8045db05180b Initial revision
darius
parents:
diff changeset
149 print "%s <-> %s costs $%d" % (i[0], i[1], i[2])
8045db05180b Initial revision
darius
parents:
diff changeset
150 msg = msg + "%s <-> %s costs $%d - %s\r\n" % (i[0], i[1], i[2], i[3])
8045db05180b Initial revision
darius
parents:
diff changeset
151
8045db05180b Initial revision
darius
parents:
diff changeset
152 msg = msg + "\r\nNote: travel period is from %s to %s" % \
8045db05180b Initial revision
darius
parents:
diff changeset
153 (frtime.strftime("%A %e %B %Y"), totime.strftime("%A %e %B %Y"))
8045db05180b Initial revision
darius
parents:
diff changeset
154 if (mailsend):
8045db05180b Initial revision
darius
parents:
diff changeset
155 server.sendmail(mailfrom, o, msg)
8045db05180b Initial revision
darius
parents:
diff changeset
156 else:
8045db05180b Initial revision
darius
parents:
diff changeset
157 print msg
8045db05180b Initial revision
darius
parents:
diff changeset
158 print