-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (42 loc) · 1.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from tornado import httpserver, ioloop, web
from spider.scraper import boot
import json, os, sys
class LandingHandler(web.RequestHandler):
def get(self):
self.render('index.html')
class SearchHandler(web.RequestHandler):
@web.asynchronous # Async connection
def get(self):
try:
query = str(self.get_query_argument('q'))
if not query:
self.send_response([]) # writing back none
return
n = int(self.get_query_argument('n'))
if n == 0:
self.send_response([])
return
except ValueError:
# Error converting to int
n = 30 # Default in HTML
except Exception as e:
self.send_response([])
return
boot(query, n, callback=self.send_response) # Boot spider
def send_response(self, *args):
self.result = args[0]
self.write(json.dumps(self.result))
self.finish()
# Url - Controller mapping
urls = [
(r'/', LandingHandler),
(r'/search/?$', SearchHandler),
]
if __name__ == '__main__':
app = web.Application(urls) # Create Tornado web application instance
# port = sys.argv[1] if len(sys.argv) > 1 else 8888
# app.listen(port) # Define port 8888 to listen at
http_server = httpserver.HTTPServer(app)
port = int(os.environ.get("PORT", 5000))
http_server.listen(port)
ioloop.IOLoop.current().start() # Create IOLoop instance and start it