Subversion Repositories SmartDukaan

Rev

Rev 14844 | Rev 14877 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14844 Rev 14875
Line 1... Line 1...
1
'''
1
'''
2
Created on Apr 15, 2015
2
Created on Apr 15, 2015
3
 
3
 
4
@author: amit
4
@author: amit
5
'''
5
'''
6
from dtr.reports.affiliatereco import SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD, \
6
from dtr.reports.affiliatereco import SMTP_SERVER, SMTP_PORT, SENDER, PASSWORD
7
    inc
-
 
8
from dtr.reports.usersegmentation import TMP_FILE
7
from dtr.reports.usersegmentation import TMP_FILE
9
from dtr.sources.spice import todict
8
from dtr.sources.spice import todict
10
from dtr.utils.utils import fetchResponseUsingProxy, sendNotification
9
from dtr.utils.utils import fetchResponseUsingProxy, sendNotification
11
from email import encoders
10
from email import encoders
12
from email.mime.base import MIMEBase
11
from email.mime.base import MIMEBase
13
from email.mime.multipart import MIMEMultipart
12
from email.mime.multipart import MIMEMultipart
14
from email.mime.text import MIMEText
13
from email.mime.text import MIMEText
15
from pymongo.mongo_client import MongoClient
14
from pymongo.mongo_client import MongoClient
16
import json
15
import json
17
import smtplib
16
import smtplib
-
 
17
import traceback
18
import xlwt
18
import xlwt
19
 
19
 
20
 
20
 
21
 
21
 
22
 
22
 
23
PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
23
PRODUCT_DETAIL_API = "https://catalog.paytm.com/v1/p/%s"
-
 
24
OFFER_DETAIL_API = "https://paytm.com/papi/v1/promosearch/product/%s/offers"
24
client = MongoClient('mongodb://localhost:27017/')
25
client = MongoClient('mongodb://localhost:27017/')
25
 
26
 
26
boldStyle = xlwt.XFStyle()
27
boldStyle = xlwt.XFStyle()
27
f = xlwt.Font()
28
f = xlwt.Font()
28
f.bold = True
29
f.bold = True
Line 55... Line 56...
55
    MAILTO = email 
56
    MAILTO = email 
56
    mailServer.login(SENDER, PASSWORD)
57
    mailServer.login(SENDER, PASSWORD)
57
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
58
    mailServer.sendmail(PASSWORD, MAILTO, msg.as_string())
58
    
59
    
59
def scrapepaytm():
60
def scrapepaytm():
-
 
61
    global i
60
    db = client.Catalog
62
    db = client.Catalog
61
    workbook = xlwt.Workbook()
63
    workbook = xlwt.Workbook()
62
    worksheet = workbook.add_sheet("Sample")
64
    worksheet = workbook.add_sheet("Sample")
-
 
65
    worksheet2 = workbook.add_sheet("Codes")
63
    add_headers(worksheet)
66
    add_headers(worksheet, worksheet2)
64
    row = 0
67
    row = 0
-
 
68
    row2 = 0
-
 
69
    offermap = {}
65
    for sku in db.MasterData.find({"source":"paytm.com"}):
70
    for sku in db.MasterData.find({"source":"paytm.com"}):
66
        try:
71
        try:
67
            row += 1
72
            if row==11:
-
 
73
                break
68
            prodUrl = sku.get("url")
74
            prodUrl = sku.get("url")
69
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
75
            prodIdentifier = prodUrl.split("?")[0].split("/")[-1]
70
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
76
            apiUrl = PRODUCT_DETAIL_API%(prodIdentifier)
-
 
77
            row += 1
71
            response = fetchResponseUsingProxy(apiUrl)
78
            response = fetchResponseUsingProxy(apiUrl)
72
            prod = json.loads(response)
79
            prod = json.loads(response)
-
 
80
            offerUrl = OFFER_DETAIL_API%(prod['parent_id'])
-
 
81
            response = fetchResponseUsingProxy(offerUrl)
-
 
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
73
            worksheet.write(row, inc(), prod["product_id"])
101
            worksheet.write(row, inc(), prod["product_id"])
74
            worksheet.write(row, inc(), prod['parent_id'])
