Subversion Repositories SmartDukaan

Rev

Rev 13760 | Rev 13781 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13569 amit.gupta 1
'''
2
Created on Jan 15, 2015
3
 
4
@author: amit
5
'''
13576 amit.gupta 6
from BeautifulSoup import BeautifulSoup
13569 amit.gupta 7
from bson.binary import Binary
13680 amit.gupta 8
from datetime import datetime, date, timedelta
13569 amit.gupta 9
from dtr import main
13576 amit.gupta 10
from dtr.dao import AffiliateInfo, Order, SubOrder
13680 amit.gupta 11
from dtr.main import getBrowserObject, ScrapeException, getStore, ParseException, Store as MStore,\
12
    ungzipResponse
13662 amit.gupta 13
from pprint import pprint
13569 amit.gupta 14
from pymongo import MongoClient
15
import json
16
import pymongo
17
import re
13662 amit.gupta 18
import traceback
13569 amit.gupta 19
import urllib
13603 amit.gupta 20
 
13721 amit.gupta 21
USERNAME='profittill2@gmail.com'
13569 amit.gupta 22
PASSWORD='spice@2020'
23
AFFILIATE_URL='http://affiliate.snapdeal.com'
24
POST_URL='https://api-p03.hasoffers.com/v3/Affiliate_Report.json'
25
ORDER_TRACK_URL='https://m.snapdeal.com/orderSummary'
26
CONFIG_URL='http://affiliate.snapdeal.com/publisher/js/config.php'
27
 
13662 amit.gupta 28
class Store(MStore):
13569 amit.gupta 29
 
30
    '''
31
    This is to map order statuses of our system to order statuses of snapdeal.
32
    And our statuses will change accordingly.
33
 
34
    '''
35
    OrderStatusMap = {
13662 amit.gupta 36
                      MStore.ORDER_PLACED : ['In Progress','N/A'],
37
                      MStore.ORDER_DELIVERED : ['Delivered'],
38
                      MStore.ORDER_SHIPPED : ['In Transit'],
39
                      MStore.ORDER_CANCELLED : ['Closed For Vendor Reallocation', 'Cancelled']
13569 amit.gupta 40
 
41
                      }
13662 amit.gupta 42
 
43
    CONF_CB_AMOUNT = MStore.CONF_CB_DISCOUNTED_PRICE
13569 amit.gupta 44
    def __init__(self,store_id):
45
        super(Store, self).__init__(store_id)
46
 
47
    def getName(self):
48
        return "snapdeal"
49
 
50
    def scrapeAffiliate(self, startDate=None, endDate=None):
51
        br = getBrowserObject()
52
        br.open(AFFILIATE_URL)
53
        br.select_form(nr=0)
54
        br.form['data[User][password]'] = PASSWORD 
55
        br.form['data[User][email]'] = USERNAME
56
        br.submit()
57
        response = br.open(CONFIG_URL)
58
 
13680 amit.gupta 59
        token =  re.findall('"session_token":"(.*?)"', ungzipResponse(response), re.IGNORECASE)[0]
13569 amit.gupta 60
 
61
        allOffers = self._getAllOffers(br, token)
62
 
13662 amit.gupta 63
        allPyOffers = []
64
        maxSaleDate = self._getLastSaleDate()
65
        newMaxSaleDate = maxSaleDate
66
        for offer in allOffers:
13680 amit.gupta 67
            pyOffer = self.covertToObj(offer).__dict__
68
            allPyOffers.append(pyOffer)
69
            saleDate = datetime.strptime(pyOffer['saleDate'],"%Y-%m-%d %H:%M:%S")
13662 amit.gupta 70
            if maxSaleDate < saleDate:
13721 amit.gupta 71
                self._updateOrdersPayBackStatus({'subTagId':pyOffer['subTagId'], 'saleDate':pyOffer['saleDate']}, {})
13662 amit.gupta 72
                if newMaxSaleDate < saleDate:
73
                    newMaxSaleDate = saleDate
74
 
75
        self._setLastSaleDate(newMaxSaleDate)
13569 amit.gupta 76
        self._saveToAffiliate(allPyOffers)
77
 
13662 amit.gupta 78
    def _setLastSaleDate(self, saleDate):
13680 amit.gupta 79
        self.db.lastSaleDtate.update({'storeId':self.store_id}, {'$set':{'saleDate':saleDate}})
13569 amit.gupta 80
 
13662 amit.gupta 81
 
82
 
83
    def _getLastSaleDate(self,):
