-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
101 lines (76 loc) · 2.9 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import datetime
import time
import useragents
import config
from flask import Flask, request, redirect, url_for, render_template
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
# Load system configurations
DEBUG = config.DEBUG
SQLALCHEMY_DATABASE_URI = config.SQLALCHEMY_DATABASE_URI
SQLALCHEMY_TRACK_MODIFICATIONS = config.SQLALCHEMY_TRACK_MODIFICATIONS
# Load user agent configurations
USER_AGENTS = useragents.USER_AGENTS
# Initialize the application
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = SQLALCHEMY_TRACK_MODIFICATIONS
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)
# Create the object we would like to display
# In this case, it's any request with the user agent
class SiteHit(db.Model):
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.String(255))
ip = db.Column(db.String(255))
useragent = db.Column(db.String(255))
beaconid = db.Column(db.Integer)
def __init__(self, timestamp, ip, useragent, beaconid):
self.timestamp = timestamp
self.ip = ip
self.useragent = useragent
self.beaconid = beaconid
# Default route
@app.route('/')
def index():
# Bounce to a nice non-commercial tourist map
return redirect('http://washington.org/dc-map')
# Beacon target
@app.route('/<int:beacon_id>')
def log_interaction(beacon_id):
# Log the beacon interaction
toLog = {}
# Get the current time
ts = time.time()
toLog['timestamp'] = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
# Pull the source IP
toLog['ip'] = request.remote_addr
# Pull the user agent
toLog['ua'] = request.headers.get('User-Agent')
# Create the object
newHit = SiteHit(toLog['timestamp'], toLog['ip'], toLog['ua'], beacon_id)
# Commit the record
db.session.add(newHit)
db.session.commit()
# Now that it's logged, send them somewhere else
return redirect(url_for('index'))
# Show results page
@app.route('/results/')
@app.route('/results/<int:beacon_id>')
def results(beacon_id=-1):
if beacon_id == -1:
hits = SiteHit.query.all()
agent_freq = db.session.query(SiteHit.useragent, func.count(SiteHit.useragent)).group_by(SiteHit.useragent).all()
else:
hits = SiteHit.query.filter_by(beaconid=beacon_id)
agent_freq = db.session.query(SiteHit.useragent, func.count(SiteHit.useragent)).filter_by(beaconid=beacon_id).group_by(SiteHit.useragent).all()
chart_data = {"x": [], "y": []}
for agent, freq in agent_freq:
chart_data["x"].append(str(agent))
chart_data["y"].append(freq)
#chart_data = {"x": [str(u"a"), str(u"b"), str(u"c")], "y": [1, 2, 3]}
print(chart_data)
return render_template('show_results.html', hits=hits, chart_data=chart_data)
# Initialize the db if it is not already
db.create_all()
if __name__ == '__main__':
app.run(debug=DEBUG)