|
| 1 | +import datetime |
| 2 | +import re |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +from lxml.cssselect import CSSSelector |
| 6 | +from lxml.etree import XPath |
| 7 | + |
| 8 | +from fundus.parser import ArticleBody, BaseParser, Image, ParserProxy, attribute |
| 9 | +from fundus.parser.utility import ( |
| 10 | + extract_article_body_with_selector, |
| 11 | + generic_author_parsing, |
| 12 | + generic_date_parsing, |
| 13 | + generic_nodes_to_text, |
| 14 | + generic_topic_parsing, |
| 15 | + image_extraction, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class LuxemburgerWortParser(ParserProxy): |
| 20 | + class V1(BaseParser): |
| 21 | + _paragraph_selector = XPath("//p[contains(@class, 'articleParagraph')]") |
| 22 | + _summary_selector = XPath("//h2[contains(@class, 'articleParagraph')]") |
| 23 | + _subheadline_selector = XPath("//h4[contains(@class, 'articleSubheading')]") |
| 24 | + |
| 25 | + _topic_selector = XPath("//div[contains(@class, 'tag-list')]//a") |
| 26 | + |
| 27 | + @attribute |
| 28 | + def body(self) -> Optional[ArticleBody]: |
| 29 | + return extract_article_body_with_selector( |
| 30 | + self.precomputed.doc, |
| 31 | + summary_selector=self._summary_selector, |
| 32 | + subheadline_selector=self._subheadline_selector, |
| 33 | + paragraph_selector=self._paragraph_selector, |
| 34 | + ) |
| 35 | + |
| 36 | + @attribute |
| 37 | + def authors(self) -> List[str]: |
| 38 | + return generic_author_parsing(self.precomputed.ld.bf_search("author")) |
| 39 | + |
| 40 | + @attribute |
| 41 | + def publishing_date(self) -> Optional[datetime.datetime]: |
| 42 | + return generic_date_parsing(self.precomputed.ld.bf_search("datePublished")) |
| 43 | + |
| 44 | + @attribute |
| 45 | + def title(self) -> Optional[str]: |
| 46 | + return self.precomputed.meta.get("og:title") |
| 47 | + |
| 48 | + @attribute |
| 49 | + def topics(self) -> List[str]: |
| 50 | + return generic_topic_parsing(generic_nodes_to_text(self._topic_selector(self.precomputed.doc))) |
| 51 | + |
| 52 | + @attribute |
| 53 | + def images(self) -> List[Image]: |
| 54 | + return image_extraction( |
| 55 | + doc=self.precomputed.doc, |
| 56 | + paragraph_selector=self._paragraph_selector, |
| 57 | + image_selector=XPath("//figure[not(contains(@class, 'Teaser'))]//img"), |
| 58 | + upper_boundary_selector=CSSSelector("h1"), |
| 59 | + caption_selector=XPath("./ancestor::figure//div[contains(@class, 'ImageCaption')]"), |
| 60 | + author_selector=re.compile(r"(?i)Foto:\s*(?P<credits>.*)"), |
| 61 | + ) |
0 commit comments