Merge pull request 'fix(news): unwrap CDATA titles at ingest and on /api/news' (#62) from wt/t_4f334c8b into master
Some checks failed
build-and-deploy / build-push-deploy (push) Failing after 6s
Some checks failed
build-and-deploy / build-push-deploy (push) Failing after 6s
Reviewed-on: #62
This commit is contained in:
commit
7c77aa3103
6 changed files with 154 additions and 1 deletions
|
|
@ -57,6 +57,7 @@ from camera_scraper import is_public_url
|
|||
from sources import GDELT_API, ingest_rss_feed, ingest_gdelt, ingest_earthquakes, ingest_social_signals
|
||||
from fire_sources import ingest_fires
|
||||
from keystore import KeyFormatError, delete_key, list_keys, set_key
|
||||
from news_title import unwrap_cdata_title
|
||||
from settings_store import SettingsError, get_app_settings, list_models, set_summary_model
|
||||
from live_layers import (
|
||||
fetch_aircraft, fetch_fire_incidents, fetch_fire_perimeters,
|
||||
|
|
@ -1348,7 +1349,7 @@ async def list_news(
|
|||
rows = (await session.execute(stmt)).mappings().all()
|
||||
return [
|
||||
NewsArticleOut(
|
||||
id=r["id"], title=r["title"], url=r["url"],
|
||||
id=r["id"], title=unwrap_cdata_title(r["title"]), url=r["url"],
|
||||
content=r["content"] if include_content else None, domain=r["domain"],
|
||||
timestamp=r["timestamp"],
|
||||
)
|
||||
|
|
|
|||
33
app/news_title.py
Normal file
33
app/news_title.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""News title cleanup for the read path.
|
||||
|
||||
Same rule as ``news/scraper/newsScraper/feed_util.py:unwrap_cdata_title``.
|
||||
The API image does not include the scraper package, so the function is
|
||||
copied rather than imported. Existing rows are fixed here on read; this
|
||||
module does not write.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
# Whole-title CDATA wrapper only. Leading/trailing whitespace around the
|
||||
# markers belongs to the wrapper, not the title.
|
||||
_CDATA_WRAPPER = re.compile(
|
||||
r"\A\s*<!\[CDATA\[(.*?)\]\]>\s*\Z",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
def unwrap_cdata_title(title: str | None) -> str | None:
|
||||
"""Strip a surrounding CDATA wrapper. Anything else is unchanged.
|
||||
|
||||
``<![CDATA[Hello]]>`` and ``\\n <![CDATA[Hello]]>`` become
|
||||
``Hello``. Real HTML (``<b>Hello</b>``) and a title that merely contains
|
||||
the markers are returned as-is. Non-strings pass through.
|
||||
"""
|
||||
if not isinstance(title, str):
|
||||
return title
|
||||
match = _CDATA_WRAPPER.match(title)
|
||||
if match is None:
|
||||
return title
|
||||
return match.group(1).strip()
|
||||
Loading…
Reference in a new issue