| 14811 |
amit.gupta |
1 |
'''
|
|
|
2 |
Created on Apr 12, 2015
|
|
|
3 |
|
|
|
4 |
@author: amit
|
|
|
5 |
'''
|
|
|
6 |
from bs4 import BeautifulSoup
|
|
|
7 |
from dtr import main
|
| 16371 |
amit.gupta |
8 |
from dtr.dao import Order, SubOrder
|
| 14811 |
amit.gupta |
9 |
from dtr.main import getBrowserObject, getStore, ParseException, ungzipResponse, \
|
| 16371 |
amit.gupta |
10 |
Store as MStore, sourceMap, tprint, todict
|
| 16283 |
amit.gupta |
11 |
from dtr.sources.amazon import readSSh
|
| 14811 |
amit.gupta |
12 |
from dtr.utils.utils import fetchResponseUsingProxy
|
|
|
13 |
from pymongo.mongo_client import MongoClient
|
| 16371 |
amit.gupta |
14 |
from datetime import datetime, timedelta
|
|
|
15 |
import json
|
|
|
16 |
import traceback
|
| 16374 |
amit.gupta |
17 |
from elixir import *
|
| 14811 |
amit.gupta |
18 |
|
| 16374 |
amit.gupta |
19 |
from dtr.storage.DataService import paytm_coupon_usages
|
|
|
20 |
|
| 14811 |
amit.gupta |
21 |
class Store(MStore):
|
|
|
22 |
OrderStatusMap = {
|
| 16371 |
amit.gupta |
23 |
main.Store.ORDER_PLACED : ['in process', 'ready to be shipped', 'order recieved', 'order received'],
|
| 16283 |
amit.gupta |
24 |
main.Store.ORDER_DELIVERED : ['delivered'],
|
| 16371 |
amit.gupta |
25 |
main.Store.ORDER_SHIPPED : ['in transit', 'shipped'],
|
|
|
26 |
main.Store.ORDER_CANCELLED : ['failed', 'payment failed']
|
| 14811 |
amit.gupta |
27 |
|
|
|
28 |
}
|
|
|
29 |
def __init__(self,store_id):
|
|
|
30 |
client = MongoClient('mongodb://localhost:27017/')
|
|
|
31 |
self.db = client.dtr
|
|
|
32 |
super(Store, self).__init__(store_id)
|
|
|
33 |
|
|
|
34 |
def saveOrder(self, merchantOrder):
|
|
|
35 |
MStore.saveOrder(self, merchantOrder)
|
| 16371 |
amit.gupta |
36 |
|
|
|
37 |
def parseTrackingUrl(self, trackingUrl):
|
|
|
38 |
subOrder = {}
|
|
|
39 |
page = fetchResponseUsingProxy(trackingUrl)
|
|
|
40 |
soup = BeautifulSoup(page)
|
|
|
41 |
subOrder['detailedStatus'] = soup.h1.text
|
|
|
42 |
subOrder['status'] = self._getStatusFromDetailedStatus(subOrder['detailedStatus'])
|
| 14811 |
amit.gupta |
43 |
|
| 16371 |
amit.gupta |
44 |
|
| 14811 |
amit.gupta |
45 |
def scrapeStoreOrders(self):
|
| 16371 |
amit.gupta |
46 |
orders = self.db.merchantOrder.find({"storeId":1, "closed":False, "subOrders":{"$elemMatch":{"closed":False, "trackingUrl":{"$exists":True}}}})
|
|
|
47 |
for merchantOrder in orders:
|
|
|
48 |
executeBulk = False
|
|
|
49 |
try:
|
|
|
50 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
|
|
51 |
closed = True
|
|
|
52 |
map1 = {}
|
|
|
53 |
for subOrder in merchantOrder.get("subOrders"):
|
|
|
54 |
if subOrder.get("closed"):
|
|
|
55 |
continue
|
|
|
56 |
elif subOrder.get("trackingUrl") is None:
|
|
|
57 |
closed = False
|
|
|
58 |
continue
|
|
|
59 |
executeBulk = True
|
|
|
60 |
findMap = {"orderId":merchantOrder.get("orderId"), "subOrders.merchantSubOrderId":subOrder.get("merchantSubOrderId")}
|
|
|
61 |
trackingUrl = subOrder.get("trackingUrl")
|
|
|
62 |
if not map1.has_key(trackingUrl):
|
|
|
63 |
map1[trackingUrl] = self.parseTrackingUrl(trackingUrl)
|
|
|
64 |
newOrder = map1.get(trackingUrl)
|
|
|
65 |
updateMap = self.getUpdateMap(newOrder, subOrder.get('cashBackStatus'))
|
|
|
66 |
print findMap, "\n", updateMap
|
|
|
67 |
bulk.find(findMap).update({'$set' : updateMap})
|
|
|
68 |
closed = closed and newOrder['closed']
|
|
|
69 |
if executeBulk:
|
|
|
70 |
bulk.find({"orderId":merchantOrder.get("orderId")}).update({"$set":{"closed":closed, "parseError":False}})
|
|
|
71 |
bulk.execute()
|
|
|
72 |
except:
|
|
|
73 |
tprint("Could not update " + str(merchantOrder['orderId']) + " For store " + self.getName())
|
|
|
74 |
self.db.merchantOrder.update({"orderId":merchantOrder['orderId']}, {"$set":{"parseError":True}})
|
|
|
75 |
traceback.print_exc()
|
| 14811 |
amit.gupta |
76 |
|
| 16371 |
amit.gupta |
77 |
def getTrackingUrls(self, userId):
|
|
|
78 |
missingOrderUrls = []
|
|
|
79 |
#missingOrders = self._getMissingOrders({'userId':userId})
|
|
|
80 |
#for missingOrder in missingOrders:
|
|
|
81 |
# missingOrderUrls.append(ORDER_REDIRECT_URL%(missingOrder['merchantOrderId']))
|
|
|
82 |
orders = self._getActiveOrders({'userId':userId, "subOrders.trackingUrl":{"$exists":False} })
|
|
|
83 |
for order in orders:
|
|
|
84 |
"https://paytm.com/shop/orderdetail/%s?actions=1&channel=web&version=2"%("")
|
|
|
85 |
return missingOrderUrls
|
|
|
86 |
|
|
|
87 |
def trackOrdersForUser(self, userId, url, rawHtml):
|
|
|
88 |
ordermap = json.loads(rawHtml).get("order")
|
|
|
89 |
for item in ordermap.get("items"):
|
|
|
90 |
pass
|
|
|
91 |
|
| 16283 |
amit.gupta |
92 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
|
| 16371 |
amit.gupta |
93 |
resp = {}
|
|
|
94 |
#orderSuccessUrl = "https://paytm.com/shop/orderdetail/1155961075?actions=1&channel=web&version=2"
|
|
|
95 |
ordermap = json.loads(rawHtml).get("order")
|
|
|
96 |
if not ordermap.get("need_shipping"):
|
|
|
97 |
resp['result'] = 'PAYTM_RECHARGE_ORDER'
|
|
|
98 |
return resp
|
| 16375 |
amit.gupta |
99 |
elif ordermap.get("payment_status") == "PROCESSING":
|
|
|
100 |
resp['result'] = 'ORDER_PENDING_PAYMENT'
|
|
|
101 |
return resp
|
| 16371 |
amit.gupta |
102 |
order = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
|
|
103 |
order.deliveryCharges = ordermap.get("shipping_charges") - ordermap.get("shipping_discount")
|
|
|
104 |
order.merchantOrderId = str(ordermap.get("id"))
|
|
|
105 |
order.discountApplied = ordermap.get("discount_amount")
|
|
|
106 |
order.paidAmount = ordermap.get("grandtotal")
|
|
|
107 |
order.placedOn = ordermap.get("date") + " "+ ordermap.get("time")
|
|
|
108 |
order.requireDetail = False
|
|
|
109 |
order.status = 'success'
|
|
|
110 |
order.totalAmount = ordermap.get("subtotal")
|
|
|
111 |
subOrders = []
|
|
|
112 |
order.subOrders = subOrders
|
| 16374 |
amit.gupta |
113 |
coupon_code = None
|
|
|
114 |
total_cashback = 0
|
| 16371 |
amit.gupta |
115 |
for item in ordermap.get("items"):
|
|
|
116 |
product = item.get("product")
|
|
|
117 |
shareUrl = product.get("seourl").replace("catalog.paytm.com/v1", "paytm.com/shop")
|
|
|
118 |
paytmcashback = 0
|
| 16374 |
amit.gupta |
119 |
if item.get("promo_code"):
|
|
|
120 |
coupon_code = item.get("promo_code")
|
|
|
121 |
for s in item.get("promo_text").split():
|
|
|
122 |
if s.isdigit():
|
|
|
123 |
paytmcashback = int(s)
|
|
|
124 |
total_cashback += paytmcashback
|
|
|
125 |
break
|
| 16371 |
amit.gupta |
126 |
amountPaid = item.get("subtotal") - paytmcashback
|
|
|
127 |
detailedStatus = item.get("status_text")
|
|
|
128 |
status = self._getStatusFromDetailedStatus(detailedStatus)
|
| 16375 |
amit.gupta |
129 |
if status == None:
|
|
|
130 |
status = MStore.ORDER_PLACED
|
| 16371 |
amit.gupta |
131 |
subOrder = SubOrder(item.get("title"), item.get("product").get(""), ordermap.get("date"), amountPaid, status, item.get("quantity"))
|
|
|
132 |
subOrder.imgUrl = product.get("thumbnail")
|
| 16375 |
amit.gupta |
133 |
subOrder.detailedStatus = detailedStatus
|
| 16371 |
amit.gupta |
134 |
subOrder.productUrl = shareUrl
|
|
|
135 |
subOrder.productCode = product.get("url").split("?")[0].split("/")[-1]
|
|
|
136 |
subOrder.merchantSubOrderId = str(item.get("id"))
|
|
|
137 |
if status == MStore.ORDER_PLACED:
|
| 16377 |
amit.gupta |
138 |
if item.get("status_flow")[1].get("date"):
|
|
|
139 |
subOrder.estimatedDeliveryDate = datetime.strftime(getISTDate(item.get("status_flow")[1].get("date")), "%d %b")
|
|
|
140 |
if item.get("status_flow")[2].get("date"):
|
|
|
141 |
subOrder.estimatedShippingDate = datetime.strftime(getISTDate(item.get("status_flow")[2].get("date")), "%d %b")
|
| 16371 |
amit.gupta |
142 |
else:
|
|
|
143 |
subOrder.closed = False
|
|
|
144 |
subOrders.append(subOrder)
|
| 16374 |
amit.gupta |
145 |
if coupon_code:
|
|
|
146 |
usage = paytm_coupon_usages()
|
|
|
147 |
usage.cashback = total_cashback
|
|
|
148 |
usage.coupon = coupon_code
|
|
|
149 |
usage.user_id = userId
|
|
|
150 |
usage.order_id = orderId
|
|
|
151 |
session.commit()
|
| 16371 |
amit.gupta |
152 |
self.populateDerivedFields(order)
|
|
|
153 |
if self._saveToOrder(todict(order)):
|
|
|
154 |
resp['result'] = 'ORDER_CREATED'
|
|
|
155 |
else:
|
|
|
156 |
resp['result'] = 'ORDER_ALREADY_CREATED_IGNORED'
|
| 14811 |
amit.gupta |
157 |
|
| 16371 |
amit.gupta |
158 |
return resp
|
|
|
159 |
|
|
|
160 |
def _getStatusFromDetailedStatus(self, detailedStatus):
|
|
|
161 |
for key, value in Store.OrderStatusMap.iteritems():
|
|
|
162 |
if detailedStatus.lower() in value:
|
|
|
163 |
return key
|
| 16375 |
amit.gupta |
164 |
print "Detailed Status-", detailedStatus, "need to be mapped for Store-", self.store_name
|
| 16371 |
amit.gupta |
165 |
return None
|
|
|
166 |
def getISTDate(tzString):
|
|
|
167 |
tzDate = datetime.strptime(tzString, "%Y-%m-%dT%H:%M:%S.%fZ")
|
|
|
168 |
tzDate = tzDate + timedelta(0,19800)
|
|
|
169 |
return tzDate
|
|
|
170 |
|
| 14811 |
amit.gupta |
171 |
def main():
|
|
|
172 |
store = getStore(6)
|
| 16283 |
amit.gupta |
173 |
#store.scrapeStoreOrders()
|
| 16371 |
amit.gupta |
174 |
store.parseOrderRawHtml(312119, 'SUB1', 14, readSSh("/home/amit/paytm.json"), "someurl")
|
| 14811 |
amit.gupta |
175 |
|
|
|
176 |
if __name__ == '__main__':
|
|
|
177 |
main()
|