Subversion Repositories SmartDukaan

Rev

Rev 14875 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14844 amit.gupta 1
'''
2
Created on Apr 15, 2015
3
 
4
@author: amit
5
'''
6
from dtr.reports.affiliatereco import SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD, \
7
    inc
8
from dtr.reports.usersegmentation import TMP_FILE
9
from dtr.sources.spice import todict
10
from dtr.utils.utils import fetchResponseUsingProxy, sendNotification
11
from email import encoders
12
from email.mime.base import MIMEBase
13
from email.mime.multipart import MIMEMultipart
14
from email.mime.text import MIMEText
15
from pymongo.mongo_client import MongoClient
16
import json
17
import smtplib
18
import xlwt
19
 
20
 
21
 
22
 
23
PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
24
client = MongoClient('mongodb://localhost:27017/')
25
 
26
boldStyle = xlwt.XFStyle()
27
f = xlwt.Font()
28
f.bold = True
29
boldStyle.font = f
30
 
31
 
32
def sendmail(email, message, fileName, title):
33
    if email == "":
34
        return
35
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
36
    mailServer.ehlo()
37
    mailServer.starttls()
38
    mailServer.ehlo()
39
 
40
    # Create the container (outer) email message.
41
    msg = MIMEMultipart()
42
    msg['Subject'] = title
43
    msg.preamble = title
44
    html_msg = MIMEText(message, 'html')
45
    msg.attach(html_msg)
46
 
47
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
48
    fileMsg.set_payload(file(TMP_FILE).read())
49
    encoders.encode_base64(fileMsg)
50
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
51
    msg.attach(fileMsg)
52
 
53
 
54
    email.append('amit.gupta@shop2020.in')
55
    MAILTO = email 
56
    mailServer.login(SENDER, PASSWORD)
57
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
58
 
59
def scrapepaytm():
60
    db = client.Catalog
61
    workbook = xlwt.Workbook()
62
    worksheet = workbook.add_sheet("Sample")
63
    add_headers(worksheet)
64
    row = 0
65
    for sku in db.MasterData.find({"source":"paytm.com"}):
66
        try:
67
            row += 1
68
            prodUrl = sku.get("url")
69
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
70
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
71
            response = fetchResponseUsingProxy(apiUrl)
72
            prod = json.loads(response)
73
            worksheet.write(row, inc(), prod["product_id"])
74
            worksheet.write(row, inc(), prod['parent_id'])
75
            worksheet.write(row, inc(), prod['instock'])
76
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
77
            worksheet.write(row, inc(), prod['productName'])
78
            worksheet.write(row, inc(), prod['actual_price'])
79
            worksheet.write(row, inc(), prod['offer_price'])
80
            worksheet.write(row, inc(), prod.get('promo_code'))
81
            worksheet.write(row, inc(), prod['promo_text'])
82
            sellerValues = prod['other_sellers'].get("values")
83
            if sellerValues > 0 and len(sellerValues) > 0:
84
                worksheet.write(row, inc(), sellerValues[0]['offer_price'])
85
            else:
86
                worksheet.write(row, inc(), None)
87
        except:
88
            pass
89
    workbook.save("/home/amit/paytmcatalog")
90
def add_headers(worksheet):
91
    global i
92
    i=0
93
    worksheet.write(0, inc(), 'productid')
94
    worksheet.write(0, inc(), 'parentid')
95
    worksheet.write(0, inc(), 'instock')
96
    worksheet.write(0, inc(), 'cod supported')
97
    worksheet.write(0, inc(), 'title')
98
    worksheet.write(0, inc(), 'actual price')
99
    worksheet.write(0, inc(), 'box price')
100
    worksheet.write(0,inc(), 'lowest price')
101
    worksheet.write(0,inc(), 'promo code')
102
    worksheet.write(0,inc(), 'promo text')
103
    worksheet.write(0, inc(), 'image Url')
104
 
105
 
106
 
107
def main():
108
    db = client.Dtr
109
    refunds = db.refund.findOne({"batch":1428949802})
110
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
111
    for ref in refunds:
112
        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")))
113
 
114
if __name__ == '__main__':
115
    main()