| Line 7... |
Line 7... |
| 7 |
from base64 import encode
|
7 |
from base64 import encode
|
| 8 |
from bs4 import BeautifulSoup
|
8 |
from bs4 import BeautifulSoup
|
| 9 |
from datetime import datetime
|
9 |
from datetime import datetime
|
| 10 |
from dtr.dao import Order, SubOrder
|
10 |
from dtr.dao import Order, SubOrder
|
| 11 |
from dtr.main import getStore, Store as MStore, ParseException, getBrowserObject, \
|
11 |
from dtr.main import getStore, Store as MStore, ParseException, getBrowserObject, \
|
| 12 |
ungzipResponse
|
12 |
ungzipResponse, tprint
|
| 13 |
from dtr.sources.flipkart import todict
|
13 |
from dtr.sources.flipkart import todict
|
| 14 |
from paramiko import sftp
|
14 |
from paramiko import sftp
|
| 15 |
from paramiko.client import SSHClient
|
15 |
from paramiko.client import SSHClient
|
| 16 |
from paramiko.sftp_client import SFTPClient
|
16 |
from paramiko.sftp_client import SFTPClient
|
| - |
|
17 |
import mechanize
|
| 17 |
import os.path
|
18 |
import os.path
|
| 18 |
import paramiko
|
19 |
import paramiko
|
| 19 |
import re
|
20 |
import re
|
| 20 |
import time
|
21 |
import time
|
| 21 |
import traceback
|
22 |
import traceback
|
| 22 |
|
23 |
|
| 23 |
ORDER_REDIRECT_URL = 'https://www.amazon.in/gp/css/summary/edit.html?orderID=%s'
|
24 |
ORDER_REDIRECT_URL = 'https://www.amazon.in/gp/css/summary/edit.html?orderID=%s'
|
| 24 |
ORDER_SUCCESS_URL = 'https://www.amazon.in/gp/buy/spc/handlers/static-submit-decoupled.html'
|
25 |
ORDER_SUCCESS_URL = 'https://www.amazon.in/gp/buy/spc/handlers/static-submit-decoupled.html'
|
| 25 |
THANKYOU_URL = 'https://www.amazon.in/gp/buy/thankyou/handlers/display.html'
|
26 |
THANKYOU_URL = 'https://www.amazon.in/gp/buy/thankyou/handlers/display.html'
|
| - |
|
27 |
AMAZON_AFF_URL = 'https://assoc-datafeeds-eu.amazon.com/datafeed/listReports'
|
| 26 |
class Store(MStore):
|
28 |
class Store(MStore):
|
| 27 |
|
29 |
|
| 28 |
orderStatusRegexMap = { MStore.ORDER_PLACED : ['ordered from', 'not yet dispatched','dispatching now', 'preparing for dispatch'],
|
30 |
orderStatusRegexMap = { MStore.ORDER_PLACED : ['ordered from', 'not yet dispatched','dispatching now', 'preparing for dispatch'],
|
| 29 |
MStore.ORDER_SHIPPED : ['dispatched on','dispatched', 'on the way', 'out for delivery', 'Out for delivery'],
|
31 |
MStore.ORDER_SHIPPED : ['dispatched on','dispatched', 'on the way', 'out for delivery', 'Out for delivery'],
|
| 30 |
MStore.ORDER_CANCELLED : ['return complete', 'refunded', 'cancelled'],
|
32 |
MStore.ORDER_CANCELLED : ['return complete', 'refunded', 'cancelled'],
|
| Line 86... |
Line 88... |
| 86 |
resp['result'] = 'ORDER_NOT_CREATED'
|
88 |
resp['result'] = 'ORDER_NOT_CREATED'
|
| 87 |
return resp
|
89 |
return resp
|
| 88 |
|
90 |
|
| 89 |
#This should be exposed from api for specific sources
|
91 |
#This should be exposed from api for specific sources
|
| 90 |
def scrapeStoreOrders(self):
|
92 |
def scrapeStoreOrders(self):
|
| 91 |
# br = getBrowserObject()
|
93 |
br = getBrowserObject()
|
| 92 |
# orders = self.db.merchantOrder.find({"storeId":1, "closed":False, "subOrders.closed":False, "subOrders.trackingUrl":{"$exists":True}})
|
94 |
orders = self.db.merchantOrder.find({"storeId":1, "closed":False, "subOrders.closed":False, "subOrders.trackingUrl":{"$exists":True}})
|
| 93 |
# for merchantOrder in orders:
|
95 |
for merchantOrder in orders:
|
| - |
|
96 |
try:
|
| - |
|
97 |
bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
|
| - |
|
98 |
closed = True
|
| - |
|
99 |
map1 = {}
|
| 94 |
# for subOrder in merchantOrder.get("subOrders"):
|
100 |
for subOrder in merchantOrder.get("subOrders"):
|
| 95 |
# trackingUrl = subOrder.get("trackingUrl")
|
101 |
if subOrder.get("closed"):
|
| - |
|
102 |
continue
|
| 96 |
# if not subOrder.get("closed") and trackingUrl is not None:
|
103 |
elif subOrder.get("trackingUrl") is None:
|
| - |
|
104 |
closed = False
|
| 97 |
#
|
105 |
continue
|
| - |
|
106 |
findMap = {"orderId":merchantOrder.get("orderId"), "subOrders.merchantSubOrderId":subOrder.get("merchantSubOrderId")}
|
| - |
|
107 |
trackingUrl = subOrder.get("trackingUrl")
|
| - |
|
108 |
if not map1.has_key(trackingUrl):
|
| 98 |
# self.parseTrackingUrl(br, trackingUrl)
|
109 |
map1[trackingUrl] = self.parseTrackingUrl(br, trackingUrl)
|
| - |
|
110 |
newOrder = map1.get(trackingUrl)
|
| - |
|
111 |
newOrder['cashBackStatus'] = subOrder.get('cashBackStatus')
|
| - |
|
112 |
updateMap = self.getUpdateMap(newOrder)
|
| - |
|
113 |
print findMap, "\n", updateMap
|
| - |
|
114 |
bulk.find(findMap).update({'$set' : updateMap})
|
| - |
|
115 |
closed = closed and newOrder['closed']
|
| - |
|
116 |
if closed:
|
| - |
|
117 |
print "Closed", {"orderId":merchantOrder.get("orderId")}, {"closed":True}
|
| - |
|
118 |
bulk.find({"orderId":merchantOrder.get("orderId")}).update({"$set":{"closed":True}})
|
| - |
|
119 |
result = bulk.execute()
|
| - |
|
120 |
tprint(result)
|
| 99 |
pass
|
121 |
except:
|
| - |
|
122 |
tprint("Could not update " + str(merchantOrder['orderId']))
|
| - |
|
123 |
traceback.print_exc()
|
| - |
|
124 |
|
| 100 |
|
125 |
|
| 101 |
|
126 |
|
| 102 |
|
127 |
|
| 103 |
|
- |
|
| 104 |
def parseOldStlye(self, merchantOrder, soup):
|
128 |
def parseOldStlye(self, merchantOrder, soup):
|
| 105 |
merchantOrder.orderTrackingUrl = merchantOrder.orderSuccessUrl
|
129 |
merchantOrder.orderTrackingUrl = merchantOrder.orderSuccessUrl
|
| 106 |
table = soup.body.findAll("table", recursive=False)[1]
|
130 |
table = soup.body.findAll("table", recursive=False)[1]
|
| 107 |
#print table
|
131 |
#print table
|
| 108 |
tables = table.tr.td.findAll("table", recursive=False)
|
132 |
tables = table.tr.td.findAll("table", recursive=False)
|
| Line 275... |
Line 299... |
| 275 |
|
299 |
|
| 276 |
missingOrderUrls = []
|
300 |
missingOrderUrls = []
|
| 277 |
missingOrders = self._getMissingOrders({'userId':userId})
|
301 |
missingOrders = self._getMissingOrders({'userId':userId})
|
| 278 |
for missingOrder in missingOrders:
|
302 |
for missingOrder in missingOrders:
|
| 279 |
missingOrderUrls.append(ORDER_REDIRECT_URL%(missingOrder['merchantOrderId']))
|
303 |
missingOrderUrls.append(ORDER_REDIRECT_URL%(missingOrder['merchantOrderId']))
|
| 280 |
orders = self._getActiveOrders({'userId':userId})
|
304 |
orders = self._getActiveOrders({'userId':userId, "$exists":{"subOrders.trackingUrl":False} })
|
| 281 |
count = len(orders)
|
305 |
count = len(orders)
|
| 282 |
print "count", count
|
306 |
print "count", count
|
| 283 |
if count > 0:
|
307 |
if count > 0:
|
| 284 |
return missingOrderUrls + ['https://www.amazon.in/gp/css/order-history', 'https://www.amazon.in/gp/css/order-history/?orderFilter=cancelled', 'https://www.amazon.in/gp/css/order-history?orderFilter=cancelled&startIndex=10']
|
308 |
return missingOrderUrls + ['https://www.amazon.in/gp/css/order-history', 'https://www.amazon.in/gp/css/order-history/?orderFilter=cancelled', 'https://www.amazon.in/gp/css/order-history?orderFilter=cancelled&startIndex=10']
|
| 285 |
else:
|
309 |
else:
|
| Line 392... |
Line 416... |
| 392 |
return key
|
416 |
return key
|
| 393 |
|
417 |
|
| 394 |
print "Detailed Status need to be mapped"
|
418 |
print "Detailed Status need to be mapped"
|
| 395 |
print "Found new order status", detailedStatus
|
419 |
print "Found new order status", detailedStatus
|
| 396 |
raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
|
420 |
raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
|
| - |
|
421 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
| - |
|
422 |
pass
|
| - |
|
423 |
|
| 397 |
|
424 |
|
| 398 |
def parseTrackingUrl(self, br, trackingUrl):
|
425 |
def parseTrackingUrl(self, br, trackingUrl):
|
| - |
|
426 |
subOrder = {}
|
| 399 |
response = br.open(trackingUrl)
|
427 |
response = br.open(trackingUrl)
|
| 400 |
page = ungzipResponse(response)
|
428 |
page = ungzipResponse(response)
|
| 401 |
soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
429 |
soup = BeautifulSoup(page)
|
| 402 |
alertContainer = soup.find("a-box-inner a-alert-container")
|
430 |
alertContainer = soup.find("div", {"class":"a-box-inner a-alert-container"})
|
| - |
|
431 |
if alertContainer is not None:
|
| - |
|
432 |
statusText = alertContainer.h4.span.text
|
| - |
|
433 |
detailedStatus = statusText.split(":")[0].strip()
|
| - |
|
434 |
subOrder['detailedStatus'] = detailedStatus
|
| - |
|
435 |
if detailedStatus.lower() == "in transit":
|
| - |
|
436 |
pass
|
| - |
|
437 |
elif detailedStatus.lower() == "undeliverable":
|
| - |
|
438 |
subOrder['status'] = MStore.ORDER_CANCELLED
|
| - |
|
439 |
return subOrder
|
| - |
|
440 |
else:
|
| - |
|
441 |
summaryLeft = soup.find(id="summaryLeft")
|
| - |
|
442 |
statusText = summaryLeft.h2.text
|
| - |
|
443 |
detailedStatus = statusText.split(":")[0].strip()
|
| - |
|
444 |
subOrder['detailedStatus'] = detailedStatus
|
| - |
|
445 |
if detailedStatus.lower() == "delivered":
|
| - |
|
446 |
subOrder['deliveredOn'] = summaryLeft.findAll("span")[-2].text.strip()
|
| - |
|
447 |
subOrder['status'] = MStore.ORDER_DELIVERED
|
| - |
|
448 |
return subOrder
|
| - |
|
449 |
else:
|
| - |
|
450 |
subOrder['expectedDelivery'] = summaryLeft.findAll("span")[-1].text.strip()
|
| - |
|
451 |
return subOrder
|
| - |
|
452 |
|
| - |
|
453 |
|
| - |
|
454 |
|
| 403 |
|
455 |
|
| 404 |
|
456 |
|
| 405 |
|
457 |
|
| 406 |
|
458 |
|
| 407 |
def main():
|
459 |
def main():
|
| 408 |
store = getStore(1)
|
460 |
store = getStore(1)
|
| 409 |
#br = getBrowserObject()
|
461 |
br = mechanize.Browser()
|
| 410 |
#store.parseTrackingUrl(br, "http://www.amazon.in//gp/css/shiptrack/view.html/ref=oh_aui_direct_track_pkg_o00_s00?ie=UTF8&marketplaceOfOrigin=&orderID=171-7613541-7425906&orderingShipmentId=729710930106&packageId=1&ref=")
|
462 |
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'),
|
| - |
|
463 |
('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
|
| - |
|
464 |
('Accept-Encoding', 'gzip,deflate,sdch'),
|
| - |
|
465 |
('Accept-Language', 'en-US,en;q=0.8'),
|
| - |
|
466 |
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
| - |
|
467 |
store.scrapeStoreOrders()
|
| 411 |
store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/917.html"), "https://www.amazon.in/gp/buy/spc/handlers/static-submit-decoupled.html/ref=ox_spc_place_order?ie=UTF8&hasWorkingJavascript=")
|
468 |
#store.parseOrderRawHtml(2121, "13232", 14, readSSh("/home/amit/917.html"), "https://www.amazon.in/gp/buy/spc/handlers/static-submit-decoupled.html/ref=ox_spc_place_order?ie=UTF8&hasWorkingJavascript=")
|
| 412 |
def readSSh(fileName):
|
469 |
def readSSh(fileName):
|
| 413 |
try:
|
470 |
try:
|
| 414 |
str1 = open(fileName).read()
|
471 |
str1 = open(fileName).read()
|
| 415 |
return str1
|
472 |
return str1
|
| 416 |
except:
|
473 |
except:
|