2 from urllib.request import urlopen
3 from django.apps import apps
4 from django.conf import settings
5 from django.db import models
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()
18 def build_for_date(cls, date, period):
19 Book = apps.get_model('catalogue', 'Book')
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:]
26 m = re.match('^/katalog/lektura/([^,]+)\.html,', line)
29 _url, uviews, views, _rest = line.split(',', 3)
33 book = Book.objects.get(slug=slug)
34 except Book.DoesNotExist:
37 cls.objects.update_or_create(
39 defaults={'views': views, 'unique_views': uviews}
43 class Visits(VisitsBase):
45 def build_month(cls, date):
46 cls.build_for_date(date.replace(day=1), 'month')
49 class DayVisits(VisitsBase):
51 def build_day(cls, date):
52 cls.build_for_date(date, 'day')