|
| 1 | +from datetime import date, datetime |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | +from lxml.cssselect import CSSSelector |
| 5 | +from lxml.etree import XPath |
| 6 | + |
| 7 | +from fundus.parser import ArticleBody, BaseParser, Image, ParserProxy, attribute |
| 8 | +from fundus.parser.utility import ( |
| 9 | + extract_article_body_with_selector, |
| 10 | + generic_author_parsing, |
| 11 | + generic_date_parsing, |
| 12 | + image_extraction, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class DerFreitagParser(ParserProxy): |
| 17 | + class V1(BaseParser): |
| 18 | + _summary_selector = CSSSelector("header > p.bc-article-intro__text") |
| 19 | + _paragraph_selector = CSSSelector("div.bo-article-text > p") |
| 20 | + _subheadline_selector = CSSSelector("div.bo-article-text > h2") |
| 21 | + |
| 22 | + @attribute |
| 23 | + def title(self) -> Optional[str]: |
| 24 | + return self.precomputed.meta.get("og:title") |
| 25 | + |
| 26 | + @attribute |
| 27 | + def body(self) -> Optional[ArticleBody]: |
| 28 | + return extract_article_body_with_selector( |
| 29 | + self.precomputed.doc, |
| 30 | + summary_selector=self._summary_selector, |
| 31 | + subheadline_selector=self._subheadline_selector, |
| 32 | + paragraph_selector=self._paragraph_selector, |
| 33 | + ) |
| 34 | + |
| 35 | + @attribute |
| 36 | + def authors(self) -> List[str]: |
| 37 | + return generic_author_parsing(self.precomputed.ld.bf_search("author")) |
| 38 | + |
| 39 | + @attribute |
| 40 | + def publishing_date(self) -> Optional[datetime]: |
| 41 | + return generic_date_parsing(self.precomputed.ld.bf_search("datePublished")) |
| 42 | + |
| 43 | + @attribute |
| 44 | + def topics(self) -> List[str]: |
| 45 | + return self.precomputed.ld.bf_search("keywords") |
| 46 | + |
| 47 | + @attribute |
| 48 | + def images(self) -> List[Image]: |
| 49 | + return image_extraction( |
| 50 | + doc=self.precomputed.doc, |
| 51 | + paragraph_selector=self._paragraph_selector, |
| 52 | + upper_boundary_selector=CSSSelector("header.bc-article-intro"), |
| 53 | + lower_boundary_selector=CSSSelector("span.freitag-article-end"), |
| 54 | + image_selector=CSSSelector("figure img,div[role='figure'] img"), |
| 55 | + caption_selector=XPath("./ancestor::figure//figcaption//span[@class='bo-image__caption__desc']"), |
| 56 | + author_selector=XPath("./ancestor::figure//figcaption//span[@class='bo-image__caption__credit']"), |
| 57 | + ) |
0 commit comments