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, \incfrom dtr.reports.usersegmentation import TMP_FILEfrom dtr.sources.spice import todictfrom dtr.utils.utils import fetchResponseUsingProxy, sendNotificationfrom email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom pymongo.mongo_client import MongoClientimport jsonimport smtplibimport xlwtPRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"client = MongoClient('mongodb://localhost:27017/')boldStyle = xlwt.XFStyle()f = xlwt.Font()f.bold = TrueboldStyle.font = fdef sendmail(email, message, fileName, title):if email == "":returnmailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)mailServer.ehlo()mailServer.starttls()mailServer.ehlo()# Create the container (outer) email message.msg = MIMEMultipart()msg['Subject'] = titlemsg.preamble = titlehtml_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 = emailmailServer.login(SENDER, PASSWORD)mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())def scrapepaytm():db = client.Catalogworkbook = xlwt.Workbook()worksheet = workbook.add_sheet("Sample")add_headers(worksheet)row = 0for sku in db.MasterData.find({"source":"paytm.com"}):try:row += 1prodUrl = 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:passworkbook.save("/home/amit/paytmcatalog")def add_headers(worksheet):global ii=0worksheet.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.Dtrrefunds = 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()