Rev 16472 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from BeautifulSoup import BeautifulSoupfrom datetime import datetimefrom dtr.utils.utils import to_java_date, fetchResponseUsingProxyfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom scripts.AmazonBestSellers import getSoupObjectimport jsonimport optparseimport pymongoimport reimport smtplibimport urllib2con = Noneparser = optparse.OptionParser()parser.add_option("-m", "--m", dest="mongoHost",default="localhost",type="string", help="The HOST where the mongo server is running",metavar="mongo_host")(options, args) = parser.parse_args()exceptionList = []asin_regex = r'/([A-Z0-9]{10})'bestSellers = []now = datetime.now()class __RankInfo:def __init__(self, identifier, rank, category):self.identifier = identifierself.rank = rankself.category = categorydef get_mongo_connection(host=options.mongoHost, port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef scrapeBestSellerMobiles():global bestSellersrank = 0mobileCategoryUrl = 'https://catalog.paytm.com/v1/g/electronics/mobile-accessories/mobiles?page_count=%d&items_per_page=25&sort_popular=1&cat_tree=1'for i in range(1,5):data = fetchResponseUsingProxy(mobileCategoryUrl%(i))jsonResponse = json.loads(data)for jsonProduct in jsonResponse['grid_layout']:rank = rank + 1identifier = jsonProduct["url"].split("?")[0].split("/")[-1]r_info = __RankInfo(identifier,rank, None)print rank, identifierbestSellers.append(r_info)def commitBestSellers(category):global exceptionListprint "Rank",print '\t',print 'Identifier'for x in bestSellers:print x.rank,print '\t',print x.identifier,col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier, 'source_id':6})print "count sku",print '\n',if len(list(col)) == 0:x.category = categoryexceptionList.append(x)else:get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier, 'source_id':6 }, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}})def scrapeBestSellerTablets():global bestSellersrank = 0bestSellerTabletsUrl = 'https://catalog.paytm.com/v1/g/electronics/mobile-accessories/headsets?page_count=%d&items_per_page=25&sort_popular=1'for i in range(1,5):data = fetchResponseUsingProxy(bestSellerTabletsUrl%(i))jsonResponse = json.loads(data)for jsonProduct in jsonResponse['grid_layout']:rank = rank + 1identifier = jsonProduct["url"].split("?")[0].split("/")[-1]r_info = __RankInfo(identifier,rank, None)print rank, identifierbestSellers.append(r_info)def resetRanks(category_id):get_mongo_connection().Catalog.MasterData.update({'rank':{'$gt':0},'source_id':6,'category_id':category_id}, {'$set':{'rank':0}}, upsert=False, multi=True)def sendEmail():message="""<html><body><h3>PayTM Best Sellers not in master</h3><table border="1" style="width:100%;"><thead><tr><th>Identifier</th><th>Category</th><th>Rank</th></tr></thead><tbody>"""for item in exceptionList:message+="""<tr><td style="text-align:center">"""+(item.identifier)+"""</td><td style="text-align:center">"""+(item.category)+"""</td><td style="text-align:center">"""+str(item.rank)+"""</td></tr>"""message+="""</tbody></table></body></html>"""print message#recipients = ['amit.gupta@saholic.com']recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com','chaitnaya.vats@saholic.com','manoj.kumar@saholic.com','amit.gupta@saholic.com']msg = MIMEMultipart()msg['Subject'] = "Amazon Best Sellers" + ' - ' + str(datetime.now())msg['From'] = ""msg['To'] = ",".join(recipients)msg.preamble = "Amazon Best Sellers" + ' - ' + str(datetime.now())html_msg = MIMEText(message, 'html')msg.attach(html_msg)smtpServer = smtplib.SMTP('localhost')smtpServer.set_debuglevel(1)sender = 'dtr@shop2020.in'try:smtpServer.sendmail(sender, recipients, msg.as_string())print "Successfully sent email"except:print "Error: unable to send email."def main():scrapeBestSellerMobiles()if len(bestSellers) > 0:resetRanks(6)commitBestSellers("MOBILE")scrapeBestSellerTablets()if len(bestSellers) > 0:resetRanks(5)commitBestSellers("TABLET")sendEmail()if __name__=='__main__':main()