Commit cdca4150 authored by fred's avatar fred
Browse files

webstats: store referrer

parent 58ff5c71
......@@ -13,7 +13,7 @@ from panikweb.webstats.models import PodcastLogLine
class Command(BaseCommand):
def handle(self, *args, **kwargs):
regex = re.compile(
r'([(a-f\d\.\:)]+) - - \[(.*?)\] "GET /media/sounds/(.*?) HTTP/..." \d+ \d+ ".*?" "(.*?)"'
r'([(a-f\d\.\:)]+) - - \[(.*?)\] "GET /media/sounds/(.*?) HTTP/..." \d+ \d+ "(.*?)" "(.*?)"'
)
seen = {}
with open(settings.ACCESS_LOG_FILENAME) as fd:
......@@ -21,7 +21,7 @@ class Command(BaseCommand):
match = regex.match(line)
if not match:
continue
ip, date, path, user_agent = match.groups()
ip, date, path, referrer, user_agent = match.groups()
if not (path.endswith('.ogg') or path.endswith('.mp3')):
continue
log_datetime = dateutil.parser.parse(date.replace(':', ' ', 1))
......@@ -36,12 +36,15 @@ class Command(BaseCommand):
except SoundFile.DoesNotExist:
continue
PodcastLogLine.objects.get_or_create(
PodcastLogLine.objects.update_or_create(
timestamp=log_datetime,
ip=self.anonymise_ip(ip),
path=path,
soundfile=soundfile,
user_agent=user_agent,
defaults={
'soundfile': soundfile,
'user_agent': user_agent,
'referrer': referrer,
},
)
def anonymise_ip(self, ip):
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2020-12-13 16:59
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webstats', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='podcastlogline',
name='referrer',
field=models.CharField(max_length=255, null=True),
),
]
......@@ -7,4 +7,5 @@ class PodcastLogLine(models.Model):
path = models.CharField(max_length=255)
soundfile = models.ForeignKey('emissions.SoundFile', null=True, on_delete=models.SET_NULL)
user_agent = models.CharField(max_length=255)
referrer = models.CharField(max_length=255, null=True)
is_bot = models.NullBooleanField()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment