Skip to content

Commit 8a3f673

Browse files
committed
Add bounded source text command
1 parent b2e3853 commit 8a3f673

3 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from pathlib import Path
5+
6+
import typer
7+
8+
from news_pipeline.extractors.article_text import ArticleDetails, fetch_article_details
9+
from news_pipeline.queue.service import QueueService
10+
11+
12+
def queue_source_text_command(
13+
queue_id: str,
14+
source_index: int = typer.Option(0, "--source-index", min=0, help="Zero-based draft source index to read."),
15+
max_paragraphs: int = typer.Option(12, "--max-paragraphs", min=1, max=30, help="Maximum extracted paragraphs."),
16+
max_chars: int = typer.Option(6000, "--max-chars", min=500, max=12000, help="Maximum extracted text characters."),
17+
json_output: bool = typer.Option(False, "--json", help="Emit structured JSON."),
18+
) -> None:
19+
if not isinstance(source_index, int):
20+
source_index = 0
21+
if not isinstance(max_paragraphs, int):
22+
max_paragraphs = 12
23+
if not isinstance(max_chars, int):
24+
max_chars = 6000
25+
if not isinstance(json_output, bool):
26+
json_output = False
27+
28+
root = Path.cwd()
29+
service = QueueService(root / "news_pipeline/data/queue")
30+
item = service.store.load(queue_id)
31+
if item is None:
32+
raise typer.BadParameter(f"queue item not found: {queue_id}")
33+
if source_index >= len(item.draft_sources):
34+
raise typer.BadParameter(f"source index out of range: {source_index}")
35+
36+
source = item.draft_sources[source_index]
37+
details = fetch_article_details(str(source.url), max_paragraphs=max_paragraphs, max_chars=max_chars)
38+
payload = {
39+
"queueId": item.queue_id,
40+
"title": item.draft_title,
41+
"source": {
42+
"index": source_index,
43+
"name": source.name,
44+
"url": str(source.url),
45+
},
46+
"publishedAt": details.published_at.isoformat() if details.published_at else None,
47+
"textChars": len(details.snippet),
48+
"text": details.snippet,
49+
}
50+
if json_output:
51+
print(json.dumps(payload, ensure_ascii=False, indent=2))
52+
return
53+
54+
print(f"queue_id: {payload['queueId']}")
55+
print(f"title: {payload['title']}")
56+
print(f"source: {source.name}: {source.url}")
57+
if payload["publishedAt"]:
58+
print(f"published_at: {payload['publishedAt']}")
59+
print(f"text_chars: {payload['textChars']}")
60+
print()
61+
print(details.snippet)

‎news_pipeline/news_pipeline/cli/main.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from news_pipeline.cli.commands.queue_review import queue_review_command
1818
from news_pipeline.cli.commands.queue_summary import queue_summary_command
1919
from news_pipeline.cli.commands.queue_polish import queue_polish_command
20+
from news_pipeline.cli.commands.queue_source_text import queue_source_text_command
2021
from news_pipeline.cli.commands.heartbeat_prepare_one import prepare_one_command
2122
from news_pipeline.cli.commands.heartbeat_publish_one import publish_one_command
2223

@@ -39,6 +40,7 @@
3940
queue_app.command("summary")(queue_summary_command)
4041
queue_app.command("cleanup")(queue_cleanup_command)
4142
queue_app.command("polish")(queue_polish_command)
43+
queue_app.command("source-text")(queue_source_text_command)
4244
heartbeat_app.command("prepare-one")(prepare_one_command)
4345
heartbeat_app.command("publish-one")(publish_one_command)
4446
demo_app.command("seed")(demo_seed_command)

‎news_pipeline/tests/test_safety_gates.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from news_pipeline.cli.commands.queue_approve import queue_approve_command
1414
from news_pipeline.cli.commands.queue_cleanup import queue_cleanup_command
1515
from news_pipeline.cli.commands.queue_polish import queue_polish_command
16+
from news_pipeline.cli.commands import queue_source_text
1617
from news_pipeline.cli.commands.heartbeat_publish_one import _is_excluded_source_format, _select_candidate, publish_one_command
1718
from news_pipeline.cli.commands.heartbeat_prepare_one import _board_score, _build_editorial_packs, _candidate_reason, _hot_category, _recent_live_posts, _select_headline_board, _selection_policy
1819
from news_pipeline.cli.commands.publish import _assert_not_duplicate_live, _assert_not_duplicate_topic, publish_command, publish_queue_item
@@ -1045,6 +1046,25 @@ def test_autopublish_allows_turkish_fact_with_english_source_name() -> None:
10451046
assert is_autopublish_candidate(item) == (True, None)
10461047

10471048

1049+
def test_queue_source_text_returns_bounded_extracted_text(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
1050+
monkeypatch.chdir(tmp_path)
1051+
article = _article("source-text", url="https://example.org/demo/source-text")
1052+
item = _item("source-text", normalized_id=article.id, url="https://example.org/demo/source-text")
1053+
_save_runtime(tmp_path, item, article)
1054+
1055+
monkeypatch.setattr(
1056+
queue_source_text,
1057+
"fetch_article_details",
1058+
lambda *_, **__: queue_source_text.ArticleDetails(snippet="Temiz kaynak metni.", published_at=None),
1059+
)
1060+
1061+
queue_source_text.queue_source_text_command(item.queue_id, json_output=True)
1062+
output = capsys.readouterr().out
1063+
1064+
assert '"queueId": "source-text"' in output
1065+
assert '"text": "Temiz kaynak metni."' in output
1066+
1067+
10481068

10491069
def test_headline_board_penalizes_recent_topic_family_saturation(tmp_path: Path) -> None:
10501070
content = tmp_path / "src/content/equinoxHaber"

0 commit comments

Comments
 (0)