| 16381 |
amit.gupta |
1 |
from BeautifulSoup import BeautifulSoup
|
|
|
2 |
from datetime import datetime
|
|
|
3 |
from dtr.utils.utils import to_java_date, fetchResponseUsingProxy
|
|
|
4 |
from email.mime.multipart import MIMEMultipart
|
|
|
5 |
from email.mime.text import MIMEText
|
|
|
6 |
import json
|
|
|
7 |
import optparse
|
|
|
8 |
import pymongo
|
|
|
9 |
import re
|
|
|
10 |
import smtplib
|
|
|
11 |
import urllib2
|
| 16512 |
kshitij.so |
12 |
from dtr.utils.PaytmOfferScraper import fetchOffers
|
| 21135 |
kshitij.so |
13 |
from shop2020.utils.EmailAttachmentSender import get_attachment_part
|
|
|
14 |
from shop2020.utils import EmailAttachmentSender
|
| 16381 |
amit.gupta |
15 |
|
|
|
16 |
con = None
|
|
|
17 |
parser = optparse.OptionParser()
|
|
|
18 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
19 |
default="localhost",
|
|
|
20 |
type="string", help="The HOST where the mongo server is running",
|
|
|
21 |
metavar="mongo_host")
|
|
|
22 |
|
|
|
23 |
(options, args) = parser.parse_args()
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
exceptionList = []
|
|
|
27 |
bestSellers = []
|
|
|
28 |
now = datetime.now()
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
class __RankInfo:
|
|
|
32 |
|
| 16512 |
kshitij.so |
33 |
def __init__(self, identifier, rank, category, available_price, gross_price, in_stock, coupon, thumbnail, source_product_name, marketPlaceUrl, cod):
|
| 16381 |
amit.gupta |
34 |
self.identifier = identifier
|
|
|
35 |
self.rank = rank
|
|
|
36 |
self.category = category
|
| 16512 |
kshitij.so |
37 |
self.available_price = available_price
|
|
|
38 |
self.gross_price = gross_price
|
|
|
39 |
self.in_stock = in_stock
|
|
|
40 |
self.coupon = coupon
|
|
|
41 |
self.thumbnail = thumbnail
|
|
|
42 |
self.source_product_name = source_product_name
|
|
|
43 |
self.marketPlaceUrl = marketPlaceUrl
|
|
|
44 |
self.cod = cod
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
|
| 16381 |
amit.gupta |
48 |
|
|
|
49 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
|
|
50 |
global con
|
|
|
51 |
if con is None:
|
|
|
52 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
53 |
try:
|
|
|
54 |
con = pymongo.MongoClient(host, port)
|
|
|
55 |
except Exception, e:
|
|
|
56 |
print e
|
|
|
57 |
return None
|
|
|
58 |
return con
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
|
62 |
def scrapeBestSellerMobiles():
|
|
|
63 |
global bestSellers
|
|
|
64 |
rank = 0
|
|
|
65 |
mobileCategoryUrl = 'https://catalog.paytm.com/v1/g/electronics/mobile-accessories/mobiles?page_count=%d&items_per_page=25&sort_popular=1&cat_tree=1'
|
|
|
66 |
for i in range(1,5):
|
|
|
67 |
data = fetchResponseUsingProxy(mobileCategoryUrl%(i))
|
|
|
68 |
jsonResponse = json.loads(data)
|
|
|
69 |
for jsonProduct in jsonResponse['grid_layout']:
|
|
|
70 |
rank = rank + 1
|
| 17293 |
kshitij.so |
71 |
identifier = str(jsonProduct['complex_product_id'])
|
| 16513 |
kshitij.so |
72 |
r_info = __RankInfo(identifier,rank, None, None,None,None,None,None,None,None,None)
|
| 16381 |
amit.gupta |
73 |
print rank, identifier
|
|
|
74 |
bestSellers.append(r_info)
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
def commitBestSellers(category):
|
|
|
78 |
global exceptionList
|
|
|
79 |
print "Rank",
|
|
|
80 |
print '\t',
|
|
|
81 |
print 'Identifier'
|
|
|
82 |
for x in bestSellers:
|
|
|
83 |
print x.rank,
|
|
|
84 |
print '\t',
|
|
|
85 |
print x.identifier,
|
| 16472 |
kshitij.so |
86 |
print '\t',
|
|
|
87 |
col = list(get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier, 'source_id':6}))
|
|
|
88 |
print "count sku",len(col)
|
|
|
89 |
print '\n'
|
|
|
90 |
if len(col) == 0:
|
| 16381 |
amit.gupta |
91 |
x.category = category
|
|
|
92 |
exceptionList.append(x)
|
|
|
93 |
else:
|
|
|
94 |
get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier, 'source_id':6 }, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}})
|
|
|
95 |
|
|
|
96 |
def scrapeBestSellerTablets():
|
|
|
97 |
global bestSellers
|
| 16464 |
kshitij.so |
98 |
bestSellers = []
|
| 16381 |
amit.gupta |
99 |
rank = 0
|
|
|
100 |
bestSellerTabletsUrl = 'https://catalog.paytm.com/v1/g/electronics/mobile-accessories/headsets?page_count=%d&items_per_page=25&sort_popular=1'
|
|
|
101 |
for i in range(1,5):
|
|
|
102 |
data = fetchResponseUsingProxy(bestSellerTabletsUrl%(i))
|
|
|
103 |
jsonResponse = json.loads(data)
|
|
|
104 |
for jsonProduct in jsonResponse['grid_layout']:
|
|
|
105 |
rank = rank + 1
|
| 18251 |
kshitij.so |
106 |
identifier = str(jsonProduct['complex_product_id'])
|
| 16513 |
kshitij.so |
107 |
r_info = __RankInfo(identifier,rank, None, None,None,None,None,None,None,None,None)
|
| 16381 |
amit.gupta |
108 |
print rank, identifier
|
|
|
109 |
bestSellers.append(r_info)
|
|
|
110 |
|
|
|
111 |
def resetRanks(category_id):
|
|
|
112 |
get_mongo_connection().Catalog.MasterData.update({'rank':{'$gt':0},'source_id':6,'category_id':category_id}, {'$set':{'rank':0}}, upsert=False, multi=True)
|
|
|
113 |
|
|
|
114 |
def sendEmail():
|
|
|
115 |
message="""<html>
|
|
|
116 |
<body>
|
|
|
117 |
<h3>PayTM Best Sellers not in master</h3>
|
|
|
118 |
<table border="1" style="width:100%;">
|
|
|
119 |
<thead>
|
|
|
120 |
<tr><th>Identifier</th>
|
|
|
121 |
<th>Category</th>
|
|
|
122 |
<th>Rank</th>
|
| 16512 |
kshitij.so |
123 |
<th>Available_price</th>
|
|
|
124 |
<th>Gross_price</th>
|
|
|
125 |
<th>In_stock</th>
|
|
|
126 |
<th>Coupon</th>
|
|
|
127 |
<th>Thumbnail</th>
|
|
|
128 |
<th>Source_product_name</th>
|
|
|
129 |
<th>MarketPlaceUrl</th>
|
|
|
130 |
<th>Cod</th>
|
| 16381 |
amit.gupta |
131 |
</tr></thead>
|
|
|
132 |
<tbody>"""
|
|
|
133 |
for item in exceptionList:
|
| 16518 |
kshitij.so |
134 |
try:
|
|
|
135 |
message+="""<tr>
|
|
|
136 |
<td style="text-align:center">"""+(item.identifier)+"""</td>
|
|
|
137 |
<td style="text-align:center">"""+(item.category)+"""</td>
|
|
|
138 |
<td style="text-align:center">"""+str(item.rank)+"""</td>
|
|
|
139 |
<td style="text-align:center">"""+str(item.available_price)+"""</td>
|
|
|
140 |
<td style="text-align:center">"""+str(item.gross_price)+"""</td>
|
|
|
141 |
<td style="text-align:center">"""+str(item.in_stock)+"""</td>
|
|
|
142 |
<td style="text-align:center">"""+str(item.coupon)+"""</td>
|
|
|
143 |
<td style="text-align:center">"""+str(item.thumbnail)+"""</td>
|
|
|
144 |
<td style="text-align:center">"""+str(item.source_product_name)+"""</td>
|
|
|
145 |
<td style="text-align:center">"""+str(item.marketPlaceUrl)+"""</td>
|
|
|
146 |
<td style="text-align:center">"""+str(item.cod)+"""</td>
|
|
|
147 |
</tr>"""
|
|
|
148 |
except:
|
|
|
149 |
continue
|
| 16381 |
amit.gupta |
150 |
message+="""</tbody></table></body></html>"""
|
|
|
151 |
print message
|
|
|
152 |
#recipients = ['amit.gupta@saholic.com']
|
| 21135 |
kshitij.so |
153 |
recipients = ['kshitij.sood@saholic.com','ritesh.chauhan@saholic.com','aishwarya.singh@saholic.com']
|
| 23839 |
amit.gupta |
154 |
EmailAttachmentSender.mail_send_grid("dtr@smartdukaan.com","apikey", "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", recipients, "Paytm Best Sellers",message ,[],[],[])
|
| 16381 |
amit.gupta |
155 |
|
| 21135 |
kshitij.so |
156 |
|
| 16512 |
kshitij.so |
157 |
|
|
|
158 |
def addExtraAttributes():
|
|
|
159 |
for e in exceptionList:
|
|
|
160 |
url = "https://catalog.paytm.com/v1/mobile/product/%s" %(e.identifier.strip())
|
|
|
161 |
try:
|
|
|
162 |
response_data = fetchResponseUsingProxy(url, proxy=False)
|
|
|
163 |
except:
|
|
|
164 |
continue
|
|
|
165 |
input_json = json.loads(response_data)
|
|
|
166 |
offerPrice = float(input_json['offer_price'])
|
|
|
167 |
e.cod = int(input_json['pay_type_supported'].get('COD'))
|
|
|
168 |
offerUrl = (input_json['offer_url'])
|
|
|
169 |
e.in_stock = (input_json['instock'])
|
|
|
170 |
try:
|
|
|
171 |
offers = fetchOffers(offerUrl)
|
|
|
172 |
except:
|
|
|
173 |
continue
|
|
|
174 |
bestOffer = {}
|
| 16514 |
kshitij.so |
175 |
e.gross_price = float(offerPrice)
|
| 16512 |
kshitij.so |
176 |
effective_price = offerPrice
|
|
|
177 |
coupon = ""
|
|
|
178 |
for offer_data in offers.get('codes'):
|
|
|
179 |
if effective_price > offer_data.get('effective_price'):
|
|
|
180 |
effective_price = offer_data.get('effective_price')
|
|
|
181 |
bestOffer = offer_data
|
|
|
182 |
coupon = bestOffer.get('code')
|
|
|
183 |
e.source_product_name = input_json['name']
|
|
|
184 |
e.thumbnail = input_json['thumbnail']
|
|
|
185 |
e.marketPlaceUrl = input_json['shareurl']
|
|
|
186 |
e.coupon = coupon
|
| 16514 |
kshitij.so |
187 |
e.available_price = float(effective_price)
|
| 16512 |
kshitij.so |
188 |
|
| 16381 |
amit.gupta |
189 |
|
|
|
190 |
|
|
|
191 |
|
|
|
192 |
def main():
|
|
|
193 |
scrapeBestSellerMobiles()
|
|
|
194 |
if len(bestSellers) > 0:
|
| 16464 |
kshitij.so |
195 |
resetRanks(3)
|
| 16381 |
amit.gupta |
196 |
commitBestSellers("MOBILE")
|
|
|
197 |
scrapeBestSellerTablets()
|
|
|
198 |
if len(bestSellers) > 0:
|
|
|
199 |
resetRanks(5)
|
|
|
200 |
commitBestSellers("TABLET")
|
| 16512 |
kshitij.so |
201 |
addExtraAttributes()
|
| 16381 |
amit.gupta |
202 |
sendEmail()
|
|
|
203 |
|
|
|
204 |
if __name__=='__main__':
|
|
|
205 |
main()
|