| Line 9... |
Line 9... |
| 9 |
from datetime import datetime, timedelta
|
9 |
from datetime import datetime, timedelta
|
| 10 |
from dtr.dao import Order, SubOrder, AmazonAffiliateInfo
|
10 |
from dtr.dao import Order, SubOrder, AmazonAffiliateInfo
|
| 11 |
from dtr.main import getStore, Store as MStore, ParseException, getBrowserObject, \
|
11 |
from dtr.main import getStore, Store as MStore, ParseException, getBrowserObject, \
|
| 12 |
ungzipResponse, tprint
|
12 |
ungzipResponse, tprint
|
| 13 |
from dtr.sources.flipkart import todict, AFF_REPORT_URL
|
13 |
from dtr.sources.flipkart import todict, AFF_REPORT_URL
|
| - |
|
14 |
from dtr.utils.utils import fetchResponseUsingProxy
|
| 14 |
from paramiko import sftp
|
15 |
from paramiko import sftp
|
| 15 |
from paramiko.client import SSHClient
|
16 |
from paramiko.client import SSHClient
|
| 16 |
from paramiko.sftp_client import SFTPClient
|
17 |
from paramiko.sftp_client import SFTPClient
|
| 17 |
import base64
|
18 |
import base64
|
| 18 |
import gzip
|
19 |
import gzip
|
| Line 39... |
Line 40... |
| 39 |
|
40 |
|
| 40 |
def __init__(self,store_id):
|
41 |
def __init__(self,store_id):
|
| 41 |
super(Store, self).__init__(store_id)
|
42 |
super(Store, self).__init__(store_id)
|
| 42 |
|
43 |
|
| 43 |
def getName(self):
|
44 |
def getName(self):
|
| 44 |
return "flipkart"
|
45 |
return "amazon"
|
| 45 |
|
46 |
|
| 46 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl, insert=False):
|
47 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl, insert=False):
|
| 47 |
resp = {}
|
48 |
resp = {}
|
| 48 |
if ORDER_SUCCESS_URL in orderSuccessUrl or THANKYOU_URL in orderSuccessUrl:
|
49 |
if ORDER_SUCCESS_URL in orderSuccessUrl or THANKYOU_URL in orderSuccessUrl:
|
| 49 |
try:
|
50 |
try:
|
| Line 74... |
Line 75... |
| 74 |
|
75 |
|
| 75 |
else:
|
76 |
else:
|
| 76 |
try:
|
77 |
try:
|
| 77 |
mo = self.db.merchantOrder.find_one({"orderId":orderId})
|
78 |
mo = self.db.merchantOrder.find_one({"orderId":orderId})
|
| 78 |
if mo is not None:
|
79 |
if mo is not None:
|
| 79 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
80 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl, False)
|
| 80 |
merchantOrder.createdOn = mo.get("createdOn")
|
81 |
merchantOrder.createdOn = mo.get("createdOn")
|
| 81 |
merchantOrder.createdOnInt = mo.get("createdOnInt")
|
82 |
merchantOrder.createdOnInt = mo.get("createdOnInt")
|
| 82 |
else:
|
83 |
else:
|
| 83 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl, False)
|
84 |
merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
|
| 84 |
soup = BeautifulSoup(rawHtml)
|
85 |
soup = BeautifulSoup(rawHtml)
|
| 85 |
try:
|
86 |
try:
|
| 86 |
self.parseOldStlye(merchantOrder, soup)
|
87 |
self.parseOldStlye(merchantOrder, soup)
|
| 87 |
except:
|
88 |
except:
|
| 88 |
try:
|
89 |
try:
|
| Line 102... |
Line 103... |
| 102 |
resp['result'] = 'ORDER_NOT_CREATED'
|
103 |
resp['result'] = 'ORDER_NOT_CREATED'
|
| 103 |
return resp
|
104 |
return resp
|
| 104 |
|
105 |
|
| 105 |
#This should be exposed from api for specific sources
|
106 |
#This should be exposed from api for specific sources
|
| 106 |
def scrapeStoreOrders(self):
|
107 |
def scrapeStoreOrders(self):
|
| 107 |
br = getBrowserObject()
|
- |
|
| 108 |
orders = self.db.merchantOrder.find({"storeId":1, "closed":False, "subOrders.closed":False, "subOrders.trackingUrl":{"$exists":True}})
|
108 |
orders = self.db.merchantOrder.find({"storeId":1, "closed":False, "subOrders.closed":False, "subOrders.trackingUrl":{"$exists":True}})
|
| 109 |
for merchantOrder in orders:
|
109 |
for merchantOrder in orders:
|
| 110 |
try:
|
110 |
try:
|
| 111 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
111 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
| 112 |
closed = True
|
112 |
closed = True
|
| Line 118... |
Line 118... |
| 118 |
closed = False
|
118 |
closed = False
|
| 119 |
continue
|
119 |
continue
|
| 120 |
findMap = {"orderId":merchantOrder.get("orderId"), "subOrders.merchantSubOrderId":subOrder.get("merchantSubOrderId")}
|
120 |
findMap = {"orderId":merchantOrder.get("orderId"), "subOrders.merchantSubOrderId":subOrder.get("merchantSubOrderId")}
|
| 121 |
trackingUrl = subOrder.get("trackingUrl")
|
121 |
trackingUrl = subOrder.get("trackingUrl")
|
| 122 |
if not map1.has_key(trackingUrl):
|
122 |
if not map1.has_key(trackingUrl):
|
| 123 |
map1[trackingUrl] = self.parseTrackingUrl(br, trackingUrl)
|
123 |
map1[trackingUrl] = self.parseTrackingUrl(trackingUrl)
|
| 124 |
newOrder = map1.get(trackingUrl)
|
124 |
newOrder = map1.get(trackingUrl)
|
| 125 |
newOrder['cashBackStatus'] = subOrder.get('cashBackStatus')
|
125 |
newOrder['cashBackStatus'] = subOrder.get('cashBackStatus')
|
| 126 |
updateMap = self.getUpdateMap(newOrder)
|
126 |
updateMap = self.getUpdateMap(newOrder)
|
| 127 |
print findMap, "\n", updateMap
|
127 |
print findMap, "\n", updateMap
|
| 128 |
bulk.find(findMap).update({'$set' : updateMap})
|
128 |
bulk.find(findMap).update({'$set' : updateMap})
|
| Line 470... |
Line 470... |
| 470 |
self.db.amazonAffiliateInfo.insert(todict(amazonAffiliate))
|
470 |
self.db.amazonAffiliateInfo.insert(todict(amazonAffiliate))
|
| 471 |
else:
|
471 |
else:
|
| 472 |
break
|
472 |
break
|
| 473 |
|
473 |
|
| 474 |
|
474 |
|
| 475 |
def parseTrackingUrl(self, br, trackingUrl):
|
475 |
def parseTrackingUrl(self, trackingUrl):
|
| 476 |
subOrder = {}
|
476 |
subOrder = {}
|
| 477 |
response = br.open(trackingUrl)
|
477 |
page = fetchResponseUsingProxy(trackingUrl)
|
| 478 |
page = ungzipResponse(response)
|
- |
|
| 479 |
soup = BeautifulSoup(page)
|
478 |
soup = BeautifulSoup(page)
|
| 480 |
alertContainer = soup.find("div", {"class":"a-box-inner a-alert-container"})
|
479 |
alertContainer = soup.find("div", {"class":"a-box-inner a-alert-container"})
|
| 481 |
if alertContainer is not None:
|
480 |
if alertContainer is not None:
|
| 482 |
statusText = alertContainer.h4.span.text
|
481 |
statusText = alertContainer.h4.span.text
|
| 483 |
detailedStatus = statusText.split(":")[0].strip()
|
482 |
detailedStatus = statusText.split(":")[0].strip()
|
| Line 506... |
Line 505... |
| 506 |
|
505 |
|
| 507 |
|
506 |
|
| 508 |
|
507 |
|
| 509 |
def main():
|
508 |
def main():
|
| 510 |
store = getStore(1)
|
509 |
store = getStore(1)
|
| - |
|
510 |
store.scrapeStoreOrders()
|
| 511 |
# br = mechanize.Browser()
|
511 |
# br = mechanize.Browser()
|
| 512 |
# br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
|
512 |
# br.addheaders = [('User-agent','Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'),
|
| 513 |
# ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
|
513 |
# ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
|
| 514 |
# ('Accept-Encoding', 'gzip,deflate,sdch'),
|
514 |
# ('Accept-Encoding', 'gzip,deflate,sdch'),
|
| 515 |
# ('Accept-Language', 'en-US,en;q=0.8'),
|
515 |
# ('Accept-Language', 'en-US,en;q=0.8'),
|
| 516 |
# ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
516 |
# ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
| 517 |
# store.scrapeStoreOrders()
|
517 |
# store.scrapeStoreOrders()
|
| 518 |
store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/1313.html"), "https://www.amazon.in/gp/css/summary/edit.html?orderID=171-5857029-0804336")
|
518 |
#store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/1313.html"), "https://www.amazon.in/gp/css/summary/edit.html?orderID=171-5857029-0804336")
|
| 519 |
#store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/f1.html"), "https://www.amazon.in/gp/css/summary/edit.html?orderID=171-5196461-3230730")
|
519 |
#store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/f1.html"), "https://www.amazon.in/gp/css/summary/edit.html?orderID=171-5196461-3230730")
|
| 520 |
#readSSh("/tmp/User211/2015-04-01 15:31:42.250309")
|
520 |
#readSSh("/tmp/User211/2015-04-01 15:31:42.250309")
|
| 521 |
#store.scrapeAffiliate()
|
521 |
#store.scrapeAffiliate()
|
| 522 |
def readSSh(fileName):
|
522 |
def readSSh(fileName):
|
| 523 |
try:
|
523 |
try:
|