Subversion Repositories SmartDukaan

Rev

Rev 18161 | Rev 18377 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 18161 Rev 18376
Line 21... Line 21...
21
import base64
21
import base64
22
import dtr
22
import dtr
23
import gzip
23
import gzip
24
import mechanize
24
import mechanize
25
import os.path
25
import os.path
26
import pyquery
26
from pyquery import PyQuery as pq
27
import re
27
import re
28
import time
28
import time
29
import traceback
29
import traceback
30
import urllib2
30
import urllib2
31
 
31
 
Line 54... Line 54...
54
        print parseString, "Order Id to be parsed is :", orderId
54
        print parseString, "Order Id to be parsed is :", orderId
55
        resp = {}
55
        resp = {}
56
        resp['result'] = 'ORDER_NOT_CREATED'
56
        resp['result'] = 'ORDER_NOT_CREATED'
57
        if ORDER_SUCCESS_URL in orderSuccessUrl or THANKYOU_URL in orderSuccessUrl:
57
        if ORDER_SUCCESS_URL in orderSuccessUrl or THANKYOU_URL in orderSuccessUrl:
58
            try:
58
            try:
59
                from pyquery import PyQuery as pq
-
 
60
                doc = pq(rawHtml)
59
                doc = pq(rawHtml)
61
                try:
60
                try:
62
                    orderUrl = doc('a.a-touch-link').attr.href
61
                    orderUrl = doc('a.a-touch-link').attr.href
63
                    merchantOrderId = re.findall(r'.*&oid=(.*)&?.*?', orderUrl)[0]
62
                    merchantOrderId = re.findall(r'.*&oid=(.*)&?.*?', orderUrl)[0]
64
                except:
63
                except:
Line 129... Line 128...
129
                        self.parseNewStlye(merchantOrder, soup)
128
                        self.parseNewStlye(merchantOrder, soup)
130
                        resp['result'] = 'DETAIL_CREATED'
129
                        resp['result'] = 'DETAIL_CREATED'
131
                    except:
130
                    except:
132
                        try:
131
                        try:
133
                            traceback.print_exc()
132
                            traceback.print_exc()
134
                            self.parseOldStlye(merchantOrder, soup)
133
                            self.parseAnotherStlye(merchantOrder, pq(rawHtml))
135
                            resp['result'] = 'DETAIL_CREATED'
134
                            resp['result'] = 'DETAIL_CREATED'
136
                        except:
135
                        except:
137
                            traceback.print_exc()
-
 
138
                            try:
136
                            try:
-
 
137
                                traceback.print_exc()
139
                                self.parseCancelled(merchantOrder, soup)
138
                                self.parseOldStlye(merchantOrder, soup)
140
                                resp['result'] = 'ORDER_CANCELLED'
139
                                resp['result'] = 'DETAIL_CREATED'
141
                            except:
140
                            except:
-
 
141
                                traceback.print_exc()
142
                                try:
142
                                try:
143
                                    if soup.find("h1").text.strip() in ["Your Account"] or soup.find("h1").span.text=="Account":
143
                                    self.parseCancelled(merchantOrder, soup)
144
                                        resp['result'] = 'DETAIL_NOT_CREATED_KNOWN'
144
                                    resp['result'] = 'ORDER_CANCELLED'
145
                                    else:
-
 
146
                                        raise
-
 
147
                                except:
145
                                except:
-
 
146
                                    try:
-
 
147
                                        if soup.find("h1").text.strip() in ["Your Account"] or soup.find("h1").span.text=="Account":
-
 
148
                                            resp['result'] = 'DETAIL_NOT_CREATED_KNOWN'
-
 
149
                                        else:
-
 
150
                                            raise
-
 
151
                                    except:
148
                                    if soup.find(id="ap_signin_pagelet_title").find("h1").text.strip()=="Sign In":
152
                                        if soup.find(id="ap_signin_pagelet_title").find("h1").text.strip()=="Sign In":
149
                                        resp['result'] = 'DETAIL_NOT_CREATED_KNOWN'
153
                                            resp['result'] = 'DETAIL_NOT_CREATED_KNOWN'
150
                                    else:
154
                                        else:
151
                                        raise
155
                                            raise
152
                if resp['result'] == 'DETAIL_NOT_CREATED_KNOWN':
156
                if resp['result'] == 'DETAIL_NOT_CREATED_KNOWN':
153
                    self.db.merchantOrder.update({"orderId":orderId}, {"$set":{"status":"html_required"}})
157
                    self.db.merchantOrder.update({"orderId":orderId}, {"$set":{"status":"html_required"}})
154
            except:
158
            except:
155
                self.db.merchantOrder.update({"orderId":orderId}, {"$set":{"status":"html_required"}})
159
                self.db.merchantOrder.update({"orderId":orderId}, {"$set":{"status":"html_required"}})
156
                print "Error occurred"
160
                print "Error occurred"
Line 284... Line 288...
284
            diff = totalAmount - grandAmount
288
            diff = totalAmount - grandAmount
285
            for subOrder in merchantOrder.subOrders:
289
            for subOrder in merchantOrder.subOrders:
286
                subOrder.amountPaid -= int(diff*(1-subOrder.amountPaid/totalAmount))
290
                subOrder.amountPaid -= int(diff*(1-subOrder.amountPaid/totalAmount))
287
        merchantOrder.status='success'
291
        merchantOrder.status='success'
288
        self._updateToOrder(todict(merchantOrder))
292
        self._updateToOrder(todict(merchantOrder))
-
 
293
        
-
 
294
    def parseAnotherStlye(self, merchantOrder, pqobj):
-
 
295
        counter=0
-
 
