| 3232 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 31-Aug-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
|
|
6 |
|
|
|
7 |
import tornado.httpserver
|
|
|
8 |
import tornado.ioloop
|
|
|
9 |
import tornado.web
|
|
|
10 |
import json, os, ConfigParser, sys
|
|
|
11 |
from PyLucene.Retriever import Retriever
|
|
|
12 |
from Utils import getItemsWithTopScore, isPriceSame
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
#shop2020/clients/CatalogClient.py
|
|
|
16 |
cmd_folder = os.path.dirname(os.path.abspath("/home/varungupta/code/trunk/PyProj/src/shop2020/"))
|
|
|
17 |
if cmd_folder not in sys.path:
|
|
|
18 |
sys.path.insert(0, cmd_folder)
|
|
|
19 |
|
|
|
20 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
21 |
|
|
|
22 |
class BaseHandler(tornado.web.RequestHandler):
|
|
|
23 |
def get_current_user(self):
|
|
|
24 |
return self.get_secure_cookie('userauth')
|
|
|
25 |
|
|
|
26 |
class LoginHandler(BaseHandler):
|
|
|
27 |
def get(self):
|
|
|
28 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
29 |
self.write(self.loader.load('LoginForm.html').generate())
|
|
|
30 |
|
|
|
31 |
def post(self):
|
|
|
32 |
config = ConfigParser.SafeConfigParser()
|
|
|
33 |
config.read('app.cfg')
|
|
|
34 |
|
|
|
35 |
username = self.get_argument('username')
|
|
|
36 |
password = self.get_argument('password')
|
|
|
37 |
|
|
|
38 |
if username == config.get('auth', 'username') and password == config.get('auth', 'password'):
|
|
|
39 |
print 'Password Matched'
|
|
|
40 |
self.set_secure_cookie("userauth", username + '_' + password)
|
|
|
41 |
self.redirect('/')
|
|
|
42 |
else:
|
|
|
43 |
self.redirect('/login')
|
|
|
44 |
|
|
|
45 |
class MainHandler(BaseHandler):
|
|
|
46 |
|
|
|
47 |
@tornado.web.authenticated
|
|
|
48 |
def get(self):
|
|
|
49 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
50 |
catalog_client = CatalogClient().get_client()
|
|
|
51 |
items = catalog_client.getAllItems(True)
|
|
|
52 |
|
|
|
53 |
retriever = Retriever()
|
|
|
54 |
products = {}
|
|
|
55 |
|
|
|
56 |
for item in items:
|
|
|
57 |
if item.category in (10002, 10003, 10004, 10005, 10010): products[item.catalogItemId] = item
|
|
|
58 |
|
|
|
59 |
comparative_prices = []
|
|
|
60 |
count_all, count_success, count_conflict, count_notfound = 0, 0, 0, 0
|
|
|
61 |
|
|
|
62 |
for item in sorted(products.itervalues(), key = lambda item: item.brand):
|
|
|
63 |
count_all += 1
|
|
|
64 |
try:
|
|
|
65 |
model_name = item.modelName.strip() if len(item.modelName.strip()) > 0 else None
|
|
|
66 |
model_number = item.modelNumber.strip() if len(item.modelNumber.strip()) > 0 else None
|
|
|
67 |
|
|
|
68 |
search_results = retriever.retrieve(model_number = model_number, model_name = model_name, brand = item.brand)
|
|
|
69 |
phones_with_top_score = getItemsWithTopScore(search_results)
|
|
|
70 |
|
|
|
71 |
if len(phones_with_top_score) > 0:
|
|
|
72 |
if isPriceSame(phones_with_top_score):
|
|
|
73 |
flipkart_price = phones_with_top_score[0]['price']
|
|
|
74 |
count_success += 1
|
|
|
75 |
data = ''
|
|
|
76 |
url = "http://www.flipkart.com%s" % phones_with_top_score[0]['url']
|
|
|
77 |
else:
|
|
|
78 |
flipkart_price = 'Conflict'
|
|
|
79 |
print item.brand, model_name, model_number
|
|
|
80 |
print 'CONFLICT\n'
|
|
|
81 |
print search_results
|
|
|
82 |
print phones_with_top_score
|
|
|
83 |
count_conflict += 1
|
|
|
84 |
data = json.dumps(phones_with_top_score)
|
|
|
85 |
url = '#'
|
|
|
86 |
|
|
|
87 |
else:
|
|
|
88 |
flipkart_price = 'Not Found'
|
|
|
89 |
print item.brand, model_name, model_number
|
|
|
90 |
print 'NOT FOUND\n'
|
|
|
91 |
print search_results
|
|
|
92 |
print phones_with_top_score
|
|
|
93 |
count_notfound += 1
|
|
|
94 |
data = ''
|
|
|
95 |
url = ''
|
|
|
96 |
|
|
|
97 |
product_name = "%s " % item.brand
|
|
|
98 |
product_name += "%s " % model_name if model_name is not None else ''
|
|
|
99 |
product_name += model_number if model_number is not None else ''
|
|
|
100 |
|
|
|
101 |
comparative_prices.append({
|
|
|
102 |
'product_name': product_name,
|
|
|
103 |
'saholic_price': item.sellingPrice,
|
|
|
104 |
'flipkart_price': flipkart_price.replace('Rs. ', ''),
|
|
|
105 |
'td_class': 'conflict' if flipkart_price in ('Conflict') else '',
|
|
|
106 |
'data': data,
|
|
|
107 |
'url': url
|
|
|
108 |
})
|
|
|
109 |
except Exception as e:
|
|
|
110 |
print e.message
|
|
|
111 |
|
|
|
112 |
print count_all, count_success, count_conflict, count_notfound
|
|
|
113 |
self.write(self.loader.load('PriceChart.html').generate(data = comparative_prices))
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
class SearchHandler(tornado.web.RequestHandler):
|
|
|
117 |
|
|
|
118 |
def get(self):
|
|
|
119 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
120 |
|
|
|
121 |
try:
|
|
|
122 |
search_query = self.get_argument("q")
|
|
|
123 |
results = Retriever().retrieve(search_query)
|
|
|
124 |
self.write(self.loader.load('SearchResults.html').generate(phones = results, query = search_query))
|
|
|
125 |
|
|
|
126 |
except Exception:
|
|
|
127 |
self.write(self.loader.load('SearchForm.html').generate())
|
|
|
128 |
|
|
|
129 |
settings = {
|
|
|
130 |
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
|
|
|
131 |
'login_url': '/login',
|
|
|
132 |
'cookie_secret' :"61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo="
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
application = tornado.web.Application([
|
|
|
136 |
(r"/", MainHandler),
|
|
|
137 |
(r"/search", SearchHandler),
|
|
|
138 |
(r"/login", LoginHandler),
|
|
|
139 |
(r"/(jquery-1.6.2.min\.js)", tornado.web.StaticFileHandler, dict(path=settings['static_path']))
|
|
|
140 |
], **settings)
|
|
|
141 |
|
|
|
142 |
if __name__ == '__main__':
|
|
|
143 |
http_server = tornado.httpserver.HTTPServer(application)
|
|
|
144 |
http_server.listen(8889)
|
|
|
145 |
tornado.ioloop.IOLoop.instance().start()
|