X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/a7c64b2ae44b54daac2d4bce4dc0c2d675e04504..5649823d030f580e26857963075af04f932c18ba:/src/depot/publishers/woblink.py?ds=sidebyside diff --git a/src/depot/publishers/woblink.py b/src/depot/publishers/woblink.py index bac0f73b..e98ec585 100644 --- a/src/depot/publishers/woblink.py +++ b/src/depot/publishers/woblink.py @@ -124,6 +124,8 @@ class Woblink(BasePublisher): STEP1_URL = BASE_URL + 'catalog/edit/%s' STEP2_URL = BASE_URL + 'catalog/edit/%s/2' STEP3_URL = BASE_URL + 'catalog/edit/%s/3' + STEP4_URL = BASE_URL + 'catalog/edit/%s/4' + STEP5_URL = BASE_URL + 'catalog/edit/%s/5' UPLOAD_URL = BASE_URL + 'file/upload-%s' JOB_STATUS_URL = BASE_URL + 'task/status' GENERATE_DEMO_URL = BASE_URL + 'task/run/generate-%s-demo/%s/%d' @@ -218,6 +220,12 @@ class Woblink(BasePublisher): if errors is not None: errors.append(NoMainThemaWarning()) thema_codes.extend(meta.thema) + + thema_codes.extend( + Audience.objects.filter(code__in=meta.audiences).exclude( + thema=None).values_list('thema', flat=True) + ) + if not thema_codes: if errors is not None: errors.append(NoThema()) @@ -243,26 +251,35 @@ class Woblink(BasePublisher): return category_ids def get_series(self, meta, errors=None): - return list(Audience.objects.filter(code__in=audiences).exclude( + return list(Audience.objects.filter(code__in=meta.audiences).exclude( woblink=None).values_list('woblink', flat=True)) def get_abstract(self, wldoc, errors=None, description_add=None): description = self.get_description(wldoc, description_add) parts = description.split('\n', 1) - if len(parts) == 1 or len(parts[0]) > 200: - p1 = description[:200].rsplit(' ', 1)[0] - p2 = description[len(p1):] - p1 += '…' - p2 = '…' + p2 + if len(parts) == 1 or len(parts[0]) > 240: + # No newline found here. + # Try to find last sentence end.. + parts = re.split(r' \.', description[240::-1], 1) + if len(parts) == 2: + p1 = parts[1][::-1] + '.' + p2 = description[len(p1) + 1:] + else: + # No sentence end found. + # Just find a space. + p1 = description[:240].rsplit(' ', 1)[0] + p2 = description[len(p1) + 1:] + p1 += '…' + p2 = '…' + p2 parts = [p1, p2] m = re.search(r'<[^>]+$', parts[0]) if m is not None: - parts[0] = parts[:-len(m.group(0))] + parts[0] = parts[0][:-len(m.group(0))] parts[1] = m.group(0) + parts[1] opened = [] - for tag in re.findall(r'<[^>]+[^/>]>', parts[0]): + for tag in re.findall(r'<[^>]*[^/>]>', parts[0]): if tag[1] == '/': opened.pop() else: @@ -279,7 +296,12 @@ class Woblink(BasePublisher): return lang_code_3to2(meta.language) def get_price(self, shop, wldoc, errors=None): - stats = wldoc.get_statistics()['total'] + try: + stats = wldoc.get_statistics()['total'] + except: + if errors: + errors.append(NoPrice(shop)) + return 0 words = stats['words_with_fn'] pages = stats['chars_with_fn'] / 1800 price = shop.get_price(words, pages) @@ -295,6 +317,7 @@ class Woblink(BasePublisher): d = { 'warnings': [], 'errors': [], + 'info': [], } errors = [] book_data = self.get_book_data(shop, wldoc, errors) @@ -306,11 +329,15 @@ class Woblink(BasePublisher): errlist.append(error.as_html()) if book_data.get('genres'): - d['comment'] = format_html( + d['info'].append(format_html( 'W kategoriach: {cat} ({price} zł)', cat=', '.join(self.describe_category(g) for g in book_data['genres']), - price=book_data['price'] - ) + price=book_data['price'], + )) + d['info'].append(mark_safe( + '' + book_data['abstract']['header'] + + '
' + book_data['abstract']['rest'] + )) return d @@ -354,16 +381,22 @@ class Woblink(BasePublisher): self.edit_step1(book.woblink_id, book_data) self.edit_step2(book.woblink_id, book_data) self.edit_step3(book.woblink_id, book_data) - self.send_cover(book.woblink_id, wldoc) + cover_id = self.send_cover(book.woblink_id, wldoc) + texts = shop.get_texts() - self.send_epub( + epub_id, epub_demo = self.send_epub( book.woblink_id, wldoc, book.gallery_path(), fundraising=texts ) - self.send_mobi( + mobi_id, mobi_demo = self.send_mobi( book.woblink_id, wldoc, book.gallery_path(), fundraising=texts ) + self.edit_step4( + book.woblink_id, book_data, + cover_id, epub_id, epub_demo, mobi_id, mobi_demo, + ) + self.edit_step5(book.woblink_id, book_data) def get_book_data(self, shop, wldoc, errors=None): return { @@ -412,8 +445,7 @@ class Woblink(BasePublisher): 'pubNote': data['abstract']['rest'], 'pubCulture': data['lang2code'], 'npwPublicationHasAwards': '[]', - 'npwPublicationHasSeriess': json.dumps(series_id), - # "[{\"Id\":6153,\"PublicationId\":73876,\"SeriesId\":1615,\"Tome\":null}]" + 'npwPublicationHasSeriess': json.dumps(series_data), } d = self.with_form_name(d, 'EditPublicationStep1') d['roles'] = [author_type for (author_type, author_id) in data['authors']] @@ -462,10 +494,30 @@ class Woblink(BasePublisher): 'pubFileFormat': 'epub+mobi', 'pubIsAcs': '0', 'pubPublisherIndex': '', + 'save_and_continue': '', } d = self.with_form_name(d, 'EditPublicationStep3') return self.session.post(self.STEP3_URL % woblink_id, data=d) + def edit_step4(self, woblink_id, book_data, cover_id, epub_id, epub_demo, mobi_id, mobi_demo): + d = { + 'pubCoverResId': cover_id, + 'pubEpubResId': epub_id, + 'pubEpubDemoResId': epub_demo, + 'pubMobiResId': mobi_id, + 'pubMobiDemoResId': mobi_demo, + 'pubFileFormat': 'epub+mobi', + 'pubId': woblink_id, + 'save_and_continue': '', + } + d = self.with_form_name(d, 'EditPublicationStep4') + return self.session.post(self.STEP4_URL % woblink_id, data=d) + + def edit_step5(self, woblink_id, book_data): + d = {'save': ''} + d = self.with_form_name(d, 'EditPublicationStep5') + return self.session.post(self.STEP5_URL % woblink_id, data=d) + def wait_for_job(self, job_id): while True: response = self.session.post( @@ -475,16 +527,21 @@ class Woblink(BasePublisher): data = response.json()[job_id] if data['ready']: assert data['successful'] - return + return data.get('returnValue') sleep(2) - def upload_file(self, woblink_id, filename, content, form_name, field_name, mime_type): + def upload_file(self, woblink_id, filename, content, field_name, mime_type): + form_name = f'Upload{field_name}' + id_field = f'pub{field_name}ResId' + field_name = field_name.lower() + data = { 'pubId': woblink_id, } files = { field_name: (filename, content, mime_type) } + response = self.session.post( self.UPLOAD_URL % field_name, data=self.with_form_name(data, form_name), @@ -492,8 +549,10 @@ class Woblink(BasePublisher): ) resp_data = response.json() assert resp_data['success'] is True + file_id = resp_data[id_field] if 'jobId' in resp_data: self.wait_for_job(resp_data['jobId']) + return file_id def generate_demo(self, woblink_id, file_format, check=True): percent = 10 @@ -502,7 +561,7 @@ class Woblink(BasePublisher): self.GENERATE_DEMO_URL % (file_format, woblink_id, percent), ).json()['jobId'] try: - self.wait_for_job(job_id) + file_id = self.wait_for_job(job_id) except AssertionError: if percent < 50: percent += 10 @@ -517,6 +576,7 @@ class Woblink(BasePublisher): self.CHECK_DEMO_URL % (file_format, woblink_id) ).json()['jobId'] ) + return file_id def send_epub(self, woblink_id, doc, gallery_path, fundraising=None): from librarian.builders import EpubBuilder @@ -524,15 +584,15 @@ class Woblink(BasePublisher): base_url='file://' + gallery_path + '/', fundraising=fundraising or [], ).build(doc).get_file() - self.upload_file( + file_id = self.upload_file( woblink_id, doc.meta.url.slug + '.epub', content, - 'UploadEpub', - 'epub', + 'Epub', 'application/epub+zip' ) - self.generate_demo(woblink_id, 'epub') + demo_id = self.generate_demo(woblink_id, 'epub') + return file_id, demo_id def send_mobi(self, woblink_id, doc, gallery_path, fundraising=None): from librarian.builders import MobiBuilder @@ -540,15 +600,15 @@ class Woblink(BasePublisher): base_url='file://' + gallery_path + '/', fundraising=fundraising or [], ).build(doc).get_file() - self.upload_file( + file_id = self.upload_file( woblink_id, doc.meta.url.slug + '.mobi', content, - 'UploadMobi', - 'mobi', + 'Mobi', 'application/x-mobipocket-ebook' ) - self.generate_demo(woblink_id, 'mobi', check=False) + demo_id = self.generate_demo(woblink_id, 'mobi', check=False) + return file_id, demo_id def send_cover(self, woblink_id, doc): from librarian.cover import make_cover @@ -558,11 +618,11 @@ class Woblink(BasePublisher): content = io.BytesIO() cover.final_image().save(content, cover.format) content.seek(0) - self.upload_file( + file_id = self.upload_file( woblink_id, doc.meta.url.slug + '.jpeg', content, - 'UploadCover', - 'cover', + 'Cover', cover.mime_type() ) + return file_id