-
Notifications
You must be signed in to change notification settings - Fork 1
/
amazon_feed.py
344 lines (252 loc) · 10.7 KB
/
amazon_feed.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
340
341
342
343
344
import re
from datetime import datetime
from urllib.parse import quote_plus, urlencode, urlparse
import nh3
from bs4 import BeautifulSoup
from flask import abort
from requests.exceptions import JSONDecodeError, RequestException
from requests_cache import AnyResponse
from amazon_feed_data import (BOT_PATTERN, AmazonAsinQuery, AmazonKeywordQuery,
FilterableQuery)
from json_feed_data import JSONFEED_VERSION_URL, JsonFeedItem, JsonFeedTopLevel
ITEM_QUANTITY = 1
allowed_tags = {"a", "img", "p"}
allowed_attributes = {"a": {"href", "title"}, "img": {"src"}}
STREAM_DELIMITER = "&&&" # application/json-amazonui-streaming
# mimic headers from Firefox 84.0
headers = {
"Accept": "text/html,*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"X-Requested-With": "XMLHttpRequest",
"Connection": "keep-alive",
"Content-Type": "application/json",
"TE": "Trailers",
}
def reset_query_session(query: FilterableQuery):
query.config.useragent = ""
query.config.session.cookies.clear()
def handle_response(response: AnyResponse, query: FilterableQuery):
logger = query.config.logger
try:
return response.json()
except JSONDecodeError as jdex:
logger.error(f'"{query.query_str}" - {type(jdex)}: {jdex}')
logger.debug(f'"{query.query_str}" - dumping response: {response.text}')
return None
def get_response_dict(url: str, query: FilterableQuery):
logger = query.config.logger
session = query.config.session
headers["User-Agent"] = query.config.useragent
headers["Referer"] = "https://" + query.locale.domain + "/"
session.headers = headers
logger.debug(f'"{query.query_str}" - querying endpoint: {url}')
try:
response = session.get(url)
except RequestException as rex:
reset_query_session(query)
logger.error(f'"{query.query_str}" - {type(rex)}: {rex}')
return None
# return HTTP error code
if not response.ok:
if response.status_code == 503 or re.search(BOT_PATTERN, response.text):
bot_msg = f'"{query.query_str}" - API paywall triggered, resetting session'
reset_query_session(query)
logger.warning(bot_msg)
abort(429, description=bot_msg)
else:
logger.error(f'"{query.query_str}" - error from source')
logger.debug(f'"{query.query_str}" - dumping response: {response.text}')
return None
else:
logger.debug(f'"{query.query_str}" - response cached: {response.from_cache}')
return handle_response(response, query)
def get_search_url(base_url: str, query: FilterableQuery, is_xhr: bool = True):
search_uri = f"{base_url}/s/query?" if is_xhr else f"{base_url}/s?"
search_dict = {"k": quote_plus(query.query_str)}
price_param_value = min_price = max_price = None
if query.min_price or query.max_price:
price_param = "p_36:"
if query.min_price:
min_price = query.min_price + "00"
if query.max_price:
max_price = query.max_price + "00"
price_param_value = "".join(
item for item in [price_param, min_price, "-", max_price] if item
)
if price_param_value:
search_dict["rh"] = price_param_value
return search_uri + urlencode(search_dict)
def get_item_url(base_url: str, item_id: str):
return base_url + "/gp/product/" + item_id
def get_top_level_feed(
base_url: str, query: FilterableQuery, feed_items: list[JsonFeedItem]
):
parse_object = urlparse(base_url)
domain = parse_object.netloc
title_strings = [domain, query.query_str]
filters = []
if isinstance(query, AmazonKeywordQuery):
home_page_url = get_search_url(base_url, query, is_xhr=False)
if query.strict:
filters.append("strict")
elif isinstance(query, AmazonAsinQuery):
home_page_url = get_item_url(base_url, query.query_str)
if query.min_price:
filters.append(f"min {query.locale.currency}{query.min_price}")
if query.max_price:
filters.append(f"max {query.locale.currency}{query.max_price}")
if filters:
title_strings.append(f"filtered by {', '.join(filters)}")
json_feed = JsonFeedTopLevel(
version=JSONFEED_VERSION_URL,
items=feed_items,
title=" - ".join(title_strings),
home_page_url=home_page_url,
favicon=base_url + "/favicon.ico",
)
return json_feed
def generate_item(
base_url: str,
item_id: str,
item_title: str,
item_price_text: str,
item_thumbnail_url: str,
):
item_title_text = item_title.strip() if item_title else item_id
item_thumbnail_html = f'<img src="{item_thumbnail_url}" />'
timestamp = datetime.now().timestamp()
item_link_url = get_item_url(base_url, item_id)
item_link_html = f'<p><a href="{item_link_url}">Product Link</a></p>'
item_add_to_cart_url = (
f"{base_url}/gp/aws/cart/add.html?ASIN.1={item_id}&Quantity.1={ITEM_QUANTITY}"
)
item_add_to_cart_html = f'<p><a href="{item_add_to_cart_url}">Add to Cart</a></p>'
content_body_list = [item_link_html, item_add_to_cart_html]
if item_thumbnail_url:
content_body_list.insert(0, item_thumbnail_html)
content_body = "".join(content_body_list)
sanitized_html = nh3.clean(
content_body, tags=allowed_tags, attributes=allowed_attributes
).replace(
"&", "&"
) # restore raw ampersands: https://github.com/mozilla/bleach/issues/192
feed_item = JsonFeedItem(
id=datetime.utcfromtimestamp(timestamp).isoformat("T"),
url=item_link_url,
title=f"[{item_price_text}] {item_title_text}",
content_html=sanitized_html,
image=item_thumbnail_url,
date_published=datetime.utcfromtimestamp(timestamp).isoformat("T"),
)
return feed_item
def get_keyword_results(search_query: AmazonKeywordQuery):
logger = search_query.config.logger
base_url = "https://" + search_query.locale.domain
search_url = get_search_url(base_url, search_query)
json_dict = get_response_dict(search_url, search_query)
results_dict = (
{
k: v
for k, v in json_dict.items()
if k.startswith("data-main-slot:search-result-")
}
if json_dict
else {}
)
term_list: list[str] = []
if search_query.strict:
term_list = set([term.lower() for term in search_query.query_str.split()])
logger.debug(
f'"{search_query.query_str}" - strict mode enabled, title or asin must contain: {term_list}'
)
results_count = len(results_dict)
generated_items = []
for result in results_dict.values():
item_id: str = result.get("asin")
item_soup = BeautifulSoup(result.get("html"), features="html.parser")
# select product title, use wildcard CSS selector for better international compatibility
item_title_soup = item_soup.select_one("[class*='s-line-clamp-']")
item_title = item_title_soup.text.strip() if item_title_soup else ""
item_price_soup = item_soup.select_one(".a-price .a-offscreen")
item_price = item_price_soup.text.strip() if item_price_soup else None
item_price_text = item_price if item_price else "N/A"
# reformat discounted price
if item_price_soup and item_price_soup.select_one("span.price-large"):
price_strings = list(item_price_soup.stripped_strings)
item_price_text = (
price_strings[0] + price_strings[1] + "." + price_strings[2]
)
item_thumbnail_soup = item_soup.find(
attrs={"data-component-type": "s-product-image"}
)
item_thumbnail_img_soup = item_thumbnail_soup.select_one(".s-image")
item_thumbnail_url = (
item_thumbnail_img_soup.get("src") if item_thumbnail_img_soup else None
)
if item_price_soup:
# search term must exist in item title or ASIN
if search_query.strict and (
term_list
and not (
all(item_title.lower().find(term) >= 0 for term in term_list)
or item_id == search_query.query_str
)
):
logger.debug(
f'"{search_query.query_str}" - strict mode - removed {item_id} "{item_title}"'
)
else:
feed_item = generate_item(
base_url, item_id, item_title, item_price_text, item_thumbnail_url
)
generated_items.append(feed_item)
logger.info(
f'"{search_query.query_str}" - found {results_count} - published {len(generated_items)}'
)
json_feed = get_top_level_feed(base_url, search_query, generated_items)
return json_feed
def get_dimension_url(query: AmazonAsinQuery, item_id: str):
# Call the "dimension" endpoint which is used on mobile pages
# to display price and optionally availability for product variants
locale_data = query.locale
base_url = "https://" + locale_data.domain
dimension_endpoint = base_url + "/gp/product/ajax?"
query_dict = {
"asinList": item_id,
"experienceId": "twisterDimensionSlotsDefault",
"asin": item_id,
"deviceType": "mobile",
}
return dimension_endpoint + urlencode(query_dict)
def get_item_listing(query: AmazonAsinQuery):
logger = query.config.logger
item_id = query.query_str
base_url = "https://" + query.locale.domain
item_dimension_url = get_dimension_url(query, item_id)
json_dict = get_response_dict(item_dimension_url, query)
item_price: str = ""
if json_dict:
# Assume one item is returned per response
result = (
json_dict.get("Value", {}).get("content", {}).get("twisterSlotJson", {})
)
item_price = result.get("price")
json_feed = get_top_level_feed(base_url, query, [])
if not item_price:
logger.error(query.query_str + " - price not found")
return json_feed
# exit if exceeded max price
if item_price and query.max_price:
item_price_clean = "".join(filter(str.isnumeric, item_price))
# handle currencies without decimal places
max_price_clean = (
query.max_price + "00" if "." in item_price else query.max_price
)
if float(item_price_clean) > float(max_price_clean):
logger.info(f'"{query.query_str}" - exceeded max price {query.max_price}')
return json_feed
formatted_price = query.locale.currency + item_price
feed_item = generate_item(base_url, item_id, "", formatted_price, "")
json_feed = get_top_level_feed(base_url, query, [feed_item])
return json_feed