|
| 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_topic_parsing, |
| 14 | + image_extraction, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +class MorgunbladidParser(ParserProxy): |
| 19 | + class V1(BaseParser): |
| 20 | + _summary_selector = XPath("//div[@class='main-layout']//div[@class='is-merking']/p") |
| 21 | + _paragraph_selector = XPath( |
| 22 | + "//div[@class='main-layout' or @data-element-type='body-facts']" "/p[not(a and not(text()))]" |
| 23 | + ) |
| 24 | + _subheadline_selector = XPath("//div[@class='main-layout' or @class='et_pb_text_inner']/h3") |
| 25 | + |
| 26 | + @attribute |
| 27 | + def body(self) -> Optional[ArticleBody]: |
| 28 | + return extract_article_body_with_selector( |
| 29 | + self.precomputed.doc, |
| 30 | + subheadline_selector=self._subheadline_selector, |
| 31 | + paragraph_selector=self._paragraph_selector, |
| 32 | + ) |
| 33 | + |
| 34 | + @attribute |
| 35 | + def authors(self) -> List[str]: |
| 36 | + return generic_author_parsing(self.precomputed.ld.bf_search("author")) |
| 37 | + |
| 38 | + @attribute |
| 39 | + def publishing_date(self) -> Optional[datetime.datetime]: |
| 40 | + return generic_date_parsing(self.precomputed.ld.bf_search("datePublished")) |
| 41 | + |
| 42 | + @attribute |
| 43 | + def title(self) -> Optional[str]: |
| 44 | + return self.precomputed.ld.bf_search("headline") |
| 45 | + |
| 46 | + @attribute |
| 47 | + def images(self) -> List[Image]: |
| 48 | + return image_extraction( |
| 49 | + doc=self.precomputed.doc, |
| 50 | + paragraph_selector=self._paragraph_selector, |
| 51 | + image_selector=XPath("//div[@class='image']//img"), |
| 52 | + caption_selector=XPath("./ancestor::div[contains(@class, 'newsitem-image')]//span[@class='caption']"), |
| 53 | + author_selector=XPath("./ancestor::div[contains(@class, 'newsitem-image')]//span[@class='credit']"), |
| 54 | + ) |
0 commit comments