Subversion Repositories SmartDukaan

Rev

Rev 14879 | 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
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 traceback
import xlwt




PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
OFFER_DETAIL_API = "https://paytm.com/papi/v1/promosearch/product/%s/offers"
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():
    global i
    db = client.Catalog
    workbook = xlwt.Workbook()
    worksheet = workbook.add_sheet("Sample")
    worksheet2 = workbook.add_sheet("Codes")
    add_headers(worksheet, worksheet2)
    row = 0
    row2 = 0
    offermap = {}
    for sku in db.MasterData.find({"source":"paytm.com"}):
        try:
            prodUrl = sku.get("url")
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
            response = fetchResponseUsingProxy(apiUrl)
            prod = json.loads(response)
            if prod.get("error"):
                continue
            offerUrl = OFFER_DETAIL_API%(prod['parent_id'])
            response = fetchResponseUsingProxy(offerUrl)
            row += 1
            offers = json.loads(response)
            codes=[]
            for code in offers.get("codes"):
                offercode = code.get("code")
                row2 += 1
                i =-1
                worksheet2.write(row2, inc(), prod['parent_id'])
                worksheet2.write(row2, inc(), offercode)
                worksheet2.write(row2, inc(), code['offerText'])
                worksheet2.write(row2, inc(), code['terms'])
                worksheet2.write(row2, inc(), code['priority'])
                worksheet2.write(row2, inc(), code['valid_upto'])
                codes.append(offercode)
                if not offermap.has_key(offercode): 
                    offermap[offercode] = code
                    
                    
            allCodes = ",".join(codes)
            i=-1
            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['name'])
            worksheet.write(row, inc(), prod['actual_price'])
            worksheet.write(row, inc(), prod['offer_price'])
            sellerValues = prod['other_sellers'].get("values")
            if sellerValues is not None and len(sellerValues) > 0:
                lowestPrice = sellerValues[0]['offer_price']
                if lowestPrice >= prod['offer_price']:
                    worksheet.write(row, inc(), prod['offer_price'])
                else:
                    worksheet.write(row, inc(), lowestPrice)
            else:
                worksheet.write(row, inc(), "")

            worksheet.write(row, inc(), prod['promo_text'])
            worksheet.write(row, inc(), allCodes)
        except:
            traceback.print_exc()
        
    row = 0
#    for key, val in offermap.iteritems():
#        row += 1
#        i =-1
#        worksheet2.write(row, inc(), key)
#        worksheet2.write(row, inc(), val['offerText'])
#        worksheet2.write(row, inc(), val['terms'])
#        worksheet2.write(row, inc(), val['priority'])
#        worksheet2.write(row, inc(), val['valid_upto'])
    workbook.save("/paytmcatalog.xls")
def add_headers(worksheet, worksheet2):
    global i
    i=-1
    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(), 'name')
    worksheet.write(0, inc(), 'actual price')
    worksheet.write(0, inc(), 'box price')
    worksheet.write(0,inc(), 'lowest price')
    worksheet.write(0,inc(), 'promo text')
    worksheet.write(0,inc(), 'promocodes')
    i=-1
    worksheet2.write(0, inc(), 'productid')
    worksheet2.write(0, inc(), 'code')
    worksheet2.write(0, inc(), 'offer_text')
    worksheet2.write(0, inc(), 'terms')
    worksheet2.write(0, inc(), 'priority')
    worksheet2.write(0, inc(), 'valid_upto')


def inc():
    global i
    i+=1
    return i
        
    
def main():
    scrapepaytm()
    #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()