Rev 21135 | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pymongoimport refrom dtr.utils.utils import to_java_dateimport optparsefrom datetime import datetimefrom time import sleepimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom shop2020.model.v1.catalog.script.AmazonAdvertisingApi import get_best_seller_rankfrom shop2020.utils.EmailAttachmentSender import get_attachment_partfrom shop2020.utils import EmailAttachmentSendercon = Noneparser = 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 = identifierself.rank = rankself.title = titleself.category = categorydef get_mongo_connection(host=options.mongoHost, port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef getBestSellers(browseNode, category):global bestSellersrank = 1for 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 + 1def commitBestSellers():global exceptionListprint "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.titlecol = 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 messagerecipients = ['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()