| 13828 |
kshitij.so |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
|
|
4 |
from dtr.utils.utils import to_java_date
|
|
|
5 |
from datetime import datetime
|
|
|
6 |
import time
|
|
|
7 |
|
|
|
8 |
con = None
|
|
|
9 |
now = datetime.now()
|
|
|
10 |
print to_java_date(now)
|
|
|
11 |
|
|
|
12 |
headers = {
|
|
|
13 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
|
|
14 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
15 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
16 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
20 |
global con
|
|
|
21 |
if con is None:
|
|
|
22 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
23 |
try:
|
|
|
24 |
con = pymongo.MongoClient(host, port)
|
|
|
25 |
except Exception, e:
|
|
|
26 |
print e
|
|
|
27 |
return None
|
|
|
28 |
return con
|
|
|
29 |
|
|
|
30 |
def updatePrices():
|
|
|
31 |
snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3}))
|
|
|
32 |
for data in snapdealBestSellers:
|
|
|
33 |
print data['identifier']
|
| 13847 |
kshitij.so |
34 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13828 |
kshitij.so |
35 |
print "continue"
|
|
|
36 |
continue
|
|
|
37 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
|
|
|
38 |
print url
|
|
|
39 |
time.sleep(1)
|
| 13844 |
kshitij.so |
40 |
lowestOfferPrice = 0
|
|
|
41 |
instock = 0
|
| 13828 |
kshitij.so |
42 |
req = urllib2.Request(url,headers=headers)
|
|
|
43 |
response = urllib2.urlopen(req)
|
|
|
44 |
json_input = response.read()
|
| 13844 |
kshitij.so |
45 |
if len(json_input) > 0:
|
|
|
46 |
vendorInfo = json.loads(json_input)
|
|
|
47 |
for vendor in vendorInfo:
|
|
|
48 |
lowestOfferPrice = float(vendor['sellingPrice'])
|
|
|
49 |
stock = vendor['buyableInventory']
|
|
|
50 |
if stock > 0 and lowestOfferPrice > 0:
|
|
|
51 |
instock = 1
|
|
|
52 |
break
|
|
|
53 |
|
| 13828 |
kshitij.so |
54 |
print lowestOfferPrice
|
|
|
55 |
print instock
|
|
|
56 |
print stock
|
|
|
57 |
print "*************"
|
|
|
58 |
if instock == 1:
|
|
|
59 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
|
|
|
60 |
else:
|
|
|
61 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
|
|
|
62 |
|
|
|
63 |
def main():
|
|
|
64 |
updatePrices()
|
|
|
65 |
|
|
|
66 |
if __name__=='__main__':
|
|
|
67 |
main()
|