| 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
|
| 14948 |
amit.gupta |
8 |
from datetime import datetime
|
| 14705 |
kshitij.so |
9 |
#TODO Need to add messy stuff to conf.
|
| 14708 |
kshitij.so |
10 |
con=None
|
| 14736 |
kshitij.so |
11 |
headers = {
|
|
|
12 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
|
|
13 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
14 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
15 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
|
|
16 |
'Connection':'keep-alive',
|
|
|
17 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
|
|
18 |
}
|
| 14803 |
kshitij.so |
19 |
PROXY_URL = "http://192.161.160.203:8800/"
|
| 14843 |
amit.gupta |
20 |
PUSH_NOTIFICATION_URL='http://api.profittill.com/admin/users/push'
|
|
|
21 |
DTR_API_BASIC_AUTH = base64.encodestring('%s:%s' % ("dtr", "dtr18Feb2015")).replace('\n', '')
|
| 14747 |
kshitij.so |
22 |
|
| 14705 |
kshitij.so |
23 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
24 |
global con
|
|
|
25 |
if con is None:
|
|
|
26 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
27 |
try:
|
|
|
28 |
con = pymongo.MongoClient(host, port)
|
|
|
29 |
except Exception, e:
|
|
|
30 |
print e
|
|
|
31 |
return None
|
|
|
32 |
return con
|
|
|
33 |
|
| 13572 |
kshitij.so |
34 |
def to_java_date(py_timestamp):
|
|
|
35 |
try:
|
|
|
36 |
java_date = int(time.mktime(py_timestamp.timetuple())) * 1000 + py_timestamp.microsecond / 1000
|
|
|
37 |
return java_date
|
|
|
38 |
except:
|
| 14705 |
kshitij.so |
39 |
return None
|
|
|
40 |
|
|
|
41 |
def getCashBack(skuId, source_id, category_id, mc, mongoHost):
|
|
|
42 |
if not bool(mc.get("category_cash_back")):
|
|
|
43 |
populateCashBack(mc, mongoHost)
|
|
|
44 |
itemCashBackMap = mc.get("item_cash_back")
|
|
|
45 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
46 |
if itemCashBack is not None:
|
|
|
47 |
return itemCashBack
|
|
|
48 |
cashBackMap = mc.get("category_cash_back")
|
|
|
49 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
50 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
51 |
for cashBack in sourceCashBack:
|
|
|
52 |
if cashBack.get(category_id) is None:
|
|
|
53 |
continue
|
|
|
54 |
else:
|
|
|
55 |
return cashBack.get(category_id)
|
|
|
56 |
else:
|
|
|
57 |
return {}
|
|
|
58 |
|
|
|
59 |
def populateCashBack(mc, mongoHost):
|
|
|
60 |
print "Populating cashback"
|
|
|
61 |
cashBackMap = {}
|
|
|
62 |
itemCashBackMap = {}
|
|
|
63 |
cashBack = list(get_mongo_connection(host=mongoHost).Catalog.CategoryCashBack.find())
|
|
|
64 |
for row in cashBack:
|
|
|
65 |
temp_map = {}
|
|
|
66 |
temp_list = []
|
|
|
67 |
if cashBackMap.has_key(row['source_id']):
|
|
|
68 |
arr = cashBackMap.get(row['source_id'])
|
|
|
69 |
for val in arr:
|
|
|
70 |
temp_list.append(val)
|
|
|
71 |
temp_map[row['category_id']] = row
|
|
|
72 |
temp_list.append(temp_map)
|
|
|
73 |
cashBackMap[row['source_id']] = temp_list
|
|
|
74 |
else:
|
|
|
75 |
temp_map[row['category_id']] = row
|
|
|
76 |
temp_list.append(temp_map)
|
|
|
77 |
cashBackMap[row['source_id']] = temp_list
|
|
|
78 |
itemCashBack = list(get_mongo_connection(host=mongoHost).Catalog.ItemCashBack.find())
|
|
|
79 |
for row in itemCashBack:
|
|
|
80 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
81 |
itemCashBackMap[row['skuId']] = row
|
|
|
82 |
mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
|
|
|
83 |
mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)
|
|
|
84 |
|
| 14736 |
kshitij.so |
85 |
def ungzipResponse(r):
|
|
|
86 |
headers = r.info()
|
|
|
87 |
if headers.get('Content-Encoding')=='gzip':
|
|
|
88 |
print "********************"
|
|
|
89 |
print "Deflating gzip response"
|
|
|
90 |
print "********************"
|
|
|
91 |
url_f = StringIO.StringIO(r.read())
|
|
|
92 |
gz = gzip.GzipFile(fileobj=url_f)
|
|
|
93 |
html = gz.read()
|
|
|
94 |
gz.close()
|
|
|
95 |
return html
|
|
|
96 |
return r.read()
|
|
|
97 |
|
|
|
98 |
|
|
|
99 |
def fetchResponseUsingProxy(url, headers=headers):
|
| 14987 |
amit.gupta |
100 |
# proxy = urllib2.ProxyHandler({'http': PROXY_URL})
|
|
|
101 |
# opener = urllib2.build_opener(proxy)
|
|
|
102 |
# urllib2.install_opener(opener)
|
| 14736 |
kshitij.so |
103 |
req = urllib2.Request(url,headers=headers)
|
|
|
104 |
response = urllib2.urlopen(req)
|
|
|
105 |
response_data = ungzipResponse(response)
|
|
|
106 |
response.close()
|
|
|
107 |
return response_data
|
|
|
108 |
|
| 14843 |
amit.gupta |
109 |
def sendNotification(userIds, campaignName, title, message,notificationtype, url):
|
|
|
110 |
usertuples = ()
|
|
|
111 |
count = -1
|
|
|
112 |
for userId in userIds:
|
|
|
113 |
count += 1
|
|
|
114 |
usertuples += (("userIds[" + str(count) + "]", userId),)
|
|
|
115 |
parameters = usertuples + (
|
|
|
116 |
("User[name]", campaignName),
|
|
|
117 |
("User[title]", title ),
|
|
|
118 |
("User[message]", message),
|
|
|
119 |
("User[type]", notificationtype),
|
|
|
120 |
("User[url]", url),)
|
|
|
121 |
parameters = urllib.urlencode(parameters)
|
| 14987 |
amit.gupta |
122 |
#print parameters
|
| 14843 |
amit.gupta |
123 |
pushpostrequest = urllib2.Request(PUSH_NOTIFICATION_URL, parameters, headers=headers)
|
|
|
124 |
pushpostrequest.add_header("Authorization", "Basic %s" % DTR_API_BASIC_AUTH)
|
| 14845 |
amit.gupta |
125 |
urllib2.urlopen(pushpostrequest).read()
|
| 14948 |
amit.gupta |
126 |
|
|
|
127 |
def getCurrTimeStamp():
|
|
|
128 |
return toTimeStamp(datetime.now())
|
|
|
129 |
|
|
|
130 |
def toTimeStamp(dateTimeObj):
|
|
|
131 |
return int(time.mktime(dateTimeObj.timetuple()))
|
| 14987 |
amit.gupta |
132 |
def fromTimeStamp(timestamp):
|
|
|
133 |
return datetime.fromtimestamp(timestamp)
|