| 13828 |
kshitij.so |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
|
|
4 |
from dtr.utils.utils import to_java_date
|
| 13917 |
kshitij.so |
5 |
from datetime import datetime, timedelta
|
| 13828 |
kshitij.so |
6 |
import time
|
| 14180 |
kshitij.so |
7 |
from multiprocessing import Pool as ThreadPool
|
|
|
8 |
from multiprocessing import cpu_count
|
| 14254 |
kshitij.so |
9 |
import optparse
|
| 13828 |
kshitij.so |
10 |
|
|
|
11 |
con = None
|
|
|
12 |
|
| 14254 |
kshitij.so |
13 |
parser = optparse.OptionParser()
|
|
|
14 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
15 |
default="localhost",
|
|
|
16 |
type="string", help="The HOST where the mongo server is running",
|
|
|
17 |
metavar="mongo_host")
|
|
|
18 |
|
|
|
19 |
(options, args) = parser.parse_args()
|
|
|
20 |
|
|
|
21 |
|
| 13828 |
kshitij.so |
22 |
headers = {
|
|
|
23 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
|
|
24 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
25 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
26 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
|
|
27 |
}
|
|
|
28 |
|
| 14254 |
kshitij.so |
29 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
30 |
global con
|
|
|
31 |
if con is None:
|
|
|
32 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
33 |
try:
|
|
|
34 |
con = pymongo.MongoClient(host, port)
|
|
|
35 |
except Exception, e:
|
|
|
36 |
print e
|
|
|
37 |
return None
|
|
|
38 |
return con
|
|
|
39 |
|
| 14180 |
kshitij.so |
40 |
def populate():
|
|
|
41 |
toScrapMap = {}
|
| 14132 |
kshitij.so |
42 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
|
|
|
43 |
for bestSeller in bestSellers:
|
|
|
44 |
snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
|
|
|
45 |
for data in snapdealBestSellers:
|
| 14180 |
kshitij.so |
46 |
if not toScrapMap.has_key(data['_id']):
|
|
|
47 |
toScrapMap[data['_id']] = data
|
| 14252 |
kshitij.so |
48 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':3,'showDeal':1,'totalPoints':{'$gt':0}}))
|
|
|
49 |
for deal in dealFlagged:
|
|
|
50 |
if not toScrapMap.has_key(deal['_id']):
|
| 14261 |
kshitij.so |
51 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
|
|
52 |
toScrapMap[deal['_id']] = data[0]
|
| 14180 |
kshitij.so |
53 |
pool = ThreadPool(cpu_count() *2)
|
|
|
54 |
pool.map(updatePrices,toScrapMap.values())
|
|
|
55 |
pool.close()
|
|
|
56 |
pool.join()
|
| 14252 |
kshitij.so |
57 |
print "joining threads at %s"%(str(datetime.now()))
|
| 14180 |
kshitij.so |
58 |
|
|
|
59 |
|
|
|
60 |
def updatePrices(data):
|
|
|
61 |
if data['source_id']!=3:
|
|
|
62 |
return
|
|
|
63 |
print data['identifier']
|
|
|
64 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
|
|
65 |
print "returning"
|
|
|
66 |
return
|
|
|
67 |
|
|
|
68 |
try:
|
|
|
69 |
if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
|
|
|
70 |
print "sku id is already updated",data['_id']
|
|
|
71 |
return
|
|
|
72 |
except:
|
|
|
73 |
pass
|
|
|
74 |
|
|
|
75 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
|
|
|
76 |
print url
|
|
|
77 |
time.sleep(1)
|
|
|
78 |
lowestOfferPrice = 0
|
|
|
79 |
instock = 0
|
|
|
80 |
req = urllib2.Request(url,headers=headers)
|
|
|
81 |
response = urllib2.urlopen(req)
|
| 14209 |
kshitij.so |
82 |
json_input = response.read()
|
| 14187 |
kshitij.so |
83 |
response.close()
|
| 14180 |
kshitij.so |
84 |
if len(json_input) > 0:
|
|
|
85 |
vendorInfo = json.loads(json_input)
|
|
|
86 |
for vendor in vendorInfo:
|
|
|
87 |
lowestOfferPrice = float(vendor['sellingPrice'])
|
| 14181 |
kshitij.so |
88 |
try:
|
|
|
89 |
stock = vendor['buyableInventory']
|
|
|
90 |
except:
|
|
|
91 |
stock = 0
|
| 14180 |
kshitij.so |
92 |
if stock > 0 and lowestOfferPrice > 0:
|
|
|
93 |
instock = 1
|
|
|
94 |
break
|
| 14181 |
kshitij.so |
95 |
else:
|
|
|
96 |
lowestOfferPrice = 0
|
|
|
97 |
stock = 0
|
|
|
98 |
instock = 0
|
|
|
99 |
|
| 14180 |
kshitij.so |
100 |
print lowestOfferPrice
|
|
|
101 |
print instock
|
|
|
102 |
print stock
|
| 14181 |
kshitij.so |
103 |
print "Lowest Offer Price for id %d is %d , stock is %d and stock count is %d" %(data['_id'],lowestOfferPrice,instock,stock)
|
| 14180 |
kshitij.so |
104 |
print "*************"
|
|
|
105 |
if instock == 1:
|
|
|
106 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':instock}}, multi=True)
|
|
|
107 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
|
|
|
108 |
else:
|
|
|
109 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':instock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
|
|
|
110 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
|
|
|
111 |
|
|
|
112 |
try:
|
|
|
113 |
recomputeDeal(data['skuBundleId'])
|
|
|
114 |
except:
|
|
|
115 |
print "Unable to compute deal for ",data['skuBundleId']
|
|
|
116 |
|
|
|
117 |
|
| 13917 |
kshitij.so |
118 |
def recomputeDeal(skuBundleId):
|
|
|
119 |
"""Lets recompute deal for this bundle"""
|
|
|
120 |
print "Recomputing for bundleId",skuBundleId
|
|
|
121 |
|
|
|
122 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
123 |
bestPrice = float("inf")
|
|
|
124 |
bestOne = None
|
|
|
125 |
bestSellerPoints = 0
|
|
|
126 |
toUpdate = []
|
|
|
127 |
for similarItem in similarItems:
|
| 13974 |
kshitij.so |
128 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
|
| 13917 |
kshitij.so |
129 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
130 |
continue
|
|
|
131 |
if similarItem['available_price'] < bestPrice:
|
|
|
132 |
bestOne = similarItem
|
|
|
133 |
bestPrice = similarItem['available_price']
|
|
|
134 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
135 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
136 |
bestOne = similarItem
|
|
|
137 |
bestPrice = similarItem['available_price']
|
|
|
138 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
139 |
else:
|
|
|
140 |
pass
|
|
|
141 |
if bestOne is not None:
|
|
|
142 |
for similarItem in similarItems:
|
|
|
143 |
toUpdate.append(similarItem['_id'])
|
|
|
144 |
toUpdate.remove(bestOne['_id'])
|
|
|
145 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
146 |
if len(toUpdate) > 0:
|
|
|
147 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
|
|
148 |
|
| 13828 |
kshitij.so |
149 |
def main():
|
| 14180 |
kshitij.so |
150 |
populate()
|
| 13828 |
kshitij.so |
151 |
|
|
|
152 |
if __name__=='__main__':
|
|
|
153 |
main()
|