From 9b34bf899d6fee6edf5ed6e3b5902da89cc1a727 Mon Sep 17 00:00:00 2001 From: Loic Houpert <10154151+lhoupert@users.noreply.github.com> Date: Thu, 14 Jan 2021 16:24:53 +0100 Subject: [PATCH 1/3] cover update2 --- recipes/mediapart.recipe | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/recipes/mediapart.recipe b/recipes/mediapart.recipe index facf3e7aab..b0b366e5e5 100644 --- a/recipes/mediapart.recipe +++ b/recipes/mediapart.recipe @@ -7,7 +7,7 @@ # been added through custom entries in the function my_parse_index. # 3) Fix the cover image so it doesnt disappear from the Kindle menu # ( cover image format is changed to .jpeg) -# +# 14 Jan 2021 - Add Mediapart Logo url as masthead_url from __future__ import unicode_literals __license__ = 'GPL v3' @@ -18,6 +18,7 @@ Mediapart import re from datetime import date, datetime, timedelta +from time import strftime from calibre.web.feeds import feeds_from_index from calibre.web.feeds.news import BasicNewsRecipe @@ -50,6 +51,7 @@ class Mediapart(BasicNewsRecipe): remove_tags = [classes('login-subscribe print-source_url')] conversion_options = {'smarten_punctuation': True} + masthead_url = "https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart_masthead.png" cover_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart.jpeg' # -- @@ -240,3 +242,22 @@ class Mediapart(BasicNewsRecipe): br['password'] = self.password br.submit() return br + + def default_cover(self, cover_file): + """ + Create a generic cover for recipes that don't have a cover + This override adds time to the cover + """ + try: + from calibre.ebooks import calibre_cover + title = self.title if isinstance(self.title, unicode) else \ + self.title.decode('utf-8', 'replace') + date = strftime(self.timefmt) + time = strftime('%a %d %b %Y %-H:%M') + img_data = calibre_cover(title, date, time) + cover_file.write(img_data) + cover_file.flush() + print(f'title: {title}\ndate: {date}\ntime: {time}') + except: + self.log.exception('Failed to generate default cover') + return False From 678892168458a8edadba6e5ff257b3c232d8f199 Mon Sep 17 00:00:00 2001 From: Loic Houpert <10154151+lhoupert@users.noreply.github.com> Date: Thu, 14 Jan 2021 22:46:00 +0100 Subject: [PATCH 2/3] add date of creation on cover --- recipes/mediapart.recipe | 85 +++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 15 deletions(-) diff --git a/recipes/mediapart.recipe b/recipes/mediapart.recipe index b0b366e5e5..e4911a9cf2 100644 --- a/recipes/mediapart.recipe +++ b/recipes/mediapart.recipe @@ -7,7 +7,8 @@ # been added through custom entries in the function my_parse_index. # 3) Fix the cover image so it doesnt disappear from the Kindle menu # ( cover image format is changed to .jpeg) -# 14 Jan 2021 - Add Mediapart Logo url as masthead_url +# 14 Jan 2021 - Add Mediapart Logo url as masthead_url and change cover +# by adding overlaying the date on top of the Mediapart cover from __future__ import unicode_literals __license__ = 'GPL v3' @@ -18,11 +19,15 @@ Mediapart import re from datetime import date, datetime, timedelta -from time import strftime - +import urllib.request +from PyQt5.Qt import ( + QImage, Qt, QFont, QPainter, QPointF, QTextLayout, QTextOption, + QFontMetrics, QTextCharFormat, QColor, QRect, QBrush, QLinearGradient, + QPainterPath, QPen, QRectF, QTransform, QRadialGradient +) from calibre.web.feeds import feeds_from_index from calibre.web.feeds.news import BasicNewsRecipe - +from polyglot.builtins import unicode_type def classes(classes): q = frozenset(classes.split(' ')) @@ -52,7 +57,7 @@ class Mediapart(BasicNewsRecipe): conversion_options = {'smarten_punctuation': True} masthead_url = "https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart_masthead.png" - cover_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart.jpeg' + # cover_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart.jpeg' # -- @@ -243,21 +248,71 @@ class Mediapart(BasicNewsRecipe): br.submit() return br + # def default_cover(self, cover_file): + # """ + # Create a generic cover for recipes that don't have a cover + # This override adds time to the cover + # """ + # try: + # from calibre.ebooks import calibre_cover + # title = self.title if isinstance(self.title, unicode) else \ + # self.title.decode('utf-8', 'replace') + # date = strftime(self.timefmt) + # time = strftime('%a %d %b %Y %-H:%M') + # img_data = calibre_cover(title, date, time) + # cover_file.write(img_data) + # cover_file.flush() + # print(f'title: {title}\ndate: {date}\ntime: {time}') + # except: + # self.log.exception('\nFAILED TO GENERATE DEFAULT COVER\n') + # return False + + + def default_cover(self, cover_file): - """ + ''' Create a generic cover for recipes that don't have a cover - This override adds time to the cover - """ + ''' + from calibre.gui2 import ensure_app, config, load_builtin_fonts, pixmap_to_data + def init_environment(): + ensure_app() + load_builtin_fonts() + + def create_cover_mediapart(date): + ' Create a cover for mediapart adding the date on Mediapart Cover' + init_environment() + # Get data + image_url = 'https://raw.githubusercontent.com/lhoupert/calibre_contrib/main/mediapart.jpeg' #'mediapart.jpeg' + data = urllib.request.urlopen(image_url).read() + img = QImage() + img.loadFromData(data) + p = QPainter(img) + + pen = QPen(Qt.black) + pen.setWidth(4) + p.setPen(pen) + + font = QFont() + font.setFamily('Times') + font.setBold(True) + font.setPointSize(78) + p.setFont(font) + + # Add date + p.drawText(125, 750, date) + p.end() + print(f'create_cover_mediapart\nDate: {date}\n') + return pixmap_to_data(img) + try: - from calibre.ebooks import calibre_cover - title = self.title if isinstance(self.title, unicode) else \ - self.title.decode('utf-8', 'replace') - date = strftime(self.timefmt) - time = strftime('%a %d %b %Y %-H:%M') - img_data = calibre_cover(title, date, time) + today=datetime.today() + date = today.strftime('%d %b %Y') + img_data = create_cover_mediapart(date) cover_file.write(img_data) cover_file.flush() - print(f'title: {title}\ndate: {date}\ntime: {time}') except: self.log.exception('Failed to generate default cover') + import sys + print(sys.argv) return False + return True From d0ced3eb48ef4dbdf287af29929676d5fe462fd9 Mon Sep 17 00:00:00 2001 From: Loic Houpert <10154151+lhoupert@users.noreply.github.com> Date: Fri, 15 Jan 2021 09:11:05 +0100 Subject: [PATCH 3/3] center text overlay for cover --- recipes/mediapart.recipe | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/recipes/mediapart.recipe b/recipes/mediapart.recipe index e4911a9cf2..269fa9bd03 100644 --- a/recipes/mediapart.recipe +++ b/recipes/mediapart.recipe @@ -299,7 +299,8 @@ class Mediapart(BasicNewsRecipe): p.setFont(font) # Add date - p.drawText(125, 750, date) + r = QRect(0, 600, 744,200) + p.drawText(r, Qt.AlignmentFlag.AlignJustify | Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignCenter, date) p.end() print(f'create_cover_mediapart\nDate: {date}\n') return pixmap_to_data(img)