X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/d66f9960d12d739c44a89d1d4e013f52783097e7..00cdeb066b2337b877a95ac5cc467cef6cba498e:/src/references/models.py?ds=inline diff --git a/src/references/models.py b/src/references/models.py index 70493d347..d971ba615 100644 --- a/src/references/models.py +++ b/src/references/models.py @@ -1,4 +1,5 @@ import json +import urllib.parse from django.db import models from wikidata.client import Client @@ -8,6 +9,7 @@ class Entity(models.Model): WIKIDATA_IMAGE = 'P18' WIKIDATA_COORDINATE_LOCATION = 'P625' WIKIDATA_EARTH = 'Q2' + WIKIDATA_IMAGE_QUERY = './w/api.php?action=query&titles={}&format=json&prop=imageinfo&iiprop=url&iiurlwidth=240&iiurlheight=200' uri = models.CharField(max_length=255, unique=True, db_index=True) label = models.CharField(max_length=1024, blank=True) @@ -29,6 +31,9 @@ class Entity(models.Model): def populate_from_wikidata(self, wikidata_id): client = Client() + client.opener.addheaders = [( + 'User-Agent', 'Wolne Lektury / https://wolnelektury.pl / Python-wikidata' + )] entity = client.get(wikidata_id) self.label = entity.label.get('pl', entity.label) or '' @@ -43,20 +48,91 @@ class Entity(models.Model): self.lon = location.longitude images = entity.getlist(client.get(self.WIKIDATA_IMAGE)) - self.images = json.dumps([ - { + image_data_list = [] + for image in images: + image_data = { "url": image.image_url, "page": image.page_url, "resolution": image.image_resolution, - } for image in images - ]) + } + result = client.request( + self.WIKIDATA_IMAGE_QUERY.format( + urllib.parse.quote(image.title) + ) + ) + + result_data = next(iter(result['query']['pages'].values()))['imageinfo'][0] + image_data['thumburl'] = result_data['thumburl'] + image_data['thumbresolution'] = [ + result_data['thumbwidth'], + result_data['thumbheight'] + ] + if 'responsiveUrls' in result_data: + image_data['responsiveUrls'] = result_data['responsiveUrls'] + + image_data_list.append(image_data) + + self.images = json.dumps(image_data_list) + + def get_images(self): + return json.loads(self.images) + + def get_pretty_coords(self): + def to_minutes(n): + coo = [] + coo.append(int(n)) + n -= coo[-1] + n *= 60 + coo.append(int(n)) + n -= coo[-1] + n *= 60 + coo.append(n) + return coo + + if not self.lat or not self.lon: + return '' + + lat, latm, lon, lonm = self.lat, 'N', self.lon, 'E' + if lat < 0: + latm = 'S' + lat = -lat + if lon < 0: + lonm = 'W' + lon = -lon + + return '%d°%d\'%d"%s %d°%d\'%d"%s' % ( + *to_minutes(lat), + latm, + *to_minutes(lon), + lonm, + ) + + def get_json(self): + data = { + "id": self.id, + "images": self.get_images(), + "label": self.label, + "description": self.description, + "wikipedia_link": self.wikipedia_link, + } + if self.lat and self.lon: + data['location'] = [self.lat, self.lon] + return json.dumps(data) + class Reference(models.Model): book = models.ForeignKey('catalogue.Book', models.CASCADE) entity = models.ForeignKey(Entity, models.CASCADE) - first_section = models.CharField(max_length=255) class Meta: unique_together = (('book', 'entity'),) + +class Occurence(models.Model): + reference = models.ForeignKey(Reference, models.CASCADE) + section = models.IntegerField() + html = models.TextField() + + class Meta: + ordering = ('section',)