| 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'
|
| 13569 |
amit.gupta |
22 |
|
|
|
23 |
def getStore(source_id):
|
|
|
24 |
#module = sourceMap[source_id]
|
|
|
25 |
store = Store(source_id)
|
| 13631 |
amit.gupta |
26 |
try:
|
|
|
27 |
module = importlib.import_module("dtr.sources." + sourceMap[source_id])
|
|
|
28 |
store = getattr(module, "Store")(source_id)
|
|
|
29 |
return store
|
|
|
30 |
except:
|
|
|
31 |
traceback.print_exc()
|
|
|
32 |
return None
|
| 13569 |
amit.gupta |
33 |
|
|
|
34 |
class ScrapeException(Exception):
|
|
|
35 |
"""Exception raised for errors in the input.
|
|
|
36 |
|
|
|
37 |
Attributes:
|
|
|
38 |
expr -- input expression in which the error occurred
|
|
|
39 |
msg -- explanation of the error
|
|
|
40 |
"""
|
|
|
41 |
|
|
|
42 |
def __init__(self, expr, msg):
|
|
|
43 |
self.expr = expr
|
|
|
44 |
self.msg = msg
|
|
|
45 |
|
|
|
46 |
class ParseException(Exception):
|
|
|
47 |
"""Exception raised for errors in the input.
|
|
|
48 |
|
|
|
49 |
Attributes:
|
|
|
50 |
expr -- input expression in which the error occurred
|
|
|
51 |
msg -- explanation of the error
|
|
|
52 |
"""
|
|
|
53 |
|
|
|
54 |
def __init__(self, expr, msg):
|
|
|
55 |
self.expr = expr
|
|
|
56 |
self.msg = msg
|
|
|
57 |
|
|
|
58 |
class Store(object):
|
|
|
59 |
|
|
|
60 |
ORDER_PLACED = 'Order Placed'
|
|
|
61 |
ORDER_DELIVERED = 'Delivered'
|
|
|
62 |
ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
|
|
|
63 |
ORDER_CANCELLED = 'Cancelled'
|
|
|
64 |
|
| 13662 |
amit.gupta |
65 |
CB_INIT = 'Waiting Confirmation'
|
| 13610 |
amit.gupta |
66 |
CB_PENDING = 'Pending'
|
|
|
67 |
CB_CREDITED = 'Credited to wallet'
|
|
|
68 |
CB_NA = 'Not Applicable'
|
|
|
69 |
CB_APPROVED = 'Approved'
|
|
|
70 |
CB_CANCELLED = 'Cancelled'
|
| 13569 |
amit.gupta |
71 |
|
| 13662 |
amit.gupta |
72 |
CONF_CB_SELLING_PRICE = 0
|
|
|
73 |
CONF_CB_DISCOUNTED_PRICE = 1
|
| 13610 |
amit.gupta |
74 |
|
| 13569 |
amit.gupta |
75 |
def __init__(self, store_id):
|
| 13662 |
amit.gupta |
76 |
client = MongoClient('mongodb://localhost:27017/')
|
|
|
77 |
self.db = client.Dtr
|
| 13569 |
amit.gupta |
78 |
self.store_id = store_id
|
| 13576 |
amit.gupta |
79 |
self.store_name = sourceMap[store_id]
|
| 13569 |
amit.gupta |
80 |
|
| 13662 |
amit.gupta |
81 |
'''
|
|
|
82 |
To Settle payback for respective stores.
|
|
|
83 |
Also ensures that settlement happens only for approved orders
|
|
|
84 |
'''
|
|
|
85 |
def settlePayBack(self):
|
|
|
86 |
orders = self.db.merchantOrder.find({'subOrders.cashBackStatus':Store.CB_APPROVED}, {'userId':1, 'subOrders.cashBackAmount':1})
|
|
|
87 |
#for
|
|
|
88 |
|
|
|
89 |
|
| 13569 |
amit.gupta |
90 |
|
|
|
91 |
def getName(self):
|
|
|
92 |
raise NotImplementedError
|
|
|
93 |
|
|
|
94 |
def scrapeAffiliate(self, startDate=None, endDate=None):
|
|
|
95 |
raise NotImplementedError
|
|
|
96 |
|
|
|
97 |
def saveToAffiliate(self, offers):
|
|
|
98 |
raise NotImplementedError
|
|
|
99 |
|
|
|
100 |
def scrapeStoreOrders(self,):
|
|
|
101 |
raise NotImplementedError
|
| 13610 |
amit.gupta |
102 |
|
| 13662 |
amit.gupta |
103 |
|
|
|
104 |
def getCashbackAmount(self, productCode, amount):
|
|
|
105 |
alagvar = CASHBACK_URL % (self.store_id,productCode)
|
|
|
106 |
print alagvar
|
|
|
107 |
filehandle = urllib2.Request(alagvar,headers=headers)
|
|
|
108 |
x= urllib2.urlopen(filehandle)
|
|
|
109 |
map = json.loads(x.read())
|
|
|
110 |
if map['cashback']==0:
|
|
|
111 |
return 0
|
|
|
112 |
else:
|
|
|
113 |
if map['cashback_type'] == 'percentage':
|
|
|
114 |
return math.floor((amount * map['cashback'])/100)
|
|
|
115 |
else:
|
|
|
116 |
return map['cashback']
|
|
|
117 |
|
| 13569 |
amit.gupta |
118 |
'''
|
|
|
119 |
Parses the order for specific store
|
|
|
120 |
|
|
|
121 |
order id, total amount, created on(now() if could not parse
|
|
|
122 |
suborder id, title, quantity, unit price, expected delivery date,
|
|
|
123 |
status (default would be Order placed)
|
|
|
124 |
|
|
|
125 |
once products are identified, each suborder can then be updated
|
|
|
126 |
with respective cashback.
|
|
|
127 |
|
|
|
128 |
Possible fields to display for Not yet delivered orders are
|
|
|
129 |
Product/Quantity/Amount/Store/CashbackAmount/OrderDate/ExpectedDelivery/OrderStaus/DetailedStatus/CashbackStatus
|
|
|
130 |
No need to show cancelled orders.
|
| 13610 |
amit.gupta |
131 |
CashbackStatus - NotApplicable/Pending/Approved/Cancelled/CreditedToWallet
|
| 13569 |
amit.gupta |
132 |
OrderStatus - Placed/Cancelled/Delivered
|
|
|
133 |
'''
|
| 13576 |
amit.gupta |
134 |
def parseOrderRawHtml(self, orderId, subTagId, userId, rawHtml, orderSuccessUrl):
|
| 13569 |
amit.gupta |
135 |
|
|
|
136 |
pass
|
|
|
137 |
|
| 13662 |
amit.gupta |
138 |
def main():
|
|
|
139 |
store = getStore(3)
|
|
|
140 |
print store.getCashbackAmount('864683341', 100)
|
|
|
141 |
|
| 13569 |
amit.gupta |
142 |
if __name__ == '__main__':
|
| 13662 |
amit.gupta |
143 |
main()
|
| 13569 |
amit.gupta |
144 |
|
|
|
145 |
|
|
|
146 |
def getBrowserObject():
|
|
|
147 |
import cookielib
|
|
|
148 |
br = mechanize.Browser(factory=mechanize.RobustFactory())
|
|
|
149 |
cj = cookielib.LWPCookieJar()
|
|
|
150 |
br.set_cookiejar(cj)
|
|
|
151 |
br.set_handle_equiv(True)
|
|
|
152 |
br.set_handle_redirect(True)
|
|
|
153 |
br.set_handle_referer(True)
|
|
|
154 |
br.set_handle_robots(False)
|
|
|
155 |
br.set_debug_http(False)
|
|
|
156 |
br.set_debug_redirects(False)
|
|
|
157 |
br.set_debug_responses(False)
|
|
|
158 |
|
|
|
159 |
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
|
|
|
160 |
|
|
|
161 |
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'),
|
|
|
162 |
('Accept', 'text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8'),
|
|
|
163 |
('Accept-Encoding', 'gzip,deflate,sdch'),
|
|
|
164 |
('Accept-Language', 'en-US,en;q=0.8'),
|
|
|
165 |
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')]
|
|
|
166 |
return br
|