| Line 16... |
Line 16... |
| 16 |
import pymongo
|
16 |
import pymongo
|
| 17 |
import re
|
17 |
import re
|
| 18 |
import traceback
|
18 |
import traceback
|
| 19 |
import urllib
|
19 |
import urllib
|
| 20 |
|
20 |
|
| 21 |
USERNAME='saholic1@gmail.com'
|
21 |
USERNAME='profittill2@gmail.com'
|
| 22 |
PASSWORD='spice@2020'
|
22 |
PASSWORD='spice@2020'
|
| 23 |
AFFILIATE_URL='http://affiliate.snapdeal.com'
|
23 |
AFFILIATE_URL='http://affiliate.snapdeal.com'
|
| 24 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
24 |
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
|
| 25 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
25 |
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
|
| 26 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
26 |
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
|
| Line 66... |
Line 66... |
| 66 |
for offer in allOffers:
|
66 |
for offer in allOffers:
|
| 67 |
pyOffer = self.covertToObj(offer).__dict__
|
67 |
pyOffer = self.covertToObj(offer).__dict__
|
| 68 |
allPyOffers.append(pyOffer)
|
68 |
allPyOffers.append(pyOffer)
|
| 69 |
saleDate = datetime.strptime(pyOffer['saleDate'],"%Y-%m-%d %H:%M:%S")
|
69 |
saleDate = datetime.strptime(pyOffer['saleDate'],"%Y-%m-%d %H:%M:%S")
|
| 70 |
if maxSaleDate < saleDate:
|
70 |
if maxSaleDate < saleDate:
|
| 71 |
self._updateOrdersPayBackStatus(pyOffer['subTagId'],pyOffer['saleDate'])
|
71 |
self._updateOrdersPayBackStatus({'subTagId':pyOffer['subTagId'], 'saleDate':pyOffer['saleDate']}, {})
|
| 72 |
if newMaxSaleDate < saleDate:
|
72 |
if newMaxSaleDate < saleDate:
|
| 73 |
newMaxSaleDate = saleDate
|
73 |
newMaxSaleDate = saleDate
|
| 74 |
|
74 |
|
| 75 |
self._setLastSaleDate(newMaxSaleDate)
|
75 |
self._setLastSaleDate(newMaxSaleDate)
|
| 76 |
self._saveToAffiliate(allPyOffers)
|
76 |
self._saveToAffiliate(allPyOffers)
|
| 77 |
|
77 |
|
| 78 |
def _setLastSaleDate(self, saleDate):
|
78 |
def _setLastSaleDate(self, saleDate):
|
| 79 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
79 |
self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
|
| 80 |
|
80 |
|
| 81 |
def _updateOrdersPayBackStatus(self, subTagId, saleDate):
|
- |
|
| 82 |
self.db.merchantOrder.update({"subTagId":subTagId, "placedOn":saleDate, "subOrders.cashbackStatus":MStore.CB_INIT},
|
- |
|
| 83 |
{ '$set': { "subOrders.$.cashbackStatus" : MStore.CB_PENDING } })
|
- |
|
| 84 |
|
- |
|
| 85 |
|
81 |
|
| 86 |
|
82 |
|
| 87 |
def _getLastSaleDate(self,):
|
83 |
def _getLastSaleDate(self,):
|
| 88 |
lastDaySaleObj = self.db.lastDaySale.find_one({"storeId":self.store_id})
|
84 |
lastDaySaleObj = self.db.lastDaySale.find_one({"storeId":self.store_id})
|
| 89 |
if lastDaySaleObj is None:
|
85 |
if lastDaySaleObj is None:
|
| Line 169... |
Line 165... |
| 169 |
subOrder.amountPaid = amountPaid
|
165 |
subOrder.amountPaid = amountPaid
|
| 170 |
subOrder.deliveryCharges = deliveryCharges
|
166 |
subOrder.deliveryCharges = deliveryCharges
|
| 171 |
subOrder.offerDiscount = offerDiscount
|
167 |
subOrder.offerDiscount = offerDiscount
|
| 172 |
subOrder.unitPrice = unitPrice
|
168 |
subOrder.unitPrice = unitPrice
|
| 173 |
subOrder.productCode = re.findall(r'\d+$', productUrl)[0]
|
169 |
subOrder.productCode = re.findall(r'\d+$', productUrl)[0]
|
| 174 |
cashbackAmount = self.getCashbackAmount(subOrder.productCode, amount)
|
170 |
(cashbackAmount, percentage) = self.getCashbackAmount(subOrder.productCode, amount)
|
| 175 |
cashbackStatus = Store.CB_INIT
|
171 |
cashbackStatus = Store.CB_INIT
|
| 176 |
if cashbackAmount <= 0:
|
172 |
if cashbackAmount <= 0:
|
| 177 |
cashbackStatus = Store.CB_NA
|
173 |
cashbackStatus = Store.CB_NA
|
| 178 |
subOrder.cashBackStatus = cashbackStatus
|
174 |
subOrder.cashBackStatus = cashbackStatus
|
| 179 |
subOrder.cashBackAmount = cashbackAmount
|
175 |
subOrder.cashBackAmount = cashbackAmount
|
| - |
|
176 |
if percentage > 0:
|
| - |
|
177 |
subOrder.cashBackPercentage = percentage
|
| 180 |
|
178 |
|
| 181 |
|
179 |
|
| 182 |
trackAnchor = div.find("a")
|
180 |
trackAnchor = div.find("a")
|
| 183 |
if trackAnchor is not None:
|
181 |
if trackAnchor is not None:
|
| 184 |
subOrder.tracingkUrl = str(trackAnchor['href'])
|
182 |
subOrder.tracingkUrl = str(trackAnchor['href'])
|
| Line 241... |
Line 239... |
| 241 |
for subOrderElement in subOrders:
|
239 |
for subOrderElement in subOrders:
|
| 242 |
closed = True
|
240 |
closed = True
|
| 243 |
divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
|
241 |
divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
|
| 244 |
if len(divs)<=0:
|
242 |
if len(divs)<=0:
|
| 245 |
raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
|
243 |
raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
|
| 246 |
|
244 |
subOrder = None
|
| 247 |
for div in divs:
|
245 |
for div in divs:
|
| 248 |
divStr = str(div)
|
246 |
divStr = str(div)
|
| 249 |
divStr = divStr.replace("\n","").replace("\t", "")
|
247 |
divStr = divStr.replace("\n","").replace("\t", "")
|
| 250 |
updateMap = {}
|
248 |
updateMap = {}
|
| 251 |
for line in divStr.split("<br />"):
|
249 |
for line in divStr.split("<br />"):
|
| 252 |
if "Suborder ID" in line:
|
250 |
if "Suborder ID" in line:
|
| 253 |
merchantSubOrderId = re.findall(r'\d+', line)[0]
|
251 |
merchantSubOrderId = re.findall(r'\d+', line)[0]
|
| 254 |
#break if suborder is inactive
|
252 |
#break if suborder is inactive
|
| 255 |
findMap = {"orderId": order['orderId'], "subOrders.merchantSubOrderId": merchantSubOrderId}
|
253 |
findMap = {"orderId": order['orderId'], "subOrders.merchantSubOrderId": merchantSubOrderId}
|
| 256 |
if not self._isSubOrderActive(order, merchantSubOrderId):
|
254 |
subOrder = self._isSubOrderActive(order, merchantSubOrderId)
|
| - |
|
255 |
if subOrder is None:
|
| 257 |
break
|
256 |
break
|
| 258 |
elif "Status" in line:
|
257 |
elif "Status" in line:
|
| 259 |
detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
258 |
detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
| 260 |
detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
259 |
detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
|
| 261 |
updateMap["subOrders.$.detailedStatus"] = detailedStatus
|
260 |
updateMap["subOrders.$.detailedStatus"] = detailedStatus
|
| 262 |
status = self._getStatusFromDetailedStatus(detailedStatus)
|
261 |
status = self._getStatusFromDetailedStatus(detailedStatus)
|
| 263 |
closedStatus = status in [Store.ORDER_DELIVERED, Store.ORDER_CANCELLED]
|
262 |
closedStatus = status in [Store.ORDER_DELIVERED, Store.ORDER_CANCELLED]
|
| 264 |
if closedStatus:
|
263 |
if closedStatus:
|
| 265 |
#if status is closed then change the paybackStatus accordingly
|
264 |
#if status is closed then change the paybackStatus accordingly
|
| 266 |
if closedStatus == Store.ORDER_DELIVERED:
|
265 |
if closedStatus == Store.ORDER_DELIVERED:
|
| 267 |
if order.get("cashBackStatus") == Store.CB_PENDING:
|
266 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| 268 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_APPROVED
|
267 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_APPROVED
|
| 269 |
elif closedStatus == Store.ORDER_CANCELLED:
|
268 |
elif closedStatus == Store.ORDER_CANCELLED:
|
| 270 |
if order.get("cashBackStatus") == Store.CB_PENDING:
|
269 |
if subOrder.get("cashBackStatus") == Store.CB_PENDING:
|
| 271 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_CANCELLED
|
270 |
updateMap["subOrders.$.cashBackStatus"] = Store.CB_CANCELLED
|
| 272 |
|
271 |
|
| 273 |
else:
|
272 |
else:
|
| 274 |
closed = False
|
273 |
closed = False
|
| 275 |
elif "Est. Shipping Date" in line:
|
274 |
elif "Est. Shipping Date" in line:
|
| Line 284... |
Line 283... |
| 284 |
elif "Tracking No" in line:
|
283 |
elif "Tracking No" in line:
|
| 285 |
trackingNumber = line.split(":")[1].strip()
|
284 |
trackingNumber = line.split(":")[1].strip()
|
| 286 |
updateMap["subOrders.$.trackingNumber"] = trackingNumber
|
285 |
updateMap["subOrders.$.trackingNumber"] = trackingNumber
|
| 287 |
|
286 |
|
| 288 |
bulk.find(findMap).update({'$set' : updateMap})
|
287 |
bulk.find(findMap).update({'$set' : updateMap})
|
| 289 |
bulk.find({'orderId': order['orderId']}).update({'$set':{'closed': closed}})
|
288 |
bulk.find({'orderId': order['orderId']}).update({'$set':{'closed': closed}})
|
| 290 |
result = bulk.execute()
|
289 |
result = bulk.execute()
|
| 291 |
pprint(result)
|
290 |
pprint(result)
|
| 292 |
|
291 |
|
| 293 |
|
292 |
|
| 294 |
def _isSubOrderActive(self,order, merchantSubOrderId):
|
- |
|
| 295 |
subOrders = order.get("subOrders")
|
- |
|
| 296 |
for subOrder in subOrders:
|
- |
|
| 297 |
if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
|
- |
|
| 298 |
return True
|
- |
|
| 299 |
return False
|
- |
|
| 300 |
|
- |
|
| 301 |
|
- |
|
| 302 |
|
- |
|
| 303 |
def _saveToAffiliate(self, offers):
|
293 |
def _saveToAffiliate(self, offers):
|
| 304 |
collection = self.db.snapdealOrderAffiliateInfo
|
294 |
collection = self.db.snapdealOrderAffiliateInfo
|
| 305 |
try:
|
295 |
try:
|
| 306 |
collection.insert(offers,continue_on_error=True)
|
296 |
collection.insert(offers,continue_on_error=True)
|
| 307 |
except pymongo.errors.DuplicateKeyError as e:
|
297 |
except pymongo.errors.DuplicateKeyError as e:
|