Subversion Repositories SmartDukaan

Rev

Rev 14257 | Rev 15088 | 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
14379 kshitij.so 7
import smtplib
8
from email.mime.text import MIMEText
9
from email.mime.multipart import MIMEMultipart
13754 kshitij.so 10
 
14257 kshitij.so 11
con = None
12
parser = optparse.OptionParser()
13
parser.add_option("-m", "--m", dest="mongoHost",
14
                      default="localhost",
15
                      type="string", help="The HOST where the mongo server is running",
16
                      metavar="mongo_host")
17
 
18
(options, args) = parser.parse_args()
19
 
14379 kshitij.so 20
exceptionList = []
21
 
13754 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
 
29
bestSellers = []
13828 kshitij.so 30
now = datetime.now()
13754 kshitij.so 31
 
32
class __RankInfo:
33
 
14379 kshitij.so 34
    def __init__(self, identifier, rank, category):
13754 kshitij.so 35
        self.identifier = identifier
36
        self.rank  = rank
14379 kshitij.so 37
        self.category  =category
13754 kshitij.so 38
 
14257 kshitij.so 39
def get_mongo_connection(host=options.mongoHost, port=27017):
13754 kshitij.so 40
    global con
41
    if con is None:
42
        print "Establishing connection %s host and port %d" %(host,port)
43
        try:
44
            con = pymongo.MongoClient(host, port)
45
        except Exception, e:
46
            print e
47
            return None
48
    return con
49
 
50
def scrapeBestSellerMobiles():
51
    global bestSellers
52
    rank = 1
53
    for z in [0,20,40,60,80]:
54
        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)
55
        print url
56
        request = urllib2.Request(url,headers=headers)
57
        response = urllib2.urlopen(request)
58
 
59
        json_input = response.read()
60
        info = json.loads(json_input)
61
        for offer in info['productOfferGroupDtos']:
62
            for identifiers in offer['offers']:
14379 kshitij.so 63
                r_info = __RankInfo((identifiers['supcs'])[0],rank, None)
13754 kshitij.so 64
                bestSellers.append(r_info)
65
            rank += 1
66
 
67
def scrapeBestSellerTablets():
68
    global bestSellers
69
    bestSellers = []
70
    rank = 1
71
    for z in [0,20,40,60,80]:
72
        url = "http://www.snapdeal.com/acors/json/product/get/search/133/%d/20?sort=bstslr&keyword=&clickSrc=&viewType=List&lang=en&snr=false" %(z)
73
        print url
74
        request = urllib2.Request(url,headers=headers)
75
        response = urllib2.urlopen(request)
76
 
77
        json_input = response.read()
78
        info = json.loads(json_input)
79
        for offer in info['productOfferGroupDtos']:
80
            for identifiers in offer['offers']:
14379 kshitij.so 81
                r_info = __RankInfo((identifiers['supcs'])[0],rank, None)
13754 kshitij.so 82
                bestSellers.append(r_info)
83
            rank += 1
84
 
85
def resetRanks(category):
13828 kshitij.so 86
    oldRankedItems = get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3,'category':category})
87
    for item in oldRankedItems:
88
        get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']}, {'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 89
 
14379 kshitij.so 90
def commitBestSellers(category):
91
    global exceptionList
13754 kshitij.so 92
    print "Rank",
93
    print '\t',
94
    print 'Identifier'
95
    for x in bestSellers:
96
        print x.rank,
97
        print '\t',
98
        print x.identifier,
99
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
100
        print "count sku",
101
        print '\t',
14379 kshitij.so 102
        if len(list(col)) == 0:
103
            x.category = category
104
            exceptionList.append(x)
105
        else:
106
            get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip()}, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 107
 
14379 kshitij.so 108
def sendEmail():
109
    message="""<html>
110
            <body>
111
            <h3>Snapdeal Best Sellers not in master</h3>
112
            <table border="1" style="width:100%;">
113
            <thead>
114
            <tr><th>Identifier</th>
115
            <th>Category</th>
116
            <th>Rank</th>
117
            </tr></thead>
118
            <tbody>"""
119
    for item in exceptionList:
120
        message+="""<tr>
121
        <td style="text-align:center">"""+(item.identifier)+"""</td>
122
        <td style="text-align:center">"""+(item.category)+"""</td>
123
        <td style="text-align:center">"""+str(item.rank)+"""</td>
124
        </tr>"""
125
    message+="""</tbody></table></body></html>"""
126
    print message
127
    #recipients = ['kshitij.sood@saholic.com']
128
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com']
129
    msg = MIMEMultipart()
130
    msg['Subject'] = "Snapdeal Best Sellers" + ' - ' + str(datetime.now())
131
    msg['From'] = ""
132
    msg['To'] = ",".join(recipients)
133
    msg.preamble = "Snapdeal Best Sellers" + ' - ' + str(datetime.now())
134
    html_msg = MIMEText(message, 'html')
135
    msg.attach(html_msg)
136
 
137
    smtpServer = smtplib.SMTP('localhost')
138
    smtpServer.set_debuglevel(1)
139
    sender = 'dtr@shop2020.in'
140
    try:
141
        smtpServer.sendmail(sender, recipients, msg.as_string())
142
        print "Successfully sent email"
143
    except:
144
        print "Error: unable to send email."
145
 
146
 
147
 
13754 kshitij.so 148
def main():
149
    scrapeBestSellerMobiles()
150
    if len(bestSellers) > 0:
151
        resetRanks('Mobiles')
14379 kshitij.so 152
        commitBestSellers('Mobiles')
13754 kshitij.so 153
    scrapeBestSellerTablets()
154
    if len(bestSellers) > 0:
155
        resetRanks('Tablets')
14379 kshitij.so 156
        commitBestSellers('Tablets')
157
 
158
    sendEmail()
13754 kshitij.so 159
 
160
if __name__=='__main__':
161
    main()