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, PASSWORDfrom 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 tracebackimport xlwtPRODUCT_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 = 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():global idb = client.Catalogworkbook = xlwt.Workbook()worksheet = workbook.add_sheet("Sample")worksheet2 = workbook.add_sheet("Codes")add_headers(worksheet, worksheet2)row = 0row2 = 0offermap = {}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"):continueofferUrl = OFFER_DETAIL_API%(prod['parent_id'])response = fetchResponseUsingProxy(offerUrl)row += 1offers = json.loads(response)codes=[]for code in offers.get("codes"):offercode = code.get("code")row2 += 1i =-1worksheet2.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] = codeallCodes = ",".join(codes)i=-1worksheet.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 ii=-1worksheet.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=-1worksheet2.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 ii+=1return idef 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()