| Line 4... |
Line 4... |
| 4 |
@author: amit
|
4 |
@author: amit
|
| 5 |
'''
|
5 |
'''
|
| 6 |
from BeautifulSoup import BeautifulSoup
|
6 |
from BeautifulSoup import BeautifulSoup
|
| 7 |
from datetime import datetime, date, timedelta
|
7 |
from datetime import datetime, date, timedelta
|
| 8 |
from dtr import main
|
8 |
from dtr import main
|
| 9 |
from dtr.dao import AffiliateInfo, Order, SubOrder, FlipkartAffiliateInfo
|
9 |
from dtr.dao import AffiliateInfo, Order, SubOrder, FlipkartAffiliateInfo, obj
|
| 10 |
from dtr.main import getBrowserObject, getStore, ParseException, ungzipResponse, \
|
10 |
from dtr.main import getBrowserObject, getStore, ParseException, ungzipResponse, \
|
| 11 |
Store as MStore, sourceMap, tprint
|
11 |
Store as MStore, sourceMap, tprint
|
| 12 |
from dtr.storage.DataService import Clicks, Users, FlipkartOrders, OrdersRaw
|
12 |
from dtr.storage.DataService import Clicks, Users, FlipkartOrders, OrdersRaw
|
| 13 |
from dtr.utils import utils
|
13 |
from dtr.utils import utils
|
| 14 |
from dtr.utils.utils import fetchResponseUsingProxy, todict, getSkuData, \
|
14 |
from dtr.utils.utils import fetchResponseUsingProxy, todict, getSkuData, \
|
| 15 |
ORDER_PLACED
|
15 |
ORDER_PLACED, readSSh
|
| 16 |
from elixir import *
|
16 |
from elixir import *
|
| 17 |
from pprint import pprint
|
17 |
from pprint import pprint
|
| 18 |
from pymongo.mongo_client import MongoClient
|
18 |
from pymongo.mongo_client import MongoClient
|
| 19 |
from urlparse import urlparse
|
19 |
from urlparse import urlparse
|
| 20 |
import StringIO
|
20 |
import StringIO
|
| Line 52... |
Line 52... |
| 52 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
52 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
| 53 |
'Connection':'keep-alive',
|
53 |
'Connection':'keep-alive',
|
| 54 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
54 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
| 55 |
}
|
55 |
}
|
| 56 |
|
56 |
|
| 57 |
TRACK_URLS = ['https://www.flipkart.com/api/1/self-serve/orders?pageNum=1&link=myAccounts', 'https://www.flipkart.com/api/1/self-serve/orders?pageNum=2&link=myAccounts', 'https://www.flipkart.com/api/1/self-serve/orders?pageNum=1&link=myAccounts']
|
57 |
TRACK_URLS = ['https://www.flipkart.com/api/1/self-serve/orders?pageNum=1&link=myAccounts', 'https://www.flipkart.com/api/1/self-serve/orders?pageNum=2&link=myAccounts', 'https://www.flipkart.com/api/1/self-serve/orders?pageNum=3&link=myAccounts']
|
| 58 |
TRACK_HEADERS = {
|
58 |
TRACK_HEADERS = {
|
| 59 |
"sn":"2.VIBAC8BD9D21024F649B0482FAC385814A.SIE6C5F25D78974A5EB850D89167D52009.VSE7C76F85C2EE463786F89407CA46CA31.1492756338",
|
59 |
"sn":"2.VIBAC8BD9D21024F649B0482FAC385814A.SIE6C5F25D78974A5EB850D89167D52009.VSE7C76F85C2EE463786F89407CA46CA31.1492756338",
|
| 60 |
"Connection":"keep-alive",
|
60 |
"Connection":"keep-alive",
|
| 61 |
"Referer":"https://www.flipkart.com/rv/orders?link=myAccounts",
|
61 |
"Referer":"https://www.flipkart.com/rv/orders?link=myAccounts",
|
| 62 |
"cache-control":"no-cache",
|
62 |
"cache-control":"no-cache",
|
| Line 73... |
Line 73... |
| 73 |
}
|
73 |
}
|
| 74 |
|
74 |
|
| 75 |
|
75 |
|
| 76 |
class Store(MStore):
|
76 |
class Store(MStore):
|
| 77 |
OrderStatusMap = {
|
77 |
OrderStatusMap = {
|
| 78 |
main.Store.ORDER_PLACED : ['approval', 'processing', 'shipping'],
|
78 |
main.Store.ORDER_PLACED : ['approval', 'processing', 'shipping', 'your order has been placed.'],
|
| 79 |
main.Store.ORDER_DELIVERED : ['your item has been delivered'],
|
79 |
main.Store.ORDER_DELIVERED : ['your item has been delivered'],
|
| 80 |
main.Store.ORDER_SHIPPED : ['in transit', 'shipment yet to be delivered'],
|
80 |
main.Store.ORDER_SHIPPED : ['in transit', 'shipment yet to be delivered'],
|
| 81 |
main.Store.ORDER_CANCELLED : ['shipment is returned', 'your item has been returned', 'your shipment has been cancelled', 'your shipment has been cancelled.']
|
81 |
main.Store.ORDER_CANCELLED : ['shipment is returned', 'your item has been returned', 'your shipment has been cancelled', 'your shipment has been cancelled.']
|
| 82 |
|
82 |
|
| 83 |
}
|
83 |
}
|
| Line 88... |
Line 88... |
| 88 |
|
88 |
|
| 89 |
def getName(self):
|
89 |
def getName(self):
|
| 90 |
return "flipkart"
|
90 |
return "flipkart"
|
| 91 |
|
91 |
|
| 92 |
def _parseJSON(self,merchantOrder, transaction):
|
92 |
def _parseJSON(self,merchantOrder, transaction):
|
| - |
|
93 |
merchantOrder = obj(merchantOrder)
|
| 93 |
update = False
|
94 |
update = False
|
| 94 |
if not merchantOrder.subOrders:
|
95 |
if not hasattr(merchantOrder, 'subOrders'):
|
| 95 |
merchantOrder.placedOn = transaction['orderDate']
|
96 |
merchantOrder.placedOn = transaction['orderDate']
|
| 96 |
merchantOrder.paidAmount = transaction['amount']
|
97 |
merchantOrder.paidAmount = transaction['amount']
|
| 97 |
subOrders = []
|
98 |
subOrders = []
|
| 98 |
merchantOrder.subOrders = subOrders
|
99 |
merchantOrder.subOrders = subOrders
|
| 99 |
for shipment in transaction['groupedItems']:
|
100 |
for shipment in transaction['groupedItems']:
|
| 100 |
for item in shipment:
|
101 |
for item in shipment:
|
| 101 |
productUrl = "https://www.flipkart.com/" + item['itemMetadata']
|
102 |
productUrl = "https://www.flipkart.com" + item['itemMetadata']['url']
|
| 102 |
merchantsubOrder = SubOrder(item['title']+ item['itemMetadata']['color'], productUrl, merchantOrder.placedOn, item['amount'],MStore.ORDER_PLACED, item['quantity'])
|
103 |
merchantsubOrder = SubOrder(item['title']+ item['itemMetadata']['color'], productUrl, merchantOrder.placedOn, item['amount'],MStore.ORDER_PLACED, item['quantity'])
|
| 103 |
merchantsubOrder.merchantSubOrderId = item['orderItemId']
|
104 |
merchantsubOrder.merchantSubOrderId = item['orderItemId']
|
| 104 |
merchantsubOrder.imgUrl = item['itemMetadata']['images']['1000x1000']
|
105 |
merchantsubOrder.imgUrl = item['itemMetadata']['images']['1000x1000']
|
| 105 |
productCode=productUrl.split("pid=")[1]
|
106 |
productCode=productUrl.split("pid=")[1]
|
| 106 |
merchantsubOrder.productCode = productCode
|
107 |
merchantsubOrder.productCode = productCode
|
| 107 |
merchantsubOrder.merchantSubOrderId = item['orderItemId']
|
108 |
merchantsubOrder.merchantSubOrderId = item['orderItemId']
|
| 108 |
merchantsubOrder.amount = item['sellingPrice']*merchantsubOrder.quantity
|
109 |
merchantsubOrder.amount = item['itemSellingPrice']*merchantsubOrder.quantity
|
| 109 |
merchantsubOrder.unitPrice = item['sellingPrice']
|
110 |
merchantsubOrder.unitPrice = item['itemSellingPrice']
|
| 110 |
merchantsubOrder.detailedStatus = item['desktopSubStatus']
|
111 |
merchantsubOrder.detailedStatus = item['desktopSubStatus']
|
| 111 |
subOrders.append(merchantsubOrder)
|
112 |
subOrders.append(merchantsubOrder)
|
| 112 |
else:
|
113 |
else:
|
| 113 |
update = True
|
114 |
update = True
|
| 114 |
for shipment in transaction['groupedItems']:
|
115 |
for shipment in transaction['groupedItems']:
|
| Line 116... |
Line 117... |
| 116 |
subOrder = self._isSubOrderActive(merchantOrder, item['orderItemId'])
|
117 |
subOrder = self._isSubOrderActive(merchantOrder, item['orderItemId'])
|
| 117 |
if subOrder:
|
118 |
if subOrder:
|
| 118 |
subOrder.detailedStatus = item['desktopSubStatus']
|
119 |
subOrder.detailedStatus = item['desktopSubStatus']
|
| 119 |
|
120 |
|
| 120 |
self.populateDerivedFields(merchantOrder, update)
|
121 |
self.populateDerivedFields(merchantOrder, update)
|
| - |
|
122 |
self._saveToOrder(todict(merchantOrder))
|
| 121 |
|
123 |
|
| 122 |
def _getActiveOrders(self, searchMap={}, collectionMap={}):
|
124 |
def _getActiveOrders(self, searchMap={}, collectionMap={}):
|
| 123 |
collection = self.db.merchantOrder
|
125 |
collection = self.db.merchantOrder
|
| 124 |
searchMap = dict(searchMap.items()+ {"closed": False, "storeId" : self.store_id}.items())
|
126 |
searchMap = dict(searchMap.items()+ {"closed": False, "storeId" : self.store_id}.items())
|
| 125 |
#collectionMap = dict(collectionMap.items() + {"orderSuccessUrl":1, "orderId":1,"subOrders":1, "placedOn":1, "orderTracking"}.items())
|
127 |
#collectionMap = dict(collectionMap.items() + {"orderSuccessUrl":1, "orderId":1,"subOrders":1, "placedOn":1, "orderTracking"}.items())
|
| Line 143... |
Line 145... |
| 143 |
else:
|
145 |
else:
|
| 144 |
return {"headers":TRACK_HEADERS, "urls":[]}
|
146 |
return {"headers":TRACK_HEADERS, "urls":[]}
|
| 145 |
else:
|
147 |
else:
|
| 146 |
jsonObject = json.loads(rawHtml)
|
148 |
jsonObject = json.loads(rawHtml)
|
| 147 |
#Get only orders less than one month old
|
149 |
#Get only orders less than one month old
|
| 148 |
activeOrders = self._getActiveOrders({"createdOnInt":{"$gt":int(time.mktime(datetime.now().timetuple())) - - 30*86400}})
|
150 |
activeOrders = self._getActiveOrders({"createdOnInt":{"$gt":int(time.mktime(datetime.now().timetuple())) - 30*86400}})
|
| 149 |
for order in activeOrders:
|
151 |
for order in activeOrders:
|
| 150 |
for transaction in jsonObject['RESPONSE']['orderGranularDetails']:
|
152 |
for transaction in jsonObject['RESPONSE']['orderGranularDetails']:
|
| 151 |
if transaction['orderId'][:-3] == order["mechantOrderId"]:
|
153 |
if transaction['orderId'][:-2] == order["merchantOrderId"]:
|
| 152 |
self._parseJSON(order, transaction)
|
154 |
self._parseJSON(order, transaction)
|
| 153 |
break
|
155 |
break
|
| 154 |
|
156 |
|
| 155 |
|
157 |
|
| 156 |
|
158 |
|
| Line 409... |
Line 411... |
| 409 |
m = hashlib.md5()
|
411 |
m = hashlib.md5()
|
| 410 |
print(m.digest())
|
412 |
print(m.digest())
|
| 411 |
|
413 |
|
| 412 |
def main():
|
414 |
def main():
|
| 413 |
store = getStore(2)
|
415 |
store = getStore(2)
|
| 414 |
store.parseOrderRawHtml(1, '1234', 14, '', 'https://www.flipkart.com/rv/orderConfirmation/orderresponse?pr=1&reference_id=OD1090350248246880&src=or&type=physical&token=3f720f955fc94219e7fef31992302b38')
|
416 |
store.trackOrdersForUser(30916, "https://www.flipkart.com/api/1/self-serve/orders?pageNum=1&link=myAccounts", readSSh("/home/amit/Downloads/htmldata"))
|
| 415 |
#store.scrapeStoreOrders()
|
417 |
#store.scrapeStoreOrders()
|
| 416 |
|
418 |
|
| 417 |
if __name__ == '__main__':
|
419 |
if __name__ == '__main__':
|
| 418 |
main()
|
420 |
main()
|
| 419 |
|
421 |
|
| 420 |
|
422 |
|