Subversion Repositories SmartDukaan

Rev

Rev 14878 | Go to most recent revision | Details | Compare with Previous | 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
'''
14875 amit.gupta 6
from dtr.reports.affiliatereco import SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD
14844 amit.gupta 7
from dtr.reports.usersegmentation import TMP_FILE
8
from dtr.sources.spice import todict
9
from dtr.utils.utils import fetchResponseUsingProxy, sendNotification
10
from email import encoders
11
from email.mime.base import MIMEBase
12
from email.mime.multipart import MIMEMultipart
13
from email.mime.text import MIMEText
14
from pymongo.mongo_client import MongoClient
15
import json
16
import smtplib
14875 amit.gupta 17
import traceback
14844 amit.gupta 18
import xlwt
19
 
20
 
21
 
22
 
23
PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
14875 amit.gupta 24
OFFER_DETAIL_API = "https://paytm.com/papi/v1/promosearch/product/%s/offers"
14844 amit.gupta 25
client = MongoClient('mongodb://localhost:27017/')
26
 
27
boldStyle = xlwt.XFStyle()
28
f = xlwt.Font()
29
f.bold = True
30
boldStyle.font = f
31
 
32
 
33
def sendmail(email, message, fileName, title):
34
    if email == "":
35
        return
36
    mailServer = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
37
    mailServer.ehlo()
38
    mailServer.starttls()
39
    mailServer.ehlo()
40
 
41
    # Create the container (outer) email message.
42
    msg = MIMEMultipart()
43
    msg['Subject'] = title
44
    msg.preamble = title
45
    html_msg = MIMEText(message, 'html')
46
    msg.attach(html_msg)
47
 
48
    fileMsg = MIMEBase('application', 'vnd.ms-excel')
49
    fileMsg.set_payload(file(TMP_FILE).read())
50
    encoders.encode_base64(fileMsg)
51
    fileMsg.add_header('Content-Disposition', 'attachment;filename=' + fileName)
52
    msg.attach(fileMsg)
53
 
54
 
55
    email.append('amit.gupta@shop2020.in')
56
    MAILTO = email 
57
    mailServer.login(SENDER, PASSWORD)
58
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
59
 
60
def scrapepaytm():
14875 amit.gupta 61
    global i
14844 amit.gupta 62
    db = client.Catalog
63
    workbook = xlwt.Workbook()
64
    worksheet = workbook.add_sheet("Sample")
14875 amit.gupta 65
    worksheet2 = workbook.add_sheet("Codes")
66
    add_headers(worksheet, worksheet2)
14844 amit.gupta 67
    row = 0
14875 amit.gupta 68
    row2 = 0
69
    offermap = {}
14844 amit.gupta 70
    for sku in db.MasterData.find({"source":"paytm.com"}):
71
        try:
72
            prodUrl = sku.get("url")
73
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
74
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
75
            response = fetchResponseUsingProxy(apiUrl)
76
            prod = json.loads(response)
14875 amit.gupta 77
            offerUrl = OFFER_DETAIL_API%(prod['parent_id'])
78
            response = fetchResponseUsingProxy(offerUrl)
14879 amit.gupta 79
            row += 1
14875 amit.gupta 80
            offers = json.loads(response)
81
            codes=[]
82
            for code in offers.get("codes"):
83
                offercode = code.get("code")
84
                row2 += 1
85
                i =-1
86
                worksheet2.write(row2, inc(), prod['parent_id'])
87
                worksheet2.write(row2, inc(), offercode)
88
                worksheet2.write(row2, inc(), code['offerText'])
89
                worksheet2.write(row2, inc(), code['terms'])
90
                worksheet2.write(row2, inc(), code['priority'])
91
                worksheet2.write(row2, inc(), code['valid_upto'])
92
                codes.append(offercode)
93
                if not offermap.has_key(offercode): 
94
                    offermap[offercode] = code
95
 
96
 
97
            allCodes = ",".join(codes)
98
            i=-1
14844 amit.gupta 99
            worksheet.write(row, inc(), prod["product_id"])
100
            worksheet.write(row, inc(), prod['parent_id'])
101
            worksheet.write(row, inc(), prod['instock'])
102
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
14878 amit.gupta 103
            worksheet.write(row, inc(), prod['name'])
14844 amit.gupta 104
            worksheet.write(row, inc(), prod['actual_price'])
105
            worksheet.write(row, inc(), prod['offer_price'])
106
            sellerValues = prod['other_sellers'].get("values")
14875 amit.gupta 107
            if sellerValues is not None and len(sellerValues) > 0:
108
                lowestPrice = sellerValues[0]['offer_price']
109
                if lowestPrice >= prod['offer_price']:
110
                    worksheet.write(row, inc(), prod['offer_price'])
111
                else:
112
                    worksheet.write(row, inc(), lowestPrice)
14844 amit.gupta 113
            else:
14875 amit.gupta 114
                worksheet.write(row, inc(), "")
115
 
116
            worksheet.write(row, inc(), prod['promo_text'])
117
            worksheet.write(row, inc(), allCodes)
14844 amit.gupta 118
        except:
14875 amit.gupta 119
            traceback.print_exc()
120
 
121
    row = 0
122
#    for key, val in offermap.iteritems():
123
#        row += 1
124
#        i =-1
125
#        worksheet2.write(row, inc(), key)
126
#        worksheet2.write(row, inc(), val['offerText'])
127
#        worksheet2.write(row, inc(), val['terms'])
128
#        worksheet2.write(row, inc(), val['priority'])
129
#        worksheet2.write(row, inc(), val['valid_upto'])
14877 amit.gupta 130
    workbook.save("/paytmcatalog.xls")
14875 amit.gupta 131
def add_headers(worksheet, worksheet2):
14844 amit.gupta 132
    global i
14875 amit.gupta 133
    i=-1
14844 amit.gupta 134
    worksheet.write(0, inc(), 'productid')
135
    worksheet.write(0, inc(), 'parentid')
136
    worksheet.write(0, inc(), 'instock')
137
    worksheet.write(0, inc(), 'cod supported')
14875 amit.gupta 138
    worksheet.write(0, inc(), 'name')
14844 amit.gupta 139
    worksheet.write(0, inc(), 'actual price')
140
    worksheet.write(0, inc(), 'box price')
141
    worksheet.write(0,inc(), 'lowest price')
142
    worksheet.write(0,inc(), 'promo text')
14875 amit.gupta 143
    worksheet.write(0,inc(), 'promocodes')
144
    i=-1
145
    worksheet2.write(0, inc(), 'productid')
146
    worksheet2.write(0, inc(), 'code')
147
    worksheet2.write(0, inc(), 'offer_text')
148
    worksheet2.write(0, inc(), 'terms')
149
    worksheet2.write(0, inc(), 'priority')
150
    worksheet2.write(0, inc(), 'valid_upto')
14844 amit.gupta 151
 
14875 amit.gupta 152
 
153
def inc():
154
    global i
155
    i+=1
156
    return i
14844 amit.gupta 157
 
158
 
159
def main():
14875 amit.gupta 160
    scrapepaytm()
161
    #refunds = db.refund.findOne({"batch":1428949802})
14844 amit.gupta 162
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
14875 amit.gupta 163
    #for ref in refunds:
164
        #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")))
14844 amit.gupta 165
 
166
if __name__ == '__main__':
167
    main()