Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaner code for LD and JSON parsing #625

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/fundus/parser/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import datetime
from functools import total_ordering
from typing import (
Any,
Callable,
ClassVar,
Dict,
Expand All @@ -18,6 +17,7 @@
Pattern,
Type,
Union,
cast,
)

import lxml.html
Expand All @@ -33,6 +33,7 @@
LinkedDataMapping,
TextSequence,
)
from fundus.utils.serialization import JSONVal

logger = create_logger(__name__)

Expand Down Expand Up @@ -154,25 +155,34 @@ def extract_nodes(selector: XPath, node_type: Type[Node]) -> List[Node]:

_ld_node_selector = XPath("//script[@type='application/ld+json']")
_json_pattern = re.compile(r"(?P<json>{[\s\S]*}|\[\s*{[\s\S]*}\s*](?!\s*}))")
_json_undefined = re.compile(r'(?P<key>"[^"]*?"):\s*undefined')


def sanitize_json(text: str) -> Optional[str]:
# capture only content enclosed as follows: {...} or [{...}]
match = re.search(_json_pattern, text)
if match is not None and (sanitized := match.group("json")):
return sanitized
return None


def extract_json_from_dom(root: lxml.html.HtmlElement, selector: XPath) -> Iterable[Dict[str, Any]]:
json_nodes = selector(root)
jsons = []
for node in json_nodes:
json_content = sanitize_json(node.text_content()) or ""
try:
jsons.append(json.loads(json_content))
except json.JSONDecodeError as error:
logger.debug(f"Encountered {error!r} during JSON parsing")
if match is None or not (sanitized := match.group("json")):
return None

# substitute "bad" values
sanitized = re.sub(_json_undefined, r"\g<key>:null", sanitized)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is possible? 🤯 You don't want to know how often I worked around that to avoid selecting text I don't want to loose due to substitution...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel you 😄 regex is a rabbit hole and every time I have to use it I find myself half an hour and 30 open tabs later scratching my head.


return sanitized


def parse_json(text: str) -> Optional[Dict[str, JSONVal]]:
if not (json_content := sanitize_json(text)):
return None

try:
return cast(Dict[str, JSONVal], json.loads(json_content))
except json.JSONDecodeError as error:
logger.debug(f"Encountered {error!r} during JSON parsing")
return None


def extract_json_from_dom(root: lxml.html.HtmlElement, selector: XPath) -> Iterable[Dict[str, JSONVal]]:
jsons = [parsed_json for node in selector(root) if (parsed_json := parse_json(node.text_content())) is not None]
return more_itertools.collapse(jsons, base_type=dict)


Expand Down
20 changes: 4 additions & 16 deletions src/fundus/publishers/au/west_australian.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import json
import re
from typing import List, Optional

Expand All @@ -11,7 +10,7 @@
generic_author_parsing,
generic_date_parsing,
generic_topic_parsing,
sanitize_json,
parse_json,
)


Expand All @@ -21,23 +20,12 @@ class V1(BaseParser):
"string(//script[re:test(text(), 'window.PAGE_DATA')])",
namespaces={"re": "http://exslt.org/regular-expressions"},
)
_json_undefined_pattern = re.compile(r'":\s*undefined')

@function(priority=1)
def _parse_page_content(self):
page_data_content = self._page_data_selector(self.precomputed.doc)

if not page_data_content or not (sanitized := sanitize_json(page_data_content)):
return

json_string = re.sub(self._json_undefined_pattern, r'": null', sanitized)

try:
json_content = json.loads(json_string)
except json.JSONDecodeError:
return

self.precomputed.ld.add_ld(json_content, "windows.PAGE_DATA")
if not (parsed_json := parse_json(self._page_data_selector(self.precomputed.doc))):
raise ValueError("Couldn't parse page data")
self.precomputed.ld.add_ld(parsed_json, "windows.PAGE_DATA")

@attribute
def body(self) -> ArticleBody:
Expand Down
5 changes: 1 addition & 4 deletions src/fundus/publishers/na/the_namibian.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import lxml.html
from lxml.etree import XPath

from fundus.parser import ArticleBody, BaseParser, ParserProxy, attribute, function
from fundus.parser.base_parser import Precomputed
from fundus.parser import ArticleBody, BaseParser, ParserProxy, attribute
from fundus.parser.utility import (
extract_article_body_with_selector,
generic_author_parsing,
generic_date_parsing,
get_ld_content,
get_meta_content,
)


Expand Down