84
        lastDaySaleObj = self.db.lastDaySale.find_one({"storeId":self.store_id})
85
        if lastDaySaleObj is None:
86
            return datetime.min
87
 
13760 amit.gupta 88
    def _parse(self, orderId, subTagId, userId, page, orderSuccessUrl):
13662 amit.gupta 89
 
13760 amit.gupta 90
        #page=page.decode("utf-8")
91
        soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
92
        #orderHead = soup.find(name, attrs, recursive, text)
93
        sections = soup.findAll("section")
94
 
95
        #print sections
96
 
97
        order = sections[1]
98
        orderTrs = order.findAll("tr")
99
 
100
        placedOn = str(orderTrs[0].findAll("td")[1].text)
101
 
102
        #Pop two section elements
103
        sections.pop(0) 
104
        sections.pop(0)
105
        subOrders = sections
106
 
107
 
108
        merchantSubOrders = []
109
 
110
        merchantOrder = Order(orderId, userId, subTagId, self.store_id, orderSuccessUrl)
111
        merchantOrder.merchantOderId = re.findall(r'\d+', str(soup.find("div", {"class":"deals_heading"})))[1]
112
        for orderTr in orderTrs:
113
            orderTrString = str(orderTr)
114
            if "Total Amount" in orderTrString:
115
                merchantOrder.totalAmount = re.findall(r'\d+', orderTrString)[0]
116
            elif "Delivery Charges" in orderTrString:
117
                merchantOrder.deliveryCharges = re.findall(r'\d+', orderTrString)[0]
118
            elif "Discount Applied" in orderTrString:
119
                merchantOrder.discountApplied = re.findall(r'\d+', orderTrString)[0]
120
            elif "Paid Amount" in orderTrString:
121
                merchantOrder.paidAmount = re.findall(r'\d+', orderTrString)[0]
122
 
123
        for subOrderElement in subOrders:
124
            subOrder = self.parseSubOrder(subOrderElement, placedOn)                           
125
            merchantSubOrders.append(subOrder)   
126
 
127
        merchantOrder.subOrders = merchantSubOrders
128
        return merchantOrder
129
 
130
    def parseSubOrder(self, subOrderElement, placedOn):
131
        productUrl = str(subOrderElement.find("a")['href'])
132
        subTable = subOrderElement.find("table", {"class":"lrPad"})
133
        subTrs = subTable.findAll("tr")
134
        unitPrice=None
135
        offerDiscount = None
136
        deliveryCharges = None
137
        amountPaid = None
138
        for subTr in subTrs:
139
            subTrString = str(subTr)
140
            if "Unit Price" in subTrString:
141
                unitPrice = re.findall(r'\d+', subTrString)[0]
142
            if "Quantity" in subTrString:
143
                qty = re.findall(r'\d+', subTrString)[0]
144
            elif "Offer Discount" in subTrString:
145
                offerDiscount =   re.findall(r'\d+', subTrString)[0]
146
            elif "Delivery Charges" in subTrString:
147
                deliveryCharges =   re.findall(r'\d+', subTrString)[0]
148
            elif "Subtotal" in subTrString:
149
                if int(qty) > 0:
150
                    amountPaid =   str(int(re.findall(r'\d+', subTrString)[0])/int(qty))
151
                else:
152
                    amountPaid =   "0"
153
        if self.CONF_CB_AMOUNT == MStore.CONF_CB_SELLING_PRICE or offerDiscount is None:
154
            amount = int(unitPrice)
155
        else:
156
            amount = int(unitPrice) - int(offerDiscount)
157
 
158
        divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
159
        if len(divs)<=0:
160
            raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
161
 
162
        for div in divs:
163
            productTitle = str(subOrderElement.find("a").text)
164
            productUrl = "http://m.snapdeal.com/" + productUrl 
165
            subOrder = SubOrder(productTitle, productUrl, placedOn, amountPaid)
166
 
167
            subOrder.amountPaid = amountPaid
168
            subOrder.deliveryCharges = deliveryCharges
169
            subOrder.offerDiscount = offerDiscount
170
            subOrder.unitPrice = unitPrice
171
            subOrder.productCode = re.findall(r'\d+$', productUrl)[0]
172
            (cashbackAmount, percentage) = self.getCashbackAmount(subOrder.productCode, amount)
13770 amit.gupta 173
            cashbackStatus = Store.CB_PENDING
13760 amit.gupta 174
            if cashbackAmount <= 0:
175
                cashbackStatus = Store.CB_NA
