Subversion Repositories SmartDukaan

Rev

Rev 20320 | Rev 21135 | 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 pymongo
2
import re
13828 kshitij.so 3
from dtr.utils.utils import to_java_date
14379 kshitij.so 4
import optparse
13828 kshitij.so 5
from datetime import datetime
20375 kshitij.so 6
from time import sleep
14379 kshitij.so 7
import smtplib
8
from email.mime.text import MIMEText
9
from email.mime.multipart import MIMEMultipart
20320 kshitij.so 10
from shop2020.model.v1.catalog.script.AmazonAdvertisingApi import get_best_seller_rank
13754 kshitij.so 11
 
14257 kshitij.so 12
con = None
13
parser = optparse.OptionParser()
14
parser.add_option("-m", "--m", dest="mongoHost",
15
                      default="localhost",
16
                      type="string", help="The HOST where the mongo server is running",
17
                      metavar="mongo_host")
18
 
19
(options, args) = parser.parse_args()
20
 
21
 
14379 kshitij.so 22
exceptionList = []
13754 kshitij.so 23
asin_regex = r'/([A-Z0-9]{10})'
24
bestSellers = []
13828 kshitij.so 25
now = datetime.now()
13754 kshitij.so 26
 
14379 kshitij.so 27
 
13754 kshitij.so 28
class __RankInfo:
29
 
20319 kshitij.so 30
    def __init__(self, identifier, rank, title ,category):
13754 kshitij.so 31
        self.identifier = identifier
32
        self.rank  = rank
20319 kshitij.so 33
        self.title = title
14379 kshitij.so 34
        self.category = category
13754 kshitij.so 35
 
14257 kshitij.so 36
def get_mongo_connection(host=options.mongoHost, port=27017):
13754 kshitij.so 37
    global con
38
    if con is None:
39
        print "Establishing connection %s host and port %d" %(host,port)
40
        try:
41
            con = pymongo.MongoClient(host, port)
42
        except Exception, e:
43
            print e
44
            return None
45
    return con
46
 
47
 
48
 
20319 kshitij.so 49
def getBestSellers(browseNode, category):
13754 kshitij.so 50
    global bestSellers
20319 kshitij.so 51
    rank = 1
52
    for i in range(1,11):
20375 kshitij.so 53
        sleep(5)
20320 kshitij.so 54
        result = get_best_seller_rank(browseNode, i)
20319 kshitij.so 55
        for x in result:
56
            r_info = __RankInfo(x['asin'], rank, x['product_name'], category)
13754 kshitij.so 57
            bestSellers.append(r_info)
58
            rank = rank + 1
59
 
20319 kshitij.so 60
def commitBestSellers():
14379 kshitij.so 61
    global exceptionList
13754 kshitij.so 62
    print "Rank",
63
    print '\t',
20319 kshitij.so 64
    print 'Identifier',
65
    print '\t',
66
    print 'title'
13754 kshitij.so 67
    for x in bestSellers:
68
        print x.rank,
69
        print '\t',
70
        print x.identifier,
20319 kshitij.so 71
        print '\t',
72
        print x.title
13754 kshitij.so 73
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
14379 kshitij.so 74
        if len(list(col)) == 0:
75
            exceptionList.append(x)
76
        else:
77
            get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip() }, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 78
 
20319 kshitij.so 79
def resetRanks():
80
    get_mongo_connection().Catalog.MasterData.update({'rank':{'$gt':0},'source_id':1},{'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
13754 kshitij.so 81
 
14379 kshitij.so 82
def sendEmail():
83
    message="""<html>
84
            <body>
85
            <h3>Amazon Best Sellers not in master</h3>
86
            <table border="1" style="width:100%;">
87
            <thead>
88
            <tr><th>Identifier</th>
89
            <th>Category</th>
90
            <th>Rank</th>
20319 kshitij.so 91
            <th>Title</th>
14379 kshitij.so 92
            </tr></thead>
93
            <tbody>"""
94
    for item in exceptionList:
95
        message+="""<tr>
96
        <td style="text-align:center">"""+(item.identifier)+"""</td>
97
        <td style="text-align:center">"""+(item.category)+"""</td>
98
        <td style="text-align:center">"""+str(item.rank)+"""</td>
20319 kshitij.so 99
        <td style="text-align:center">"""+str(item.title)+"""</td>
14379 kshitij.so 100
        </tr>"""
101
    message+="""</tbody></table></body></html>"""
102
    print message
20172 aman.kumar 103
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','ritesh.chauhan@saholic.com','khushal.bhatia@saholic.com']
14379 kshitij.so 104
    msg = MIMEMultipart()
105
    msg['Subject'] = "Amazon Best Sellers" + ' - ' + str(datetime.now())
106
    msg['From'] = ""
107
    msg['To'] = ",".join(recipients)
108
    msg.preamble = "Amazon Best Sellers" + ' - ' + str(datetime.now())
109
    html_msg = MIMEText(message, 'html')
110
    msg.attach(html_msg)
111
 
112
    smtpServer = smtplib.SMTP('localhost')
113
    smtpServer.set_debuglevel(1)
114
    sender = 'dtr@shop2020.in'
115
    try:
116
        smtpServer.sendmail(sender, recipients, msg.as_string())
117
        print "Successfully sent email"
118
    except:
119
        print "Error: unable to send email."
13754 kshitij.so 120
 
14379 kshitij.so 121
 
122
 
13754 kshitij.so 123
def main():
20319 kshitij.so 124
    getBestSellers("1389432031","Mobiles")
125
    getBestSellers("1375458031","Tablets")
126
    resetRanks()
127
    commitBestSellers()
14379 kshitij.so 128
    sendEmail()
13754 kshitij.so 129
 
130
if __name__=='__main__':
131
    main()