-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_parser.py
83 lines (68 loc) · 3.44 KB
/
response_parser.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
# This function parses amazon response
def parse_response(response):
if hasattr(response, 'search_result'):
items = response.search_result.items
else:
items = response
res_items = []
if items is not None:
for item_0 in items:
it_parsed = {}
if item_0 is not None:
# if contains an image
if item_0.images.primary.large is not None:
it_parsed["image"] = item_0.images.primary.large.url
# if contains a description
if item_0.item_info.features is not None and item_0.item_info.features.display_values is not None:
desc = ""
tmp = item_0.item_info.features.display_values
length = 0
for el in tmp:
if length < 3:
desc += el
length += 1
else:
break
if len(desc) > 24:
it_parsed["description"] = desc[0:24]
# if contains price and/or offer price
if item_0.offers is not None and item_0.offers.listings[0] and item_0.offers.listings[
0].price is not None and item_0.offers.listings[0].price.savings is not None:
if item_0.offers.listings[0].is_buy_box_winner is not None:
it_parsed["off"] = item_0.offers.listings[0].is_buy_box_winner
it_parsed["savings"] = item_0.offers.listings[0].price.savings.amount
op = float(item_0.offers.listings[0].price.savings.amount) + float(
item_0.offers.listings[0].price.amount)
it_parsed["original_price"] = '%.2f' % (op)
# get item id
if item_0.asin is not None:
it_parsed["id"] = item_0.asin
# get item amazon url
if item_0.detail_page_url is not None:
it_parsed["url"] = item_0.detail_page_url
# get item title
if (
item_0.item_info is not None
and item_0.item_info.title is not None
and item_0.item_info.title.display_value is not None
):
it_parsed["title"] = item_0.item_info.title.display_value
# get item price
if (
item_0.offers is not None
and item_0.offers.listings is not None
and item_0.offers.listings[0].price is not None
and item_0.offers.listings[0].price.display_amount is not None
):
it_parsed["price"] = f'{item_0.offers.listings[0].price.display_amount}'
# get savings percentage
if (
item_0.offers is not None
and item_0.offers.listings is not None
and item_0.offers.listings[0].price is not None
and item_0.offers.listings[0].price.savings is not None
and item_0.offers.listings[0].price.savings.percentage is not None
):
it_parsed["percentage"] = f'{item_0.offers.listings[0].price.savings.percentage}'
res_items.append(it_parsed)
return res_items