55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from flask import Flask, request, session, g, jsonify, render_template
|
|
import MySQLdb as sql
|
|
import config
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = config.secret_key
|
|
app.debug = True
|
|
|
|
def sql_connect():
|
|
app.conn = sql.connect(config.db_host, config.db_user, config.db_pass, config.db)
|
|
|
|
def sql_execute(query, *a, **kw):
|
|
rval = None
|
|
try:
|
|
rval = g.db.execute(query, *a, **kw)
|
|
except (sql.OperationalError, AttributeError):
|
|
sql_connect()
|
|
make_cursor()
|
|
rval = g.db.execute(query, *a, **kw)
|
|
finally:
|
|
return rval
|
|
|
|
@app.before_request
|
|
def make_cursor():
|
|
g.db = app.conn.cursor(sql.cursors.DictCursor)
|
|
|
|
@app.teardown_request
|
|
def close_cursor(exception):
|
|
g.db.close()
|
|
|
|
@app.route('/shamed')
|
|
def home():
|
|
sql_execute('select id, ip, longitude, latitude, geo from f2s_hosts')
|
|
points = g.db.fetchmany(config.limit)
|
|
return jsonify(points=points)
|
|
|
|
@app.route('/shameon/<int:pointID>')
|
|
def shameon(pointID):
|
|
sql_execute('select * from f2s_bans where id = %s order by time desc', (pointID, ))
|
|
bans = g.db.fetchall()
|
|
for b in bans:
|
|
b['time'] = str(b['time'])
|
|
return jsonify(bans=bans)
|
|
|
|
@app.route('/')
|
|
def root():
|
|
return render_template('root.html')
|
|
|
|
@app.before_first_request
|
|
def initialize():
|
|
sql_connect()
|
|
|
|
if __name__ == '__main__':
|
|
app.run('0.0.0.0', config.port, debug=True)
|