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