176
            subOrder.cashBackStatus = cashbackStatus
177
            subOrder.cashBackAmount = cashbackAmount
178
            if percentage > 0:
179
                subOrder.cashBackPercentage = percentage
180
 
181
 
182
            trackAnchor = div.find("a")   
183
            if trackAnchor is not None:
184
                subOrder.tracingkUrl = str(trackAnchor['href'])
185
 
186
            divStr = str(div)
187
            divStr = divStr.replace("\n","").replace("\t", "")
188
 
189
            for line in divStr.split("<br />"):
190
                if "Suborder ID" in line:
191
                    subOrder.merchantSubOrderId = re.findall(r'\d+', line)[0]   
192
                elif "Status" in line:
193
                    subOrder.detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
194
                elif "Est. Shipping Date" in line:
195
                    subOrder.estimatedShippingDate = line.split(":")[1].strip()
196
                elif "Est. Delivery Date" in line:
197
                    subOrder.estimatedDeliveryDate = line.split(":")[1].strip()
198
                elif "Courier Name" in line:
199
                    subOrder.courierName = line.split(":")[1].strip()
200
                elif "Tracking No" in line:
201
                    subOrder.trackingNumber = line.split(":")[1].strip()
202
            return subOrder
203
 
13576 amit.gupta 204
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
13760 amit.gupta 205
                    #print merchantOrder
13582 amit.gupta 206
        try:
207
            br = getBrowserObject()
208
            url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', orderSuccessUrl,re.IGNORECASE)[0]
209
            response = br.open(url)
13680 amit.gupta 210
            page = ungzipResponse(response)
13760 amit.gupta 211
            merchantOrder = self._parse(orderId, subTagId, userId, page, orderSuccessUrl)   
13582 amit.gupta 212
            self._saveToOrder(todict(merchantOrder))
213
            return True
214
        except:
215
            print "Error occurred"
13603 amit.gupta 216
            traceback.print_exc()
13569 amit.gupta 217
 
13582 amit.gupta 218
        return False
13569 amit.gupta 219
        #soup = BeautifulSoup(rawHtml,convertEntities=BeautifulSoup.HTML_ENTITIES)
220
        #soup.find(name, attrs, recursive, text)
13576 amit.gupta 221
 
222
    def _getStatusFromDetailedStatus(self, detailedStatus):
223
        for key, value in Store.OrderStatusMap.iteritems():
224
            if detailedStatus in value:
225
                return key
13662 amit.gupta 226
            print "Detailed Status need to be mapped"
13576 amit.gupta 227
        raise ParseException("_getStatusFromDetailedStatus", "Found new order status" + detailedStatus)
13569 amit.gupta 228
 
13610 amit.gupta 229
 
13569 amit.gupta 230
    def scrapeStoreOrders(self,):
13760 amit.gupta 231
        #collectionMap = {'palcedOn':1}
13576 amit.gupta 232
        orders = self._getActiveOrders()
13730 amit.gupta 233
        print "Found orders", orders
13576 amit.gupta 234
        br = getBrowserObject()
235
        for order in orders:
236
            url = ORDER_TRACK_URL + re.findall('.*(\?.*?)$', order['orderSuccessUrl'],re.IGNORECASE)[0]
237
            response = br.open(url)
13680 amit.gupta 238
            page = ungzipResponse(response)
13576 amit.gupta 239
            #page=page.decode("utf-8")
240
            soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
241
            sections = soup.findAll("section")
13760 amit.gupta 242
            orderEl = sections[1]
243
            orderTrs = orderEl.findAll("tr")
244
 
245
            placedOn = str(orderTrs[0].findAll("td")[1].text)
13576 amit.gupta 246
            sections.pop(0)
247
            sections.pop(0)
248
 
249
            subOrders = sections
250
            bulk = self.db.merchantOrder.initialize_ordered_bulk_op()
251
            for subOrderElement in subOrders:
252
                closed = True
253
                divs = subOrderElement.findAll("div", {"class": "blk lrPad subordrs"})
254
                if len(divs)<=0:
255
                    raise ParseException("subOrder", "Could not Parse suborders for Snapdeal")
13721 amit.gupta 256
                subOrder = None
13760 amit.gupta 257
                breakFlag = False
13576 amit.gupta 258
                for div in divs:
259
                    divStr = str(div)
260
                    divStr = divStr.replace("\n","").replace("\t", "")
261
                    updateMap = {}
262
                    for line in divStr.split("<br />"):
