Subversion Repositories SmartDukaan

Rev

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

Rev 13569 Rev 13658
Line 2... Line 2...
2
Created on Jan 15, 2015
2
Created on Jan 15, 2015
3
 
3
 
4
@author: amit
4
@author: amit
5
'''
5
'''
6
from dtr import main
6
from dtr import main
-
 
7
import importlib
-
 
8
from dtr.main import getBrowserObject, getStore, ParseException
-
 
9
from dtr.main import sourceMap
-
 
10
from pymongo.mongo_client import MongoClient
-
 
11
from BeautifulSoup import BeautifulSoup
-
 
12
import re
-
 
13
from dtr.dao import AffiliateInfo, Order, SubOrder
-
 
14
import urllib
-
 
15
 
-
 
16
from pprint import pprint
-
 
17
USERNAME='saholic1@gmail.com'
-
 
18
PASSWORD='spice@2020'
-
 
19
ORDER_TRACK_URL='https://m.flipkart.com/order_details'
-
 
20
 
7
class Store(main.Store):
21
class Store(main.Store):
-
 
22
    OrderStatusMap = {
-
 
23
                      main.Store.ORDER_PLACED : ['In Progress','N/A'],
-
 
24
                      main.Store.ORDER_DELIVERED : ['Delivered'],
-
 
25
                      main.Store.ORDER_SHIPPED : ['In Transit'],
-
 
26
                      main.Store.ORDER_CANCELLED : ['Closed For Vendor Reallocation']
-
 
27
                      
-
 
28
                      }
8
    def __init__(self):
29
    def __init__(self,store_id):
-
 
30
        client = MongoClient('mongodb://localhost:27017/')
-
 
31
        self.db = client.dtr
9
        super(Store, self).__init__()
32
        super(Store, self).__init__(store_id)
10
        
33
 
11
    def getName(self):
34
    def getName(self):
12
        return "flipkart"
35
        return "flipkart"
13
    
36
    
14
    def scrapeAffiliate(self, startDate=None, endDate=None):
-
 
15
        raise NotImplementedError
-
 
16
    
37
    
17
    def parseOrderPage(self, htmlString=None): 
-
 
18
        raise NotImplementedError
-
 
19
38
    
-
 
39
    
-
 
40
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
-
 
41
        br = getBrowserObject()
-
 
42
        url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', orderSuccessUrl,re.IGNORECASE)[0]
-
 
43
       
-
 
44
        response = br.open(url)
-
 
45
        page = ungzipResponse(response, br)
-
 
46
       
-
 
47
        
-
 
48
        soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
-
 
49
        
-
 
50
        sections = soup.findAll("div", {"class":"ui-app-card-body"})
-
 
51
        mainOrder = soup.findAll("ul",{"class":"m-bottom p-cart"})
-
 
52
        sections.pop(1)
-
 
53
        
-
 
54
        #fetching summary of the products
-
 
55
        for data in sections:
-
 
56
            name = data.findAll("span")
-
 
57
            i=0
-
 
58
            print "Summary Details of Flipkart"
-
 
59
            while i< len(name):
-
 
60
                if "key" in str(name[i]):
-
 
61
                    if "Grand Total" in str(name[i]):
-
 
62
                        #Total Amount
-
 
63
                        print "Total Amount " + name[i+1].text
-
 
64
                    elif "Status" in str(name[i]):
-
 
65
                        print "Summary Status " + name[i+1].text
-
 
66
                    elif "Order ID" in str(name[i]):
-
 
67
                        j=i+1
-
 
68
                        while j < i+5:
-
 
69
                            if "value" in str(name[j]):
-
 
70
                                print "Order ID " + str(j-i) + " " +name[j].text
-
 
71
                                j=j+1
-
 
72
                            else: 
-
 
73
                                break        
-
 
74
                    elif "Order Date" in str(name[i]):
-
 
75
                        print "Order Date " + name[i+1].text    
