add basic webapp

main
Bryce Allen 11 years ago
parent a607186abc
commit bbff48fb75

@ -62,6 +62,13 @@ class MHDB(object):
v = self.cache[key][args] = cursor.fetchall()
return v
def get_item_names(self):
v = self._get_memoized("item_names", """
SELECT name FROM items
WHERE type IN ('Bone', 'Flesh', 'Sac/Fluid')
""")
return v
def get_item(self, item_id):
v = self._get_memoized("item", """
SELECT * FROM items

@ -254,7 +254,7 @@ def print_quests_and_rewards(db, item_row, out):
out.write(" %20s %s / 100\n" % ("Cap", _format_range(*cap_ev)))
if shiny_ev:
out.write(" %20s %5.2f / 100\n" % ("Shiny", shiny_ev))
out.write("\n")
out.write("\n")
if __name__ == '__main__':

@ -0,0 +1,57 @@
<html>
<head>
<title>Monster Hunter Item Search</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<style>
.flex {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
#output { flex: 99 1 auto; }
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#search").click(update_search);
$("#item").keypress(function(e) {
if (e.which == 13) { update_search(); }
});
setup_autocomplete();
});
function setup_autocomplete() {
$.getJSON("/mhapi/item_name_list",
function(data) {
$("#item").autocomplete({ source: data });
});
}
function update_search() {
var item_name = $.trim($("#item").val());
$.get("/mhapi/rewards",
{ "item_name": item_name },
function(data) {
$("#output").text(data);
});
}
</script>
</head>
<body>
<div class="flex">
<label for="item">Item:</label>
<input id="item" type="text" size="20" />
<br />
<button id="search">Find Quests and Monsters</button>
<br />
<textarea readonly="true" rows="10" cols="80" id="output"></textarea>
</div>
</body>

@ -0,0 +1,6 @@
from flup.server.fcgi import WSGIServer
import reward_webapp
if __name__ == '__main__':
WSGIServer(reward_webapp.application).run()

@ -0,0 +1,108 @@
#!/usr/bin/env python
import os
import logging
from webob import Request, Response, exc
import mhdb
import mhrewards
# TODO: etag based on db version + manual code version
DB_VERSION = "20150313"
PREFIX = "/mhapi/"
logging.basicConfig(filename="/tmp/reward_webapp.log", level=logging.INFO)
class App(object):
def __init__(self):
self.web_path = os.path.dirname(__file__)
self.project_path = os.path.abspath(os.path.join(self.web_path, ".."))
db_path = os.path.join(self.project_path, "db", "mh4u.db")
self.db = mhdb.MHDB(db_path)
log_path = os.path.join(self.project_path, "web.log")
self.log = logging.getLogger("reward_webapp")
self.log.info("app started")
def __call__(self, environ, start_response):
req = Request(environ)
resp = Response()
resp.charset = "utf8"
if req.path in ("", "/", "/index.html"):
resp = self.index(req, resp)
elif req.path == PREFIX + "rewards":
resp = self.find_item_rewards(req, resp)
elif req.path == PREFIX + "item_name_list":
resp = self.get_all_names(req, resp)
else:
resp = exc.HTTPNotFound()
return resp(environ, start_response)
def index(self, req, resp):
resp.cache_control = "max-age=86400"
html_path = os.path.join(self.web_path, "index.html")
resp.content_type = "text/html"
resp.body = open(html_path, "rb").read()
return resp
def find_item_rewards(self, req, resp):
version = "1"
resp.cache_control = "max-age=60"
resp.etag = DB_VERSION + version
self.log.info("etag = %s", resp.etag)
if req.if_match == resp.etag:
return HTTPNotModified()
resp.content_type = "text/plan"
item_name = req.params.get("item_name", "").strip()
if not item_name:
resp.body = "Please enter an item name"
else:
item_row = mhrewards.find_item(self.db, item_name, resp.body_file)
if item_row is not None:
mhrewards.print_quests_and_rewards(self.db, item_row,
resp.body_file)
return resp
def get_all_names(self, req, resp):
version = "2"
resp.cache_control = "max-age=60"
resp.etag = DB_VERSION + version
self.log.info("get all names etag = %s", resp.etag)
if req.if_match == resp.etag:
return HTTPNotModified()
resp.content_type = "application/json"
resp.body_file.write("[")
items = self.db.get_item_names()
first = True
for item in items:
if first:
first = False
else:
resp.body_file.write(", ")
resp.body_file.write('"')
resp.body_file.write(item["name"])
resp.body_file.write('"')
resp.body_file.write("]")
return resp
application = App()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server("localhost", 8080, application)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print "^C"
Loading…
Cancel
Save