Subversion Repositories SmartDukaan

Rev

Rev 21135 | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
import re
from dtr.utils.utils import to_java_date
import optparse
from datetime import datetime
from time import sleep
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from shop2020.model.v1.catalog.script.AmazonAdvertisingApi import get_best_seller_rank
from shop2020.utils.EmailAttachmentSender import get_attachment_part
from shop2020.utils import EmailAttachmentSender

con = None
parser = optparse.OptionParser()
parser.add_option("-m", "--m", dest="mongoHost",
                      default="localhost",
                      type="string", help="The HOST where the mongo server is running",
                      metavar="mongo_host")

(options, args) = parser.parse_args()


exceptionList = []
asin_regex = r'/([A-Z0-9]{10})'
bestSellers = []
now = datetime.now()


class __RankInfo:
    
    def __init__(self, identifier, rank, title ,category):
        self.identifier = identifier
        self.rank  = rank
        self.title = title
        self.category = category

def get_mongo_connection(host=options.mongoHost, port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con



def getBestSellers(browseNode, category):
    global bestSellers
    rank = 1
    for i in range(1,11):
        sleep(5)
        result = get_best_seller_rank(browseNode, i)
        for x in result:
            r_info = __RankInfo(x['asin'], rank, x['product_name'], category)
            bestSellers.append(r_info)
            rank = rank + 1

def commitBestSellers():
    global exceptionList
    print "Rank",
    print '\t',
    print 'Identifier',
    print '\t',
    print 'title'
    for x in bestSellers:
        print x.rank,
        print '\t',
        print x.identifier,
        print '\t',
        print x.title
        col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier.strip()})
        if len(list(col)) == 0:
            exceptionList.append(x)
        else:
            get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier.strip() }, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
        
def resetRanks():
    get_mongo_connection().Catalog.MasterData.update({'rank':{'$gt':0},'source_id':1},{'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)

def sendEmail():
    message="""<html>
            <body>
            <h3>Amazon Best Sellers not in master</h3>
            <table border="1" style="width:100%;">
            <thead>
            <tr><th>Identifier</th>
            <th>Category</th>
            <th>Rank</th>
            <th>Title</th>
            </tr></thead>
            <tbody>"""
    for item in exceptionList:
        message+="""<tr>
        <td style="text-align:center">"""+(item.identifier)+"""</td>
        <td style="text-align:center">"""+(item.category)+"""</td>
        <td style="text-align:center">"""+str(item.rank)+"""</td>
        <td style="text-align:center">"""+str(item.title)+"""</td>
        </tr>"""
    message+="""</tbody></table></body></html>"""
    print message
    recipients = ['kshitij.sood@saholic.com','ritesh.chauhan@saholic.com','aishwarya.singh@saholic.com']
    EmailAttachmentSender.mail_send_grid("dtr@smartdukaan.com","apikey", "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", recipients, "Amazon Best Sellers",message ,[],[],[])              
            
            
            
            
def main():
    getBestSellers("1389432031","Mobiles")
    getBestSellers("1375458031","Tablets")
    resetRanks()
    commitBestSellers()
    sendEmail()
        
if __name__=='__main__':
    main()