From 0fa1a0d6b1cc0a1bbfc557ae1e276aa75b30fcf5 Mon Sep 17 00:00:00 2001 From: Henrik Holm Date: Fri, 18 Apr 2025 22:51:15 +0200 Subject: [PATCH] Define the `get_cover_url()` method --- recipes/fokus.recipe | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/recipes/fokus.recipe b/recipes/fokus.recipe index d8360d530b..1067204753 100644 --- a/recipes/fokus.recipe +++ b/recipes/fokus.recipe @@ -2,6 +2,9 @@ # vim:fileencoding=utf-8 from datetime import datetime, timezone +from mechanize import Request + +from calibre.ebooks.BeautifulSoup import BeautifulSoup from calibre.web.feeds.news import BasicNewsRecipe @@ -67,6 +70,31 @@ class Fokus(BasicNewsRecipe): dict(name='div', class_='wp-block-core-paragraph'), ] + def get_cover_url(self) -> str: + # Create a `mechanize.Request` object. + req = Request(url=self.main_url, method='POST') + + # Open the requested URL in the built-in browser of the `BasicNewsRecipe` parent class. + browser = self.get_browser() + response = browser.open(req) + + # Parse the response into a BeautifulSoup soup. + soup = BeautifulSoup(response.get_data(), "html.parser") + + # The cover image of the current edition is located in a
tag with class 'Issue__thumbnail'. + try: + figure_tag = soup.find('figure', class_='Issue__thumbnail') + img_tag = figure_tag.find('img') + # Set the `img_tag` to `None` if it is falsy. This way, we can force an `AttributeError` if no cover URL + # can be found. + img_tag = img_tag if img_tag else None + cover_url = img_tag["src"] + except AttributeError: + self.log.error("Failed to identify the cover image URL. Does an 'Issue__thumbnail' figure still exist?") + return '' + + return cover_url + def get_browser(self): br = BasicNewsRecipe.get_browser(self) if self.username and self.password: