-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapers.py
339 lines (260 loc) · 9.08 KB
/
scrapers.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from itertools import chain
import json
import re
import requests
import sys
from urllib.parse import unquote
from CLIppy import flatten, safe_encode, soup_me
def get_bar(query):
"""
query -> (barname, bar_url)
"""
BASE_URL = 'https://www.beermenus.com/{}'
PARAMS = {'q': safe_encode(query)}
soup = soup_me(BASE_URL.format('search'), PARAMS)
try:
PATTERN = re.compile('^/places/')
top_hit, *_ = [match for match in soup('h3', class_='mb-0 text-normal')
if match('a', href=PATTERN)]
except:
print('\ncouldn\'t find that bar...\n')
sys.exit(0)
barname = top_hit.a.text
bar_url = BASE_URL.format(top_hit.a['href'])
return barname, bar_url
def get_beers(bar_url):
"""
bar_url -> list of beernames, num on tap (listed 1st)
"""
soup = soup_me(bar_url, from_headless=True)
PATTERN = re.compile('^/beers/')
beers = [li for li in soup('li', class_='pure-list-item')
if li('a', href=PATTERN)]
beer_names = [beer.a.text for beer in beers]
beer_infos = [
(beer.find('p', class_='caption text-gray mb-0').text
.strip().split('·'))
for beer in beers
]
beer_servinginfos = [
[p.text.strip().split() for p in
beer('p', class_='caption text-right mb-0')]
for beer in beers
]
def make_tuple(lst, n):
""" fill in missing info, and strip extraneous spaces
"""
lst = list(lst) + [''] * n # pad with fallback if missing
return tuple(_.strip() for _ in lst)[:n] # filter to n-tuple
# TODO when info missing, detect abv ([0-9]%) and use to calibrate rest ?
KEYS_INFO = ('style', 'abv', 'where') #, 'serving')
KEYS_SERVINGINFO = ('volume', 'type', 'price')
# beerstyle, abv, beerplace
beer_infos = [make_tuple(info, len(KEYS_INFO)) for info in beer_infos]
# volume, servingtype, price
beer_servinginfos = [[make_tuple(info, len(KEYS_SERVINGINFO))
for info in filter(None, infos)]
for infos in beer_servinginfos]
d_stats = {
beername: dict(zip(chain(KEYS_INFO, ('serving',)),
chain(info, ([dict(zip(KEYS_SERVINGINFO,
servinginfo))
for servinginfo in servinginfos],))))
for beername, info, servinginfos in
zip(beer_names, beer_infos, beer_servinginfos)
}
return d_stats
# TODO wrapper to grab key from SECRETS ?
def get_reviews_ratebeer(query, beerpage=None, verbose=False):
""" Get beer stats
:query: query beername str
"""
if verbose:
print('ratebeer...')
BASE_URL_API = 'https://beta.ratebeer.com/v1/api/graphql/'
FIELDS = [
# 'id',
'name',
'averageRating',
'abv',
# 'overallScore',
'description',
# 'ratingCount'
]
UNRATED = '0.00'
UNABVED = None
data = {
'query': 'query beerSearch($query: String, $order: SearchOrder, $first: Int, $after: ID) { searchResultsArr: beerSearch(query: $query, order: $order, first: $first, after: $after) { totalCount last items { beer { %s __typename } review { id score __typename } __typename } __typename } }' % (' '.join(FIELDS)),
'variables': {'query': query, 'order': 'MATCH', 'first': 20},
'operationName': 'beerSearch'
}
d_hits = (
requests
.post(BASE_URL_API,
data=json.dumps(data),
headers={'content-type': 'application/json'})
.json()['data']['searchResultsArr']
)
if d_hits['totalCount'] == 0: # no match found
return {}
top_hit = d_hits['items'][0]['beer']
# mean rating = "?.xx" / 5
try:
rating = '{:.2f}'.format(top_hit['averageRating'])
except(TypeError): # None
rating = UNRATED
# abv = "?%"
try:
abv = '{:.2f}%'.format(top_hit['abv'])
except(TypeError): # None
abv = UNABVED
# TODO ?
# style
# where
# description
description = top_hit['description']
beer_stats = {
# 'abv': abv,
'description': description
# 'style': style,
# 'where': where,
}
if rating != UNRATED:
beer_stats['rating'] = rating
if abv != UNABVED:
beer_stats['abv'] = abv
return beer_stats
def get_reviews_untappd(query, beerpage=None, verbose=False):
""" Get beer stats
:query: query beername str
"""
if verbose:
print('untappd...')
BASE_URL = 'https://untappd.com/{}'
UNRATED = 'N/A'
UNABVED = 'No'
def get_beerpage(query):
"""
:query: beername query str
:returns: (name, id)
"""
PARAMS = {'q': safe_encode(query)}
soup = soup_me(BASE_URL.format('search'), PARAMS)
top_hit = soup.find('p', class_='name')
return BASE_URL.format(top_hit.a['href'])
try:
beerpage = beerpage if beerpage is not None else get_beerpage(query)
except(AttributeError): # not found
return {}
soup = soup_me(beerpage)
# mean rating = "?" / 5 #"?/5.0"
try:
rating = re.sub('[\(\)]', '',soup.find('span', class_='num').text)
except(AttributeError): # no match
assert soup.find('p', class_='info'), "oops - your IP's been blocked. skipping..."
return {}
# abv = "?%"
abv = re.sub(' ABV$', '', soup.find('p', class_='abv').text.strip())
# style
style = soup.find('p', class_='style').text
# TODO ?
# where
# long desc
try:
description = re.sub(re.compile(' ?Show Less$'), '', soup.find(
'div', class_="beer-descrption-read-less").text.strip())
except(AttributeError): # no match
description = ''
beer_stats = {
# 'abv': abv,
'style': style,
# 'where': where,
'description': description
}
if rating != UNRATED:
beer_stats['rating'] = rating
if abv != UNABVED:
beer_stats['abv'] = abv
return beer_stats
def get_reviews_beeradvocate(query, beerpage=None, verbose=False):
""" Get beer stats
:query: query beername str
"""
if verbose:
print('beeradvocate...')
BASE_URL = 'https://www.beeradvocate.com/{}'
UNRATED = '0'
UNABVED = None
def get_beerpage(query):
"""
:query: beername query str
:returns: (name, id)
"""
PARAMS = {'q': safe_encode(query),
'qt': 'beer'}
soup = soup_me(BASE_URL.format('search/'), PARAMS)
try: # redirected directly to beerpage
relative_url = re.match('^/beer/profile/', soup.find(
'input', attrs={'name': 'redirect'})['value']).string
except:
top_hit = soup.find('a', href=re.compile("^/beer/profile/"))
relative_url = top_hit['href']
return BASE_URL.format(relative_url)
queried = beerpage is None
try:
beerpage = beerpage if beerpage is not None else get_beerpage(query)
except(TypeError): # no hits
return {}
soup = soup_me(beerpage)
try:
assert 'beers' in {s.text.lower() for s in soup('span', itemprop='title')}
except(AssertionError): # i.e. place page rather than beer page
return (get_reviews_beeradvocate(query, verbose=verbose)
if not queried else {}) # if already tried querying beeradvocate, just leave
# mean rating = "?" / 5 # "?/5.0"
rating = soup.find('span', class_='ba-ravg').text
stats = soup('dd', class_='beerstats')
# abv = "?%"
try:
abv, = [s.text.strip() for s in stats if s.find(
'span', title='Percentage of alcohol by volume.')]
except(ValueError): # not reported
abv = UNABVED
# style
PATTERN = re.compile('^/beer/styles')
style, = [s.text.strip() for s in stats if s.find('a', href=PATTERN)]
# where = "state, country"
PATTERN = re.compile('^/place/directory')
where, = [s.text.strip() for s in stats if s.find('a', href=PATTERN)]
beer_stats = {
# 'abv': abv,
'style': style,
'where': where
}
if rating != UNRATED:
beer_stats['rating'] = rating
if abv != UNABVED:
beer_stats['abv'] = abv
return beer_stats
def get_beerpages_en_masse(query):
"""Get beer pages via google search
:returns: {site: beer_url} for subset of sites found
"""
D_SITES = dict(
untappd = 'https://untappd.com/b/',
ratebeer = 'https://www.ratebeer.com/beer/',
beeradvocate = 'https://www.beeradvocate.com/beer/'
)
BASE_URL = 'https://www.google.com/search'
PARAMS = {'q': safe_encode(query)}
soup = soup_me(BASE_URL, PARAMS)
extract_url = lambda site: re.sub('\/url\?q=([^&]*)&.*', '\\1', soup.find(
'a', href=re.compile('^/url\?q={}'.format(site)))['href'])
D_URLS ={}
for k, v in D_SITES.items():
try:
D_URLS[k] = unquote(extract_url(v)) # fix % encoding
except(TypeError): # not found
pass
# return {k: extract_url(v) for k,v in D_SITES.items()}
return D_URLS