| 13828 |
kshitij.so |
1 |
import urllib
|
|
|
2 |
import urllib2
|
|
|
3 |
from BeautifulSoup import BeautifulSoup
|
|
|
4 |
import pymongo
|
|
|
5 |
import re
|
|
|
6 |
from dtr.utils.utils import to_java_date
|
|
|
7 |
from datetime import datetime
|
| 14257 |
kshitij.so |
8 |
import optparse
|
| 13828 |
kshitij.so |
9 |
|
|
|
10 |
con = None
|
| 14257 |
kshitij.so |
11 |
parser = optparse.OptionParser()
|
|
|
12 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
13 |
default="localhost",
|
|
|
14 |
type="string", help="The HOST where the mongo server is running",
|
|
|
15 |
metavar="mongo_host")
|
|
|
16 |
|
|
|
17 |
(options, args) = parser.parse_args()
|
|
|
18 |
|
| 13828 |
kshitij.so |
19 |
bestSellers = []
|
|
|
20 |
now = datetime.now()
|
|
|
21 |
mobUrl = "http://www.saholic.com/mobile-phone/10006"
|
|
|
22 |
tabUrl = "http://www.saholic.com/all-tablets/10010"
|
|
|
23 |
|
|
|
24 |
class __RankInfo:
|
|
|
25 |
|
|
|
26 |
def __init__(self, identifier, rank):
|
|
|
27 |
self.identifier = identifier
|
|
|
28 |
self.rank = rank
|
|
|
29 |
|
| 14257 |
kshitij.so |
30 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
31 |
global con
|
|
|
32 |
if con is None:
|
|
|
33 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
34 |
try:
|
|
|
35 |
con = pymongo.MongoClient(host, port)
|
|
|
36 |
except Exception, e:
|
|
|
37 |
print e
|
|
|
38 |
return None
|
|
|
39 |
return con
|
|
|
40 |
|
|
|
41 |
def getSoupObject(url,data):
|
|
|
42 |
print "Getting soup object for"
|
|
|
43 |
global RETRY_COUNT
|
|
|
44 |
RETRY_COUNT = 1
|
|
|
45 |
while RETRY_COUNT < 10:
|
|
|
46 |
try:
|
|
|
47 |
soup = None
|
|
|
48 |
request = urllib2.Request(url,data)
|
|
|
49 |
request.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
|
|
|
50 |
request.add_header('Accept-Language','en-US,en;q=0.8,hi;q=0.6')
|
|
|
51 |
request.add_header('Connection','keep-alive')
|
|
|
52 |
request.add_header('User-Agent','Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')
|
|
|
53 |
|
|
|
54 |
response = urllib2.urlopen(request)
|
|
|
55 |
response_data = response.read()
|
|
|
56 |
response.close()
|
|
|
57 |
try:
|
|
|
58 |
page=response_data.decode("utf-8")
|
|
|
59 |
soup = BeautifulSoup(page,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
60 |
except:
|
|
|
61 |
soup = BeautifulSoup(response_data,convertEntities=BeautifulSoup.HTML_ENTITIES)
|
|
|
62 |
if soup is None:
|
|
|
63 |
raise
|
|
|
64 |
return soup
|
|
|
65 |
except Exception as e:
|
|
|
66 |
print e
|
|
|
67 |
print "Retrying"
|
|
|
68 |
RETRY_COUNT = RETRY_COUNT + 1
|
|
|
69 |
|
|
|
70 |
def scrapeBestSellerMobiles():
|
|
|
71 |
global bestSellers
|
|
|
72 |
rank = 1
|
|
|
73 |
for i in range(1,6):
|
|
|
74 |
query_args = { 'fq':'F_50028:In Stock', 'page':i }
|
|
|
75 |
data = urllib.urlencode(query_args)
|
|
|
76 |
soup = getSoupObject(mobUrl,data)
|
|
|
77 |
titleTags = soup.findAll('div',{'class':'title'})
|
|
|
78 |
for titleTag in titleTags:
|
|
|
79 |
print titleTag.find('a').text
|
|
|
80 |
identifier = getSaholicIdentifier(titleTag.find('a')['href'])
|
|
|
81 |
r_info = __RankInfo(str(identifier), rank)
|
|
|
82 |
bestSellers.append(r_info)
|
|
|
83 |
rank = rank + 1
|
|
|
84 |
|
|
|
85 |
def scrapeBestSellerTablets():
|
|
|
86 |
global bestSellers
|
|
|
87 |
bestSellers = []
|
|
|
88 |
rank = 1
|
|
|
89 |
for i in range(1,6):
|
|
|
90 |
query_args = { 'fq':'F_50028:In Stock', 'page':i }
|
|
|
91 |
data = urllib.urlencode(query_args)
|
|
|
92 |
soup = getSoupObject(tabUrl, data)
|
|
|
93 |
titleTags = soup.findAll('div',{'class':'title'})
|
|
|
94 |
for titleTag in titleTags:
|
|
|
95 |
print titleTag.find('a').text
|
|
|
96 |
identifier = getSaholicIdentifier(titleTag.find('a')['href'])
|
|
|
97 |
r_info = __RankInfo(str(identifier), rank)
|
|
|
98 |
bestSellers.append(r_info)
|
|
|
99 |
rank = rank + 1
|
|
|
100 |
|
|
|
101 |
def getSaholicIdentifier(url):
|
|
|
102 |
return url[url.rfind('-')+len('-'):]
|
|
|
103 |
|
|
|
104 |
def resetRanks(category):
|
|
|
105 |
oldRankedItems = get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':4,'category':category})
|
|
|
106 |
for item in oldRankedItems:
|
|
|
107 |
print item['_id']
|
|
|
108 |
get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']}, {'$set' : {'rank':0,'updatedOn':to_java_date(now)}}, multi=True)
|
|
|
109 |
|
|
|
110 |
def commitBestSellers():
|
|
|
111 |
print "Rank",
|
|
|
112 |
print '\t',
|
|
|
113 |
print 'Identifier'
|
|
|
114 |
for x in bestSellers:
|
|
|
115 |
print x.rank,
|
|
|
116 |
print '\t',
|
| 14057 |
kshitij.so |
117 |
print x.identifier,
|
| 13828 |
kshitij.so |
118 |
col = get_mongo_connection().Catalog.MasterData.find({'identifier':x.identifier})
|
|
|
119 |
print "count sku",
|
|
|
120 |
print '\t',
|
|
|
121 |
print len(list(col))
|
|
|
122 |
get_mongo_connection().Catalog.MasterData.update({'identifier':x.identifier,'source_id':4}, {'$set' : {'rank':x.rank,'updatedOn':to_java_date(now)}}, multi=True)
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
def main():
|
|
|
127 |
scrapeBestSellerMobiles()
|
|
|
128 |
if len(bestSellers) > 0:
|
|
|
129 |
resetRanks('Mobiles')
|
|
|
130 |
commitBestSellers()
|
|
|
131 |
scrapeBestSellerTablets()
|
|
|
132 |
if len(bestSellers) > 0:
|
|
|
133 |
resetRanks('Tablets')
|
|
|
134 |
commitBestSellers()
|
|
|
135 |
if __name__=='__main__':
|
|
|
136 |
main()
|