-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import datetime | ||
from typing import List, Optional | ||
|
||
from lxml.cssselect import CSSSelector | ||
from lxml.etree import XPath | ||
|
||
from fundus.parser import ArticleBody, BaseParser, ParserProxy, attribute | ||
from fundus.parser.utility import ( | ||
extract_article_body_with_selector, | ||
generic_author_parsing, | ||
generic_date_parsing, | ||
generic_nodes_to_text, | ||
) | ||
|
||
|
||
class LesEchosParser(ParserProxy): | ||
class V1(BaseParser): | ||
_summary_selector = CSSSelector("article header > p") | ||
_subheadline_selector = CSSSelector("article div.post-paywall > h3") | ||
|
||
_bloat_regex_ = r"^Pour ne rien rater de l'actualité politique" | ||
|
||
_paragraph_selector = XPath( | ||
f'//article //div[contains(@class, "post-paywall")] /p[not(re:test(string(), "{_bloat_regex_}"))]', | ||
namespaces={"re": "http://exslt.org/regular-expressions"}, | ||
) | ||
|
||
_topic_selector = CSSSelector("header div.sc-108qdzy-3 div.sc-108qdzy-2 > div") | ||
|
||
@attribute | ||
def body(self) -> ArticleBody: | ||
return extract_article_body_with_selector( | ||
self.precomputed.doc, | ||
paragraph_selector=self._paragraph_selector, | ||
summary_selector=self._summary_selector, | ||
subheadline_selector=self._subheadline_selector, | ||
) | ||
|
||
@attribute | ||
def title(self) -> Optional[str]: | ||
# Use the `get` function to retrieve data from the `meta` precomputed attribute | ||
return self.precomputed.meta.get("og:title") | ||
|
||
@attribute | ||
def topics(self) -> List[str]: | ||
topic_nodes = self._topic_selector(self.precomputed.doc) | ||
return generic_nodes_to_text(topic_nodes) | ||
|
||
@attribute | ||
def publishing_date(self) -> Optional[datetime.datetime]: | ||
return generic_date_parsing( | ||
self.precomputed.meta.get("article:published_time") or self.precomputed.ld.bf_search("datePublished") | ||
) | ||
|
||
@attribute | ||
def authors(self) -> List[str]: | ||
return generic_author_parsing(self.precomputed.ld.bf_search("author")) |