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, )