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