Subversion Repositories SmartDukaan

Rev

Rev 13690 | Rev 13724 | 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
'''
6
from pymongo.mongo_client import MongoClient
7
import importlib
13662 amit.gupta 8
import json
9
import math
13569 amit.gupta 10
import mechanize
13631 amit.gupta 11
import traceback
13662 amit.gupta 12
import urllib
13
import urllib2
13582 amit.gupta 14
sourceMap = {1:"amazon", 2:"flipkart", 3:"snapdeal", 4:"spice", 5:"homeshop18"}
13662 amit.gupta 15
headers = { 
16
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
17
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
18
            'Accept-Language' : 'en-US,en;q=0.8',                     
19
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
20
        }
21
CASHBACK_URL = 'http://api.profittill.com/cashbacks/index/%s/%s'
13677 amit.gupta 22
USER_LOOKUP_URL = 'http://api.profittill.com/user_account/saholic/%s'
23
WALLET_CREDIT_URL = 'http://www.shop2020.in:8080/mobileapi/wallet?userId=%s&isLoggedIn=true'
13569 amit.gupta 24
 
25
def getStore(source_id):
26
    #module = sourceMap[source_id]
27
    store = Store(source_id)
13631 amit.gupta 28
    try:
29
        module = importlib.import_module("dtr.sources." + sourceMap[source_id])
30
        store = getattr(module, "Store")(source_id)
31
        return store
32
    except:
13678 amit.gupta 33
        #traceback.print_exc()
13631 amit.gupta 34
        return None
13569 amit.gupta 35
 
36
class ScrapeException(Exception):
37
    """Exception raised for errors in the input.
38
 
39
    Attributes:
40
        expr -- input expression in which the error occurred
41
        msg  -- explanation of the error
42
    """
43
 
44
    def __init__(self, expr, msg):
45
        self.expr = expr
46
        self.msg = msg
47
 
48
class ParseException(Exception):
49
    """Exception raised for errors in the input.
50
 
51
    Attributes:
52
        expr -- input expression in which the error occurred
53
        msg  -- explanation of the error
54
    """
55
 
56
    def __init__(self, expr, msg):
57
        self.expr = expr
58
        self.msg = msg
59
 
13677 amit.gupta 60
client = MongoClient('mongodb://localhost:27017/') 
61
 
13569 amit.gupta 62
class Store(object):
63
 
64
    ORDER_PLACED = 'Order Placed'
65
    ORDER_DELIVERED = 'Delivered'
66
    ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
67
    ORDER_CANCELLED = 'Cancelled'
68
 
13662 amit.gupta 69
    CB_INIT = 'Waiting Confirmation'
13610 amit.gupta 70
    CB_PENDING = 'Pending'
71
    CB_CREDITED = 'Credited to wallet'
72
    CB_NA = 'Not Applicable'
73
    CB_APPROVED = 'Approved'
74
    CB_CANCELLED = 'Cancelled'
13569 amit.gupta 75
 
13662 amit.gupta 76
    CONF_CB_SELLING_PRICE = 0
77
    CONF_CB_DISCOUNTED_PRICE = 1
13610 amit.gupta 78
 
13569 amit.gupta 79
    def __init__(self, store_id):
13662 amit.gupta 80
        self.db = client.Dtr
13569 amit.gupta 81
        self.store_id = store_id
13576 amit.gupta 82
        self.store_name = sourceMap[store_id]
13569 amit.gupta 83
 
13662 amit.gupta 84
    '''
85
    To Settle payback for respective stores.
86
    Also ensures that settlement happens only for approved orders
87
    '''
13569 amit.gupta 88
 
89
    def getName(self):
90
        raise NotImplementedError
91
 
92
    def scrapeAffiliate(self, startDate=None, endDate=None):
93
        raise NotImplementedError
94
 
95
    def saveToAffiliate(self, offers):
96
        raise NotImplementedError
97
 
98
    def scrapeStoreOrders(self,):
99
        raise NotImplementedError
13610 amit.gupta 100
 
13690 amit.gupta 101
    def _saveToOrder(self, order):
102
        collection = self.db.merchantOrder
103
        try:
104
            order = collection.insert(order)
105
            #merchantOder 
106
        except Exception as e:
107
            traceback.print_exc()
13662 amit.gupta 108
 
109
    def getCashbackAmount(self, productCode, amount):
110
        alagvar = CASHBACK_URL % (self.store_id,productCode)
111
        filehandle = urllib2.Request(alagvar,headers=headers)
112
        x= urllib2.urlopen(filehandle)
113
        map = json.loads(x.read())
114
        if map['cashback']==0:
13721 amit.gupta 115
            return (0,0)
13662 amit.gupta 116
        else:
117
            if map['cashback_type'] == 'percentage':
13721 amit.gupta 118
                return (math.floor((amount * map['cashback'])/100), map['cashback'])
13662 amit.gupta 119
            else:
13721 amit.gupta 120
                return (map['cashback'], 0)
121
 
13662 amit.gupta 122
 
13569 amit.gupta 123
    '''