263
                        if "Suborder ID" in line:
13634 amit.gupta 264
                            merchantSubOrderId = re.findall(r'\d+', line)[0]
265
                            #break if suborder is inactive   
13721 amit.gupta 266
                            subOrder =  self._isSubOrderActive(order, merchantSubOrderId)
267
                            if subOrder is None:
13760 amit.gupta 268
                                subOrder = self.parseSubOrder(subOrderElement, placedOn)
269
                                self.db.merchantOrder.update({"orderId":order['orderId']},{'$push':{"subOrders":todict(subOrder)}})
270
                                print "Added new suborder with subOrder Id:", subOrder.merchantSubOrderId
271
                                closed = False
272
                                return
273
                            elif subOrder['closed']:
274
                                breakFlag = True
13634 amit.gupta 275
                                break
13760 amit.gupta 276
                            else: 
277
                                findMap = {"orderId": order['orderId'], "subOrders.merchantSubOrderId": merchantSubOrderId}
13576 amit.gupta 278
                        elif "Status" in line:
279
                            detailedStatus = re.findall('>(.*?)</span>', line, re.IGNORECASE)[0]
280
                            updateMap["subOrders.$.detailedStatus"] = detailedStatus
281
                            status = self._getStatusFromDetailedStatus(detailedStatus) 
13634 amit.gupta 282
                            closedStatus = status in [Store.ORDER_DELIVERED, Store.ORDER_CANCELLED]
13760 amit.gupta 283
                            updateMap["subOrders.$.status"] = status
284
                            if detailedStatus == 'Closed For Vendor Reallocation':
13770 amit.gupta 285
                                #if it is more than 6hours mark closed.
13760 amit.gupta 286
                                closeAt = subOrder.get("closeAt") 
287
                                if closeAt is None:
288
                                    closeAt = datetime.now() + timedelta(hours=6)
13770 amit.gupta 289
                                    updateMap["subOrders.$.closeAt"] = datetime.strftime(closeAt,"%Y-%m-%d %H:%M:%S")
13760 amit.gupta 290
                                else:
13770 amit.gupta 291
                                    closeAt = datetime.strptime(closeAt,"%Y-%m-%d %H:%M:%S")
13760 amit.gupta 292
                                    if datetime.now() > closeAt:
293
                                        closedStatus = True
294
 
295
 
13634 amit.gupta 296
                            if closedStatus:
297
                                #if status is closed then change the paybackStatus accordingly
13760 amit.gupta 298
                                updateMap["subOrders.$.closed"] = True
299
                                if status == Store.ORDER_DELIVERED:
13721 amit.gupta 300
                                    if subOrder.get("cashBackStatus") == Store.CB_PENDING:
13634 amit.gupta 301
                                        updateMap["subOrders.$.cashBackStatus"] = Store.CB_APPROVED
13760 amit.gupta 302
                                elif status == Store.ORDER_CANCELLED:
13721 amit.gupta 303
                                    if subOrder.get("cashBackStatus") == Store.CB_PENDING:
13634 amit.gupta 304
                                        updateMap["subOrders.$.cashBackStatus"] = Store.CB_CANCELLED
305
 
306
                            else:
307
                                closed = False
13576 amit.gupta 308
                        elif "Est. Shipping Date" in line:
309
                            estimatedShippingDate = line.split(":")[1].strip()
310
                            updateMap["subOrders.$.estimatedShippingDate"] = estimatedShippingDate
311
                        elif "Est. Delivery Date" in line:
312
                            estimatedDeliveryDate = line.split(":")[1].strip()
313
                            updateMap["subOrders.$.estimatedDeliveryDate"] = estimatedDeliveryDate
314
                        elif "Courier Name" in line:
315
                            courierName = line.split(":")[1].strip()
316
                            updateMap["subOrders.$.courierName"] = courierName
317
                        elif "Tracking No" in line:
318
                            trackingNumber = line.split(":")[1].strip()
319
                            updateMap["subOrders.$.trackingNumber"] = trackingNumber
13760 amit.gupta 320
 
321
                    if breakFlag:
322
                        break
13576 amit.gupta 323
 
324
                    bulk.find(findMap).update({'$set' : updateMap})
13721 amit.gupta 325
                bulk.find({'orderId': order['orderId']}).update({'$set':{'closed': closed}})
13576 amit.gupta 326
            result = bulk.execute()
327
            pprint(result)        
328
 
329
 
13569 amit.gupta 330
    def _saveToAffiliate(self, offers):
