Subversion Repositories SmartDukaan

Rev

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