Switch cover for legimi.
[redakcja.git] / src / depot / legimi.py
index 3a6829a..75ddaa1 100644 (file)
@@ -4,8 +4,9 @@ from django.conf import settings
 from librarian.functions import lang_code_3to2
 from librarian.html import transform_abstrakt
 from librarian.builders import EpubBuilder, MobiBuilder
-from librarian.cover import LegimiCornerCover, LegimiCover
+from librarian.covers.marquise import MarquiseCover, LabelMarquiseCover
 import requests
+from slugify import slugify
 
 
 
@@ -17,6 +18,9 @@ fundraising=[
             "Przekaż 1% podatku na Wolne Lektury.<br/>\nKRS: 0000070056<br/>\nNazwa organizacji: Fundacja Nowoczesna Polska<br/>\nKażda wpłacona kwota zostanie przeznaczona na rozwój Wolnych Lektur."
 ]
 
+description_add = '<p>Książkę polecają <a href="https://wolnelektury.pl">Wolne Lektury</a> — najpopularniejsza biblioteka on-line.</p>'
+
+
 class Legimi:
     #BASE_URL = 'https://wydawca.legimi.com'
     BASE_URL = 'https://panel.legimi.pl'
@@ -73,20 +77,20 @@ class Legimi:
         wlbook = book.wldocument(librarian2=True)
         meta = wlbook.meta
 
-        cover = LegimiCornerCover(meta, width=1200).output_file()
-        epub_file = EpubBuilder(cover=LegimiCover, fundraising=fundraising).build(wlbook).get_file()
-        mobi_file = MobiBuilder(cover=LegimiCover, fundraising=fundraising).build(wlbook).get_file()
+        cover = LabelMarquiseCover(meta, width=1200).output_file()
+        epub_file = EpubBuilder(cover=MarquiseCover, fundraising=fundraising).build(wlbook).get_file()
+        mobi_file = MobiBuilder(cover=MarquiseCover, fundraising=fundraising).build(wlbook).get_file()
 
         book_data = {
             "Title": meta.title,
             "Author": ", ".join(p.readable() for p in meta.authors),
             "Year": meta.created_at[:4],
 
-            'GenreId': '11', # TODO
+            'GenreId': str(self.get_genre(wlbook)),
             'Isbn': '',
             'LanguageLocale': lang_code_3to2(meta.language),
 
-            'Description': '<p>—</p>',
+            'Description': self.get_description(wlbook),
         }
         if meta.isbn_html:
             isbn = meta.isbn_html
@@ -97,11 +101,6 @@ class Legimi:
 
         files_data = {}
 
-        abstract = wlbook.tree.find('.//abstrakt')
-        if abstract is not None:
-            book_data['Description'] = transform_abstrakt(abstract)
-        
-
         cover_data = self.upload(
             (meta.url.slug + '.jpg', cover.get_file(), 'image/jpeg')
         )
@@ -142,6 +141,77 @@ class Legimi:
                 book.legimi_id = legimi_id
                 book.save(update_fields=['legimi_id'])
 
+    def get_description(self, wlbook):
+        description = ''
+        abstract = wlbook.tree.find('.//abstrakt')
+        if abstract is not None:
+            description = transform_abstrakt(abstract)
+        description += description_add
+        description += '<p>'
+        description += ', '.join(
+            '<a href="https://wolnelektury.pl/katalog/autor/{}/">{}</a>'.format(
+                slugify(p.readable()),
+                p.readable(),
+            )
+            for p in wlbook.meta.authors
+        ) + '<br>'
+        description += '<a href="https://wolnelektury.pl/katalog/lektura/{}/">{}</a><br>'.format(
+            wlbook.meta.url.slug,
+            wlbook.meta.title
+        )
+        if wlbook.meta.translators:
+            description += 'tłum. ' + ', '.join(p.readable() for p in wlbook.meta.translators) + '<br>'
+        description += 'Epoka: ' + ', '.join(
+            '<a href="https://wolnelektury.pl/katalog/epoka/{}/">{}</a>'.format(
+                slugify(p),
+                p,
+            )
+            for p in wlbook.meta.epochs
+        ) + ' '
+        description += 'Rodzaj: ' + ', '.join(
+            '<a href="https://wolnelektury.pl/katalog/rodzaj/{}/">{}</a>'.format(
+                slugify(p),
+                p,
+            )
+            for p in wlbook.meta.kinds
+        ) + ' '
+        description += 'Gatunek: ' + ', '.join(
+            '<a href="https://wolnelektury.pl/katalog/gatunek/{}/">{}</a>'.format(
+                slugify(p),
+                p,
+            )
+            for p in wlbook.meta.genres
+        ) + '</p>'
+
+        if wlbook.meta.audience:
+            description += '<p><em>{}</em> to lektura szkolna.'.format(wlbook.meta.title)
+            if wlbook.tree.find('//pe') is not None:
+                description += '<br>Ebook <em>{title}</em> zawiera przypisy opracowane specjalnie dla uczennic i uczniów {school}.'.format(
+                    title=wlbook.meta.title,
+                    school='szkoły podstawowej' if wlbook.meta.audience == 'SP' else 'liceum i technikum'
+                )
+            description += '</p>'
+        return description
+
+    def get_genre(self, wlbook):
+        epoch_map = {
+            'Starożytność': 80,
+            'Średniowiecze': 81,
+            'Renesans': 82,
+            'Barok': 83,
+            'Oświecenie': 84,
+            'Romantyzm': 85,
+            'Pozytywizm': 86,
+            'Modernizm': 87,
+            'Dwudziestolecie międzywojenne': 88,
+            'Współczesność': 90,
+        }
+
+        for epoch in wlbook.meta.epochs:
+            if epoch in epoch_map:
+                return epoch_map[epoch]
+        return 11
+    
     def create_book(self, book_data, files_data):
         data = {
             'createValidationTrue': 'true',