|
| 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) |
0 commit comments