X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/2d538a50605add2666172861744229599487f1b2..180cb43d814a4abe2811f29630ffffaaf9cfa9b7:/src/depot/publishers/legimi.py?ds=sidebyside diff --git a/src/depot/publishers/legimi.py b/src/depot/publishers/legimi.py index c7fa26df..11a1c038 100644 --- a/src/depot/publishers/legimi.py +++ b/src/depot/publishers/legimi.py @@ -1,18 +1,20 @@ from datetime import date import re +from urllib.parse import urljoin from django.conf import settings from django.utils.html import escape from django.utils.safestring import mark_safe from librarian.functions import lang_code_3to2 from librarian.builders import EpubBuilder, MobiBuilder from librarian.covers.marquise import MarquiseCover, LabelMarquiseCover +from lxml import html +from catalogue.models import Audience, Thema from .base import BasePublisher class Legimi(BasePublisher): - BASE_URL = 'https://panel.legimi.pl' - LOGIN_URL = BASE_URL + '/publishers/membership' - UPLOAD_URL = BASE_URL + '/administration/upload/start' + BASE_URL = 'https://wydawca.legimi.pl' + UPLOAD_URL = BASE_URL + '/upload/start' CREATE_URL = BASE_URL + '/publishers/publications/create' EDIT_URL = BASE_URL + '/publishers/publications/edit/%s' EDIT_FILES_URL = BASE_URL + '/publishers/publications/editfiles/%s' @@ -120,38 +122,35 @@ class Legimi(BasePublisher): } def login(self): - self._session.post( - self.LOGIN_URL, - data={ - 'ValidationTrue': 'true', - 'UserName': self.username, - 'Password': self.password, - }) - - def can_publish(self, shop, book): - meta = book.wldocument(librarian2=True).meta + r = self._session.get(self.BASE_URL) + h = html.fromstring(r.text) + form = h.findall('.//form')[0] + login_url = urljoin(r.url, form.action) + data = {e.name: e.value for e in form.findall('.//input')} + data['Login'] = self.username + data['Password'] = self.password + self._session.post(login_url, data=data) + + def can_publish(self, site, book): d = { 'errors': [], 'warnings': [], + 'info': [] } - if meta.thema_main or meta.thema: - if meta.thema_main: - comment = "w kategorii {code}".format( - code=escape(meta.thema_main) - ) - if meta.thema: - comment += " oraz: " + ", ".join( - "{code}".format(code=escape(t)) - for t in meta.thema - ) - d['comment'] = mark_safe(comment) - elif meta.thema: - d['comment'] = mark_safe( - "w kategorii " + ", ".join( - "{code}".format(code=escape(t)) - for t in meta.thema - ) + try: + meta = book.wldocument(librarian2=True).meta + except: + d['errors'].append('Nieprawidłowy dokument.') + return d + thema = self.get_thema(meta) + if thema: + d['info'].append(mark_safe( + "w kategorii " + ", ".join( + "{code}".format(code=escape(t)) + for t in thema ) + )) + if not meta.thema_main: d['warnings'].append('Brak głównej kategorii Thema') else: d['errors'].append('Brak kategorii Thema.') @@ -173,12 +172,28 @@ class Legimi(BasePublisher): "url": model['Url'], } - def send_book(self, shop, book, changes=None): + def get_thema(self, meta): + thema = [] + if meta.thema_main: + thema.append(meta.thema_main) + thema.extend(meta.thema) + + thema.extend( + Audience.objects.filter(code__in=meta.audiences).exclude( + thema=None).values_list('thema', flat=True) + ) + thema = [Thema.standardize(t) for t in thema] + return thema + + def send_book(self, site_book_publish, changes=None): + site_book = site_book_publish.site_book + site = site_book.site + book = site_book.book wlbook = book.wldocument(librarian2=True, changes=changes) meta = wlbook.meta cover = LabelMarquiseCover(meta, width=1200).output_file() - texts = shop.get_texts() + texts = site.get_texts() epub_file = EpubBuilder( cover=MarquiseCover, fundraising=texts, @@ -190,23 +205,18 @@ class Legimi(BasePublisher): base_url='file://' + book.gallery_path() + '/' ).build(wlbook).get_file() - thema = [] - if meta.thema_main: - thema.append(meta.thema_main) - thema.extend(meta.thema) - book_data = { "Title": meta.title, "Author": ", ".join(p.readable() for p in meta.authors), "Year": str(date.today().year), 'GenreId': str(self.get_genre(wlbook)), - 'themaCategories': ';'.join(thema), + 'themaCategories': ';'.join(self.get_thema(meta)), 'thema-search': '', 'Isbn': '', 'LanguageLocale': lang_code_3to2(meta.language), - 'Description': self.get_description(wlbook, shop.description_add), + 'Description': self.get_description(wlbook, site.description_add), } if meta.isbn_html: isbn = meta.isbn_html @@ -243,22 +253,22 @@ class Legimi(BasePublisher): 'BookMobi.Name': mobi_data['name'], }) - if book.legimi_id: + if site_book.external_id: self.edit( - book.legimi_id, + site_book.external_id, book_data ) self.edit_files( - book.legimi_id, + site_book.external_id, files_data ) else: legimi_id = self.create_book(book_data, files_data) if legimi_id: - book.legimi_id = legimi_id - book.save(update_fields=['legimi_id']) + site_book.external_id = legimi_id + site_book.save(update_fields=['external_id']) - self.edit_sale(book) + self.edit_sale(site_book) def get_genre(self, wlbook): if wlbook.meta.legimi and wlbook.meta.legimi in self.CATEGORIES: @@ -308,8 +318,8 @@ class Legimi(BasePublisher): response = self.session.post(self.CREATE_URL, data=data) m = re.search(r'/(\d+)$', response.url) - if m is not None: - return m.group(1) + assert m is not None, 'Legimi.create_book: ' + response.text + return m.group(1) def edit(self, legimi_id, data): current = { @@ -353,10 +363,11 @@ class Legimi(BasePublisher): data=current ) - def edit_sale(self, book): - assert book.legimi_id + def edit_sale(self, site_book): + book = site_book.book + assert site_book.external_id - words = book.wldocument().get_statistics()['total']['words_with_fn'] + words = book.wldocument(librarian2=True).get_statistics()['total']['words_with_fn'] price = settings.LEGIMI_SMALL_PRICE if words > settings.LEGIMI_SMALL_WORDS: @@ -366,7 +377,7 @@ class Legimi(BasePublisher): data = { 'ValidationTrue': 'true', - 'Id': book.legimi_id, + 'Id': site_book.external_id, 'SalesPromotionId': "0", 'IsLibraryPass': "False", 'OriginalEnterToTheMarketType': "No", @@ -385,6 +396,6 @@ class Legimi(BasePublisher): } self.session.post( - self.EDIT_SALE_URL % book.legimi_id, + self.EDIT_SALE_URL % site_book.external_id, data=data )