124
    Parses the order for specific store
125
 
126
    order id, total amount, created on(now() if could not parse
127
    suborder id, title, quantity, unit price, expected delivery date,
128
    status (default would be Order placed)
129
 
130
    once products are identified, each suborder can then be updated
131
    with respective cashback.
132
 
133
    Possible fields to display for Not yet delivered orders are 
134
    Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
135
    No need to show cancelled orders.
13610 amit.gupta 136
    CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
13569 amit.gupta 137
    OrderStatus - Placed/Cancelled/Delivered
138
    '''
13576 amit.gupta 139
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
13569 amit.gupta 140
 
141
        pass
142
 
13721 amit.gupta 143
    def _updateOrdersPayBackStatus(self, searchMap, updateMap):
144
        searchMap['subOrders.cashbackStatus'] = Store.CB_INIT
145
        updateMap['subOrders.$.cashbackStatus'] = Store.CB_PENDING
146
        self.db.merchantOrder.update(searchMap, { '$set': updateMap })
147
 
148
    def _getActiveOrders(self):
149
        collection = self.db.merchantOrder
150
        collectionMap =  {"orderSuccessUrl":1, "orderId":1,"subOrders":1}
151
        stores = collection.find({"closed": False, "storeId" : self.store_id, "subOrders.closed":False}, collectionMap)
152
        return [store for store in stores]
153
 
154
    def _isSubOrderActive(self,order, merchantSubOrderId):
155
        subOrders = order.get("subOrders")
156
        for subOrder in subOrders:
157
            if merchantSubOrderId == subOrder.get("merchantSubOrderId"):
158
                return subOrder
159
        return None
160
 
161
 
162
 
13662 amit.gupta 163
def main():
13677 amit.gupta 164
    #store = getStore(3)
165
    #print store.getCashbackAmount('864683341', 100)
166
    data = urllib.urlencode({'orderId':6000, 'amount':200})
167
    request = urllib2.Request(WALLET_CREDIT_URL % (483649), headers=headers)
168
    response = urllib2.urlopen(request, data)
169
    print response.read()
13662 amit.gupta 170
 
13569 amit.gupta 171
if __name__ == '__main__':
13662 amit.gupta 172
    main()
13569 amit.gupta 173
 
174
 
13677 amit.gupta 175
def settlePayBack():
176
        orders = client.Dtr.merchantOrder.find({'subOrders.cashBackStatus':Store.CB_APPROVED}, {'userId':1, 'orderId': 1, 'subOrders.cashBackAmount':1, 'subOrders.merchantSubOrderId':1})
177
        for order in orders:
178
            print order
179
            for subOrder in order.get('subOrders'):
180
                if subOrder.get('cashBackAmount') is not None:
181
                    if __refundToWallet(order['userId'], order['orderId'], subOrder.get('cashBackAmount')):
182
                        client.Dtr.merchantOrder.update({'orderId':order['orderId'], 'subOrders.merchantSubOrderId':subOrder['merchantSubOrderId']},{'subOrders.$.cashBackStatus':Store.CB_CREDITED})
183
                        print "Settled order-", order['orderId'], "Sub Order-", subOrder['merchantSubOrderId'], "for amount", subOrder['cashBackAmount']
184
 
185
def __refundToWallet(self, userId, orderId, amount):
186
    userLookupRequest = urllib2.Request(USER_LOOKUP_URL %(userId), headers=headers)
187
    response = urllib2.urlopen(userLookupRequest).read()
188
    saholicUserId = json.loads(response)['account_id']
189
 
190
    request = urllib2.Request(WALLET_CREDIT_URL % (saholicUserId), headers=headers)
191
    data = urllib.urlencode({'orderId':orderId, 'amount':amount})
192
    response = urllib2.urlopen(request, data)
193
 
194
    return json.loads(response.read())['credited']
195
 
13569 amit.gupta 196
def getBrowserObject():
197
    import cookielib
198
    br = mechanize.Browser(factory=mechanize.RobustFactory())
199
    cj = cookielib.LWPCookieJar()
200
    br.set_cookiejar(cj)
201
    br.set_handle_equiv(True)
202
    br.set_handle_redirect(True)
203
    br.set_handle_referer(True)
204
    br.set_handle_robots(False)
205
    br.set_debug_http(False)
206
    br.set_debug_redirects(False)
207
    br.set_debug_responses(False)
208
 
209
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
210
 
211
    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'),
212
                     ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
213
                     ('Accept-Encoding', 'gzip,deflate,sdch'),                  
214
                     ('Accept-Language', 'en-US,en;q=0.8'),                     
215
                     ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
13677 amit.gupta 216
    return br
217
 
13690 amit.gupta 218
def ungzipResponse(r):
219
    headers = r.info()
220
    if headers['Content-Encoding']=='gzip':
221
        import gzip
222
        print "********************"
223
        print "Deflating gzip response"
224
        print "********************"
225
        gz = gzip.GzipFile(fileobj=r, mode='rb')
226
        html = gz.read()
227
        gz.close()
228
        return html