Subversion Repositories SmartDukaan

Rev

Rev 13754 | Rev 14257 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13754 kshitij.so 1
import urllib2
2
import simplejson as json
3
import pymongo
13828 kshitij.so 4
from dtr.utils.utils import to_java_date
5
from datetime import datetime
13754 kshitij.so 6
 
7
headers = { 
8
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
9
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
10
            'Accept-Language' : 'en-US,en;q=0.8',                     
11
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
12
        }
13
 
14
con = None
15
bestSellers = []
13828 kshitij.so 16
now = datetime.now()
13754 kshitij.so 17
 
18
class __RankInfo:
19
 
20
    def __init__(self, identifier, rank):
21
        self.identifier = identifier
22
        self.rank  = rank
23
 
24
def get_mongo_connection(host='localhost', port=27017):
25
    global con
26
    if con is None:
27
        print "Establishing connection %s host and port %d" %(host,port)
28
        try:
29
            con = pymongo.MongoClient(host, port)
30
        except Exception, e:
31
            print e
32
            return None
33
    return con
34
 
35
def scrapeBestSellerMobiles():
36
    global bestSellers
37
    rank = 1
38
    for z in [0,20,40,60,80]:
39
        url = "http://www.snapdeal.com/acors/json/product/get/search/175/%d/20?q=&sort=bstslr&keyword=&clickSrc=&viewType=List&lang=en&snr=false" %(z)
40
        print url
41
        request = urllib2.Request(url,headers=headers)
42
        response = urllib2.urlopen(request)
43
 
44
        json_input = response.read()
45
        info = json.loads(json_input)
46
        for offer in info['productOfferGroupDtos']:
47
            for identifiers in offer['offers']:
48
                r_info = __RankInfo((identifiers['supcs'])[0],rank)
49
                bestSellers.append(r_info)
50
            rank += 1
51
 
52
def scrapeBestSellerTablets():
53
    global bestSellers
54
    bestSellers = []
55
    rank = 1
56
    for z in [0,20,40,60,80]:
57
        url = "http://www.snapdeal.com/acors/json/product/get/search/133/%d/20?sort=bstslr&keyword=&clickSrc=&viewType=List&lang=en&snr=false" %(z)
58
        print url
59
        request = urllib2.Request(url,headers=headers)
60
        response = urllib2.urlopen(request)
61
 
62
        json_input = response.read()
63
        info = json.loads(json_input)
64
        for offer in info['productOfferGroupDtos']:
65
            for identifiers in offer['offers']:
66
                r_info = __RankInfo((identifiers['supcs'])[0],rank)
67
                bestSellers.append(r_info)
68
            rank += 1
69
 
70
def resetRanks(category):
13828 kshitij.so 71
    oldRankedItems = get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3,'category':category})
72
    for item in oldRankedItems:
73
        get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']}, {'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 74
 
75
def commitBestSellers():
76
    print "Rank",
77
    print '\t',
78
    print 'Identifier'
79
    for x in bestSellers:
80
        print x.rank,
81
        print '\t',
82
        print x.identifier,
83
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
84
        print "count sku",
85
        print '\t',
86
        print len(list(col))
13828 kshitij.so 87
        get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip()}, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 88
 
89
def main():
90
    scrapeBestSellerMobiles()
91
    if len(bestSellers) > 0:
92
        resetRanks('Mobiles')
93
        commitBestSellers()
94
    scrapeBestSellerTablets()
95
    if len(bestSellers) > 0:
96
        resetRanks('Tablets')
97
        commitBestSellers()
98
 
99
if __name__=='__main__':
100
    main()