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