Fetch thumbnails from wikidata.
authorRadek Czajka <rczajka@rczajka.pl>
Mon, 5 Apr 2021 13:03:23 +0000 (15:03 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Mon, 5 Apr 2021 13:03:23 +0000 (15:03 +0200)
src/catalogue/models/book.py
src/catalogue/templates/catalogue/book_text.html
src/references/models.py
src/wolnelektury/static/js/book_text/references.js
src/wolnelektury/static/scss/book_text/references.scss

index 5a0f169..978bca4 100644 (file)
@@ -677,6 +677,10 @@ class Book(models.Model):
                 entity.save()
         Reference.objects.filter(book=self).exclude(entity__uri__in=found).delete()
     
+    @property
+    def references(self):
+        return self.reference_set.all().select_related('entity')
+
     @classmethod
     @transaction.atomic
     def repopulate_ancestors(cls):
index 27601d8..e10bc96 100644 (file)
   {% localize off %}
   <script type="application/json" id="interesting-references">
    {
-       {% for ref in book.reference_set.all %}
+       {% for ref in book.references %}
        {% if ref.entity.is_interesting %}
        "{{ ref.entity.uri }}": {
            {% if ref.entity.lat and ref.entity.lon %}
index 70493d3..c986471 100644 (file)
@@ -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)
@@ -43,13 +45,31 @@ 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']
+            ]
+            image_data['responsiveUrls'] = result_data['responsiveUrls']
+
+            image_data_list.append(image_data)
+
+        self.images = json.dumps(image_data_list)
 
 
 class Reference(models.Model):
index 4c72fe6..b75a4da 100644 (file)
             $.each(ref.images, function(i, e) {
                 $i = $("<a target='_blank'><img></a>");
                 $i.attr('href', e.page);
-                $('img', $i).attr('src', e.url);
+                $('img', $i).attr('src', e.thumburl || e.url);
+                if (e.thumbresolution) {
+                    $('img', $i).attr('width', e.thumbresolution[0]).attr('height', e.thumbresolution[1]);
+                }
+
                 $("#reference-images").append($i);
             })
         }
index e188025..3ef469a 100644 (file)
@@ -34,7 +34,11 @@ a.reference.interesting::after {
             vertical-align: middle;
             margin: 0 10px 0 0;
             img {
-                height: 100px;
+               width: auto;
+               border: 5px solid white;
+               box-shadow: 4px 4px 4px #666;
+               max-width: 240px;
+               max-height: 200px;
             }
         }
     }