Subversion Repositories SmartDukaan

Rev

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