import pymongo import re from datetime import datetime from django con

 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
import pymongo
import re
from datetime import datetime
from django.conf import settings
from database import stat_db
RE_GOOGLE_SEARCH = re.compile(r'[^-a-z0-9]google\.[a-z]{2,3}(\.[a-z]{2,3})?(\?|/)')
RE_YANDEX_SEARCH = re.compile(r'[^-a-z0-9]yandex\.[a-z]{2,3}(\.[a-z]{2,3})?(\?|/)')
def strip_www(val):
if val.startswith('www.'):
return val[4:]
else:
return val
class StatMiddleware(object):
def process_request(self, request):
if request.path.startswith(settings.MEDIA_URL):
return
if request.path.startswith(('/favicon.ico')):
return
if request.path == '/stat':
return
if request.path.startswith('/%s/' % request.salt['memberzone']):
return
host = strip_www(request.get_host().split(':')[0])
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
update = {
'hit': 1,
}
user_agent = request.META.get('HTTP_USER_AGENT', '')
referer = request.META.get('HTTP_REFERER', '')
if 'http://yandex.com/bots' in user_agent:
update['yandex_bot'] = 1
if 'http://www.google.com/bot.html' in user_agent:
update['google_bot'] = 1
if RE_YANDEX_SEARCH.search(referer):
update['yandex_search'] = 1
if RE_GOOGLE_SEARCH.search(referer):
update['google_search'] = 1
print 'UPDATE', host, today
stat_db.stat.update(
{'host': host, 'date': today},
{'$inc': update},
upsert=True,
)