-
 
76
                i=i+1    
-
 
77
        print "Details of Sub Orders"
-
 
78
        #fetching suborders details
-
 
79
        for subOrder in mainOrder:
-
 
80
            subOrd = subOrder.findAll("li")
-
 
81
           
-
 
82
            subOrd.pop(len(subOrd)-1)
-
 
83
            for productInfo in subOrd:
-
 
84
                y = productInfo.findAll("span")
-
 
85
                for suborderId in y:
-
 
86
                    if "value emp" in str(suborderId):
-
 
87
                        #Sub Order ID
-
 
88
                        print "Sub Order ID"  + suborderId.text
-
 
89
                x = productInfo.findAll("div",{"class":"product-info"})
-
 
90
                for maik in x:
-
 
91
                    #Product title
-
 
92
                    print maik.find("a",{"class":"product-title"}).text
-
 
93
                    mname = maik.findAll("span")
-
 
94
                    k=0
-
 
95
                    while k<len(mname):
-
 
96
                        if "note" in str(mname[k]):
-
 
97
                            if "Color:" in str(mname[k]):
-
 
98
                                #Color
-
 
99
                                print "Color " + mname[k+1].text
-
 
100
                            elif "Qty:" in str(mname[k]):
-
 
101
                                #Quantity of sub item
-
 
102
                                print "Quantity "+mname[k+1].text
-
 
103
                            elif "Subtotal:" in str(mname[k]):
-
 
104
                                #SubTotal for the item
-
 
105
                                print "Sub Total"+mname[k+1].text
-
 
106
                            elif "Delivery:" in str(mname[k]):
-
 
107
                                #Delivery Date for sub order
-
 
108
                                print "Delivery Date " +mname[k+1].text            
-
 
109
                        k=k+1           
-
 
110
                status=-1    
-
 
111
                #To track shipping details         
-
 
112
                n = productInfo.findAll("div",{"class":"tracking-progress grid-row cf"})
-
 
113
                for trackingStatus in n:
-
 
114
                    tr = trackingStatus.findAll("div",{"class":"tap-bullet-area c-tab-trigger"})
-
 
115
                    if "approveDetails-complete" in str(tr):
-
 
116
                        if "processingDetails-complete" in str(tr):
-
 
117
                            if "shippingDetails-complete" in str(tr):
-
 
118
                                if "delivery-complete" in str(tr):
-
 
119
                                    status = 3
-
 
120
                                else:     
-
 
121
                                    status = 2
-
 
122
                            else:
-
 
123
                                status = 1 
-
 
124
                        else: 
-
 
125
                            status = 0     
-
 
126
                    if "dead" in str(tr):
-
 
127
                        status = 4    
-
 
128
                print "Sub Order Status " + str(status)                      
-
 
129
                f = productInfo.findAll("div",{"class":"c-tabs-content m-top active"})
-
 
130
                for trackingDetailsActive in f:
-
 
131
                    print "Sub order tracking description " + trackingDetailsActive.text
-
 
132
                
-
 
133
    
-
 
