| 4106 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 20-Nov-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
|
|
6 |
import tornado.httpserver
|
|
|
7 |
import tornado.ioloop
|
|
|
8 |
import tornado.web
|
|
|
9 |
import json, os, ConfigParser, sys
|
|
|
10 |
|
|
|
11 |
cmd_folder = os.path.dirname(os.path.abspath(os.environ["HOME"] + "/code/trunk/PyProj/src/shop2020/"))
|
|
|
12 |
if cmd_folder not in sys.path:
|
|
|
13 |
sys.path.insert(0, cmd_folder)
|
|
|
14 |
|
|
|
15 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
16 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
|
|
17 |
|
|
|
18 |
class BaseHandler(tornado.web.RequestHandler):
|
|
|
19 |
def get_current_user(self):
|
|
|
20 |
return self.get_secure_cookie('userauth')
|
|
|
21 |
|
|
|
22 |
class LoginHandler(BaseHandler):
|
|
|
23 |
def get(self):
|
|
|
24 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
25 |
self.write(self.loader.load('LoginForm.html').generate())
|
|
|
26 |
|
|
|
27 |
def post(self):
|
|
|
28 |
config = ConfigParser.SafeConfigParser()
|
|
|
29 |
config.read('app.cfg')
|
|
|
30 |
|
|
|
31 |
username = self.get_argument('username')
|
|
|
32 |
password = self.get_argument('password')
|
|
|
33 |
|
|
|
34 |
if username == config.get('auth', 'username') and password == config.get('auth', 'password'):
|
|
|
35 |
print 'Password Matched'
|
|
|
36 |
self.set_secure_cookie("userauth", username + '_' + password)
|
|
|
37 |
self.redirect('/')
|
|
|
38 |
else:
|
|
|
39 |
self.redirect('/login')
|
|
|
40 |
|
|
|
41 |
class MainHandler(BaseHandler):
|
|
|
42 |
|
|
|
43 |
def mapSearchUrls(self, map, name):
|
|
|
44 |
|
|
|
45 |
search_urls = {
|
|
|
46 |
'flipkart': 'http://www.flipkart.com/search-mobiles?query=$$&from=all&searchGroup=mobiles',
|
|
|
47 |
'homeshop18': 'http://www.homeshop18.com/nokia%20n97/search:$$/categoryid:3024',
|
|
|
48 |
'adexmart': 'http://adexmart.com/search.php?orderby=position&orderway=desc&search_query=$$',
|
|
|
49 |
'infibeam': 'http://www.infibeam.com/Mobiles/search?q=$$',
|
| 5639 |
amar.kumar |
50 |
'letsbuy': 'http://www.letsbuy.com/advanced_search_result.php?cPath=254&keywords=$$',
|
|
|
51 |
'sulekha': 'http://mobiles.sulekha.com/search.htm?cx=partner-pub-3470583419345383%3A8ggsimfcaaa&cof=FORID%3A10&ie=ISO-8859-1&q=$$&sa=Go',
|
|
|
52 |
'tradus': 'http://www.tradus.com/search/tradus_search/?query=$$'
|
| 4106 |
varun.gupt |
53 |
}
|
|
|
54 |
|
|
|
55 |
for key in search_urls.iterkeys():
|
|
|
56 |
try:
|
|
|
57 |
if map[key]['url'] == 'Not Found':
|
|
|
58 |
map[key]['url'] = search_urls[key].replace('$$', name)
|
|
|
59 |
except KeyError:
|
|
|
60 |
map[key] = {'price': 'Not Found', 'url': search_urls[key].replace('$$', name)}
|
|
|
61 |
return map
|
|
|
62 |
|
|
|
63 |
@tornado.web.authenticated
|
|
|
64 |
def get(self):
|
|
|
65 |
self.loader = tornado.template.Loader('HTMLTemplates')
|
|
|
66 |
catalog_client = CatalogClient().get_client()
|
|
|
67 |
items = catalog_client.getAllItemsByStatus(status.ACTIVE)
|
|
|
68 |
items.extend(catalog_client.getAllItemsByStatus(status.PAUSED))
|
|
|
69 |
items.extend(catalog_client.getAllItemsByStatus(status.PAUSED_BY_RISK))
|
|
|
70 |
products = {}
|
|
|
71 |
|
|
|
72 |
for item in items:
|
|
|
73 |
if item.category in (10002, 10003, 10004, 10005, 10010): products[item.catalogItemId] = item
|
|
|
74 |
|
|
|
75 |
comparative_prices = []
|
|
|
76 |
phones_not_found = []
|
|
|
77 |
|
|
|
78 |
for item in sorted(products.itervalues(), key = lambda item: item.brand):
|
|
|
79 |
try:
|
|
|
80 |
model_name = item.modelName.strip() if len(item.modelName.strip()) > 0 else None
|
|
|
81 |
model_number = item.modelNumber.strip() if len(item.modelNumber.strip()) > 0 else None
|
|
|
82 |
|
|
|
83 |
product_name = "%s " % item.brand
|
|
|
84 |
product_name += "%s " % model_name if model_name is not None else ''
|
|
|
85 |
product_name += model_number if model_number is not None else ''
|
|
|
86 |
print product_name
|
|
|
87 |
|
| 5761 |
amar.kumar |
88 |
data_file = open('/usr/msp_dir/' + str(item.catalogItemId), 'r')
|
| 4106 |
varun.gupt |
89 |
data = json.load(data_file)
|
|
|
90 |
data = self.mapSearchUrls(data, product_name)
|
|
|
91 |
data['name'] = product_name
|
|
|
92 |
data['entity_id'] = item.catalogItemId
|
|
|
93 |
comparative_prices.append(data)
|
|
|
94 |
|
|
|
95 |
except IOError:
|
|
|
96 |
phones_not_found.append(item.catalogItemId)
|
|
|
97 |
|
|
|
98 |
except Exception as e:
|
|
|
99 |
print e
|
|
|
100 |
|
|
|
101 |
self.write(self.loader.load('MSPChart.html').generate(data = comparative_prices))
|
|
|
102 |
print len(phones_not_found), 'Phones not found:', phones_not_found
|
|
|
103 |
|
|
|
104 |
settings = {
|
|
|
105 |
'static_path': os.path.join(os.path.dirname(__file__), 'static'),
|
|
|
106 |
'login_url': '/login',
|
|
|
107 |
'cookie_secret' :"61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo="
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
application = tornado.web.Application([
|
|
|
111 |
(r"/", MainHandler),
|
|
|
112 |
(r"/login", LoginHandler),
|
|
|
113 |
(r"/(jquery-1.6.2.min\.js)", tornado.web.StaticFileHandler, dict(path=settings['static_path']))
|
|
|
114 |
], **settings)
|
|
|
115 |
|
|
|
116 |
if __name__ == '__main__':
|
|
|
117 |
http_server = tornado.httpserver.HTTPServer(application)
|
| 4198 |
varun.gupt |
118 |
http_server.listen(8888)
|
| 4106 |
varun.gupt |
119 |
tornado.ioloop.IOLoop.instance().start()
|