Subversion Repositories SmartDukaan

Rev

Rev 14879 | 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)
16380 amit.gupta 77
            if prod.get("error"):
78
                continue
14875 amit.gupta 79
            offerUrl = OFFER_DETAIL_API%(prod['parent_id'])
80
            response = fetchResponseUsingProxy(offerUrl)
14879 amit.gupta 81
            row += 1
14875 amit.gupta 82
            offers = json.loads(response)
83
            codes=[]
84
            for code in offers.get("codes"):
85
                offercode = code.get("code")
86
                row2 += 1
87
                i =-1
88
                worksheet2.write(row2, inc(), prod['parent_id'])
89
                worksheet2.write(row2, inc(), offercode)
90
                worksheet2.write(row2, inc(), code['offerText'])
91
                worksheet2.write(row2, inc(), code['terms'])
92
                worksheet2.write(row2, inc(), code['priority'])
93
                worksheet2.write(row2, inc(), code['valid_upto'])
94
                codes.append(offercode)
95
                if not offermap.has_key(offercode): 
96
                    offermap[offercode] = code
97
 
98
 
99
            allCodes = ",".join(codes)
100
            i=-1
14844 amit.gupta 101
            worksheet.write(row, inc(), prod["product_id"])
102
            worksheet.write(row, inc(), prod['parent_id'])
103
            worksheet.write(row, inc(), prod['instock'])
104
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
14878 amit.gupta 105
            worksheet.write(row, inc(), prod['name'])
14844 amit.gupta 106
            worksheet.write(row, inc(), prod['actual_price'])
107
            worksheet.write(row, inc(), prod['offer_price'])
108
            sellerValues = prod['other_sellers'].get("values")
14875 amit.gupta 109
            if sellerValues is not None and len(sellerValues) > 0:
110
                lowestPrice = sellerValues[0]['offer_price']
111
                if lowestPrice >= prod['offer_price']:
112
                    worksheet.write(row, inc(), prod['offer_price'])
113
                else:
114
                    worksheet.write(row, inc(), lowestPrice)
14844 amit.gupta 115
            else:
14875 amit.gupta 116
                worksheet.write(row, inc(), "")
117
 
118
            worksheet.write(row, inc(), prod['promo_text'])
119
            worksheet.write(row, inc(), allCodes)
14844 amit.gupta 120
        except:
14875 amit.gupta 121
            traceback.print_exc()
122
 
123
    row = 0
124
#    for key, val in offermap.iteritems():
125
#        row += 1
126
#        i =-1
127
#        worksheet2.write(row, inc(), key)
128
#        worksheet2.write(row, inc(), val['offerText'])
129
#        worksheet2.write(row, inc(), val['terms'])
130
#        worksheet2.write(row, inc(), val['priority'])
131
#        worksheet2.write(row, inc(), val['valid_upto'])
14877 amit.gupta 132
    workbook.save("/paytmcatalog.xls")
14875 amit.gupta 133
def add_headers(worksheet, worksheet2):
14844 amit.gupta 134
    global i
14875 amit.gupta 135
    i=-1
14844 amit.gupta 136
    worksheet.write(0, inc(), 'productid')
137
    worksheet.write(0, inc(), 'parentid')
138
    worksheet.write(0, inc(), 'instock')
139
    worksheet.write(0, inc(), 'cod supported')
14875 amit.gupta 140
    worksheet.write(0, inc(), 'name')
14844 amit.gupta 141
    worksheet.write(0, inc(), 'actual price')
142
    worksheet.write(0, inc(), 'box price')
143
    worksheet.write(0,inc(), 'lowest price')
144
    worksheet.write(0,inc(), 'promo text')
14875 amit.gupta 145
    worksheet.write(0,inc(), 'promocodes')
146
    i=-1
147
    worksheet2.write(0, inc(), 'productid')
148
    worksheet2.write(0, inc(), 'code')
149
    worksheet2.write(0, inc(), 'offer_text')
150
    worksheet2.write(0, inc(), 'terms')
151
    worksheet2.write(0, inc(), 'priority')
152
    worksheet2.write(0, inc(), 'valid_upto')
14844 amit.gupta 153
 
14875 amit.gupta 154
 
155
def inc():
156
    global i
157
    i+=1
158
    return i
14844 amit.gupta 159
 
160
 
161
def main():
14875 amit.gupta 162
    scrapepaytm()
163
    #refunds = db.refund.findOne({"batch":1428949802})
14844 amit.gupta 164
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
14875 amit.gupta 165
    #for ref in refunds:
166
        #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 167
 
168
if __name__ == '__main__':
169
    main()