Define the get_cover_url() method

This commit is contained in:
Henrik Holm 2025-04-18 22:51:15 +02:00
parent 43c9f02849
commit 0fa1a0d6b1
No known key found for this signature in database

View File

@ -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 <figure> 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: