| 13572 |
kshitij.so |
1 |
import time
|
| 14705 |
kshitij.so |
2 |
import pymongo
|
| 14736 |
kshitij.so |
3 |
import urllib2
|
|
|
4 |
import gzip
|
|
|
5 |
import StringIO
|
| 13572 |
kshitij.so |
6 |
|
| 14705 |
kshitij.so |
7 |
#TODO Need to add messy stuff to conf.
|
| 14708 |
kshitij.so |
8 |
con=None
|
| 14736 |
kshitij.so |
9 |
headers = {
|
|
|
10 |
'User-agent':'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
|
|
|
11 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
12 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
13 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
|
|
|
14 |
'Connection':'keep-alive',
|
|
|
15 |
'Accept-Encoding' : 'gzip,deflate,sdch'
|
|
|
16 |
}
|
| 14737 |
kshitij.so |
17 |
proxy = "http://192.161.163.60:8800"
|
| 14705 |
kshitij.so |
18 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
19 |
global con
|
|
|
20 |
if con is None:
|
|
|
21 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
22 |
try:
|
|
|
23 |
con = pymongo.MongoClient(host, port)
|
|
|
24 |
except Exception, e:
|
|
|
25 |
print e
|
|
|
26 |
return None
|
|
|
27 |
return con
|
|
|
28 |
|
| 13572 |
kshitij.so |
29 |
def to_java_date(py_timestamp):
|
|
|
30 |
try:
|
|
|
31 |
java_date = int(time.mktime(py_timestamp.timetuple())) * 1000 + py_timestamp.microsecond / 1000
|
|
|
32 |
return java_date
|
|
|
33 |
except:
|
| 14705 |
kshitij.so |
34 |
return None
|
|
|
35 |
|
|
|
36 |
def getCashBack(skuId, source_id, category_id, mc, mongoHost):
|
|
|
37 |
if not bool(mc.get("category_cash_back")):
|
|
|
38 |
populateCashBack(mc, mongoHost)
|
|
|
39 |
itemCashBackMap = mc.get("item_cash_back")
|
|
|
40 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
41 |
if itemCashBack is not None:
|
|
|
42 |
return itemCashBack
|
|
|
43 |
cashBackMap = mc.get("category_cash_back")
|
|
|
44 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
45 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
46 |
for cashBack in sourceCashBack:
|
|
|
47 |
if cashBack.get(category_id) is None:
|
|
|
48 |
continue
|
|
|
49 |
else:
|
|
|
50 |
return cashBack.get(category_id)
|
|
|
51 |
else:
|
|
|
52 |
return {}
|
|
|
53 |
|
|
|
54 |
def populateCashBack(mc, mongoHost):
|
|
|
55 |
print "Populating cashback"
|
|
|
56 |
cashBackMap = {}
|
|
|
57 |
itemCashBackMap = {}
|
|
|
58 |
cashBack = list(get_mongo_connection(host=mongoHost).Catalog.CategoryCashBack.find())
|
|
|
59 |
for row in cashBack:
|
|
|
60 |
temp_map = {}
|
|
|
61 |
temp_list = []
|
|
|
62 |
if cashBackMap.has_key(row['source_id']):
|
|
|
63 |
arr = cashBackMap.get(row['source_id'])
|
|
|
64 |
for val in arr:
|
|
|
65 |
temp_list.append(val)
|
|
|
66 |
temp_map[row['category_id']] = row
|
|
|
67 |
temp_list.append(temp_map)
|
|
|
68 |
cashBackMap[row['source_id']] = temp_list
|
|
|
69 |
else:
|
|
|
70 |
temp_map[row['category_id']] = row
|
|
|
71 |
temp_list.append(temp_map)
|
|
|
72 |
cashBackMap[row['source_id']] = temp_list
|
|
|
73 |
itemCashBack = list(get_mongo_connection(host=mongoHost).Catalog.ItemCashBack.find())
|
|
|
74 |
for row in itemCashBack:
|
|
|
75 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
76 |
itemCashBackMap[row['skuId']] = row
|
|
|
77 |
mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
|
|
|
78 |
mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)
|
|
|
79 |
|
| 14736 |
kshitij.so |
80 |
def ungzipResponse(r):
|
|
|
81 |
headers = r.info()
|
|
|
82 |
if headers.get('Content-Encoding')=='gzip':
|
|
|
83 |
print "********************"
|
|
|
84 |
print "Deflating gzip response"
|
|
|
85 |
print "********************"
|
|
|
86 |
url_f = StringIO.StringIO(r.read())
|
|
|
87 |
gz = gzip.GzipFile(fileobj=url_f)
|
|
|
88 |
html = gz.read()
|
|
|
89 |
gz.close()
|
|
|
90 |
return html
|
|
|
91 |
return r.read()
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
def fetchResponseUsingProxy(url, headers=headers):
|
| 14737 |
kshitij.so |
95 |
proxy = urllib2.ProxyHandler({'http': proxy})
|
| 14736 |
kshitij.so |
96 |
opener = urllib2.build_opener(proxy)
|
|
|
97 |
urllib2.install_opener(opener)
|
|
|
98 |
req = urllib2.Request(url,headers=headers)
|
|
|
99 |
response = urllib2.urlopen(req)
|
|
|
100 |
response_data = ungzipResponse(response)
|
|
|
101 |
response.close()
|
|
|
102 |
return response_data
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|