102
            worksheet.write(row, inc(), prod['parent_id'])
75
            worksheet.write(row, inc(), prod['instock'])
103
            worksheet.write(row, inc(), prod['instock'])
76
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
104
            worksheet.write(row, inc(), prod['pay_type_supported']['COD'])
77
            worksheet.write(row, inc(), prod['productName'])
105
            worksheet.write(row, inc(), prod['productName'])
78
            worksheet.write(row, inc(), prod['actual_price'])
106
            worksheet.write(row, inc(), prod['actual_price'])
79
            worksheet.write(row, inc(), prod['offer_price'])
107
            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")
108
            sellerValues = prod['other_sellers'].get("values")
83
            if sellerValues > 0 and len(sellerValues) > 0:
109
            if sellerValues is not None and len(sellerValues) > 0:
-
 
110
                lowestPrice = sellerValues[0]['offer_price']
-
 
111
                if lowestPrice >= prod['offer_price']:
84
                worksheet.write(row, inc(), sellerValues[0]['offer_price'])
112
                    worksheet.write(row, inc(), prod['offer_price'])
-
 
113
                else:
-
 
114
                    worksheet.write(row, inc(), lowestPrice)
85
            else:
115
            else:
86
                worksheet.write(row, inc(), None)
116
                worksheet.write(row, inc(), "")
-
 
117
 
-
 
118
            worksheet.write(row, inc(), prod['promo_text'])
-
 
119
            worksheet.write(row, inc(), allCodes)
87
        except:
120
        except:
-
 
121
            traceback.print_exc()
-
 
122
        
-
 
123
    row = 0
-
 
124
#    for key, val in offermap.iteritems():
-
 
125
#        row += 1
88
            pass
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'])
89
    workbook.save("/home/amit/paytmcatalog")
132
    workbook.save("/home/amit/paytmcatalog.xls")
90
def add_headers(worksheet):
133
def add_headers(worksheet, worksheet2):
91
    global i
134
    global i
92
    i=0
135
    i=-1
93
    worksheet.write(0, inc(), 'productid')
136
    worksheet.write(0, inc(), 'productid')
94
    worksheet.write(0, inc(), 'parentid')
137
    worksheet.write(0, inc(), 'parentid')
95
    worksheet.write(0, inc(), 'instock')
138
    worksheet.write(0, inc(), 'instock')
96
    worksheet.write(0, inc(), 'cod supported')
139
    worksheet.write(0, inc(), 'cod supported')
97
    worksheet.write(0, inc(), 'title')
140
    worksheet.write(0, inc(), 'name')
98
    worksheet.write(0, inc(), 'actual price')
141
    worksheet.write(0, inc(), 'actual price')
99
    worksheet.write(0, inc(), 'box price')
142
    worksheet.write(0, inc(), 'box price')
100
    worksheet.write(0,inc(), 'lowest price')
143
    worksheet.write(0,inc(), 'lowest price')
101
    worksheet.write(0,inc(), 'promo code')
-
 
102
    worksheet.write(0,inc(), 'promo text')
144
    worksheet.write(0,inc(), 'promo text')
-
 
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')
103
    worksheet.write(0, inc(), 'image Url')
150
    worksheet2.write(0, inc(), 'terms')
-
 
151
    worksheet2.write(0, inc(), 'priority')
-
 
152
    worksheet2.write(0, inc(), 'valid_upto')
104
 
153
 
-
 
154
 
-
 
155
def inc():
-
 
156
    global i
-
 
157
    i+=1
-
 
158
    return i
105
        
159
        
106
    
160
    
107
def main():
161
def main():
108
    db = client.Dtr
162
    scrapepaytm()
109
    refunds = db.refund.findOne({"batch":1428949802})
163
    #refunds = db.refund.findOne({"batch":1428949802})
110
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
164
    #refunds  = [{"timestamp" : "2015-04-14 00:00:02", "userId" : 2, "batch" : 1428949802, "userAmount" : 847 }]
111
    for ref in refunds:
165
    #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")))
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")))
113
 
167
 
114
if __name__ == '__main__':
168
if __name__ == '__main__':
115
    main()
169
    main()