Subversion Repositories SmartDukaan

Rev

Rev 14875 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on Apr 15, 2015

@author: amit
'''
from dtr.reports.affiliatereco import SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD, \
    inc
from dtr.reports.usersegmentation import TMP_FILE
from dtr.sources.spice import todict
from dtr.utils.utils import fetchResponseUsingProxy, sendNotification
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pymongo.mongo_client import MongoClient
import json
import smtplib
import xlwt




PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
client = MongoClient('mongodb://localhost:27017/')

boldStyle = xlwt.XFStyle()
f = xlwt.Font()
f.bold = True
boldStyle.font = f


def sendmail(email, message, fileName, title):
    if email == "":
        return
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    
    # Create the container (outer) email message.
    msg = MIMEMultipart()
    msg['Subject'] = title
    msg.preamble = title
    html_msg = MIMEText(message, 'html')
    msg.attach(html_msg)
    
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
    fileMsg.set_payload(file(TMP_FILE).read())
    encoders.encode_base64(fileMsg)
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
    msg.attach(fileMsg)


    email.append('amit.gupta@shop2020.in')
    MAILTO = email 
    mailServer.login(SENDER, PASSWORD)
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
    
def scrapepaytm():
    db = client.Catalog
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet("Sample")
    add_headers(worksheet)
    row = 0
    for sku in db.MasterData.find({"source":"paytm.com"}):
        try:
            row += 1
            prodUrl = sku.get("url")
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
            response = fetchResponseUsingProxy(apiUrl)
            prod = json.loads(response)
            worksheet.write(row, inc(), prod["product_id"])
            worksheet.write(row, inc(), prod['parent_id'])
            worksheet.write(row, inc(), prod['instock'])
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
            worksheet.write(row, inc(), prod['productName'])
            worksheet.write(row, inc(), prod['actual_price'])
            worksheet.write(row, inc(), prod['offer_price'])
            worksheet.write(row, inc(), prod.get('promo_code'))
            worksheet.write(row, inc(), prod['promo_text'])
            sellerValues = prod['other_sellers'].get("values")
            if sellerValues > 0 and len(sellerValues) > 0:
                worksheet.write(row, inc(), sellerValues[0]['offer_price'])
            else:
                worksheet.write(row, inc(), None)
        except:
            pass
    workbook.save("/home/amit/paytmcatalog")
def add_headers(worksheet):
    global i
    i=0
    worksheet.write(0, inc(), 'productid')
    worksheet.write(0, inc(), 'parentid')
    worksheet.write(0, inc(), 'instock')
    worksheet.write(0, inc(), 'cod supported')
    worksheet.write(0, inc(), 'title')
    worksheet.write(0, inc(), 'actual price')
    worksheet.write(0, inc(), 'box price')
    worksheet.write(0,inc(), 'lowest price')
    worksheet.write(0,inc(), 'promo code')
    worksheet.write(0,inc(), 'promo text')
    worksheet.write(0, inc(), 'image Url')

        
    
def main():
    db = client.Dtr
    refunds = db.refund.findOne({"batch":1428949802})
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
    for ref in refunds:
        sendNotification([ref.get("userId")], 'Batch Credit', 'Cashback Credited', 'Rs.%s was added to your wallet yesterday'%(ref.get("userAmount")), 'url', 'http://api.profittill.com/cashbacks/mine?user_id=%s'%(ref.get("userId")))

if __name__ == '__main__':
    main()