-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchBooks.py
124 lines (90 loc) · 3.98 KB
/
fetchBooks.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
from amazon.api import AmazonAPI
import idx_config as conf
from algoliasearch import algoliasearch
import json
import os
import ssl
if (not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None)):
ssl._create_default_https_context = ssl._create_unverified_context
amazon = AmazonAPI(conf.AMAZON_ACCESS_KEY, conf.AMAZON_SECRET_KEY, conf.AMAZON_ASSOC_TAG)
# bn = amazon.browse_node_lookup(BrowseNodeId=1000)
#
# print(bn.)
products = amazon.search_n(500,
Keywords="marketing",
SearchIndex='Books',
# Sort='salesrank',
# Sort='inverse-pricerank'
# ResponseGroup='SalesRank',
Sort='relevancerank',
# Sort='daterank'
# Sort='-unit-sales'
)
#TODO: https://docs.aws.amazon.com/AWSECommerceService/latest/DG/SortingbyPopularityPriceorCondition.html
# 'The value you specified for Sort is invalid. Valid values include 'relevancerank','salesrank','reviewrank','pricerank','inverse-pricerank','daterank','titlerank','-titlerank','-unit-sales','price','-price','-publication_date'.'
# Valid response groups for ItemLookup requests include ['Request','ItemIds','Small','Medium','Large','Offers','OfferFull','OfferSummary','OfferListings','PromotionSummary','PromotionDetails','VariationMinimum','VariationSummary','VariationMatrix','VariationOffers','Variations','TagsSummary','Tags','ItemAttributes','MerchantItemAttributes','Tracks','Accessories','EditorialReview','SalesRank','BrowseNodes','Images','Similarities','Subjects','Reviews','ListmaniaLists','SearchInside','PromotionalTag','SearchBins','AlternateVersions','Collections','RelatedItems','ShippingCharges','ShippingOptions'].'
# Valid sort values include
# 'relevancerank'
# ,'salesrank'
# ,'reviewrank'
# ,'pricerank'
# ,'inverse-pricerank'
# ,'daterank'
# ,'titlerank'
# ,'-titlerank'
# ,'-unit-sales'
# ,'price'
# ,'-price'
# ,'-publication_date'.'
# make products json serializable
# response = json.dumps(products, default=lambda o: o.__dict__)
# print(products[0].title)
data = {}
# for i, product in enumerate(products):
# mydict.update({i:product.title})
# print("{0}. '{1}'".format(i, product.title))
for i, product in enumerate(products):
data[i] = {"title": str(product.title)[0:90],
"sales_rank": str(product.sales_rank),
"author": str(product.author)[0:20],
"large_image_url": product.large_image_url,
"publisher": str(product.publisher)[0:34],
"publication_date": str(product.publication_date),
"detail_page_url": product.detail_page_url,
"asin": str(product.asin),
"availability": str(product.availability)[0:25],
"isbn": str(product.isbn),
"offer_url": product.offer_url,
"pages": str(product.pages),
"price_and_currency": str(product.price_and_currency[0]),
"list_price": str(product.list_price[0]),
"binding": str(product.binding)[0:25]
}
# print("{0}. '{1}'".format(i, product.title))
data_list = list(data.values())
# print(data_list)
# json dump mydict and store in mydict_json
# data_json = json.dumps(data_list)
# #
# print(data_json)
#
# with open('mt101_data.json', 'w') as outfile: #will delete the old file an create a new one.
# json.dumps(data_json, outfile)
with open('mt101_data.txt', 'w', encoding='utf-8') as f:
json.dump(data_list, f, ensure_ascii=False)
################### Algolia #################
# ApplicationID = '***'
# ApiKey = '***'
client = algoliasearch.Client(conf.ApplicationID, conf.ApiKey)
# # #
index = client.init_index("idx_mt101")
# # #
#trunc and load index
index.clear_index()
#load index
batch = json.load(open('mt101_data.txt'))
# # #
# # # # add json to index idx_mt101
index.add_objects(batch)
#
#