13725 amit.gupta 331
        if offers is None or len(offers)==0:
332
            print "no affiliate have been pushed"
333
            return
13576 amit.gupta 334
        collection = self.db.snapdealOrderAffiliateInfo
13569 amit.gupta 335
        try:
336
            collection.insert(offers,continue_on_error=True)
337
        except pymongo.errors.DuplicateKeyError as e:
338
            print e.details
339
 
340
 
341
    def _getAllOffers(self, br, token):
342
        allOffers = []
343
        nextPage = 1  
344
        while True:
345
            data = getPostData(token, nextPage)
346
            response = br.open(POST_URL, data)
13680 amit.gupta 347
            rmap = json.loads(ungzipResponse(response))
13569 amit.gupta 348
            if rmap is not None:
349
                rmap = rmap['response']
350
                if rmap is not None and len(rmap['errors'])==0:
351
                    allOffers += rmap['data']['data']
352
            nextPage += 1
353
            if rmap['data']['pageCount']<nextPage:
354
                break
355
 
356
        return allOffers
357
 
358
    def covertToObj(self,offer):
359
        offerData = offer['Stat']
360
        offer1 = AffiliateInfo(offerData['affiliate_info1'], self.store_id, offerData['conversion_status'], offerData['ad_id'], 
361
                              offerData['datetime'], offerData['payout'], offer['Offer']['name'], offerData['ip'], offerData['conversion_sale_amount'])
13680 amit.gupta 362
 
13569 amit.gupta 363
        return offer1
364
def getPostData(token, page = 1, limit= 20, startDate=None, endDate=None):
13680 amit.gupta 365
    endDate=date.today() + timedelta(days=1)
366
    startDate=endDate - timedelta(days=31)
13569 amit.gupta 367
 
368
    parameters = (
369
        ("page",str(page)),
370
        ("limit",str(limit)),
371
        ("fields[]","Stat.offer_id"),
372
        ("fields[]","Stat.datetime"),
373
        ("fields[]","Offer.name"),
374
        ("fields[]","Stat.conversion_status"),
375
        ("fields[]","Stat.conversion_sale_amount"),
376
        ("fields[]","Stat.payout"),
377
        ("fields[]","Stat.ip"),
378
        ("fields[]","Stat.ad_id"),
379
        ("fields[]","Stat.affiliate_info1"),
380
        ("sort[Stat.datetime]","desc"),
381
        ("filters[Stat.date][conditional]","BETWEEN"),
382
        ("filters[Stat.date][values][]",startDate.strftime('%Y-%m-%d')),
383
        ("filters[Stat.date][values][]",endDate.strftime('%Y-%m-%d')),
384
        ("data_start",startDate.strftime('%Y-%m-%d')),
385
        ("data_end",endDate.strftime('%Y-%m-%d')),
386
        ("Method","getConversions"),
387
        ("NetworkId","jasper"),
388
        ("SessionToken",token),
389
    )
390
    #Encode the parameters
391
    return urllib.urlencode(parameters)
392
 
393
def main():
13634 amit.gupta 394
 
13569 amit.gupta 395
    store = getStore(3)
13760 amit.gupta 396
    store.scrapeStoreOrders()
13662 amit.gupta 397
    #store._isSubOrderActive(8, "5970688907")
13760 amit.gupta 398
    #store.scrapeAffiliate()
13576 amit.gupta 399
    #store.parseOrderRawHtml(12345, "subtagId", 122323,  "html", 'https://m.snapdeal.com/purchaseMobileComplete?code=1f4166d13ea799b65aa9dea68b3e9e70&order=4509499363')
13569 amit.gupta 400
 
401
 
402
if __name__ == '__main__':
403
    main()
13576 amit.gupta 404
 
405
def todict(obj, classkey=None):
406
    if isinstance(obj, dict):
407
        data = {}
408
        for (k, v) in obj.items():
409
            data[k] = todict(v, classkey)
410
        return data
411
    elif hasattr(obj, "_ast"):
412
        return todict(obj._ast())
413
    elif hasattr(obj, "__iter__"):
414
        return [todict(v, classkey) for v in obj]
415
    elif hasattr(obj, "__dict__"):
416
        data = dict([(key, todict(value, classkey)) 
417
            for key, value in obj.__dict__.iteritems() 
418
            if not callable(value) and not key.startswith('_')])
419
        if classkey is not None and hasattr(obj, "__class__"):
420
            data[classkey] = obj.__class__.__name__
421
        return data
422
    else:
423
        return obj