Subversion Repositories SmartDukaan

Rev

Rev 14257 | Rev 15088 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14257 Rev 14379
Line 2... Line 2...
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
6
import optparse
6
import optparse
-
 
7
import smtplib
-
 
8
from email.mime.text import MIMEText
-
 
9
from email.mime.multipart import MIMEMultipart
7
 
10
 
8
con = None
11
con = None
9
parser = optparse.OptionParser()
12
parser = optparse.OptionParser()
10
parser.add_option("-m", "--m", dest="mongoHost",
13
parser.add_option("-m", "--m", dest="mongoHost",
11
                      default="localhost",
14
                      default="localhost",
12
                      type="string", help="The HOST where the mongo server is running",
15
                      type="string", help="The HOST where the mongo server is running",
13
                      metavar="mongo_host")
16
                      metavar="mongo_host")
14
 
17
 
15
(options, args) = parser.parse_args()
18
(options, args) = parser.parse_args()
16
 
19
 
-
 
20
exceptionList = []
-
 
21
 
17
headers = { 
22
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',
23
           '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',      
24
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
20
            'Accept-Language' : 'en-US,en;q=0.8',                     
25
            'Accept-Language' : 'en-US,en;q=0.8',                     
21
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
26
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
Line 24... Line 29...
24
bestSellers = []
29
bestSellers = []
25
now = datetime.now()
30
now = datetime.now()
26
 
31
 
27
class __RankInfo:
32
class __RankInfo:
28
    
33
    
29
    def __init__(self, identifier, rank):
34
    def __init__(self, identifier, rank, category):
30
        self.identifier = identifier
35
        self.identifier = identifier
31
        self.rank  = rank
36
        self.rank  = rank
-
 
37
        self.category  =category
32
 
38
 
33
def get_mongo_connection(host=options.mongoHost, port=27017):
39
def get_mongo_connection(host=options.mongoHost, port=27017):
34
    global con
40
    global con
35
    if con is None:
41
    if con is None:
36
        print "Establishing connection %s host and port %d" %(host,port)
42
        print "Establishing connection %s host and port %d" %(host,port)
Line 52... Line 58...
52
 
58
 
53
        json_input = response.read()
59
        json_input = response.read()
54
        info = json.loads(json_input)
60
        info = json.loads(json_input)
55
        for offer in info['productOfferGroupDtos']:
61
        for offer in info['productOfferGroupDtos']:
56
            for identifiers in offer['offers']:
62
            for identifiers in offer['offers']:
57
                r_info = __RankInfo((identifiers['supcs'])[0],rank)
63
                r_info = __RankInfo((identifiers['supcs'])[0],rank, None)
58
                bestSellers.append(r_info)
64
                bestSellers.append(r_info)
59
            rank += 1
65
            rank += 1
60
 
66
 
61
def scrapeBestSellerTablets():
67
def scrapeBestSellerTablets():
62
    global bestSellers
68
    global bestSellers
Line 70... Line 76...
70
 
76
 
71
        json_input = response.read()
77
        json_input = response.read()
72
        info = json.loads(json_input)
78
        info = json.loads(json_input)
73
        for offer in info['productOfferGroupDtos']:
79
        for offer in info['productOfferGroupDtos']:
74
            for identifiers in offer['offers']:
80
            for identifiers in offer['offers']:
75
                r_info = __RankInfo((identifiers['supcs'])[0],rank)
81
                r_info = __RankInfo((identifiers['supcs'])[0],rank, None)
76
                bestSellers.append(r_info)
82
                bestSellers.append(r_info)
77
            rank += 1
83
            rank += 1
78
 
84
 
79
def resetRanks(category):
85
def resetRanks(category):
80
    oldRankedItems = get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3,'category':category})
86
    oldRankedItems = get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3,'category':category})
81
    for item in oldRankedItems:
87
    for item in oldRankedItems:
82
        get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']}, {'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
88
        get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']}, {'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
83
 
89
 
84
def commitBestSellers():
90
def commitBestSellers(category):
-
 
91
    global exceptionList
85
    print "Rank",
92
    print "Rank",
86
    print '\t',
93
    print '\t',
87
    print 'Identifier'
94
    print 'Identifier'
88
    for x in bestSellers:
95
    for x in bestSellers:
89
        print x.rank,
96
        print x.rank,
90
        print '\t',
97
        print '\t',
91
        print x.identifier,
98
        print x.identifier,
92
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
99
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
93
        print "count sku",
100
        print "count sku",
94
        print '\t',
101
        print '\t',
95
        print len(list(col))
102
        if len(list(col)) == 0:
-
 
103
            x.category = category
-
 
104
            exceptionList.append(x)
-
 
105
        else:
96
        get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip()}, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
106
            get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip()}, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
-
 
107
 
-
 
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
            
97
 
147
 
98
def main():
148
def main():
99
    scrapeBestSellerMobiles()
149
    scrapeBestSellerMobiles()
100
    if len(bestSellers) > 0:
150
    if len(bestSellers) > 0:
101
        resetRanks('Mobiles')
151
        resetRanks('Mobiles')
102
        commitBestSellers()
152
        commitBestSellers('Mobiles')
103
    scrapeBestSellerTablets()
153
    scrapeBestSellerTablets()
104
    if len(bestSellers) > 0:
154
    if len(bestSellers) > 0:
105
        resetRanks('Tablets')
155
        resetRanks('Tablets')
106
        commitBestSellers()
156
        commitBestSellers('Tablets')
-
 
157
        
-
 
158
    sendEmail()
107
 
159
 
108
if __name__=='__main__':
160
if __name__=='__main__':
109
    main()
161
    main()
110
162