296
        detailSection = None
-
 
297
        shipmentSection = None
-
 
298
        summarySection = None
-
 
299
        for el in pqobj('.a-section'):
-
 
300
            if "View order details" in pq(el).text():
-
 
301
                detailSection =  pq(el)
-
 
302
                counter += 1
-
 
303
            if "Shipment details" in pq(el).text():
-
 
304
                shipmentSection =  pq(el)
-
 
305
                counter += 1
-
 
306
            if "Order Summary" in pq(el).text():
-
 
307
                summarySection = pq(el)
-
 
308
                counter += 1
-
 
309
            if counter == 3:
-
 
310
                break
-
 
311
        
-
 
312
        print "detail" if detailSection else "", "shipment" if shipmentSection else "", "summary" if summarySection else ""
-
 
313
        
-
 
314
        print pq(detailSection('li .a-row .a-span-last')[0]).text(), pq(detailSection('li .a-row .a-span-last')[1]).text(), pq(detailSection('li .a-row .a-span-last')[2]).text()
-
 
315
        
-
 
316
        i=-1    
-
 
317
        for s in shipmentSection('.a-box-group'):
-
 
318
            shipmentGroup = pq(s)
-
 
319
            for shipment in shipmentGroup('.a-box'):
-
 
320
                i += 1 
-
 
321
                if i==0:
-
 
322
                    continue
-
 
323
                shipment = pq(shipment)
-
 
324
                shipmentStatusSection = shipment('.a-section:eq(0)')
-
 
325
                productDetails = shipment('.a-section:eq(1)>.a-row')
-
 
326
                print shipmentStatusSection('h3').text(), shipmentStatusSection('span').text()
-
 
327
                for productDetail in productDetails:
-
 
328
                    productDetail = pq(productDetail)
-
 
329
                    print productDetail
-
 
330
                    pImg = productDetail.children('div').eq(0)
-
 
331
                    pQty = productDetail.children('div').eq(1)
-
 
332
                    #print pImg('a').attr('href'), pImg('a').attr('title'), pImg('img').attr('src'), pQty('.a-row:nth-child(2)')('span').text().split(':')[1].strip(), pQty('span.currencyINR')
-
 
333
                    print pImg('a').attr('href'), pQty('.a-row:nth-child(2)')('span').text().split(':')[1].strip()
-
 
334
                    print int(float(pQty('nobr').text().replace('Rs.','').replace(',', '')))
-
 
335
                    
-
 
336
                        
-
 
337
        raise   
-
 
338
           
-
 
339
                
-
 
340
        merchantOrder.orderTrackingUrl = merchantOrder.orderSuccessUrl
-
 
341
        merchantOrder.placedOn
-
 
342
        merchantOrder.merchantOrderId
-
 
343
        merchantOrder.paidAmount
289
 
344
 
290
    def parseNewStlye(self, merchantOrder, soup):
345
    def parseNewStlye(self, merchantOrder, soup):
291
        merchantOrder.orderTrackingUrl = merchantOrder.orderSuccessUrl
346
        merchantOrder.orderTrackingUrl = merchantOrder.orderSuccessUrl
292
        orderDetailsContainer = soup.body.find(id="orderDetails")
347
        orderDetailsContainer = soup.body.find(id="orderDetails")
293
        divAfterH1 = orderDetailsContainer.h1.next_sibling.next_sibling
348
        divAfterH1 = orderDetailsContainer.h1.next_sibling.next_sibling
Line 774... Line 829...
774
        
829
        
775
 
830
 
776
 
831
 
777
def main():
832
def main():
778
    store = getStore(1)
833
    store = getStore(1)
779
    #store.parseOrderRawHtml("444444", '123', 14, readSSh('/home/amit/amazon.html'), 'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-4294022-1187515')
834
    #store.parseOrderRawHtml("444444", '123', 14, readSSh('/home/amit/80444.html'), 'https://www.amazon.in/gp/css/summary/edit.html?orderID=402-1449548-9569169')
780
    orders = list(session.query(OrdersRaw).filter_by(status = 'DETAIL_NOT_CREATED_UNKNOWN').filter(OrdersRaw.id > 61071).all())
835
    orders = list(session.query(OrdersRaw).filter_by(status = 'DETAIL_NOT_CREATED_UNKNOWN').filter(OrdersRaw.id > 61071).all())
781
    session.close()
836
    #session.close()
782
    for o in orders:
837
    #for o in orders:
783
        try:
838
    #    try:
784
            store.trackOrdersForUser(o.id, o.order_url, o.rawhtml)
839
    #        store.trackOrdersForUser(o.id, o.order_url, o.rawhtml)
785
        finally:
840
    #    finally:
786
            session.close()
841
    #        session.close()
787
    #store.trackOrdersForUser(10466, 'https://www.amazon.in/gp/css/summary/edit.html?orderID=403-7498756-0837158', readSSh('/AmazonTrack/User10466/403-7498756-0837158'))
842
    #store.trackOrdersForUser(10466, 'https://www.amazon.in/gp/css/summary/edit.html?orderID=403-7498756-0837158', readSSh('/AmazonTrack/User10466/403-7498756-0837158'))
788
    #store.trackOrdersForUser(46195, 'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-4294022-1187515', readSSh('/home/amit/amazon.html'))
843
    #store.trackOrdersForUser(46195, 'https://www.amazon.in/gp/css/summary/edit.html?orderID=404-4294022-1187515', readSSh('/home/amit/amazon.html'))
789
    
844
    
790
def getSummaryFile(directory):
845
def getSummaryFile(directory):
791
    date1 = datetime(2015,1,1)
846
    date1 = datetime(2015,1,1)