|
| 1 | +import 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, ParserProxy, attribute |
| 8 | +from fundus.parser.data import Image |
| 9 | +from fundus.parser.utility import ( |
| 10 | + extract_article_body_with_selector, |
| 11 | + generic_author_parsing, |
| 12 | + generic_date_parsing, |
| 13 | + generic_topic_parsing, |
| 14 | + image_extraction, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class ExpressenParser(ParserProxy): |
| 19 | + class V1(BaseParser): |
| 20 | + _paragraph_selector = CSSSelector("div.article__body-text p") |
| 21 | + _summary_selector = CSSSelector("div.article__preamble") |
| 22 | + _subheadline_selector = CSSSelector("div.article__body-text h2") |
| 23 | + |
| 24 | + @attribute |
| 25 | + def body(self) -> Optional[ArticleBody]: |
| 26 | + return extract_article_body_with_selector( |
| 27 | + self.precomputed.doc, |
| 28 | + paragraph_selector=self._paragraph_selector, |
| 29 | + summary_selector=self._summary_selector, |
| 30 | + subheadline_selector=self._subheadline_selector, |
| 31 | + ) |
| 32 | + |
| 33 | + @attribute |
| 34 | + def title(self) -> Optional[str]: |
| 35 | + return self.precomputed.ld.bf_search("headline") |
| 36 | + |
| 37 | + @attribute |
| 38 | + def publishing_date(self) -> Optional[datetime.datetime]: |
| 39 | + return generic_date_parsing(self.precomputed.ld.bf_search("datePublished")) |
| 40 | + |
| 41 | + @attribute |
| 42 | + def authors(self) -> List[str]: |
| 43 | + return generic_author_parsing(self.precomputed.ld.bf_search("author")) |
| 44 | + |
| 45 | + @attribute |
| 46 | + def images(self) -> List[Image]: |
| 47 | + return image_extraction( |
| 48 | + doc=self.precomputed.doc, |
| 49 | + paragraph_selector=self._paragraph_selector, |
| 50 | + image_selector=XPath("//figure//img"), |
| 51 | + caption_selector=XPath("./ancestor::figure//figcaption//div[@class='rich-image__description']"), |
| 52 | + author_selector=XPath("./ancestor::figure//figcaption//div[@class='rich-image__credit']"), |
| 53 | + upper_boundary_selector=CSSSelector("div.article__body-text"), |
| 54 | + ) |
| 55 | + |
| 56 | + @attribute |
| 57 | + def topics(self) -> List[str]: |
| 58 | + return [topic.split("/")[-1] for topic in generic_topic_parsing(self.precomputed.ld.bf_search("keywords"))] |
0 commit comments