X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/1f4c831ff6f96b3a34fcca497adfd16838abc569..0c502550a4f04aa016df8729af2ae2f85c0b495a:/src/stats/models.py?ds=inline diff --git a/src/stats/models.py b/src/stats/models.py index ea1a79042..ca0b55dab 100644 --- a/src/stats/models.py +++ b/src/stats/models.py @@ -1,36 +1,53 @@ import re +from urllib.request import urlopen +from django.apps import apps from django.conf import settings from django.db import models -class Visits(models.Model): +class VisitsBase(models.Model): book = models.ForeignKey('catalogue.Book', models.CASCADE) - year = models.PositiveSmallIntegerField() - month = models.PositiveSmallIntegerField() + date = models.DateField() views = models.IntegerField() unique_views = models.IntegerField() + class Meta: + abstract = True + @classmethod - def build_month(cls, year, month): - ### TODO: Delete existing? + def build_for_date(cls, date, period): + Book = apps.get_model('catalogue', 'Book') - date = f'{year}-{month:02d}' - url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period=month&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1' + date = date.isoformat() + url = f'{settings.PIWIK_URL}?date={date}&filter_limit=-1&format=CSV&idSite={settings.PIWIK_SITE_ID}&language=pl&method=Actions.getPageUrls&module=API&period={period}&segment=&token_auth={settings.PIWIK_TOKEN}&flat=1' data = urlopen(url).read().decode('utf-16') lines = data.split('\n')[1:] for line in lines: m = re.match('^/katalog/lektura/([^,]+)\.html,', line) if m is not None: slug = m.group(1) - _url, uviews, views = line.split(',', 3) + _url, uviews, views, _rest = line.split(',', 3) uviews = int(uviews) views = int(views) try: book = Book.objects.get(slug=slug) - except Book.DoesNotExists: + except Book.DoesNotExist: continue else: - cls.objects.create( - book=book, year=year, month=month, - views=views, uviews=uviews + cls.objects.update_or_create( + book=book, date=date, + defaults={'views': views, 'unique_views': uviews} ) + + +class Visits(VisitsBase): + @classmethod + def build_month(cls, date): + cls.build_for_date(date.replace(day=1), 'month') + + +class DayVisits(VisitsBase): + @classmethod + def build_day(cls, date): + cls.build_for_date(date, 'day') +