Subversion Repositories SmartDukaan

Rev

Rev 13953 | Rev 13955 | 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
'''
13724 amit.gupta 6
from datetime import datetime
13869 amit.gupta 7
import time
13868 amit.gupta 8
from pprint import pprint
13569 amit.gupta 9
from pymongo.mongo_client import MongoClient
10
import importlib
13662 amit.gupta 11
import json
12
import math
13569 amit.gupta 13
import mechanize
13631 amit.gupta 14
import traceback
13662 amit.gupta 15
import urllib
16
import urllib2
13582 amit.gupta 17
sourceMap = {1:"amazon", 2:"flipkart", 3:"snapdeal", 4:"spice", 5:"homeshop18"}
13662 amit.gupta 18
headers = { 
19
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
20
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
21
            'Accept-Language' : 'en-US,en;q=0.8',                     
22
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
23
        }
24
CASHBACK_URL = 'http://api.profittill.com/cashbacks/index/%s/%s'
13939 amit.gupta 25
USER_LOOKUP_URL = 'http://api.profittill.com/user_accounts/saholic/%s'
13949 amit.gupta 26
WALLET_CREDIT_URL = 'http://shop2020.in:8080/mobileapi/wallet!batchUpdate'
13569 amit.gupta 27
 
28
def getStore(source_id):
29
    #module = sourceMap[source_id]
30
    store = Store(source_id)
13631 amit.gupta 31
    try:
32
        module = importlib.import_module("dtr.sources." + sourceMap[source_id])
33
        store = getattr(module, "Store")(source_id)
34
        return store
35
    except:
13781 amit.gupta 36
        traceback.print_exc()
13631 amit.gupta 37
        return None
13569 amit.gupta 38
 
39
class ScrapeException(Exception):
40
    """Exception raised for errors in the input.
41
 
42
    Attributes:
43
        expr -- input expression in which the error occurred
44
        msg  -- explanation of the error
45
    """
46
 
47
    def __init__(self, expr, msg):
48
        self.expr = expr
49
        self.msg = msg
50
 
51
class ParseException(Exception):
52
    """Exception raised for errors in the input.
53
 
54
    Attributes:
55
        expr -- input expression in which the error occurred
56
        msg  -- explanation of the error
57
    """
58
 
59
    def __init__(self, expr, msg):
60
        self.expr = expr
61
        self.msg = msg
62
 
13677 amit.gupta 63
client = MongoClient('mongodb://localhost:27017/') 
64
 
13569 amit.gupta 65
class Store(object):
66
 
67
    ORDER_PLACED = 'Order Placed'
68
    ORDER_DELIVERED = 'Delivered'
69
    ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
70
    ORDER_CANCELLED = 'Cancelled'
71
 
13662 amit.gupta 72
    CB_INIT = 'Waiting Confirmation'
13610 amit.gupta 73
    CB_PENDING = 'Pending'
13954 amit.gupta 74
    CB_CREDIT_IN_PROCESS = 'Credited in process'
13610 amit.gupta 75
    CB_CREDITED = 'Credited to wallet'
76
    CB_NA = 'Not Applicable'
77
    CB_APPROVED = 'Approved'
78
    CB_CANCELLED = 'Cancelled'
13569 amit.gupta 79
 
13662 amit.gupta 80
    CONF_CB_SELLING_PRICE = 0
81
    CONF_CB_DISCOUNTED_PRICE = 1
13610 amit.gupta 82
 
13569 amit.gupta 83
    def __init__(self, store_id):
13662 amit.gupta 84
        self.db = client.Dtr
13569 amit.gupta 85
        self.store_id = store_id
13576 amit.gupta 86
        self.store_name = sourceMap[store_id]
13569 amit.gupta 87
 
13662 amit.gupta 88
    '''
89
    To Settle payback for respective stores.
90
    Also ensures that settlement happens only for approved orders
91
    '''
13569 amit.gupta 92
 
93
    def getName(self):
94
        raise NotImplementedError
95
 
96
    def scrapeAffiliate(self, startDate=None, endDate=None):
97
        raise NotImplementedError
98
 
99
    def saveToAffiliate(self, offers):
100
        raise NotImplementedError
101
 
102
    def scrapeStoreOrders(self,):
103
        raise NotImplementedError
13610 amit.gupta 104
 
13690 amit.gupta 105
    def _saveToOrder(self, order):
106
        collection = self.db.merchantOrder
107
        try:
108
            order = collection.insert(order)
109
            #merchantOder 
110
        except Exception as e:
111
            traceback.print_exc()
13662 amit.gupta 112
 
113
    def getCashbackAmount(self, productCode, amount):
114
        alagvar = CASHBACK_URL % (self.store_id,productCode)
115
        filehandle = urllib2.Request(alagvar,headers=headers)
116
        x= urllib2.urlopen(filehandle)
117
        map = json.loads(x.read())
118
        if map['cashback']==0:
13721 amit.gupta 119
            return (0,0)
13662 amit.gupta 120
        else:
121
            if map['cashback_type'] == 'percentage':
13721 amit.gupta 122
                return (math.floor((amount * map['cashback'])/100), map['cashback'])
13662 amit.gupta 123
            else:
13721 amit.gupta 124
                return (map['cashback'], 0)
125
 
13662 amit.gupta 126
 
13569 amit.gupta 127
    '''
128
    Parses the order for specific store
129
 
