Fetch thumbnails from wikidata.
[wolnelektury.git] / src / references / models.py
1 import json
2 import urllib.parse
3 from django.db import models
4 from wikidata.client import Client
5
6
7 class Entity(models.Model):
8     WIKIDATA_PREFIX = 'https://www.wikidata.org/wiki/'
9     WIKIDATA_IMAGE = 'P18'
10     WIKIDATA_COORDINATE_LOCATION = 'P625'
11     WIKIDATA_EARTH = 'Q2'
12     WIKIDATA_IMAGE_QUERY = './w/api.php?action=query&titles={}&format=json&prop=imageinfo&iiprop=url&iiurlwidth=240&iiurlheight=200'
13
14     uri = models.CharField(max_length=255, unique=True, db_index=True)
15     label = models.CharField(max_length=1024, blank=True)
16     description = models.CharField(max_length=2048, blank=True)
17     wikipedia_link = models.CharField(max_length=1024, blank=True)
18     lat = models.FloatField(null=True, blank=True)
19     lon = models.FloatField(null=True, blank=True)
20     images = models.TextField(blank=True)
21
22     @property
23     def is_interesting(self):
24         return self.wikipedia_link or (self.lat and self.lon) or len(self.images)>2
25     
26     def populate(self):
27         if self.uri.startswith(self.WIKIDATA_PREFIX):
28             self.populate_from_wikidata(
29                 self.uri[len(self.WIKIDATA_PREFIX):]
30             )
31
32     def populate_from_wikidata(self, wikidata_id):
33         client = Client()
34         entity = client.get(wikidata_id)
35
36         self.label = entity.label.get('pl', entity.label) or ''
37         self.description = entity.description.get('pl', entity.description) or ''
38         sitelinks = entity.attributes.get('sitelinks', {})
39         self.wikipedia_link = sitelinks.get('plwiki', sitelinks.get('enwiki', {})).get('url') or ''
40
41         location_prop = client.get(self.WIKIDATA_COORDINATE_LOCATION)
42         location = entity.get(location_prop)
43         if location and location.globe.id == self.WIKIDATA_EARTH:
44             self.lat = location.latitude
45             self.lon = location.longitude
46
47         images = entity.getlist(client.get(self.WIKIDATA_IMAGE))
48         image_data_list = []
49         for image in images:
50             image_data = {
51                 "url": image.image_url,
52                 "page": image.page_url,
53                 "resolution": image.image_resolution,
54             }
55
56             result = client.request(
57                 self.WIKIDATA_IMAGE_QUERY.format(
58                     urllib.parse.quote(image.title)
59                 )
60             )
61
62             result_data = next(iter(result['query']['pages'].values()))['imageinfo'][0]
63             image_data['thumburl'] = result_data['thumburl']
64             image_data['thumbresolution'] = [
65                 result_data['thumbwidth'],
66                 result_data['thumbheight']
67             ]
68             image_data['responsiveUrls'] = result_data['responsiveUrls']
69
70             image_data_list.append(image_data)
71
72         self.images = json.dumps(image_data_list)
73
74
75 class Reference(models.Model):
76     book = models.ForeignKey('catalogue.Book', models.CASCADE)
77     entity = models.ForeignKey(Entity, models.CASCADE)
78     first_section = models.CharField(max_length=255)
79
80     class Meta:
81         unique_together = (('book', 'entity'),)
82