| Line 1... |
Line 1... |
| 1 |
import urllib2
|
1 |
import urllib2
|
| 2 |
import simplejson as json
|
2 |
import simplejson as json
|
| 3 |
import pymongo
|
3 |
import pymongo
|
| 4 |
from dtr.utils.utils import to_java_date
|
4 |
from dtr.utils.utils import to_java_date
|
| 5 |
from datetime import datetime
|
5 |
from datetime import datetime, timedelta
|
| 6 |
from operator import itemgetter
|
6 |
from operator import itemgetter
|
| 7 |
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
|
7 |
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
|
| 8 |
from dtr.utils import FlipkartScraper
|
8 |
from dtr.utils import FlipkartScraper
|
| 9 |
|
9 |
|
| 10 |
con = None
|
10 |
con = None
|
| 11 |
now = datetime.now()
|
- |
|
| 12 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3}
|
11 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3}
|
| 13 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
12 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
| 14 |
scraperAmazon = AmazonScraper()
|
13 |
scraperAmazon = AmazonScraper()
|
| 15 |
|
14 |
|
| 16 |
headers = {
|
15 |
headers = {
|
| Line 30... |
Line 29... |
| 30 |
print e
|
29 |
print e
|
| 31 |
return None
|
30 |
return None
|
| 32 |
return con
|
31 |
return con
|
| 33 |
|
32 |
|
| 34 |
def returnLatestPrice(data, source_id):
|
33 |
def returnLatestPrice(data, source_id):
|
| - |
|
34 |
now = datetime.now()
|
| 35 |
if source_id == 1:
|
35 |
if source_id == 1:
|
| 36 |
try:
|
36 |
try:
|
| 37 |
if data['identifier'] is None or len(data['identifier'])==0:
|
37 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 38 |
return {}
|
38 |
return {}
|
| - |
|
39 |
|
| - |
|
40 |
try:
|
| - |
|
41 |
if data['updatedOn'] + timedelta(minutes=5) > to_java_date(now):
|
| - |
|
42 |
print "sku id is already updated",data['_id']
|
| - |
|
43 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| - |
|
44 |
except:
|
| - |
|
45 |
pass
|
| - |
|
46 |
|
| - |
|
47 |
|
| 39 |
url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
|
48 |
url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
|
| 40 |
lowestPrice = 0.0
|
49 |
lowestPrice = 0.0
|
| 41 |
lowestPrice = scraperAmazon.read(url)
|
50 |
lowestPrice = scraperAmazon.read(url)
|
| 42 |
inStock = 0
|
51 |
inStock = 0
|
| 43 |
if lowestPrice > 0:
|
52 |
if lowestPrice > 0:
|
| 44 |
inStock = 1
|
53 |
inStock = 1
|
| 45 |
if lowestPrice > 0:
|
54 |
if lowestPrice > 0:
|
| 46 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
55 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| - |
|
56 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':inStock}}, multi=True)
|
| 47 |
else:
|
57 |
else:
|
| 48 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0}}, multi=True)
|
58 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0}}, multi=True)
|
| - |
|
59 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0}}, multi=True)
|
| - |
|
60 |
|
| - |
|
61 |
try:
|
| - |
|
62 |
recomputeDeal(data['skuBundleId'])
|
| - |
|
63 |
except:
|
| - |
|
64 |
print "Unable to compute deal for ",data['skuBundleId']
|
| - |
|
65 |
|
| 49 |
return {'_id':data['_id'],'available_price':lowestPrice,'in_stock':inStock,'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
66 |
return {'_id':data['_id'],'available_price':lowestPrice,'in_stock':inStock,'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 50 |
except:
|
67 |
except:
|
| 51 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
68 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 52 |
|
69 |
|
| 53 |
elif source_id ==3:
|
70 |
elif source_id ==3:
|
| 54 |
try:
|
71 |
try:
|
| 55 |
if data['identifier'] is None or len(data['identifier'])==0:
|
72 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 56 |
return {}
|
73 |
return {}
|
| - |
|
74 |
|
| - |
|
75 |
try:
|
| - |
|
76 |
if data['updatedOn'] + timedelta(minutes=5) > to_java_date(now):
|
| - |
|
77 |
print "sku id is already updated",data['_id']
|
| - |
|
78 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| - |
|
79 |
except:
|
| - |
|
80 |
pass
|
| - |
|
81 |
|
| - |
|
82 |
|
| 57 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
|
83 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
|
| 58 |
req = urllib2.Request(url,headers=headers)
|
84 |
req = urllib2.Request(url,headers=headers)
|
| 59 |
response = urllib2.urlopen(req)
|
85 |
response = urllib2.urlopen(req)
|
| 60 |
json_input = response.read()
|
86 |
json_input = response.read()
|
| 61 |
vendorInfo = json.loads(json_input)
|
87 |
vendorInfo = json.loads(json_input)
|
| Line 68... |
Line 94... |
| 68 |
inStock = 1
|
94 |
inStock = 1
|
| 69 |
break
|
95 |
break
|
| 70 |
|
96 |
|
| 71 |
print lowestOfferPrice
|
97 |
print lowestOfferPrice
|
| 72 |
print inStock
|
98 |
print inStock
|
| 73 |
print stock
|
- |
|
| 74 |
print "*************"
|
99 |
print "*************"
|
| 75 |
if inStock == 1:
|
100 |
if inStock == 1:
|
| 76 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
101 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| - |
|
102 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)
|
| 77 |
else:
|
103 |
else:
|
| 78 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
104 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| - |
|
105 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
| - |
|
106 |
|
| - |
|
107 |
try:
|
| - |
|
108 |
recomputeDeal(data['skuBundleId'])
|
| - |
|
109 |
except:
|
| - |
|
110 |
print "Unable to compute deal for ",data['skuBundleId']
|
| - |
|
111 |
|
| 79 |
return {'_id':data['_id'],'available_price':lowestOfferPrice,'in_stock':inStock,'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
112 |
return {'_id':data['_id'],'available_price':lowestOfferPrice,'in_stock':inStock,'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 80 |
except:
|
113 |
except:
|
| 81 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
114 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 82 |
|
115 |
|
| 83 |
elif source_id == 2:
|
116 |
elif source_id == 2:
|
| 84 |
try:
|
117 |
try:
|
| 85 |
if data['identifier'] is None or len(data['identifier'])==0:
|
118 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 86 |
return {}
|
119 |
return {}
|
| - |
|
120 |
|
| - |
|
121 |
try:
|
| - |
|
122 |
if data['updatedOn'] + timedelta(minutes=5) > to_java_date(now):
|
| - |
|
123 |
print "sku id is already updated",data['_id']
|
| - |
|
124 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| - |
|
125 |
except:
|
| - |
|
126 |
pass
|
| - |
|
127 |
|
| - |
|
128 |
|
| 87 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
|
129 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
|
| 88 |
vendorsData = scraperFk.read(url)
|
130 |
vendorsData = scraperFk.read(url)
|
| 89 |
sortedVendorsData = []
|
131 |
sortedVendorsData = []
|
| 90 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
132 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
| 91 |
print "data",sortedVendorsData
|
133 |
print "data",sortedVendorsData
|
| 92 |
lowestSp, iterator = (0,)*2
|
134 |
lowestSp = 0
|
| - |
|
135 |
inStock = 0
|
| 93 |
for vData in sortedVendorsData:
|
136 |
for vData in sortedVendorsData:
|
| 94 |
if iterator == 0:
|
- |
|
| 95 |
lowestSp = vData['sellingPrice']
|
137 |
lowestSp = vData['sellingPrice']
|
| 96 |
break
|
138 |
break
|
| 97 |
if lowestSp > 0:
|
139 |
if lowestSp > 0:
|
| 98 |
inStock = 1
|
140 |
inStock = 1
|
| 99 |
print lowestSp
|
141 |
print lowestSp
|
| 100 |
print inStock
|
142 |
print inStock
|
| 101 |
if lowestSp > 0:
|
143 |
if lowestSp > 0:
|
| 102 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
144 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| - |
|
145 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
|
| 103 |
else:
|
146 |
else:
|
| 104 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
147 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| - |
|
148 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
| - |
|
149 |
|
| - |
|
150 |
|
| - |
|
151 |
try:
|
| - |
|
152 |
recomputeDeal(data['skuBundleId'])
|
| - |
|
153 |
except:
|
| - |
|
154 |
print "Unable to compute deal for ",data['skuBundleId']
|
| - |
|
155 |
|
| 105 |
return {'_id':data['_id'],'available_price':lowestSp,'in_stock':inStock,'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
156 |
return {'_id':data['_id'],'available_price':lowestSp,'in_stock':inStock,'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 106 |
except:
|
157 |
except:
|
| 107 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
158 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 108 |
else:
|
159 |
else:
|
| 109 |
return {}
|
160 |
return {}
|
| 110 |
|
161 |
|
| - |
|
162 |
|
| - |
|
163 |
def recomputeDeal(skuBundleId):
|
| - |
|
164 |
"""Lets recompute deal for this bundle"""
|
| - |
|
165 |
print "Recomputing for bundleId",skuBundleId
|
| 111 |
|
166 |
|
| - |
|
167 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
| - |
|
168 |
bestPrice = float("inf")
|
| - |
|
169 |
bestOne = None
|
| - |
|
170 |
bestSellerPoints = 0
|
| - |
|
171 |
toUpdate = []
|
| - |
|
172 |
for similarItem in similarItems:
|
| - |
|
173 |
if similarItem['in_stock'] == 0:
|
| - |
|
174 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| - |
|
175 |
continue
|
| - |
|
176 |
if similarItem['available_price'] < bestPrice:
|
| - |
|
177 |
bestOne = similarItem
|
| - |
|
178 |
bestPrice = similarItem['available_price']
|
| - |
|
179 |
bestSellerPoints = similarItem['bestSellerPoints']
|
| - |
|
180 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
| - |
|
181 |
bestOne = similarItem
|
| - |
|
182 |
bestPrice = similarItem['available_price']
|
| - |
|
183 |
bestSellerPoints = similarItem['bestSellerPoints']
|
| - |
|
184 |
else:
|
| - |
|
185 |
pass
|
| - |
|
186 |
if bestOne is not None:
|
| - |
|
187 |
for similarItem in similarItems:
|
| - |
|
188 |
toUpdate.append(similarItem['_id'])
|
| - |
|
189 |
toUpdate.remove(bestOne['_id'])
|
| - |
|
190 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
| - |
|
191 |
if len(toUpdate) > 0:
|
| - |
|
192 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
| 112 |
|
193 |
|
| 113 |
def getLatestPrice(skuBundleId, source_id):
|
194 |
def getLatestPrice(skuBundleId, source_id):
|
| 114 |
temp = []
|
195 |
temp = []
|
| 115 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
196 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
| 116 |
for item in itemIds:
|
197 |
for item in itemIds:
|
| Line 118... |
Line 199... |
| 118 |
return temp
|
199 |
return temp
|
| 119 |
|
200 |
|
| 120 |
def getLatestPriceById(id):
|
201 |
def getLatestPriceById(id):
|
| 121 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))
|
202 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))
|
| 122 |
return returnLatestPrice(item[0], item[0]['source_id'])
|
203 |
return returnLatestPrice(item[0], item[0]['source_id'])
|
| 123 |
|
- |
|
| 124 |
|
- |
|
| 125 |
|
204 |
|
| 126 |
|
205 |
|
| 127 |
def main():
|
206 |
def main():
|
| 128 |
print getLatestPriceById(7)
|
207 |
print getLatestPriceById(7)
|
| 129 |
|
208 |
|