| 12430 |
kshitij.so |
1 |
from xml.dom.minidom import parseString
|
| 12363 |
kshitij.so |
2 |
import re
|
| 12430 |
kshitij.so |
3 |
import urllib
|
|
|
4 |
import hashlib
|
|
|
5 |
import hmac
|
|
|
6 |
import base64
|
|
|
7 |
from time import strftime, gmtime
|
|
|
8 |
from requests import request
|
| 12363 |
kshitij.so |
9 |
|
|
|
10 |
|
| 12430 |
kshitij.so |
11 |
class MWS(object):
|
|
|
12 |
URI = "/"
|
|
|
13 |
VERSION = "2009-01-01"
|
|
|
14 |
NS = ''
|
| 12363 |
kshitij.so |
15 |
|
| 12430 |
kshitij.so |
16 |
def __init__(self, access_key, secret_key, merchant_id,
|
|
|
17 |
domain='https://mws-eu.amazonservices.com', uri="", version=""):
|
|
|
18 |
self.access_key = access_key
|
|
|
19 |
self.secret_key = secret_key
|
|
|
20 |
self.merchant_id = merchant_id
|
|
|
21 |
self.domain = domain
|
|
|
22 |
self.uri = uri or self.URI
|
|
|
23 |
self.version = version or self.VERSION
|
| 12363 |
kshitij.so |
24 |
|
| 12430 |
kshitij.so |
25 |
def make_request(self, extra_data, action,method="GET", **kwargs):
|
| 12363 |
kshitij.so |
26 |
|
| 12430 |
kshitij.so |
27 |
params = {
|
|
|
28 |
'AWSAccessKeyId': self.access_key,
|
|
|
29 |
'SignatureVersion': '2',
|
|
|
30 |
'Timestamp': self.get_timestamp(),
|
|
|
31 |
'Version': self.version,
|
|
|
32 |
'SignatureMethod': 'HmacSHA256',
|
|
|
33 |
'Action': action
|
|
|
34 |
}
|
|
|
35 |
if action=='GetLowestOfferListingsForSKU':
|
|
|
36 |
params['ExcludeMe']='true'
|
|
|
37 |
params.update(extra_data)
|
|
|
38 |
request_description = '&'.join(['%s=%s' % (k, urllib.quote(params[k], safe='-_.~').encode('utf-8')) for k in sorted(params)])
|
|
|
39 |
signature = self.calc_signature(method, request_description)
|
|
|
40 |
url = '%s%s?%s&Signature=%s' % (self.domain, self.uri, request_description, urllib.quote(signature))
|
| 12801 |
kshitij.so |
41 |
#headers = {'User-Agent': 'AmazonJavascriptScratchpad/1.0 (Language=Python)'}
|
|
|
42 |
#headers.update(kwargs.get('extra_headers', {}))
|
| 12430 |
kshitij.so |
43 |
response = request(method, url)
|
|
|
44 |
if action=='GetLowestOfferListingsForSKU':
|
|
|
45 |
return self.parse_competitor_pricing_response(response)
|
|
|
46 |
elif action=='GetMyPriceForSKU':
|
|
|
47 |
return self.parse_my_pricing_response(response)
|
|
|
48 |
else:
|
|
|
49 |
raise
|
| 12363 |
kshitij.so |
50 |
|
| 12430 |
kshitij.so |
51 |
def parse_competitor_pricing_response(self,response):
|
|
|
52 |
spString = re.sub('<\?.*\?>','',response.text)
|
|
|
53 |
spString = "<dom>" + spString + "</dom>"
|
|
|
54 |
dom = parseString(spString)
|
|
|
55 |
skuOffers = dom.getElementsByTagName('GetLowestOfferListingsForSKUResult')
|
|
|
56 |
offerMap = {}
|
| 12597 |
kshitij.so |
57 |
otherInfo = {}
|
| 12430 |
kshitij.so |
58 |
for skuOffer in skuOffers:
|
|
|
59 |
status = skuOffer.attributes.items()[0][1]
|
|
|
60 |
sku = skuOffer.attributes.items()[1][1]
|
| 12363 |
kshitij.so |
61 |
info = []
|
| 12430 |
kshitij.so |
62 |
for offer in skuOffer.getElementsByTagName('LowestOfferListing'):
|
|
|
63 |
temp = {}
|
| 12363 |
kshitij.so |
64 |
try:
|
| 12436 |
kshitij.so |
65 |
if len(info)==3:
|
|
|
66 |
break
|
|
|
67 |
amount = offer.getElementsByTagName('Amount')[0].firstChild.nodeValue
|
| 12482 |
kshitij.so |
68 |
temp['sellingPrice'] = float(amount)
|
|
|
69 |
temp['promoPrice'] = float(amount)
|
| 12436 |
kshitij.so |
70 |
temp['fulfillmentChannel'] = offer.getElementsByTagName('FulfillmentChannel')[0].firstChild.nodeValue
|
|
|
71 |
try:
|
|
|
72 |
temp['shippingTime'] = (offer.getElementsByTagName('Max')[0].firstChild.nodeValue).replace('days','')
|
|
|
73 |
except:
|
|
|
74 |
temp['shippingTime'] = '0-0'
|
|
|
75 |
try:
|
|
|
76 |
temp['rating'] = (offer.getElementsByTagName('SellerPositiveFeedbackRating')[0].firstChild.nodeValue)
|
|
|
77 |
if temp['rating'] == 'Just Launched':
|
| 12801 |
kshitij.so |
78 |
temp['rating'] = '100'
|
| 12436 |
kshitij.so |
79 |
else:
|
|
|
80 |
str_rating = temp['rating'].replace('-',' ').replace('%','')
|
|
|
81 |
a = str_rating.split()
|
|
|
82 |
l = []
|
|
|
83 |
for x in a:
|
|
|
84 |
if x.isdigit():
|
|
|
85 |
l.append(x)
|
|
|
86 |
temp['rating'] = l[0]
|
|
|
87 |
except:
|
| 12430 |
kshitij.so |
88 |
temp['rating'] = '0'
|
|
|
89 |
except:
|
| 12436 |
kshitij.so |
90 |
pass
|
| 12430 |
kshitij.so |
91 |
temp['notOurSku'] = True
|
|
|
92 |
info.append(temp)
|
| 12597 |
kshitij.so |
93 |
offerMap[sku]=info
|
|
|
94 |
for skuOffer in skuOffers:
|
|
|
95 |
sku = skuOffer.attributes.items()[1][1]
|
|
|
96 |
temp = {}
|
|
|
97 |
temp['lowestMfnIgnored']=0.0
|
|
|
98 |
temp['isLowestMfnIgnored']=False
|
|
|
99 |
temp['lowestFba']=0.0
|
|
|
100 |
temp['isLowestFba']=False
|
|
|
101 |
temp['lowestMfn']=0.0
|
|
|
102 |
temp['isLowestMfn']=False
|
|
|
103 |
for offer in skuOffer.getElementsByTagName('LowestOfferListing'):
|
|
|
104 |
try:
|
|
|
105 |
fulfillmentChannel = offer.getElementsByTagName('FulfillmentChannel')[0].firstChild.nodeValue
|
|
|
106 |
amount = offer.getElementsByTagName('Amount')[0].firstChild.nodeValue
|
|
|
107 |
try:
|
|
|
108 |
rating = (offer.getElementsByTagName('SellerPositiveFeedbackRating')[0].firstChild.nodeValue)
|
|
|
109 |
if rating == 'Just Launched':
|
| 12801 |
kshitij.so |
110 |
rating = 100
|
| 12597 |
kshitij.so |
111 |
else:
|
|
|
112 |
str_rating = rating.replace('-',' ').replace('%','')
|
|
|
113 |
a = str_rating.split()
|
|
|
114 |
l = []
|
|
|
115 |
for x in a:
|
|
|
116 |
if x.isdigit():
|
|
|
117 |
l.append(x)
|
|
|
118 |
rating = int(l[0])
|
|
|
119 |
except:
|
|
|
120 |
rating = 0
|
|
|
121 |
if fulfillmentChannel=='Merchant' and not temp['isLowestMfnIgnored'] and not temp['isLowestMfn'] and rating < 60:
|
|
|
122 |
temp['lowestMfnIgnored'] = float(amount)
|
|
|
123 |
temp['isLowestMfnIgnored']=True
|
|
|
124 |
elif fulfillmentChannel=='Merchant' and not temp['isLowestMfn'] and rating > 60:
|
|
|
125 |
temp['lowestMfn'] = float(amount)
|
|
|
126 |
temp['isLowestMfn']=True
|
|
|
127 |
elif fulfillmentChannel=='Amazon' and not temp['isLowestFba']:
|
|
|
128 |
temp['lowestFba'] = float(amount)
|
|
|
129 |
temp['isLowestFba']=True
|
|
|
130 |
else:
|
|
|
131 |
pass
|
|
|
132 |
except:
|
|
|
133 |
pass
|
|
|
134 |
otherInfo[sku]=temp
|
|
|
135 |
return offerMap, otherInfo
|
| 12430 |
kshitij.so |
136 |
|
|
|
137 |
def parse_my_pricing_response(self,response):
|
|
|
138 |
spString = re.sub('<\?.*\?>','',response.text)
|
|
|
139 |
spString = "<dom>" + spString + "</dom>"
|
|
|
140 |
dom = parseString(spString)
|
|
|
141 |
skuOffers = dom.getElementsByTagName('GetMyPriceForSKUResult')
|
|
|
142 |
skuMap = {}
|
|
|
143 |
for skuOffer in skuOffers:
|
| 12438 |
kshitij.so |
144 |
try:
|
|
|
145 |
status = skuOffer.attributes.items()[0][1]
|
|
|
146 |
sku = skuOffer.attributes.items()[1][1]
|
|
|
147 |
asin = skuOffer.getElementsByTagName('ASIN')[0].firstChild.nodeValue
|
|
|
148 |
except:
|
|
|
149 |
continue
|
| 12430 |
kshitij.so |
150 |
for offer in skuOffer.getElementsByTagName('Offers'):
|
|
|
151 |
temp = {}
|
| 12436 |
kshitij.so |
152 |
try:
|
|
|
153 |
promoPrice = offer.getElementsByTagName('LandedPrice')[0].getElementsByTagName('Amount')[0].firstChild.nodeValue
|
|
|
154 |
regularPrice = offer.getElementsByTagName('RegularPrice')[0].getElementsByTagName('Amount')[0].firstChild.nodeValue
|
| 12482 |
kshitij.so |
155 |
temp['sellingPrice'] = float(regularPrice)
|
|
|
156 |
temp['promoPrice'] = float(promoPrice)
|
| 12436 |
kshitij.so |
157 |
if promoPrice == regularPrice:
|
|
|
158 |
temp['promotion'] = False
|
|
|
159 |
else:
|
|
|
160 |
temp['promotion'] = True
|
|
|
161 |
temp['status'] = status
|
|
|
162 |
temp['asin'] = asin
|
|
|
163 |
temp['notOurSku'] = False
|
|
|
164 |
temp['fulfillmentChannel'] ='AMAZON'
|
|
|
165 |
temp['shippingTime'] = '0-0'
|
|
|
166 |
temp['rating'] = '0'
|
|
|
167 |
except:
|
|
|
168 |
pass
|
|
|
169 |
skuMap[sku]=temp
|
| 12430 |
kshitij.so |
170 |
return skuMap
|
| 12363 |
kshitij.so |
171 |
|
| 12430 |
kshitij.so |
172 |
|
|
|
173 |
def calc_signature(self, method, request_description):
|
|
|
174 |
sig_data = method + '\n' + self.domain.replace('https://', '').lower() + '\n' + self.uri + '\n' + request_description
|
|
|
175 |
return base64.b64encode(hmac.new(str(self.secret_key), sig_data, hashlib.sha256).digest())
|
| 12363 |
kshitij.so |
176 |
|
| 12430 |
kshitij.so |
177 |
def get_timestamp(self):
|
|
|
178 |
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
class Products(MWS):
|
|
|
182 |
|
|
|
183 |
URI = '/Products/2011-10-01'
|
|
|
184 |
VERSION = '2011-10-01'
|
|
|
185 |
NS = '{https://mws-eu.amazonservices.com/Products/2011-10-01}'
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
def get_my_pricing_for_sku(self, marketplaceid, skus):
|
|
|
189 |
|
|
|
190 |
data = dict(SellerId=self.merchant_id, MarketplaceId=marketplaceid)
|
|
|
191 |
num=0
|
|
|
192 |
for sku in skus:
|
|
|
193 |
data['SellerSKUList.SellerSKU.%d' % (num + 1)] = sku
|
|
|
194 |
num+=1
|
|
|
195 |
return self.make_request(data,'GetMyPriceForSKU')
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
def get_competitive_pricing_for_sku(self, marketplaceid, skus):
|
|
|
199 |
data = dict(SellerId=self.merchant_id, MarketplaceId=marketplaceid)
|
|
|
200 |
num=0
|
|
|
201 |
for sku in skus:
|
|
|
202 |
data['SellerSKUList.SellerSKU.%d' % (num + 1)] = sku
|
|
|
203 |
num+=1
|
|
|
204 |
return self.make_request(data,'GetLowestOfferListingsForSKU')
|
|
|
205 |
|
|
|
206 |
def main():
|
|
|
207 |
p = Products("AKIAII3SGRXBJDPCHSGQ", "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg", "AF6E3O0VE0X4D")
|
| 12801 |
kshitij.so |
208 |
comp = p.get_competitive_pricing_for_sku('A21TJRUUN4KGV', ['FBA16933'])
|
|
|
209 |
our = p.get_my_pricing_for_sku('A21TJRUUN4KGV', ["FBA10660"])
|
|
|
210 |
print our
|
|
|
211 |
print comp
|
|
|
212 |
# for k, v in our.iteritems():
|
|
|
213 |
# print k,
|
|
|
214 |
# print '\t',
|
|
|
215 |
# print v['sellingPrice'],
|
|
|
216 |
# print v['promoPrice'],
|
|
|
217 |
# print v['asin']
|
| 12482 |
kshitij.so |
218 |
#print our.get('promotion')
|
| 12436 |
kshitij.so |
219 |
# print comp.get('FBA12248')
|
|
|
220 |
# print our.get('FBA12248')
|
|
|
221 |
# x = (our.get('FBA12248'))
|
|
|
222 |
# x['sellingPrice']=41089
|
|
|
223 |
# l = (comp.get('FBA12248'))
|
|
|
224 |
# l.append((our.get('FBA12248')))
|
|
|
225 |
# print sorted(l, key=itemgetter('promoPrice','notOurSku'))
|
| 12430 |
kshitij.so |
226 |
|
|
|
227 |
|
|
|
228 |
if __name__=='__main__':
|
| 12801 |
kshitij.so |
229 |
main()
|