| 16561 |
amit.gupta |
1 |
from datetime import datetime
|
|
|
2 |
from paramiko.client import SSHClient
|
| 14843 |
amit.gupta |
3 |
import StringIO
|
|
|
4 |
import base64
|
|
|
5 |
import gzip
|
| 16561 |
amit.gupta |
6 |
import logging
|
|
|
7 |
import os
|
|
|
8 |
import paramiko
|
| 14843 |
amit.gupta |
9 |
import pymongo
|
| 16561 |
amit.gupta |
10 |
import random
|
|
|
11 |
import socket
|
| 13572 |
kshitij.so |
12 |
import time
|
| 14843 |
amit.gupta |
13 |
import urllib
|
| 14736 |
kshitij.so |
14 |
import urllib2
|
| 15906 |
kshitij.so |
15 |
import urlparse
|
| 14705 |
kshitij.so |
16 |
#TODO Need to add messy stuff to conf.
|
| 14708 |
kshitij.so |
17 |
con=None
|
| 14736 |
kshitij.so |
18 |
headers = {
|
|
|
19 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
|
|
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 |
'Connection':'keep-alive',
|
|
|
24 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
|
|
25 |
}
|
| 16522 |
kshitij.so |
26 |
PROXY_MESH_LIVE = "us.proxymesh.com:31280"
|
| 15338 |
kshitij.so |
27 |
PROXY_MESH_GENERAL = "us-il.proxymesh.com:31280"
|
| 15152 |
kshitij.so |
28 |
|
| 14843 |
amit.gupta |
29 |
PUSH_NOTIFICATION_URL='http://api.profittill.com/admin/users/push'
|
|
|
30 |
DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 16371 |
amit.gupta |
31 |
statusMap = {1:'Active',2:'EOL',3:'In Process',4:'Exclusive'}
|
| 14747 |
kshitij.so |
32 |
|
| 16398 |
amit.gupta |
33 |
CB_INIT = 'Waiting Confirmation'
|
|
|
34 |
CB_PENDING = 'Pending'
|
|
|
35 |
CB_CREDIT_IN_PROCESS = 'Credit in process'
|
|
|
36 |
CB_CREDITED = 'Credited to wallet'
|
|
|
37 |
CB_NA = 'Not Applicable'
|
|
|
38 |
CB_APPROVED = 'Approved'
|
|
|
39 |
CB_REJECTED = 'Rejected'
|
|
|
40 |
CB_ONHOLD = 'On hold'
|
|
|
41 |
CB_CANCELLED = 'Cancelled'
|
|
|
42 |
|
| 16399 |
amit.gupta |
43 |
ORDER_PLACED = 'Order Placed'
|
|
|
44 |
ORDER_DELIVERED = 'Delivered'
|
|
|
45 |
ORDER_SHIPPED = 'Shipped' #Lets see if we can make use of it
|
|
|
46 |
ORDER_CANCELLED = 'Cancelled'
|
| 16398 |
amit.gupta |
47 |
|
| 16538 |
kshitij.so |
48 |
AFFILIATE_OFFER_API= {1:"https://www.spicesafar.com/FreeBapp/fetchOffersSM?deviceId=%s&retailerCode=%d"}
|
|
|
49 |
AFFILIATE_OFFER_DESC_API = {1:"https://www.spicesafar.com/FreeBapp/individualOffersSM?deviceId=%s&offerId=%s&retailerCode=%d"}
|
| 16399 |
amit.gupta |
50 |
|
| 16538 |
kshitij.so |
51 |
|
| 14705 |
kshitij.so |
52 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
53 |
global con
|
|
|
54 |
if con is None:
|
|
|
55 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
56 |
try:
|
|
|
57 |
con = pymongo.MongoClient(host, port)
|
|
|
58 |
except Exception, e:
|
|
|
59 |
print e
|
|
|
60 |
return None
|
|
|
61 |
return con
|
|
|
62 |
|
| 13572 |
kshitij.so |
63 |
def to_java_date(py_timestamp):
|
|
|
64 |
try:
|
|
|
65 |
java_date = int(time.mktime(py_timestamp.timetuple())) * 1000 + py_timestamp.microsecond / 1000
|
|
|
66 |
return java_date
|
|
|
67 |
except:
|
| 14705 |
kshitij.so |
68 |
return None
|
| 16631 |
manish.sha |
69 |
|
|
|
70 |
def to_py_date(java_timestamp):
|
|
|
71 |
date = datetime.fromtimestamp(java_timestamp)
|
|
|
72 |
return date
|
| 14705 |
kshitij.so |
73 |
|
|
|
74 |
def getCashBack(skuId, source_id, category_id, mc, mongoHost):
|
|
|
75 |
if not bool(mc.get("category_cash_back")):
|
|
|
76 |
populateCashBack(mc, mongoHost)
|
|
|
77 |
itemCashBackMap = mc.get("item_cash_back")
|
|
|
78 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
79 |
if itemCashBack is not None:
|
|
|
80 |
return itemCashBack
|
|
|
81 |
cashBackMap = mc.get("category_cash_back")
|
|
|
82 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
83 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
84 |
for cashBack in sourceCashBack:
|
|
|
85 |
if cashBack.get(category_id) is None:
|
|
|
86 |
continue
|
|
|
87 |
else:
|
|
|
88 |
return cashBack.get(category_id)
|
|
|
89 |
else:
|
|
|
90 |
return {}
|
|
|
91 |
|
|
|
92 |
def populateCashBack(mc, mongoHost):
|
|
|
93 |
print "Populating cashback"
|
|
|
94 |
cashBackMap = {}
|
|
|
95 |
itemCashBackMap = {}
|
|
|
96 |
cashBack = list(get_mongo_connection(host=mongoHost).Catalog.CategoryCashBack.find())
|
|
|
97 |
for row in cashBack:
|
|
|
98 |
temp_map = {}
|
|
|
99 |
temp_list = []
|
|
|
100 |
if cashBackMap.has_key(row['source_id']):
|
|
|
101 |
arr = cashBackMap.get(row['source_id'])
|
|
|
102 |
for val in arr:
|
|
|
103 |
temp_list.append(val)
|
|
|
104 |
temp_map[row['category_id']] = row
|
|
|
105 |
temp_list.append(temp_map)
|
|
|
106 |
cashBackMap[row['source_id']] = temp_list
|
|
|
107 |
else:
|
|
|
108 |
temp_map[row['category_id']] = row
|
|
|
109 |
temp_list.append(temp_map)
|
|
|
110 |
cashBackMap[row['source_id']] = temp_list
|
|
|
111 |
itemCashBack = list(get_mongo_connection(host=mongoHost).Catalog.ItemCashBack.find())
|
|
|
112 |
for row in itemCashBack:
|
|
|
113 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
114 |
itemCashBackMap[row['skuId']] = row
|
|
|
115 |
mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
|
|
|
116 |
mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)
|
|
|
117 |
|
| 14736 |
kshitij.so |
118 |
def ungzipResponse(r):
|
|
|
119 |
headers = r.info()
|
|
|
120 |
if headers.get('Content-Encoding')=='gzip':
|
|
|
121 |
url_f = StringIO.StringIO(r.read())
|
|
|
122 |
gz = gzip.GzipFile(fileobj=url_f)
|
|
|
123 |
html = gz.read()
|
|
|
124 |
gz.close()
|
|
|
125 |
return html
|
|
|
126 |
return r.read()
|
|
|
127 |
|
|
|
128 |
|
| 15897 |
kshitij.so |
129 |
def fetchResponseUsingProxy(url, headers=headers, livePricing=None, proxy=True):
|
| 15209 |
kshitij.so |
130 |
if livePricing is None:
|
| 15338 |
kshitij.so |
131 |
PROXY_URL = PROXY_MESH_GENERAL
|
| 15209 |
kshitij.so |
132 |
else:
|
| 15338 |
kshitij.so |
133 |
PROXY_URL = PROXY_MESH_LIVE
|
| 15897 |
kshitij.so |
134 |
if proxy:
|
| 15931 |
kshitij.so |
135 |
print PROXY_URL
|
| 15897 |
kshitij.so |
136 |
proxy = urllib2.ProxyHandler({'http': PROXY_URL})
|
|
|
137 |
opener = urllib2.build_opener(proxy)
|
|
|
138 |
urllib2.install_opener(opener)
|
| 14736 |
kshitij.so |
139 |
req = urllib2.Request(url,headers=headers)
|
|
|
140 |
response = urllib2.urlopen(req)
|
|
|
141 |
response_data = ungzipResponse(response)
|
|
|
142 |
response.close()
|
|
|
143 |
return response_data
|
|
|
144 |
|
| 15225 |
amit.gupta |
145 |
def sendNotification(userIds, campaignName, title, message,notificationtype, url, expiresat='2999-01-01'):
|
| 14843 |
amit.gupta |
146 |
usertuples = ()
|
|
|
147 |
count = -1
|
|
|
148 |
for userId in userIds:
|
|
|
149 |
count += 1
|
|
|
150 |
usertuples += (("userIds[" + str(count) + "]", userId),)
|
|
|
151 |
parameters = usertuples + (
|
|
|
152 |
("User[name]", campaignName),
|
|
|
153 |
("User[title]", title ),
|
|
|
154 |
("User[message]", message),
|
|
|
155 |
("User[type]", notificationtype),
|
| 15225 |
amit.gupta |
156 |
("User[url]", url),
|
|
|
157 |
("User[expiresat]", expiresat),)
|
| 14843 |
amit.gupta |
158 |
parameters = urllib.urlencode(parameters)
|
| 14987 |
amit.gupta |
159 |
#print parameters
|
| 14843 |
amit.gupta |
160 |
pushpostrequest = urllib2.Request(PUSH_NOTIFICATION_URL, parameters, headers=headers)
|
|
|
161 |
pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)
|
| 14845 |
amit.gupta |
162 |
urllib2.urlopen(pushpostrequest).read()
|
| 14948 |
amit.gupta |
163 |
|
|
|
164 |
def getCurrTimeStamp():
|
|
|
165 |
return toTimeStamp(datetime.now())
|
|
|
166 |
|
|
|
167 |
def toTimeStamp(dateTimeObj):
|
|
|
168 |
return int(time.mktime(dateTimeObj.timetuple()))
|
| 14987 |
amit.gupta |
169 |
def fromTimeStamp(timestamp):
|
| 15166 |
amit.gupta |
170 |
return datetime.fromtimestamp(timestamp)
|
|
|
171 |
|
| 16499 |
kshitij.so |
172 |
def getNlcPoints(deal):
|
|
|
173 |
if not(deal.get('minNlc') and deal.get('maxNlc')):
|
|
|
174 |
print "Raising exception minNlc, maxNlc not found for %d"%(deal.get('_id'))
|
| 15182 |
kshitij.so |
175 |
raise
|
| 16499 |
kshitij.so |
176 |
if deal.get('status') == 2:
|
| 15182 |
kshitij.so |
177 |
eolWeight = .60
|
|
|
178 |
else:
|
|
|
179 |
eolWeight = 1.0
|
| 16499 |
kshitij.so |
180 |
if deal.get('category_id') == 3:
|
| 15182 |
kshitij.so |
181 |
basePointPercentage = 5.0
|
|
|
182 |
maxNlcPoints = 200
|
| 16499 |
kshitij.so |
183 |
elif deal.get('category_id') == 5:
|
| 15182 |
kshitij.so |
184 |
basePointPercentage = 8.0
|
|
|
185 |
maxNlcPoints = 150
|
|
|
186 |
else:
|
|
|
187 |
basePointPercentage = 10.0
|
|
|
188 |
maxNlcPoints = 150
|
| 16499 |
kshitij.so |
189 |
discFromMinNlc = float((deal.get('minNlc') - (deal.get('available_price'))))/(deal.get('available_price')) *100
|
|
|
190 |
discFromMaxNlc = float((deal.get('maxNlc') - (deal.get('available_price'))))/(deal.get('available_price')) *100
|
| 15182 |
kshitij.so |
191 |
print discFromMinNlc
|
|
|
192 |
print discFromMaxNlc
|
|
|
193 |
if discFromMinNlc > 0:
|
|
|
194 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
195 |
elif discFromMinNlc < 0 and discFromMaxNlc > 0:
|
|
|
196 |
nlcPoints = 0
|
|
|
197 |
else:
|
|
|
198 |
nlcPoints = 100/basePointPercentage * discFromMinNlc
|
|
|
199 |
if (min(nlcPoints,maxNlcPoints)) > 0:
|
|
|
200 |
nlcPoints = (min(nlcPoints,maxNlcPoints)) * eolWeight
|
|
|
201 |
else:
|
|
|
202 |
nlcPoints = (min(nlcPoints,maxNlcPoints))
|
|
|
203 |
|
|
|
204 |
return nlcPoints
|
|
|
205 |
|
| 15166 |
amit.gupta |
206 |
def getLogger(filePath):
|
|
|
207 |
lgr = logging.getLogger()
|
|
|
208 |
lgr.setLevel(logging.DEBUG)
|
|
|
209 |
fh = logging.FileHandler(filePath)
|
|
|
210 |
fh.setLevel(logging.INFO)
|
|
|
211 |
frmt = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
|
|
|
212 |
fh.setFormatter(frmt)
|
|
|
213 |
lgr.addHandler(fh)
|
|
|
214 |
return lgr
|
| 15868 |
kshitij.so |
215 |
|
|
|
216 |
def removePriceFormatting(price_string):
|
|
|
217 |
return price_string.strip().replace('Rs.', '').replace('Rs', '').replace(',', '').replace(' ', '').replace(' ', '').split('.')[0]
|
| 15906 |
kshitij.so |
218 |
|
| 15950 |
kshitij.so |
219 |
def transformUrl(url,source_id):
|
| 15906 |
kshitij.so |
220 |
if source_id == 5:
|
|
|
221 |
finalUrl = urlparse.urlparse(url)
|
|
|
222 |
return finalUrl._replace(netloc=finalUrl.netloc.replace(finalUrl.hostname, 'm.shopclues.com')).geturl()
|
| 15950 |
kshitij.so |
223 |
elif source_id ==1:
|
|
|
224 |
return url
|
|
|
225 |
elif source_id == 2:
|
|
|
226 |
finalUrl = urlparse.urlparse(url)
|
|
|
227 |
return finalUrl._replace(netloc=finalUrl.netloc.replace(finalUrl.hostname, socket.gethostbyname(finalUrl.hostname))).geturl()
|
| 15906 |
kshitij.so |
228 |
|
|
|
229 |
|
| 16421 |
amit.gupta |
230 |
|
|
|
231 |
|
|
|
232 |
def find_between( s, first, last ):
|
|
|
233 |
try:
|
|
|
234 |
start = s.find( first ) + len( first )
|
|
|
235 |
end = s.rfind( last, start )
|
|
|
236 |
if start ==-1 or end ==-1:
|
|
|
237 |
return ""
|
|
|
238 |
return s[start:end]
|
|
|
239 |
except ValueError:
|
|
|
240 |
return ""
|
| 15950 |
kshitij.so |
241 |
|
| 15906 |
kshitij.so |
242 |
if __name__ == '__main__':
|
| 15950 |
kshitij.so |
243 |
print transformUrl("http://www.flipkart.com/redmi-2/p/itme8ygtcfax6w39",2)
|
| 15906 |
kshitij.so |
244 |
|
|
|
245 |
|
| 16561 |
amit.gupta |
246 |
def readSSh(fileName):
|
|
|
247 |
try:
|
|
|
248 |
str1 = open(fileName).read()
|
|
|
249 |
return str1
|
|
|
250 |
except:
|
|
|
251 |
ssh_client = SSHClient()
|
|
|
252 |
str1 = ""
|
|
|
253 |
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
|
254 |
ssh_client.connect('dtr', 22, 'root', 'ecip$dtrMay2014')
|
|
|
255 |
sftp_client = ssh_client.open_sftp()
|
|
|
256 |
try:
|
|
|
257 |
if not os.path.exists(os.path.dirname(fileName)):
|
|
|
258 |
os.makedirs(os.path.dirname(fileName))
|
|
|
259 |
sftp_client.get(fileName, fileName)
|
|
|
260 |
try:
|
|
|
261 |
str1 = open(fileName).read()
|
|
|
262 |
return str1
|
|
|
263 |
finally:
|
|
|
264 |
pass
|
|
|
265 |
except:
|
|
|
266 |
"could not read"
|
|
|
267 |
return str1
|