c1b4d16d8e208340a013dc0227508760052460cf
[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 Visits(models.Model):
9     book = models.ForeignKey('catalogue.Book', models.CASCADE)
10     year = models.PositiveSmallIntegerField()
11     month = models.PositiveSmallIntegerField()
12     views = models.IntegerField()
13     unique_views = models.IntegerField()
14
15     @classmethod
16     def build_month(cls, year, month):
17         Book = apps.get_model('catalogue', 'Book')
18         ### TODO: Delete existing?
19
20         date = f'{year}-{month:02d}'
21         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'
22         data = urlopen(url).read().decode('utf-16')
23         lines = data.split('\n')[1:]
24         for line in lines:
25             m = re.match('^/katalog/lektura/([^,]+)\.html,', line)
26             if m is not None:
27                 slug = m.group(1)
28                 _url, uviews, views, _rest = line.split(',', 3)
29                 uviews = int(uviews)
30                 views = int(views)
31                 try:
32                     book = Book.objects.get(slug=slug)
33                 except Book.DoesNotExist:
34                     continue
35                 else:
36                     cls.objects.create(
37                         book=book, year=year, month=month,
38                         views=views, unique_views=uviews
39                     )