| 14843 |
amit.gupta |
1 |
import StringIO
|
|
|
2 |
import base64
|
|
|
3 |
import gzip
|
|
|
4 |
import pymongo
|
| 13572 |
kshitij.so |
5 |
import time
|
| 14843 |
amit.gupta |
6 |
import urllib
|
| 14736 |
kshitij.so |
7 |
import urllib2
|
| 15166 |
amit.gupta |
8 |
import logging
|
| 14948 |
amit.gupta |
9 |
from datetime import datetime
|
| 15152 |
kshitij.so |
10 |
import random
|
| 14705 |
kshitij.so |
11 |
#TODO Need to add messy stuff to conf.
|
| 14708 |
kshitij.so |
12 |
con=None
|
| 14736 |
kshitij.so |
13 |
headers = {
|
|
|
14 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
|
|
15 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
16 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
17 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
|
|
18 |
'Connection':'keep-alive',
|
|
|
19 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
|
|
20 |
}
|
| 15768 |
kshitij.so |
21 |
PROXY_MESH_LIVE = "us-ca.proxymesh.com:31280"
|
| 15338 |
kshitij.so |
22 |
PROXY_MESH_GENERAL = "us-il.proxymesh.com:31280"
|
| 15152 |
kshitij.so |
23 |
|
| 14843 |
amit.gupta |
24 |
PUSH_NOTIFICATION_URL='http://api.profittill.com/admin/users/push'
|
|
|
25 |
DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 14747 |
kshitij.so |
26 |
|
| 14705 |
kshitij.so |
27 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
28 |
global con
|
|
|
29 |
if con is None:
|
|
|
30 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
31 |
try:
|
|
|
32 |
con = pymongo.MongoClient(host, port)
|
|
|
33 |
except Exception, e:
|
|
|
34 |
print e
|
|
|
35 |
return None
|
|
|
36 |
return con
|
|
|
37 |
|
| 13572 |
kshitij.so |
38 |
def to_java_date(py_timestamp):
|
|
|
39 |
try:
|
|
|
40 |
java_date = int(time.mktime(py_timestamp.timetuple())) * 1000 + py_timestamp.microsecond / 1000
|
|
|
41 |
return java_date
|
|
|
42 |
except:
|
| 14705 |
kshitij.so |
43 |
return None
|
|
|
44 |
|
|
|
45 |
def getCashBack(skuId, source_id, category_id, mc, mongoHost):
|
|
|
46 |
if not bool(mc.get("category_cash_back")):
|
|
|
47 |
populateCashBack(mc, mongoHost)
|
|
|
48 |
itemCashBackMap = mc.get("item_cash_back")
|
|
|
49 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
50 |
if itemCashBack is not None:
|
|
|
51 |
return itemCashBack
|
|
|
52 |
cashBackMap = mc.get("category_cash_back")
|
|
|
53 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
54 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
55 |
for cashBack in sourceCashBack:
|
|
|
56 |
if cashBack.get(category_id) is None:
|
|
|
57 |
continue
|
|
|
58 |
else:
|
|
|
59 |
return cashBack.get(category_id)
|
|
|
60 |
else:
|
|
|
61 |
return {}
|
|
|
62 |
|
|
|
63 |
def populateCashBack(mc, mongoHost):
|
|
|
64 |
print "Populating cashback"
|
|
|
65 |
cashBackMap = {}
|
|
|
66 |
itemCashBackMap = {}
|
|
|
67 |
cashBack = list(get_mongo_connection(host=mongoHost).Catalog.CategoryCashBack.find())
|
|
|
68 |
for row in cashBack:
|
|
|
69 |
temp_map = {}
|
|
|
70 |
temp_list = []
|
|
|
71 |
if cashBackMap.has_key(row['source_id']):
|
|
|
72 |
arr = cashBackMap.get(row['source_id'])
|
|
|
73 |
for val in arr:
|
|
|
74 |
temp_list.append(val)
|
|
|
75 |
temp_map[row['category_id']] = row
|
|
|
76 |
temp_list.append(temp_map)
|
|
|
77 |
cashBackMap[row['source_id']] = temp_list
|
|
|
78 |
else:
|
|
|
79 |
temp_map[row['category_id']] = row
|
|
|
80 |
temp_list.append(temp_map)
|
|
|
81 |
cashBackMap[row['source_id']] = temp_list
|
|
|
82 |
itemCashBack = list(get_mongo_connection(host=mongoHost).Catalog.ItemCashBack.find())
|
|
|
83 |
for row in itemCashBack:
|
|
|
84 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
85 |
itemCashBackMap[row['skuId']] = row
|
|
|
86 |
mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
|
|
|
87 |
mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)
|
|
|
88 |
|
| 14736 |
kshitij.so |
89 |
def ungzipResponse(r):
|
|
|
90 |
headers = r.info()
|
|
|
91 |
if headers.get('Content-Encoding')=='gzip':
|
|
|
92 |
url_f = StringIO.StringIO(r.read())
|
|
|
93 |
gz = gzip.GzipFile(fileobj=url_f)
|
|
|
94 |
html = gz.read()
|
|
|
95 |
gz.close()
|
|
|
96 |
return html
|
|
|
97 |
return r.read()
|
|
|
98 |
|
|
|
99 |
|
| 15209 |
kshitij.so |
100 |
def fetchResponseUsingProxy(url, headers=headers, livePricing=None):
|
|
|
101 |
if livePricing is None:
|
| 15338 |
kshitij.so |
102 |
PROXY_URL = PROXY_MESH_GENERAL
|
| 15209 |
kshitij.so |
103 |
else:
|
| 15338 |
kshitij.so |
104 |
PROXY_URL = PROXY_MESH_LIVE
|
| 15152 |
kshitij.so |
105 |
print PROXY_URL
|
| 14993 |
amit.gupta |
106 |
proxy = urllib2.ProxyHandler({'http': PROXY_URL})
|
|
|
107 |
opener = urllib2.build_opener(proxy)
|
|
|
108 |
urllib2.install_opener(opener)
|
| 14736 |
kshitij.so |
109 |
req = urllib2.Request(url,headers=headers)
|
|
|
110 |
response = urllib2.urlopen(req)
|
|
|
111 |
response_data = ungzipResponse(response)
|
|
|
112 |
response.close()
|
|
|
113 |
return response_data
|
|
|
114 |
|
| 15225 |
amit.gupta |
115 |
def sendNotification(userIds, campaignName, title, message,notificationtype, url, expiresat='2999-01-01'):
|
| 14843 |
amit.gupta |
116 |
usertuples = ()
|
|
|
117 |
count = -1
|
|
|
118 |
for userId in userIds:
|
|
|
119 |
count += 1
|
|
|
120 |
usertuples += (("userIds[" + str(count) + "]", userId),)
|
|
|
121 |
parameters = usertuples + (
|
|
|
122 |
("User[name]", campaignName),
|
|
|
123 |
("User[title]", title ),
|
|
|
124 |
("User[message]", message),
|
|
|
125 |
("User[type]", notificationtype),
|
| 15225 |
amit.gupta |
126 |
("User[url]", url),
|
|
|
127 |
("User[expiresat]", expiresat),)
|
| 14843 |
amit.gupta |
128 |
parameters = urllib.urlencode(parameters)
|
| 14987 |
amit.gupta |
129 |
#print parameters
|
| 14843 |
amit.gupta |
130 |
pushpostrequest = urllib2.Request(PUSH_NOTIFICATION_URL, parameters, headers=headers)
|
|
|
131 |
pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)
|
| 14845 |
amit.gupta |
132 |
urllib2.urlopen(pushpostrequest).read()
|
| 14948 |
amit.gupta |
133 |
|
|
|
134 |
def getCurrTimeStamp():
|
|
|
135 |
return toTimeStamp(datetime.now())
|
|
|
136 |
|
|
|
137 |
def toTimeStamp(dateTimeObj):
|
|
|
138 |
return int(time.mktime(dateTimeObj.timetuple()))
|
| 14987 |
amit.gupta |
139 |
def fromTimeStamp(timestamp):
|
| 15166 |
amit.gupta |
140 |
return datetime.fromtimestamp(timestamp)
|
|
|
141 |
|
| 15182 |
kshitij.so |
142 |
def getNlcPoints(item, minNlc, maxNlc, available_price):
|
|
|
143 |
if not(minNlc and maxNlc):
|
| 15249 |
kshitij.so |
144 |
print "Raising exception minNlc, maxNlc not found for %d"%(item.get('_id'))
|
| 15182 |
kshitij.so |
145 |
raise
|
|
|
146 |
if item.get('status') == 2:
|
|
|
147 |
eolWeight = .60
|
|
|
148 |
else:
|
|
|
149 |
eolWeight = 1.0
|
|
|
150 |
if item.get('category_id') == 3:
|
|
|
151 |
basePointPercentage = 5.0
|
|
|
152 |
maxNlcPoints = 200
|
|
|
153 |
elif item.get('category_id') == 5:
|
|
|
154 |
basePointPercentage = 8.0
|
|
|
155 |
maxNlcPoints = 150
|
|
|
156 |
else:
|
|
|
157 |
basePointPercentage = 10.0
|
|
|
158 |
maxNlcPoints = 150
|
|
|
159 |
discFromMinNlc = float((minNlc - (available_price)))/(available_price) *100
|
|
|
160 |
discFromMaxNlc = float((maxNlc - (available_price)))/(available_price) *100
|
|
|
161 |
print discFromMinNlc
|
|
|
162 |
print discFromMaxNlc
|
|
|
163 |
if discFromMinNlc > 0:
|
|
|
164 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
165 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
|
|
166 |
nlcPoints = 0
|
|
|
167 |
else:
|
|
|
168 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
169 |
if (min(nlcPoints,maxNlcPoints)) > 0:
|
|
|
170 |
nlcPoints = (min(nlcPoints,maxNlcPoints)) * eolWeight
|
|
|
171 |
else:
|
|
|
172 |
nlcPoints = (min(nlcPoints,maxNlcPoints))
|
|
|
173 |
|
|
|
174 |
return nlcPoints
|
|
|
175 |
|
| 15166 |
amit.gupta |
176 |
def getLogger(filePath):
|
|
|
177 |
lgr = logging.getLogger()
|
|
|
178 |
lgr.setLevel(logging.DEBUG)
|
|
|
179 |
fh = logging.FileHandler(filePath)
|
|
|
180 |
fh.setLevel(logging.INFO)
|
|
|
181 |
frmt = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
|
|
|
182 |
fh.setFormatter(frmt)
|
|
|
183 |
lgr.addHandler(fh)
|
|
|
184 |
return lgr
|