# vim:fileencoding=utf-8 """ Calibre recipe for FAZ.NET (Frankfurter Allgemeine Zeitung). The site's markup changes fairly often, so this recipe carries a fair amount of bespoke cleanup for a handful of recurring content types: - plain articles - "storytelling" articles whose body is embedded as JSON in a ``__NUXT_DATA__`` script tag instead of plain HTML - Tickaroo-based live blogs ("tik4-*" classes) - "Bilderstrecke" image galleries, also delivered via ``__NUXT_DATA__`` """ from __future__ import absolute_import, division, print_function, unicode_literals import json import re from bs4 import BeautifulSoup, NavigableString from calibre.web.feeds.news import BasicNewsRecipe # --------------------------------------------------------------------------- # Shared helpers # --------------------------------------------------------------------------- # FAZ serves a "960w" entry in most srcset lists for the standard article- # width image. When present, the entry just before it is a higher-density # (retina) variant of the same image, which is what we prefer. SRCSET_MARKERS = ('960w', '960w,') def pick_srcset_source(srcset, fallback=None): """Pick the best URL out of a ``srcset`` attribute string. Returns the source right before the "960w" marker if one is found, otherwise ``fallback`` if given, otherwise the first source listed. """ sources = srcset.split() for i, source in enumerate(sources): if source in SRCSET_MARKERS: return sources[i - 1] return fallback if fallback is not None else sources[0] def find_image_source(img_tag): """Collapse an tag down to a single concrete src.""" img_tag['src'] = pick_srcset_source(img_tag['srcset'], img_tag.get('src')) del img_tag['srcset'] def format_tickaroo_liveblog(recipe, soup): """Reformat a Tickaroo live-blog ("tik4-*") article into plain flow HTML.""" IMG_SELECTOR = 'img.tik4-media-image__img[srcset]' RICH_TEXT_DIV_SELECTOR = 'div.tik4-content-block.tik4-content-block--rich-text.tik4-content-block--position-2' AUTHOR_WRAPPER_SELECTOR = '.tik4-author__wrapper' AUTHOR_NAME_SELECTOR = '.tik4-author__name__t' AUTHOR_THUMB_SELECTOR = '.tik4-author__thumb' MEDIA_BODY_SELECTOR = '.tik4-media-body' MEDIA_BODY_TITLE_CLASS = 'tik4-media-body__title' MEDIA_BODY_TITLE_SELECTOR = '.' + MEDIA_BODY_TITLE_CLASS MEDIA_BODY_PUNCTUATION_SELECTOR = '.tik4-media-body__punctuation' MEDIA_BODY_CREDIT_SELECTOR = '.tik4-media-body__credit' EXTERNAL_LINK_SELECTOR = 'a.tik4-we-simple' EXTERNAL_LINK_TITLE_SELECTOR = '.tik4-we-simple__title' EXTERNAL_LINK_DESC_SELECTOR = '.tik4-we-simple__description' EXTERNAL_TEASER_IMG_SELECTOR = '[data-external-selector="teaser-object parent-image-label"]' # 1. Resolve images that only carry a srcset. for img_tag in soup.select(IMG_SELECTOR): find_image_source(img_tag) # 2. Add a line break before each rich-text news block. for div_tag in soup.select(RICH_TEXT_DIV_SELECTOR): div_tag.insert_before(soup.new_tag('br')) # 3. Add a line break before each timestamp. for time_tag in soup.find_all('time'): time_tag.insert_before(soup.new_tag('br')) # 4. Move the author name next to the author thumbnail. for author_wrapper in soup.select(AUTHOR_WRAPPER_SELECTOR): author_name_tag = author_wrapper.select_one(AUTHOR_NAME_SELECTOR) author_thumb_tag = author_wrapper.select_one(AUTHOR_THUMB_SELECTOR) if not author_name_tag: continue extracted_name = author_name_tag.extract() extracted_name['class'] = MEDIA_BODY_TITLE_CLASS if author_thumb_tag: img = author_thumb_tag.find('img') if img and img.has_attr('srcset'): del img['srcset'] author_thumb_tag.insert_after(extracted_name) # 5. Fold punctuation and credit into the media title. for media_body in soup.select(MEDIA_BODY_SELECTOR): title = media_body.select_one(MEDIA_BODY_TITLE_SELECTOR) if not title: continue punctuation = media_body.select_one(MEDIA_BODY_PUNCTUATION_SELECTOR) credit = media_body.select_one(MEDIA_BODY_CREDIT_SELECTOR) if punctuation: title.append(punctuation.extract()) if credit: title.append(credit.extract()) # 6. Unwrap "external link" teasers (tik4-we-simple) into plain text/links. for link_tag in soup.select(EXTERNAL_LINK_SELECTOR): href = link_tag.get('href') if not href: continue teaser_soup = recipe.index_to_soup(href) if not teaser_soup: continue picture = teaser_soup.find('picture') or teaser_soup.select_one(EXTERNAL_TEASER_IMG_SELECTOR) if picture: link_tag.insert_before(soup.new_tag('br')) if picture.img: link_tag.insert_before(picture.img.extract()) link_tag.insert_after(soup.new_tag('br')) title_element = link_tag.select_one(EXTERNAL_LINK_TITLE_SELECTOR) if title_element: title_element.insert_before(soup.new_tag('br')) title_element.insert_after(soup.new_tag('br')) description_element = link_tag.select_one(EXTERNAL_LINK_DESC_SELECTOR) if description_element: description_element.name = 'a' description_element['href'] = href description_element.wrap(soup.new_tag('div')) link_tag.unwrap() def bilderstrecke(soup, script_tag): """Render a "Bilderstrecke" image gallery from its embedded JSON blob. The gallery data lives inside a ``__NUXT_DATA__`` script tag as a flat JSON array; string values are referenced elsewhere in the array by index rather than inlined, which is why the lookups below index back into ``image_data``. """ try: image_data = json.loads(script_tag.contents[0]) except ValueError, IndexError: return if not isinstance(image_data, list) or not any(isinstance(item, dict) and 'caption' in item for item in image_data): return header_teaser = soup.find_all(attrs={'data-external-selector': 'header-teaser'}) header_teaser_element = None if header_teaser: if len(header_teaser) > 1: header_teaser[0].extract() header_teaser_element = header_teaser[0] image_gallery = soup.new_tag('div') oldfigure = None for item in image_data: if not (isinstance(item, dict) and 'caption' in item and 'derivatives' in item): continue caption = soup.new_tag('p') caption.append(image_data[int(item['caption'])]) caption['class'] = 'body-elements__image-figcaption' if 'source' in item: credit = soup.new_tag('span') credit.append(image_data[int(item['source'])]) credit['class'] = 'body-elements__image-figcaption--source' caption.append(credit) figure = soup.new_tag('figure') img = soup.new_tag('img') img['src'] = image_data[int(image_data[int(item['derivatives'])]['original_aspect_ratio'])] figure.append(img) figure.append(caption) if img['src'] != oldfigure: image_gallery.append(figure) oldfigure = img['src'] if header_teaser_element: header_teaser_element.insert_after(image_gallery) else: soup.append(image_gallery) for tag in soup.find_all(class_='header-teaser__image--default'): tag.extract() def story(soup, tag): """Move the first caption next to the first image in a story article.""" first_image = soup.find('img', attrs={'loading': 'lazy'}) first_caption = soup.find('figcaption', attrs={'class': 'caption'}) if first_image and first_caption: first_image.insert_after(first_caption.extract()) class FazNet(BasicNewsRecipe): # Version 11.1m # Update 2026 # original by Armin Geller # overhaul to deal with changes in the faz.net websites title = 'FAZ.NET' __author__ = 'Unknown' description = 'Frankfurter Allgemeine Zeitung' publisher = 'Frankfurter Allgemeine Zeitung GmbH' category = 'news, politics, Germany' cover_url = 'https://upload.wikimedia.org/wikipedia/commons/7/72/Frankfurter_Allgemeine_logo.svg' encoding = 'utf-8' language = 'de' ignore_duplicate_articles = {'title', 'url'} max_articles_per_feed = 30 no_stylesheets = True remove_javascript = True scale_news_images = (10, 100) delay = 1 extra_css = ''' .scrolly-title {font-size: 1.5em; font-weight:bold; text-align:left;} .quote,.tik4-quote__text,.tik4-we-simple__title, .section-heading { font-size: 1.3em; font-weight:bold; text-align:center;color:grey;margin-bottom:1em;margin-top:1em;} .intro-text,.author, .tik4-quote__cite--source {font-size: 0.7em; font-weight:bold; text-align:center; display:block; margin-bottom: 0.95em; color:grey;} .intro-label,.header-label__content {font-size: 0.7em; font-weight:bold; text-align:left; display:block; margin-bottom: 0.95em; color:grey;} h2 {font-size:1.4em;text-align:left;} .dict-txt,.caption,.body-elements__image-figcaption,.header-teaser__image-details,.meta2, .tik4-media-body__title,.scrolly-text {line-height:0.8; display:block; margin-top:0.05em;margin-bottom:1em; font-size: 0.85em; text-align:left;} .caption__copyright,.meta2-italic,.body-elements__image-figcaption--source,.header-teaser__image-details--source, .dict-img-src,.tik4-media-body__credit, .tik4-we-simple__meta {font-size: 0.65em; font-style:italic; text-align:left;margin-left:0.4em;} .header-detail--bold {font-size:0.6em; font-weight:bold; margin-bottom:0.75em;text-align:left;} .header-detail {font-size:0.8em} time {font-size:0.6em; font-weight: normal; margin-bottom:0.75em; text-align:left; display:block;} .copy--intro,.header-teaser,.scrolly-intro {font-size:1em; font-style:italic; font-weight:bold;margin-bottom:1em;} .dict-hl {font-size:1em; font-style:italic; font-weight:bold} .tik4-media-image,.dict-box {margin-bottom:1em;margin-top:1em;} ''' keep_only_tags = [ dict(name='article', attrs={'class': ['article', 'storytelling']}), dict(name='body'), dict(name='div', attrs={'class': ['imageGallery', 'image_only']}), dict(name='div', attrs={'class': 'tik4-live__container'}), dict(name='script', attrs={'id': '__NUXT_DATA__'}), dict(name='figure', attrs={'class': 'image'}), ] remove_tags = [ dict( name='div', attrs={ 'class': [ 'tik4-sharing', 'tik4-load-more-bottom', 'tik4-by', 'header-detail__image', 'mm-adbox', 'upper-toolbar', 'tik4-chapters__label', 'tik4-we-toggle', ] }, ), dict(name='svg'), dict(name='div', attrs={'data-module': 'teaser'}), dict( attrs={ 'data-external-selector': [ 'upper-toolbar', 'article-footer', 'related-articles', 'placeholder', 'media-label', ] } ), dict(name='img', attrs={'class': 'sm:hidden'}), ] remove_attributes = ['onclick', 'initialdata', 'sizes'] # To fetch a single article instead of the full set of RSS feeds (handy # while debugging a markup change), set TEST_ARTICLE_URL below. TEST_ARTICLE_URL = None # "https://www.faz.net/aktuell/besser-leben/stil/bilderstrecke-mode-accessoires-fuer-maenner-16997909.html" if TEST_ARTICLE_URL: def parse_index(self): return [('Articles', [{'title': 'Test article', 'url': self.TEST_ARTICLE_URL}])] else: feeds = [ ('FAZ.NET Aktuell', 'https://www.faz.net/rss/aktuell/'), ('Politik', 'https://www.faz.net/rss/aktuell/politik/'), ('Wirtschaft', 'https://www.faz.net/rss/aktuell/wirtschaft/'), ('Feuilleton', 'https://www.faz.net/rss/aktuell/feuilleton/'), ('Sport', 'https://www.faz.net/rss/aktuell/sport/'), ('Lebensstil', 'https://www.faz.net/rss/aktuell/besser-leben/stil/'), ('Gesellschaft', 'https://www.faz.net/rss/aktuell/gesellschaft/'), ('Finanzen', 'https://www.faz.net/rss/aktuell/finanzen/'), ('Technik & Motor', 'https://www.faz.net/rss/aktuell/technik-motor/'), ('Wissen', 'https://www.faz.net/rss/aktuell/wissen/'), ('Reise', 'https://www.faz.net/rss/aktuell/reise/'), ('Karriere & Hochschule', 'https://www.faz.net/rss/aktuell/karriere-hochschule/'), ('Rhein-Main', 'https://www.faz.net/rss/aktuell/rhein-main/'), ] def _unpack_storytelling_article(self, soup): """Render "storytelling" articles whose body is embedded as JSON. These articles ship an empty
shell; the actual content lives inside a __NUXT_DATA__ script tag and has to be parsed out and re-inserted as HTML. """ article_tag = soup.find(name='article', attrs={'class': 'storytelling'}) if not (article_tag and not article_tag.get_text().strip()): return nuxt_script_tag = soup.find('script', attrs={'id': '__NUXT_DATA__', 'type': 'application/json'}) if not nuxt_script_tag: return try: json_data = json.loads(str(nuxt_script_tag.contents[0])) for item in json_data: if not (isinstance(item, dict) and 'body' in item): continue body = json_data[int(item['body'])] if not body: continue new_soup = BeautifulSoup(body, 'html.parser') soup.append(new_soup) storytelling_tag = soup.find(attrs={'data-content': 'storytelling'}) if storytelling_tag: storytelling_tag.unwrap() break except Exception: pass def preprocess_html(self, soup): self._unpack_storytelling_article(soup) # Format hovering images: resolve lazy-loaded src, and drop duplicate # captions that immediately repeat the same text. for figure_tag in soup.find_all('figure', class_='image'): img_tag = figure_tag.find('img', attrs={'data-src': True}) if img_tag: img_tag['src'] = img_tag['data-src'] del img_tag['data-src'] caption_tag = figure_tag.find('figcaption') if caption_tag: next_caption = caption_tag.find_next('figcaption') if next_caption and caption_tag.get_text(strip=True) == next_caption.get_text(strip=True): next_caption.extract() # Move the headline in front of the lead image, if it isn't already. lead_image = soup.find('figure', attrs={'class': 'image'}) h1 = soup.find('h1') if lead_image and h1 and h1.parent and not h1.parent.find(lead_image): lead_image.insert_before(h1.parent.extract()) # Normalize all figure captions to . for tag in soup.find_all('figcaption'): tag['class'] = 'caption' tag.name = 'span' # Format the "header-detail" byline/date block into a plain span. header_detail_selectors = ['header-detail', 'header-detail-opinion header-detail'] header_detail_tag = soup.find(attrs={'data-external-selector': header_detail_selectors}) if header_detail_tag: span = soup.new_tag('span') span['class'] = 'header-detail' span.string = header_detail_tag.get_text(separator=' ', strip=True).replace(' ,', ',') if header_detail_tag.img and header_detail_tag.img.parent and header_detail_tag != header_detail_tag.img.parent: header_detail_tag.insert_before(header_detail_tag.img.parent.extract()) header_detail_tag.replace_with(span) # Format "story"-type articles (moves the first caption next to the # first image). if soup.find(class_='storyContainer'): story(soup, None) # Extract images and text from "Bilderstrecke" image galleries: these # show up in the body as a lone " Bilder" paragraph, with the # actual gallery data embedded in a __NUXT_DATA__ script tag. for par in soup.find_all('p'): if len(par.contents) != 1: continue content = str(par.contents[0]).strip() if re.search(r'^[1-9]\d* Bilder$', content): par.extract() nuxt_script_tag = soup.find('script', attrs={'id': '__NUXT_DATA__', 'type': 'application/json'}) if nuxt_script_tag: bilderstrecke(soup, nuxt_script_tag) break # Unwrap buttons, dropping "Weitere anzeigen" (show more) entirely. for tag in soup.find_all('button'): if 'Weitere anzeigen' in tag.get_text(): tag.extract() else: tag.unwrap() # Rename the section-label span to a class we style explicitly. label_tag = soup.find('span', class_='sm:o2-medium') if label_tag: label_tag['class'] = 'header-label__content' # Drop the screen-reader-only colon inside the section label. header_label_tag = soup.find(class_='header-label__content') if header_label_tag: colon_tag = header_label_tag.find(class_='sr-only') if colon_tag: colon_tag.extract() # Clean up

headers: turn nested
s into s. h2_tag = soup.find('h2') if h2_tag: for div_tag in h2_tag.find_all('div'): div_tag.name = 'span' # Format the "header-teaser" lead paragraph. header_teaser_tag = soup.find(attrs={'data-external-selector': 'header-teaser'}) if header_teaser_tag: header_teaser_tag['class'] = 'header-teaser' # Skip articles behind the paywall. if soup.find(id='faz-paywall'): self.abort_article() # Remove the F.A.Z. newsletter ad. for tag in soup.find_all(attrs={'class': 'body-elements__paragraph'}): if tag.contents and isinstance(tag.contents[0], NavigableString): if 'F.A.Z.-Newsletter' in str(tag.contents[0]): tag.extract() # Remove the "prefer us on Google" nudge (paragraph and link forms). for tag in soup.find_all('p'): if 'in Ihren Suchergebnissen sehen' in tag.get_text(): tag.extract() for tag in soup.find_all('a'): if 'bei Google bevorzugen' in tag.get_text(): tag.extract() # Reformat Tickaroo live blogs. if soup.find(attrs={'class': 'tik4-live__container'}): format_tickaroo_liveblog(self, soup) # Resolve any remaining srcset-only images. for img_tag in soup.find_all('img', srcset=True): find_image_source(img_tag) # Resolve images. for picture_tag in soup.find_all('picture'): source_tag = picture_tag.find('source', srcset=True) img_tag = picture_tag.find('img') if source_tag else None if img_tag: img_tag['src'] = pick_srcset_source(source_tag['srcset']) # Strip all image attributes except src/alt/title; drop images with # no resolvable src entirely. attributes_to_keep = {'src', 'alt', 'title'} for img_tag in soup.find_all('img'): if not img_tag.has_attr('src'): img_tag.decompose() continue for attr in list(img_tag.attrs): if attr not in attributes_to_keep: del img_tag[attr] return soup def postprocess_html(self, soup, first_fetch): # Remove the style attribute from certain image captions. for tag in soup.select('div.meta2'): if tag.has_attr('style'): del tag['style'] if tag.span: tag.span.unwrap() # Unwrap any remaining inside a caption. for tag in soup.find_all(attrs={'class': 'caption'}): span = tag.find('span') if span: span.unwrap() # Add a trailing period between a figure caption and its credit, # where the caption doesn't already end in punctuation. caption_classes = [ 'body-elements__image-figcaption', 'header-teaser__image-details', 'caption', 'meta2', ] for tag in soup.find_all(attrs={'class': caption_classes}): if tag.string is None and tag.contents and tag.contents[0].string: tag = tag.contents[0] if tag.string: text = str(tag.string).strip() if text and text[-1] not in '.?!:': tag.string.replace_with(text + '. ') # Add a line break between an image and a following header-teaser. for tag in soup.find_all(class_='header-teaser'): tag.insert_before(soup.new_tag('br')) # Wrap every image in its own
, stripped of layout attributes. for img_tag in soup.find_all('img'): for attrib in ('height', 'width', 'border', 'align', 'style'): if img_tag.has_attr(attrib): del img_tag[attrib] parent = img_tag.parent index = parent.contents.index(img_tag) img_tag.extract() div_tag = soup.new_tag('div') parent.insert(index, div_tag) div_tag.append(img_tag) return soup