Subversion Repositories SmartDukaan

Rev

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

Rev 16472 Rev 16512
Line 8... Line 8...
8
import optparse
8
import optparse
9
import pymongo
9
import pymongo
10
import re
10
import re
11
import smtplib
11
import smtplib
12
import urllib2
12
import urllib2
-
 
13
from dtr.utils.PaytmOfferScraper import fetchOffers
13
 
14
 
14
con = None
15
con = None
15
parser = optparse.OptionParser()
16
parser = optparse.OptionParser()
16
parser.add_option("-m", "--m", dest="mongoHost",
17
parser.add_option("-m", "--m", dest="mongoHost",
17
                      default="localhost",
18
                      default="localhost",
Line 26... Line 27...
26
now = datetime.now()
27
now = datetime.now()
27
 
28
 
28
 
29
 
29
class __RankInfo:
30
class __RankInfo:
30
    
31
    
31
    def __init__(self, identifier, rank, category):
32
    def __init__(self, identifier, rank, category, available_price, gross_price, in_stock, coupon, thumbnail, source_product_name, marketPlaceUrl, cod):
32
        self.identifier = identifier
33
        self.identifier = identifier
33
        self.rank  = rank
34
        self.rank  = rank
34
        self.category = category
35
        self.category = category
-
 
36
        self.available_price = available_price
-
 
37
        self.gross_price = gross_price
-
 
38
        self.in_stock = in_stock
-
 
39
        self.coupon = coupon
-
 
40
        self.thumbnail = thumbnail
-
 
41
        self.source_product_name = source_product_name
-
 
42
        self.marketPlaceUrl = marketPlaceUrl
-
 
43
        self.cod = cod
-
 
44
        
-
 
45
        
-
 
46
        
35
 
47
 
36
def get_mongo_connection(host=options.mongoHost, port=27017):
48
def get_mongo_connection(host=options.mongoHost, port=27017):
37
    global con
49
    global con
38
    if con is None:
50
    if con is None:
39
        print "Establishing connection %s host and port %d" %(host,port)
51
        print "Establishing connection %s host and port %d" %(host,port)
Line 105... Line 117...
105
            <table border="1" style="width:100%;">
117
            <table border="1" style="width:100%;">
106
            <thead>
118
            <thead>
107
            <tr><th>Identifier</th>
119
            <tr><th>Identifier</th>
108
            <th>Category</th>
120
            <th>Category</th>
109
            <th>Rank</th>
121
            <th>Rank</th>
-
 
122
            <th>Available_price</th>
-
 
123
            <th>Gross_price</th>
-
 
124
            <th>In_stock</th>
-
 
125
            <th>Coupon</th>
-
 
126
            <th>Thumbnail</th>
-
 
127
            <th>Source_product_name</th>
-
 
128
            <th>MarketPlaceUrl</th>
-
 
129
            <th>Cod</th>
110
            </tr></thead>
130
            </tr></thead>
111
            <tbody>"""
131
            <tbody>"""
112
    for item in exceptionList:
132
    for item in exceptionList:
113
        message+="""<tr>
133
        message+="""<tr>
114
        <td style="text-align:center">"""+(item.identifier)+"""</td>
134
        <td style="text-align:center">"""+(item.identifier)+"""</td>
115
        <td style="text-align:center">"""+(item.category)+"""</td>
135
        <td style="text-align:center">"""+(item.category)+"""</td>
116
        <td style="text-align:center">"""+str(item.rank)+"""</td>
136
        <td style="text-align:center">"""+str(item.rank)+"""</td>
-
 
137
        <td style="text-align:center">"""+str(item.available_price)+"""</td>
-
 
138
        <td style="text-align:center">"""+str(item.gross_price)+"""</td>
-
 
139
        <td style="text-align:center">"""+str(item.in_stock)+"""</td>
-
 
140
        <td style="text-align:center">"""+str(item.coupon)+"""</td>
-
 
141
        <td style="text-align:center">"""+str(item.thumbnail)+"""</td>
-
 
142
        <td style="text-align:center">"""+str(item.source_product_name)+"""</td>
-
 
143
        <td style="text-align:center">"""+str(item.marketPlaceUrl)+"""</td>
-
 
144
        <td style="text-align:center">"""+str(item.cod)+"""</td>
117
        </tr>"""
145
        </tr>"""
118
    message+="""</tbody></table></body></html>"""
146
    message+="""</tbody></table></body></html>"""
119
    print message
147
    print message
120
    #recipients = ['amit.gupta@saholic.com']
148
    #recipients = ['amit.gupta@saholic.com']
121
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com','amit.gupta@saholic.com']
149
    recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com','amit.gupta@saholic.com']
Line 133... Line 161...
133
    try:
161
    try:
134
        smtpServer.sendmail(sender, recipients, msg.as_string())
162
        smtpServer.sendmail(sender, recipients, msg.as_string())
135
        print "Successfully sent email"
163
        print "Successfully sent email"
136
    except:
164
    except:
137
        print "Error: unable to send email."
165
        print "Error: unable to send email."
-
 
166
 
-
 
167
def addExtraAttributes():
-
 
168
    for e in exceptionList:
-
 
169
        url = "https://catalog.paytm.com/v1/mobile/product/%s" %(e.identifier.strip())
-
 
170
        try:
-
 
171
            response_data = fetchResponseUsingProxy(url,  proxy=False)
-
 
172
        except:
-
 
173
            continue
-
 
174
        input_json = json.loads(response_data)
-
 
175
        offerPrice = float(input_json['offer_price'])
-
 
176
        e.cod = int(input_json['pay_type_supported'].get('COD'))
-
 
177
        offerUrl = (input_json['offer_url'])
-
 
178
        e.in_stock = (input_json['instock'])
-
 
179
        try:
-
 
180
            offers = fetchOffers(offerUrl)
-
 
181
        except:
-
 
182
            continue
-
 
183
        bestOffer = {}
-
 
184
        e.gross_price = offerPrice
-
 
185
        effective_price = offerPrice
-
 
186
        coupon = ""
-
 
187
        for offer_data in offers.get('codes'):
-
 
188
            if effective_price > offer_data.get('effective_price'):
-
 
189
                effective_price = offer_data.get('effective_price')
-
 
190
                bestOffer = offer_data
-
 
191
                coupon = bestOffer.get('code')
-
 
192
        e.source_product_name = input_json['name']
-
 
193
        e.thumbnail = input_json['thumbnail']
-
 
194
        e.marketPlaceUrl = input_json['shareurl']
-
 
195
        e.coupon = coupon
-
 
196
        e.available_price = effective_price
-
 
197
        
138
            
198
            
139
            
199
            
140
            
200
            
141
def main():
201
def main():
142
    scrapeBestSellerMobiles()
202
    scrapeBestSellerMobiles()
Line 145... Line 205...
145
        commitBestSellers("MOBILE")
205
        commitBestSellers("MOBILE")
146
    scrapeBestSellerTablets()
206
    scrapeBestSellerTablets()
147
    if len(bestSellers) > 0:
207
    if len(bestSellers) > 0:
148
        resetRanks(5)
208
        resetRanks(5)
149
        commitBestSellers("TABLET")
209
        commitBestSellers("TABLET")
-
 
210
    addExtraAttributes()
150
    sendEmail()
211
    sendEmail()
151
        
212
        
152
if __name__=='__main__':
213
if __name__=='__main__':
153
    main()
214
    main()
154
215