/
trip_plan_generator.py
208 lines (178 loc) · 7.65 KB
/
trip_plan_generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from cStringIO import StringIO
import re
import string
import urlparse
from lxml import etree
import requests
import clip_logic
import data
import google_places
import geocode
from scraping import html_parsing
import serviceimpls
def parse_tree(url):
response = requests.get(url)
parser = etree.HTMLParser()
return etree.parse(StringIO(response.text.encode('utf-8')), parser)
class EntityData(object):
def __init__(self, name=None, address=None, phone=None, website=None):
self.name = name
self.address = address
self.phone = phone
self.website = website
class GoogleBasedAutoTripPlanCreator(object):
def __init__(self, url, creator=None, trip_plan_name=None, address_context=None,
use_address=False):
self.url = url
self.root = None
self.creator = creator
self.trip_plan_name = trip_plan_name
self.address_context = address_context
self.use_address = use_address
def getroot(self):
if self.root is None:
self.root = parse_tree(self.url).getroot()
return self.root
def get_trip_plan_name(self):
return self.trip_plan_name
def get_creator(self):
if self.creator:
return self.creator
host = urlparse.urlparse(self.url).netloc.lower().lstrip('www.')
return 'admin@%s' % host
def lookup_entities(self, entity_datas):
entities = []
address_context = self.get_address_context()
for entity_data in entity_datas:
if entity_data.address and self.use_address:
query = '%s, %s, %s' % (entity_data.name, entity_data.address, address_context)
else:
query = '%s, %s' % (entity_data.name, address_context)
place_result = geocode.lookup_place(query)
if not place_result or not place_result.get_reference():
print 'Could not find a place for entity data: %s' % entity_data.__dict__
continue
place = google_places.lookup_place_by_reference(place_result.get_reference())
entities.append(place.to_entity())
return self.process_entities(entities)
def process_entities(self, entities):
references_seen = set()
output_entities = []
for entity in entities:
if entity.google_reference in references_seen:
continue
references_seen.add(entity.google_reference)
entity.source_url = self.url
output_entities.append(entity)
return output_entities
def build_from_entity_data(self, entity_datas):
entities = self.lookup_entities(entity_datas)
trip_plan = create_trip_plan(self.get_trip_plan_name(), self.get_creator())
add_entities_to_trip_plan(trip_plan.trip_plan_id, self.get_creator(), entities)
print 'Created trip plan %d with %d entities' % (trip_plan.trip_plan_id, len(entities))
def get_address_context(self):
return self.address_context
def run():
raise NotImplementedError()
# Parsing 2014-style articles like
# http://www.nytimes.com/2014/04/13/travel/36-hours-in-seville-spain.html
class Nytimes36hours(GoogleBasedAutoTripPlanCreator):
NUMBERED_LINE_RE = re.compile('^\d+\..*')
def run(self):
entity_datas = []
for p in self.getroot().findall('.//footer//div[@class="story-info"]//p'):
line_text = html_parsing.tostring(p, with_tail=False)
if self.NUMBERED_LINE_RE.match(line_text):
for child in p.iterchildren():
tag = child.tag.lower()
text = html_parsing.tostring(child, with_tail=False)
if tag == 'strong':
if self.NUMBERED_LINE_RE.match(text):
name = text.split('.')[1]
else:
name = text
current_entity = EntityData(name=name.strip().strip(string.punctuation))
entity_datas.append(current_entity)
elif tag == 'a':
current_entity.website = child.get('href')
tail = child.tail.strip().strip(string.punctuation) if child.tail else ''
if tail:
parts = tail.split(';')
current_entity.address = parts[0].strip()
if len(parts) >= 2:
current_entity.phone = parts[1].strip()
self.build_from_entity_data(entity_datas)
def get_address_context(self):
return html_parsing.tostring(self.getroot().find('.//h1[@itemprop="headline"]')).split(' in ')[1].strip()
def get_trip_plan_name(self):
base_name = super(Nytimes36hours, self).get_trip_plan_name()
if base_name:
return base_name
return html_parsing.tostring(self.getroot().find('.//h1[@itemprop="headline"]'))
class TripAdvisor3Days(object):
def __init__(self, url, creator=None, trip_plan_name=None):
self.url = url
self.root = None
self.creator = creator
self.trip_plan_name = trip_plan_name
def getroot(self):
if self.root is None:
self.root = parse_tree(self.url).getroot()
return self.root
def get_trip_plan_name(self):
if self.trip_plan_name:
return self.trip_plan_name
return html_parsing.tostring(self.getroot().find('.//h1[@id="HEADING"]'))
def get_creator(self):
if self.creator:
return self.creator
host = urlparse.urlparse(self.url).netloc.lower().lstrip('www.')
return 'admin@%s' % host
def absolute_url(self, relative_url):
return urlparse.urljoin(self.url, relative_url)
def build_from_entities(self, entities):
trip_plan = create_trip_plan(self.get_trip_plan_name(), self.get_creator())
add_entities_to_trip_plan(trip_plan.trip_plan_id, self.get_creator(), entities)
print 'Created trip plan %d with %d entities' % (trip_plan.trip_plan_id, len(entities))
def run(self):
links = self.getroot().findall('.//div[@class="guideItemInfo"]//a[@class="titleLink"]')
urls_to_scrape = [self.absolute_url(link.get('href')) for link in links]
entities = [clip_logic.scrape_entity_from_url(url) for url in urls_to_scrape]
self.build_from_entities(entities)
def create_trip_plan(name, creator):
session_info = data.SessionInfo(email=creator)
trip_plan = data.TripPlan(name=name)
add_op = serviceimpls.TripPlanOperation(serviceimpls.Operator.ADD.name, trip_plan)
add_req = serviceimpls.TripPlanMutateRequest(operations=[add_op])
response = serviceimpls.TripPlanService(session_info).mutate(add_req)
return response.trip_plans[0]
def add_entities_to_trip_plan(trip_plan_id, creator, entities):
session_info = data.SessionInfo(email=creator)
operations = []
for entity in entities:
add_op = serviceimpls.EntityOperation(
serviceimpls.Operator.ADD.name, trip_plan_id, entity=entity)
operations.append(add_op)
add_req = serviceimpls.EntityMutateRequest(operations=operations)
response = serviceimpls.EntityService(session_info).mutate(add_req)
return response.entities
USAGE = '''Usage:
python trip_plan_generator.py <url>'''
def main(argv):
if len(argv) < 2:
print USAGE
return
url = argv[1]
host = urlparse.urlparse(url).netloc.lower()
if 'nytimes' in host:
s = Nytimes36hours(url)
elif 'tripadvisor.com' in host:
s = TripAdvisor3Days(url)
else:
print 'Unrecognized url'
print USAGE
return
s.run()
if __name__ == '__main__':
import sys
main(sys.argv)