-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (66 loc) · 2.68 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import traceback
from datetime import datetime, timedelta
import cherrypy
from cherrypy.process.plugins import Daemonizer
from jinja2 import Environment, FileSystemLoader
import os
import GenericOps
from DBConnectivity import Redis
from Downloader import Download
PATH = os.path.abspath(os.path.dirname(__file__))
env = Environment(loader=FileSystemLoader(''))
class Root:
@cherrypy.expose
def index(self):
try:
full_path = PATH + "/" + "tmp/" + Download(datetime.now()).file_name.format(datetime.strftime(
datetime.now() - timedelta(1), '%d%m%y'))
if not GenericOps.if_path_exists(full_path):
Download(datetime.now()).download()
template = env.get_template('index.html')
top10 = Redis().fetch_top_10_stocks()
return template.render(data=top10, title="Top 10 entries for " + datetime.strftime(datetime.now(), '%d/%m/%y'))
except Exception as e:
print(str(e))
traceback.print_exc()
# Can also log the errors into the database
return json.dumps({'response': 'Some error occurred please try again!'})
@cherrypy.expose
def search(self, search="A", searchButton="Search"):
try:
full_path = PATH + "/" + "tmp/" + Download(datetime.now()).file_name.format(datetime.strftime(
datetime.now() - timedelta(1), '%d%m%y'))
if not GenericOps.if_path_exists(full_path):
Download(datetime.now()).download()
template = env.get_template('index.html')
search_result = Redis().search_for_key(search)
return template.render(data=search_result, title="Search results for: "+search)
except Exception as e:
# Can also log the errors into the database
return json.dumps({'response': 'Some error occurred please try again!'})
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': 8080,
'engine.autoreload.on': False
})
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static'
}
}
cherrypy.tree.mount(Root(), config=conf)
if hasattr(cherrypy.engine, 'block'):
# 3.1 syntax
cherrypy.engine.start()
cherrypy.engine.block()
else:
# 3.0 syntax
cherrypy.server.quickstart()
cherrypy.engine.start()
# cherrypy.quickstart(Root(), '/', conf)