Subversion Repositories SmartDukaan

Rev

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

import pymongo
import optparse
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from datetime import datetime

ITEMS = []
RECIPIENTS = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','ritesh.chauhan@saholic.com','khushal.bhatia@saholic.com']

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()

SOURCE_MAP = {1:'AMAZON',2:'FLIPKART',3:'SNAPDEAL',4:'SAHOLIC',5:"SHOPCLUES.COM"}

class __Items():
    
    def __init__(self, skuId, skuBundleId ,productName, source_id, rank, url):
        self.skuId = skuId
        self.skuBundleId = skuBundleId
        self.productName = productName
        self.source_id = source_id
        self.rank = rank
        self.url = url
        
    

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 checkItems():
    global ITEMS
    negativeItems = get_mongo_connection().Catalog.NegativeDeals.find()
    for negativeItem in negativeItems:
        masterItem = get_mongo_connection().Catalog.MasterData.find_one({'_id':negativeItem['sku']})
        item = __Items(masterItem.get('_id'), masterItem.get('skuBundleId') ,masterItem.get('product_name'),masterItem.get('source_id'),masterItem.get('rank'), \
                       masterItem.get('marketPlaceUrl'))
        ITEMS.append(item)

def sendMail():
    message="""<html>
            <body>
            <h3>Negative Items</h3>
            <table border="1" style="width:100%;">
            <thead>
            <tr><th>Item Id</th>
            <th>Catalog Item Id</th>
            <th>Product Name</th>
            <th>Rank</th>
            <th>Source</th>
            <th>Url</th>
            </tr></thead>
            <tbody>"""
    for item in ITEMS:
        message+="""<tr>
        <td style="text-align:center">"""+str(item.skuId)+"""</td>
        <td style="text-align:center">"""+str(item.skuBundleId)+"""</td>
        <td style="text-align:center">"""+(item.productName)+"""</td>
        <td style="text-align:center">"""+str(item.rank)+"""</td>
        <td style="text-align:center">"""+str(getSourceForId(item.source_id))+"""</td>
        <td style="text-align:center">"""+str(item.url)+"""</td>
        </tr>"""
    message+="""</tbody></table></body></html>"""
    print message
    msg = MIMEMultipart()
    msg['Subject'] = "Negative Items" + ' - ' + str(datetime.now())
    msg['From'] = ""
    msg['To'] = ",".join(RECIPIENTS)
    msg.preamble = "Negative Items" + ' - ' + str(datetime.now())
    html_msg = MIMEText(message, 'html')
    msg.attach(html_msg)
    
    smtpServer = smtplib.SMTP('localhost')
    smtpServer.set_debuglevel(1)
    sender = 'dtr@shop2020.in'
    try:
        smtpServer.sendmail(sender, RECIPIENTS, msg.as_string())
        print "Successfully sent email"
    except:
        print "Error: unable to send email."

def getSourceForId(source_id):
    if SOURCE_MAP.get(source_id) is not None:
        return SOURCE_MAP.get(source_id)
    else:
        return "Not valid source"

if __name__=='__main__':
    checkItems()
    sendMail()