initial public commit

master
Tomek Dubrownik 2012-04-26 12:16:36 +02:00
commit 68de71df6c
11 changed files with 232 additions and 0 deletions

1
README Normal file
View File

@ -0,0 +1 @@
Shame/Banany

10
config.py.dist Normal file
View File

@ -0,0 +1,10 @@
port = 9090
secret_key = 'flasksecretkey'
limit = 500
db = 'fail2sql'
db_user = 'fail2sql'
db_pass = 'password'
db_host = 'localhost'
db_port = 8740

8
shame.ini Normal file
View File

@ -0,0 +1,8 @@
[uwsgi]
master = 1
http = 0.0.0.0:8081
threads = 10
chdir = /var/www/shame
module = shame
callable = app
debug = true

54
shame.py Normal file
View File

@ -0,0 +1,54 @@
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)

12
static/base.css Normal file
View File

@ -0,0 +1,12 @@
#map {
margin: 0;
min-height: 500px;
width: 100%;
height: 100%;
position: absolute;
}
body {
margin: 0;
padding: 0;
}

BIN
static/hs.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

135
static/map.js Normal file
View File

@ -0,0 +1,135 @@
(function() {
var gm = google.maps,
points = [],
hspos = new gm.LatLng(52.246246, 21.002971),
map, hsmarker,
iconBase = new gm.Size(100, 133),
shadowBase = new gm.Size(110, 146),
infoWindowCurrentPoint = -1,
infoWindowDefaultText = "<center><img src=\"/static/omgitspins.gif\" /></center>",
infoWindow = new gm.InfoWindow({content:infoWindowDefaultText});
function scaleIcon(url, base, zoom) {
var fac = Math.max(Math.min(Math.pow(2, zoom / 2 - 6), 0.25), 0.0625);
return new gm.MarkerImage(url, null, null,
new gm.Point(base.width * fac, base.height * fac),
new gm.Size(base.width * fac * 2, base.height * fac * 2))
}
function initialize() {
var style = [
{
featureType: "water",
stylers: [
{ lightness: -97 }
]
},{
featureType: "landscape",
stylers: [
{ lightness: -74 },
{ hue: "#55ff00" }
]
},{
featureType: "administrative",
elementType: "geometry",
stylers: [
{ saturation: 82 },
{ hue: "#5eff00" },
{ gamma: 0.25 },
{ lightness: -36 },
{ visibility: "on" }
]
},{
featureType: "poi",
stylers: [
{ visibility: "off" }
]
}
],
options = {
center: hspos,
zoom: 3,
mapTypeId: gm.MapTypeId.ROADMAP,
styles: style
},
hsimg = new gm.MarkerImage('/static/hs.png', null, null, new gm.Point(9.375, 9.375),
new gm.Size(18.75, 18.75));
map = new gm.Map(document.getElementById('map'), options);
gm.event.addListener(map, 'zoom_changed', function() {
points.forEach(function(p) {
var img = scaleIcon('/static/marker.png', iconBase, map.getZoom()),
shadow = scaleIcon('/static/shadow.png', shadowBase, map.getZoom());
p.marker.setIcon(img);
p.marker.setShadow(shadow);
});
});
hsmarker = new gm.Marker({
position: hspos,
map: map,
icon: hsimg,
title: 'Warsaw Hackerspace'
});
poll = function() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
refresh(JSON.parse(xhr.responseText).points);
}
}
xhr.open('GET', '/shamed', true);
xhr.send(null);
}
window.setInterval(poll, 90000);
poll();
}
function showShitForPoint(point) {
var marker = point.marker,
pointID = point.id,
geo = point.geo,
ip = point.ip,
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState ===4 && xhr.status === 200)
{
if (infoWindowCurrentPoint == pointID)
{
var bans = JSON.parse(xhr.responseText).bans,
content = "<center><b>" + ip + "</b><br/> (" + geo + ")</center><ul>\n";
bans.forEach(function(b) {
content += "<li><b>" + b.name + "</b> " + b.time + "</li>\n"
});
infoWindow.setContent(content);
}
}
}
infoWindowCurrentPoint = pointID;
xhr.open('GET', '/shameon/' + pointID, true)
xhr.send(null)
infoWindow.setContent(infoWindowDefaultText);
infoWindow.open(map, marker);
}
function createMarker(point) {
var img = scaleIcon('/static/marker.png', iconBase, map.getZoom()),
shadow = scaleIcon('/static/shadow.png', shadowBase, map.getZoom());
var marker = new gm.Marker({
position: new gm.LatLng(point.latitude, point.longitude),
map: map,
title: point.ip,
icon: img,
shadow: shadow
});
gm.event.addListener(marker, 'click', function() {
showShitForPoint(point);
});
return marker;
}
function refresh(newPoints) {
points.forEach(function(p) {
p.marker.setMap(null);
});
newPoints.forEach(function(p) {
p.marker = createMarker(p);
});
points = newPoints;
}
window.onload = initialize;
})()

BIN
static/marker.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

BIN
static/omgitspins.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
static/shadow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

12
templates/root.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<title>Hackerspace.pl hall of shame</title>
<link rel="stylesheet" href="/static/base.css">
<script lang="javascipt" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyBkCh5XPNpD9C2btjDM9Gdwgo1mTiBoOak&sensor=false"></script>
<script lang="javascipt" src="/static/map.js"></script>
</head>
<body>
<div id="map"></div>
</body>
</html>