forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (112 loc) · 4.34 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Example "Analytics" app. To start using this on your site, do the following:
* Create a postgresql database with HStore support:
createdb analytics
psql analytics -c "create extension hstore;"
* Create an account for each domain you intend to collect analytics for, e.g.
Account.create(domain='charlesleifer.com')
* Update configuration values marked "TODO", e.g. DOMAIN.
* Run this app using the WSGI server of your choice.
* Using the appropriate account id, add a `<script>` tag to each site you want
to collect analytics data from. I place mine at the bottom of the <body>:
<script src="http://yourdomain.com/a.js?id=<your account id>"></script>
Take a look at `reports.py` for some interesting queries you can perform
on your pageview data.
"""
import datetime
import os
from urlparse import parse_qsl, urlparse
from flask import Flask, Response, abort, request, g
from peewee import *
from playhouse.postgres_ext import HStoreField
from playhouse.postgres_ext import PostgresqlExtDatabase
# Analytics settings.
BEACON = '47494638396101000100800000dbdfef00000021f90401000000002c00000000010001000002024401003b'.decode('hex') # 1px gif.
DATABASE_NAME = 'analytics'
DOMAIN = 'http://analytics.yourdomain.com' # TODO: change me.
JAVASCRIPT = """(function(id){
var d=document,i=new Image,e=encodeURIComponent;
i.src='%s/a.gif?id='+id+'&url='+e(d.location.href)+'&ref='+e(d.referrer)+'&t='+e(d.title);
})(%s)""".replace('\n', '')
# Flask settings.
DEBUG = bool(os.environ.get('DEBUG'))
SECRET_KEY = 'secret - change me' # TODO: change me.
app = Flask(__name__)
app.config.from_object(__name__)
database = PostgresqlExtDatabase(
DATABASE_NAME,
register_hstore=True,
user='postgres')
class BaseModel(Model):
class Meta:
database = database
class Account(BaseModel):
domain = CharField()
def verify_url(self, url):
netloc = urlparse(url).netloc
url_domain = '.'.join(netloc.split('.')[-2:]) # Ignore subdomains.
return self.domain == url_domain
class PageView(BaseModel):
account = ForeignKeyField(Account, backref='pageviews')
url = TextField()
timestamp = DateTimeField(default=datetime.datetime.now)
title = TextField(default='')
ip = CharField(default='')
referrer = TextField(default='')
headers = HStoreField()
params = HStoreField()
@classmethod
def create_from_request(cls, account, request):
parsed = urlparse(request.args['url'])
params = dict(parse_qsl(parsed.query))
return PageView.create(
account=account,
url=parsed.path,
title=request.args.get('t') or '',
ip=request.headers.get('x-forwarded-for', request.remote_addr),
referrer=request.args.get('ref') or '',
headers=dict(request.headers),
params=params)
@app.route('/a.gif')
def analyze():
# Make sure an account id and url were specified.
if not request.args.get('id') or not request.args.get('url'):
abort(404)
# Ensure the account id is valid.
try:
account = Account.get(Account.id == request.args['id'])
except Account.DoesNotExist:
abort(404)
# Ensure the account id matches the domain of the URL we wish to record.
if not account.verify_url(request.args['url']):
abort(403)
# Store the page-view data in the database.
PageView.create_from_request(account, request)
# Return a 1px gif.
response = Response(app.config['BEACON'], mimetype='image/gif')
response.headers['Cache-Control'] = 'private, no-cache'
return response
@app.route('/a.js')
def script():
account_id = request.args.get('id')
if account_id:
return Response(
app.config['JAVASCRIPT'] % (app.config['DOMAIN'], account_id),
mimetype='text/javascript')
return Response('', mimetype='text/javascript')
@app.errorhandler(404)
def not_found(e):
return Response('<h3>Not found.</h3>')
# Request handlers -- these two hooks are provided by flask and we will use them
# to create and tear down a database connection on each request.
@app.before_request
def before_request():
g.db = database
g.db.connection()
@app.after_request
def after_request(response):
g.db.close()
return response
if __name__ == '__main__':
database.create_tables([Account, PageView], safe=True)
app.run(debug=True)