Layout fixes, cover update for files.
[wolnelektury.git] / src / stats / models.py
1 import re
2 from urllib.request import urlopen
3 from django.apps import apps
4 from django.conf import settings
5 from django.db import models
6
7
8 class VisitsBase(models.Model):
9     book = models.ForeignKey('catalogue.Book', models.CASCADE)
10     date = models.DateField()
11     views = models.IntegerField()
12     unique_views = models.IntegerField()
13
14     class Meta:
15         abstract = True
16
17     @classmethod
18     def build_for_date(cls, date, period):
19         Book = apps.get_model('catalogue', 'Book')
20
21         date = date.isoformat()
22         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'
23         data = urlopen(url).read().decode('utf-16')
24         lines = data.split('\n')[1:]
25         for line in lines:
26             m = re.match('^/katalog/lektura/([^,]+)\.html,', line)
27             if m is not None:
28                 slug = m.group(1)
29                 _url, uviews, views, _rest = line.split(',', 3)
30                 uviews = int(uviews)
31                 views = int(views)
32                 try:
33                     book = Book.objects.get(slug=slug)
34                 except Book.DoesNotExist:
35                     continue
36                 else:
37                     cls.objects.update_or_create(
38                         book=book, date=date,
39                         defaults={'views': views, 'unique_views': uviews}
40                     )
41
42
43 class Visits(VisitsBase):
44     @classmethod
45     def build_month(cls, date):
46         cls.build_for_date(date.replace(day=1), 'month')
47
48
49 class DayVisits(VisitsBase):
50     @classmethod
51     def build_day(cls, date):
52         cls.build_for_date(date, 'day')
53