Subversion Repositories SmartDukaan

Rev

Rev 13678 | Rev 13721 | 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:
115
            return 0
116
        else:
117
            if map['cashback_type'] == 'percentage':
118
                return math.floor((amount * map['cashback'])/100)
119
            else:
120
                return map['cashback']
121
 
13569 amit.gupta 122
    '''
123
    Parses the order for specific store
124
 
125
    order id, total amount, created on(now() if could not parse
126
    suborder id, title, quantity, unit price, expected delivery date,
127
    status (default would be Order placed)
128
 
129
    once products are identified, each suborder can then be updated
130
    with respective cashback.
131
 
132
    Possible fields to display for Not yet delivered orders are 
133
    Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
134
    No need to show cancelled orders.
13610 amit.gupta 135
    CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
13569 amit.gupta 136
    OrderStatus - Placed/Cancelled/Delivered
137
    '''
13576 amit.gupta 138
    def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
13569 amit.gupta 139
 
140
        pass
141
 
13662 amit.gupta 142
def main():
13677 amit.gupta 143
    #store = getStore(3)
144
    #print store.getCashbackAmount('864683341', 100)
145
    data = urllib.urlencode({'orderId':6000, 'amount':200})
146
    request = urllib2.Request(WALLET_CREDIT_URL % (483649), headers=headers)
147
    response = urllib2.urlopen(request, data)
148
    print response.read()
13662 amit.gupta 149
 
13569 amit.gupta 150
if __name__ == '__main__':
13662 amit.gupta 151
    main()
13569 amit.gupta 152
 
153
 
13677 amit.gupta 154
def settlePayBack():
155
        orders = client.Dtr.merchantOrder.find({'subOrders.cashBackStatus':Store.CB_APPROVED}, {'userId':1, 'orderId': 1, 'subOrders.cashBackAmount':1, 'subOrders.merchantSubOrderId':1})
156
        for order in orders:
157
            print order
158
            for subOrder in order.get('subOrders'):
159
                if subOrder.get('cashBackAmount') is not None:
160
                    if __refundToWallet(order['userId'], order['orderId'], subOrder.get('cashBackAmount')):
161
                        client.Dtr.merchantOrder.update({'orderId':order['orderId'], 'subOrders.merchantSubOrderId':subOrder['merchantSubOrderId']},{'subOrders.$.cashBackStatus':Store.CB_CREDITED})
162
                        print "Settled order-", order['orderId'], "Sub Order-", subOrder['merchantSubOrderId'], "for amount", subOrder['cashBackAmount']
163
 
164
def __refundToWallet(self, userId, orderId, amount):
165
    userLookupRequest = urllib2.Request(USER_LOOKUP_URL %(userId), headers=headers)
166
    response = urllib2.urlopen(userLookupRequest).read()
167
    saholicUserId = json.loads(response)['account_id']
168
 
169
    request = urllib2.Request(WALLET_CREDIT_URL % (saholicUserId), headers=headers)
170
    data = urllib.urlencode({'orderId':orderId, 'amount':amount})
171
    response = urllib2.urlopen(request, data)
172
 
173
    return json.loads(response.read())['credited']
174
 
13569 amit.gupta 175
def getBrowserObject():
176
    import cookielib
177
    br = mechanize.Browser(factory=mechanize.RobustFactory())
178
    cj = cookielib.LWPCookieJar()
179
    br.set_cookiejar(cj)
180
    br.set_handle_equiv(True)
181
    br.set_handle_redirect(True)
182
    br.set_handle_referer(True)
183
    br.set_handle_robots(False)
184
    br.set_debug_http(False)
185
    br.set_debug_redirects(False)
186
    br.set_debug_responses(False)
187
 
188
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
189
 
190
    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'),
191
                     ('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
192
                     ('Accept-Encoding', 'gzip,deflate,sdch'),                  
193
                     ('Accept-Language', 'en-US,en;q=0.8'),                     
194
                     ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
13677 amit.gupta 195
    return br
196
 
13690 amit.gupta 197
def ungzipResponse(r):
198
    headers = r.info()
199
    if headers['Content-Encoding']=='gzip':
200
        import gzip
201
        print "********************"
202
        print "Deflating gzip response"
203
        print "********************"
204
        gz = gzip.GzipFile(fileobj=r, mode='rb')
205
        html = gz.read()
206
        gz.close()
207
        return html