Subversion Repositories SmartDukaan

Rev

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