130
    order id, total amount, created on(now() if could not parse
131
    suborder id, title, quantity, unit price, expected delivery date,
132
    status (default would be Order placed)
133
 
134
    once products are identified, each suborder can then be updated
135
    with respective cashback.
136
 
137
    Possible fields to display for Not yet delivered orders are 
138
    Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
139
    No need to show cancelled orders.
13610 amit.gupta 140
    CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
13569 amit.gupta 141
    OrderStatus - Placed/Cancelled/Delivered
142
    '''
13576 amit.gupta 143
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
13569 amit.gupta 144
 
145
        pass
146
 
13721 amit.gupta 147
    def _updateOrdersPayBackStatus(self, searchMap, updateMap):
13781 amit.gupta 148
        searchMap['subOrders.missingAff'] = False
149
        updateMap['subOrders.$.missingAff'] = True
13927 amit.gupta 150
        self.db.merchantOrder.update(searchMap, { '$set': updateMap }, multi=True)
13721 amit.gupta 151
 
13781 amit.gupta 152
    def _getActiveOrders(self, searchMap={}, collectionMap={}):
13721 amit.gupta 153
        collection = self.db.merchantOrder
13781 amit.gupta 154
        searchMap = dict(searchMap.items()+ {"closed": False, "storeId" : self.store_id}.items()) 
155
        collectionMap =  dict(collectionMap.items() + {"orderSuccessUrl":1, "orderId":1,"subOrders":1, "placedOn":1}.items())
156
        stores = collection.find(searchMap, collectionMap)
13721 amit.gupta 157
        return [store for store in stores]
158
 
159
    def _isSubOrderActive(self,order, merchantSubOrderId):
160
        subOrders = order.get("subOrders")
161
        for subOrder in subOrders:
162
            if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
163
                return subOrder
164
        return None
165
 
13868 amit.gupta 166
def settlePayBack():
13930 amit.gupta 167
        client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_APPROVED},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}}, multi=True)    
13868 amit.gupta 168
        result = client.Dtr.merchantOrder\
169
            .aggregate([
13927 amit.gupta 170
                        {'$match':{'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS}},
13868 amit.gupta 171
                        {'$unwind':"$subOrders"},
172
                        { 
173
                         '$group':{
174
                                   '_id':'$userId',
175
                                   'amount': { '$sum':'$subOrders.cashBackAmount'},
176
                                   }
177
                         }
178
                    ])['result']
179
 
180
        userAmountMap = {}
13953 amit.gupta 181
        print result
13868 amit.gupta 182
        for res in result:
13927 amit.gupta 183
            userAmountMap[res['_id']] = res['amount']
184
        datetimeNow = datetime.now() 
185
        batchId = int(time.mktime(datetimeNow.timetuple()))
186
        if refundToWallet(batchId, userAmountMap):
13930 amit.gupta 187
            client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS},{'$set':{'subOrders.$.cashBackStatus':Store.CB_CREDITED, 'subOrders.$.batchId':batchId}}, multi=True)
13951 amit.gupta 188
            for key, value in userAmountMap.iteritems():
13927 amit.gupta 189
                client.Dtr.refund.insert({"userId": key, "batch":batchId, "userAmount":value, "timestamp":datetime.strftime(datetimeNow,"%Y-%m-%d %H:%M:%S")})
190
                client.Dtr.user.update({"userId":key}, {'$inc': { "credited": value}}, upsert=True)
13952 amit.gupta 191
            tprint("PayBack Settled")
13868 amit.gupta 192
        else:
13939 amit.gupta 193
            tprint("Error Occurred while running batch. Rolling Back")
13930 amit.gupta 194
            client.Dtr.merchantOrder.update({'subOrders.cashBackStatus':Store.CB_CREDIT_IN_PROCESS},{'$set':{'subOrders.$.cashBackStatus':Store.CB_APPROVED}}, multi=True)    
13721 amit.gupta 195
 
13927 amit.gupta 196
def refundToWallet(batchId, userAmountMap):
197
    batchUpdateMap = {}
198
    try :
199
        saholicUserAmountMap = {}
200
        for key, value in userAmountMap.iteritems():
201
            userLookupRequest = urllib2.Request(USER_LOOKUP_URL %(key), headers=headers)
202
            try:
203
                response = urllib2.urlopen(userLookupRequest).read()
13939 amit.gupta 204
                saholicUserId = json.loads(response)['account_key']
13927 amit.gupta 205
                saholicUserAmountMap[saholicUserId] = value
206
            except:
13934 amit.gupta 207
                tprint("Could not fetch saholic id for user : " + str(key))
13927 amit.gupta 208
                continue
13939 amit.gupta 209
        if len(saholicUserAmountMap) > 0:
210
            batchUpdateMap['userAmount'] = json.dumps(saholicUserAmountMap)
211
            batchUpdateMap['batchId'] = batchId
212
            request = urllib2.Request(WALLET_CREDIT_URL, headers=headers)
213
            data = urllib.urlencode(batchUpdateMap)
214
            response = urllib2.urlopen(request, data)
215
            return json.loads(response.read())['response']['credited']
216
        else:
217
            tprint("Nothing to Refund")
218
            return False
13927 amit.gupta 219
    except:
220
        traceback.print_exc()
221
        tprint("Could not batch refund")
222
        return False
13868 amit.gupta 223
 
13662 amit.gupta 224
def main():
13677 amit.gupta 225
    #store = getStore(3)
226
    #print store.getCashbackAmount('864683341', 100)
13868 amit.gupta 227
    #data = urllib.urlencode({'orderId':6000, 'amount':200})
228
    #request = urllib2.Request(WALLET_CREDIT_URL % (483649), headers=headers)
229
    #response = urllib2.urlopen(request, data)
230
    #print response.read()
13939 amit.gupta 231
    settlePayBack()
232
 
13662 amit.gupta 233
 
13569 amit.gupta 234
 
235
 
13868 amit.gupta 236
###
237
#Settlement process is suposed to be a batch and run weekly
238
#It is should be running on first hour of mondays. As cron should
239
# Maintain a batch id.
240
 
13677 amit.gupta 241
 
13569 amit.gupta 242
def getBrowserObject():
243
    import cookielib
244
    br = mechanize.Browser(factory=mechanize.RobustFactory())
245
    cj = cookielib.LWPCookieJar()
246
    br.set_cookiejar(cj)
247
    br.set_handle_equiv(True)
248
    br.set_handle_redirect(True)
249
    br.set_handle_referer(True)
250
    br.set_handle_robots(False)
251
    br.set_debug_http(False)
252
    br.set_debug_redirects(False)
253
    br.set_debug_responses(False)
254
 
255
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
256
 
257
    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'),
258
                     ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
259
                     ('Accept-Encoding', 'gzip,deflate,sdch'),                  
260
                     ('Accept-Language', 'en-US,en;q=0.8'),                     
261
                     ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
13677 amit.gupta 262
    return br
263
 
13690 amit.gupta 264
def ungzipResponse(r):
265
    headers = r.info()
266
    if headers['Content-Encoding']=='gzip':
267
        import gzip
268
        gz = gzip.GzipFile(fileobj=r, mode='rb')
269
        html = gz.read()
270
        gz.close()
271
        return html
13724 amit.gupta 272
 
273
def tprint(*msg):
13927 amit.gupta 274
    print datetime.now(), "-", msg
13939 amit.gupta 275
 
276
if __name__ == '__main__':
277
    main()