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