134
    def flipkartOrderTracking(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
-
 
135
        br = getBrowserObject()
-
 
136
        url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', orderSuccessUrl,re.IGNORECASE)[0]
-
 
137
       
-
 
138
        response = br.open(url)
-
 
139
        page = ungzipResponse(response, br)
-
 
140
       
-
 
141
        
-
 
142
        soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
-
 
143
        
-
 
144
        
-
 
145
        mainOrder = soup.findAll("ul",{"class":"m-bottom p-cart"})
-
 
146
        #fetching suborders details
-
 
147
        for subOrder in mainOrder:
-
 
148
            subOrd = subOrder.findAll("li")
-
 
149
           
-
 
150
            subOrd.pop(len(subOrd)-1)
-
 
151
            for productInfo in subOrd:
-
 
152
                status=-1    
-
 
153
                #To track shipping details         
-
 
154
                n = productInfo.findAll("div",{"class":"tracking-progress grid-row cf"})
-
 
155
                for trackingStatus in n:
-
 
156
                    tr = trackingStatus.findAll("div",{"class":"tap-bullet-area c-tab-trigger"})
-
 
157
                    if "approveDetails-complete" in str(tr):
-
 
158
                        if "processingDetails-complete" in str(tr):
-
 
159
                            if "shippingDetails-complete" in str(tr):
-
 
160
                                if "delivery-complete" in str(tr):
-
 
161
                                    status = 3
-
 
162
                                else:     
-
 
163
                                    status = 2
-
 
164
                            else:
-
 
165
                                status = 1 
-
 
166
                        else: 
-
 
167
                            status = 0     
-
 
168
                    if "dead" in str(tr):
-
 
169
                        status = 4    
-
 
170
                print status                      
-
 
171
               
-
 
172
     
-
 
173
                
-
 
174
def _saveToOrderFlipkart(self, order):
-
 
175
        collection = self.db.merchantOrder
-
 
176
        order = collection.insert(order)
-
 
177
 
-
 
178
def main():
-
 
179
  
-
 
180
    store = getStore(2)
-
 
181
    #store.parseOrderRawHtml(12346, "subtagId", 122324,  "html", 'https://m.flipkart.com/orderresponse?reference_id=OD3016502908102575&token=0db4c692bacbfbfc158b52358ac9e91e&src=or&pr=1')
-
 
182
    #store.parseOrderRawHtml(12346, "subtagId", 122324,  "html", 'https://m.flipkart.com/orderresponse?reference_id=OD3018701137253850&token=f7402ddcf2b63b37cc6bc528cc115d2f&src=or&pr=1')
-
 
183
    store.parseOrderRawHtml(12346, "subtagId", 122324,  "html", 'https://m.flipkart.com/orderresponse?reference_id=OD0019279584515727&token=7d85d8c24d36b5a1efc8008634390c7e&src=or&pr=1')
-
 
184
    #store.flipkartOrderTracking(12346, "subtagId", 122324,  "html", 'https://m.flipkart.com/orderresponse?reference_id=OD0019279584515727&token=7d85d8c24d36b5a1efc8008634390c7e&src=or&pr=1')
-
 
185
def ungzipResponse(r,b):
-
 
186
        headers = r.info()
-
 
187
        if headers['Content-Encoding']=='gzip':
-
 
188
            import gzip
-
 
189
        print "********************"
-
 
190
        print "Deflating gzip response"
-
 
191
        print "********************"
-
 
192
        gz = gzip.GzipFile(fileobj=r, mode='rb')
-
 
193
        html = gz.read()
-
 
194
        gz.close()
-
 
195
        return html    
-
 
196
    
-
 
197
if __name__ == '__main__':
-
 
198
        main()
-
 
199
        
-
 
200
def todict(obj, classkey=None):
-
 
201
    if isinstance(obj, dict):
-
 
202
        data = {}
-
 
203
        for (k, v) in obj.items():
-
 
204
            data[k] = todict(v, classkey)
-
 
205
        return data
-
 
206
    elif hasattr(obj, "_ast"):
-
 
207
        return todict(obj._ast())
-
 
208
    elif hasattr(obj, "__iter__"):
-
 
209
        return [todict(v, classkey) for v in obj]
-
 
210
    elif hasattr(obj, "__dict__"):
-
 
211
        data = dict([(key, todict(value, classkey)) 
-
 
212
            for key, value in obj.__dict__.iteritems() 
-
 
213
            if not callable(value) and not key.startswith('_')])
-
 
214
        if classkey is not None and hasattr(obj, "__class__"):
-
 
215
            data[classkey] = obj.__class__.__name__
-
 
216
        return data
-
 
217
    else:
-
 
218
        return obj
-
 
219
20
220