From 02a3b0d8caf160da78bd4b355a7c11d558abe036 Mon Sep 17 00:00:00 2001 From: raylu Date: Sat, 19 Oct 2013 23:57:55 -0700 Subject: corp search --- db/__init__.py | 2 +- db/importer.py | 2 +- server.py | 26 +++++++++++++------------- web/static/css/base.ccss | 3 +-- web/templates/home.html | 30 ++++-------------------------- web/templates/kill.html | 30 ++++++++++++++++++++++++++++++ web/templates/search.html | 12 ++++++++++++ 7 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 web/templates/kill.html create mode 100644 web/templates/search.html diff --git a/db/__init__.py b/db/__init__.py index ead5232..92837a2 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -4,7 +4,7 @@ from config import db as dbconfig conn = oursql.connect(host=dbconfig.host, db=dbconfig.database, user=dbconfig.user, passwd=dbconfig.password, autoreconnect=True) -def ykill_cursor(): +def cursor(): return conn.cursor(oursql.DictCursor) def execute(cursor, sql, *values): diff --git a/db/importer.py b/db/importer.py index 39f97ce..4f8f4b4 100755 --- a/db/importer.py +++ b/db/importer.py @@ -5,7 +5,7 @@ from pprint import pprint import __init__ as db -with db.ykill_cursor() as c: +with db.cursor() as c: db.execute(c, 'INSERT INTO kills (kill_id, solar_system_id, kill_time, moon_id) VALUES(?, ?, ?, ?)', kill['killID'], kill['solarSystemID'], kill['killTime'], kill['moonID']) diff --git a/server.py b/server.py index 7dcf2d3..8c87850 100755 --- a/server.py +++ b/server.py @@ -5,12 +5,12 @@ import operator import os import cleancss -import tornado.gen import tornado.httpclient import tornado.ioloop import tornado.web from config import web as config +import db class BaseHandler(tornado.web.RequestHandler): def render(self, *args, **kwargs): @@ -22,18 +22,18 @@ class BaseHandler(tornado.web.RequestHandler): return s.replace(b'\n', b'') # this is like Django's {% spaceless %} class MainHandler(BaseHandler): - @tornado.web.asynchronous - @tornado.gen.coroutine def get(self): - http_client = tornado.httpclient.AsyncHTTPClient() - kills_url = 'https://zkillboard.com/api/kills/corporationID/98182803/limit/1' - losses_url = 'https://zkillboard.com/api/losses/corporationID/98182803/limit/1' - kills_res, losses_res = yield [http_client.fetch(kills_url), http_client.fetch(losses_url)] - kills = json.loads(kills_res.body.decode('utf-8')) - losses = json.loads(losses_res.body.decode('utf-8')) - kills = sorted(kills + losses, key=operator.itemgetter('killTime'), reverse=True) - self.render('home.html', kills=kills) + self.render('home.html') +class SearchHandler(BaseHandler): + def get(self): + q = self.get_argument('q') + with db.cursor() as c: + corps = db.query(c, ''' + SELECT DISTINCT corporation_id, corporation_name FROM characters + WHERE corporation_name LIKE ? + ''', '%{}%'.format(q)) + self.render('search.html', corps=corps) class CSSHandler(tornado.web.RequestHandler): def get(self, css_path): @@ -47,10 +47,10 @@ if __name__ == '__main__': tornado.web.Application( handlers=[ (r'/', MainHandler), + (r'/search', SearchHandler), (r"/(css/.+)\.css", CSSHandler), ], - template_path=os.path.join(os.path.dirname(__file__), 'templates'), - static_path=os.path.join(os.path.dirname(__file__), 'static'), + template_path=os.path.join(os.path.dirname(__file__), 'web/templates'), cookie_secret=config.cookie_secret, debug=config.debug, ).listen(config.port) diff --git a/web/static/css/base.ccss b/web/static/css/base.ccss index e515323..9419e3f 100644 --- a/web/static/css/base.ccss +++ b/web/static/css/base.ccss @@ -13,14 +13,13 @@ body: a: text-decoration: none; - color: #dde; + color: #38c; form: input: background: #fff; border: 1px solid #cdd; padding: 5px 10px; - } .clear: clear: both; diff --git a/web/templates/home.html b/web/templates/home.html index 6d4f69c..34fe371 100644 --- a/web/templates/home.html +++ b/web/templates/home.html @@ -1,30 +1,8 @@ {% extends "base.html" %} {% block main %} - -
- {% for kill in kills %} - - {{ kill['killTime'] }} - {{ kill['solarSystemID'] }} - - - - - {{ kill['victim']['characterName'] }} - {{ kill['victim']['corporationName'] }} - {{ kill['victim']['allianceName'] }} - {% for attacker in kill['attackers'] %} - {% if attacker['finalBlow'] %} - {% set killer = attacker %} - {% break %} - {% end %} - {% end %} - {{ killer['characterName'] }} - {{ killer['corporationName'] }} - {{ killer['allianceName'] }} - - {% end %} -
- +
+ + +
{% end %} diff --git a/web/templates/kill.html b/web/templates/kill.html new file mode 100644 index 0000000..6d4f69c --- /dev/null +++ b/web/templates/kill.html @@ -0,0 +1,30 @@ +{% extends "base.html" %} + +{% block main %} + +
+ {% for kill in kills %} + + {{ kill['killTime'] }} + {{ kill['solarSystemID'] }} + + + + + {{ kill['victim']['characterName'] }} + {{ kill['victim']['corporationName'] }} + {{ kill['victim']['allianceName'] }} + {% for attacker in kill['attackers'] %} + {% if attacker['finalBlow'] %} + {% set killer = attacker %} + {% break %} + {% end %} + {% end %} + {{ killer['characterName'] }} + {{ killer['corporationName'] }} + {{ killer['allianceName'] }} + + {% end %} +
+ +{% end %} diff --git a/web/templates/search.html b/web/templates/search.html new file mode 100644 index 0000000..03980af --- /dev/null +++ b/web/templates/search.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block main %} + +

Corporations

+{% for corp in corps %} + + {{ corp['corporation_name'] }} + +{% end %} + +{% end %} -- cgit v1.2.3