選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

README.md 6.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Anya
  2. A news headline aggregator that clusters near-duplicate headlines into **stories**
  3. and reports only the stories covered by a minimum number of distinct outlets.
  4. Instead of printing every pairwise "duplicate found" alert, Anya groups matching
  5. headlines transitively (union-find over cosine similarity) and answers the useful
  6. question: *which stories are multiple independent sources reporting right now?*
  7. ## How it works
  8. 1. **Load** source URLs and stopwords from `resources/`.
  9. 2. **Fetch** each source. RSS/Atom feeds are parsed as structured XML; HTML pages
  10. are first checked for embedded JSON (JSON-LD / Next.js SSR state) before
  11. falling back to scraping `<a>`/`<span>` text, associating each headline with
  12. the nearest `<time>` publication timestamp.
  13. 3. **Normalize** each headline (lowercase, strip punctuation, remove stopwords).
  14. 4. **Cluster** headlines into stories using pairwise cosine similarity, linking
  15. matches transitively so a chain of near-duplicates collapses into one story.
  16. 5. **Filter & report** stories with at least `--min-sources` distinct outlets,
  17. optionally restricted to a date window.
  18. ## Requirements
  19. - Python 3.11+ (developed and tested on 3.14)
  20. - `requests`
  21. ```bash
  22. pip install requests
  23. ```
  24. ## Setup
  25. ```bash
  26. git clone https://ikibani.com/jbell730/anya.git
  27. cd anya
  28. pip install requests
  29. ```
  30. Run from the project root — the resource paths are relative (`./resources/...`).
  31. ## Configuration
  32. - `resources/sources.txt` — one source URL per line (HTML pages **or** RSS/Atom
  33. feeds; `#` comments and blank lines are ignored).
  34. - `resources/stopwords.txt` — one stopword per line.
  35. - `resources/excluded_phrases.txt` — boilerplate link text to ignore (see below).
  36. - Defaults live at the top of `main.py` (`SIMILARITY_THRESHOLD = 0.75`,
  37. `MIN_SOURCES = 2`) and can be overridden on the command line.
  38. ## Usage
  39. ```bash
  40. python main.py [options]
  41. ```
  42. | Option | Description | Default |
  43. | --- | --- | --- |
  44. | `--min-sources N` | Only output stories reported by at least N distinct sources | `2` |
  45. | `--threshold T` | Cosine similarity used to consider two headlines the same story | `0.75` |
  46. | `--since YYYY-MM-DD` | Only consider headlines published on or after this date | *(none)* |
  47. | `--until YYYY-MM-DD` | Only consider headlines published on or before this date | *(none)* |
  48. | `--verbose` | Enable debug logging | off |
  49. ### Examples
  50. ```bash
  51. # Stories reported by 3+ distinct outlets
  52. python main.py --min-sources 3
  53. # Stories from the last week, need 2+ outlets
  54. python main.py --since 2026-09-07
  55. # A specific range with a stricter similarity threshold
  56. python main.py --since 2026-09-01 --until 2026-09-14 --threshold 0.8
  57. ```
  58. Sample output:
  59. ```
  60. News stories covered by at least 2 distinct sources (published on or after 2026-09-13)
  61. ============================================================
  62. [3 source(s)] Federal Reserve holds interest rates steady (2026-09-14)
  63. cnn.com, foxnews.com, reuters.com
  64. [2 source(s)] Senate passes major infrastructure bill (2026-09-14)
  65. npr.org, nbcnews.com
  66. ```
  67. ## Design notes
  68. - **A "source" is a distinct domain**, not a distinct URL. `www.cnn.com/us` and
  69. `cnn.com/politics` both normalize to `cnn.com`, so one outlet counts once even
  70. when listed under multiple sections. Feed/redirect subdomains (`feeds.`, `rss.`,
  71. `moxie.`) are also stripped, so a feed and an HTML page from the same outlet
  72. still collapse to one source.
  73. - **RSS/Atom sources are preferred when available.** They're structured and far
  74. more reliable than scraping JavaScript-heavy or paywalled pages, and they carry
  75. publication timestamps directly. Feed type is auto-detected from the content, so
  76. HTML and feed URLs can live side by side in `sources.txt`.
  77. - **JS-rendered pages are recovered without a browser.** For HTML that embeds its
  78. data as JSON — JSON-LD structured data or Next.js `__NEXT_DATA__` SSR state —
  79. Anya parses that directly and never executes JavaScript. This is far lighter than
  80. a headless browser, at the cost of some per-site variation in the JSON shape.
  81. - **Date windowing drops undated headlines.** When `--since` or `--until` is set,
  82. any headline whose page carries no parseable timestamp is excluded because its
  83. recency can't be established (the count is logged). Without a date flag,
  84. everything is included.
  85. - **Publication dates** are drawn from `<time>` elements (their `datetime`/`title`
  86. attributes or inner text) and associated with headlines in document order; the
  87. association resets at each `<article>`/`<li>` boundary so undated headlines don't
  88. inherit a neighboring story's date.
  89. - **Boilerplate link text is filtered.** Navigation/footer/legal links like
  90. "skip to content" or "your privacy choices" are dropped by matching against
  91. `excluded_phrases.txt` (case- and punctuation-insensitive; an entry matches when
  92. it appears anywhere in the link text as a run of words, so `privacy choices`
  93. also catches "Your Privacy Choices"). Extend the file rather than editing code.
  94. ## Tests
  95. ```bash
  96. python -m unittest discover -s tests -p 'test_*.py'
  97. ```
  98. ## Project structure
  99. ```
  100. anya/
  101. ├── main.py # CLI entry point and orchestration
  102. ├── services/
  103. │ ├── headlines.py # fetch + parse headlines (and timestamps)
  104. │ ├── feeds.py # RSS/Atom feed detection and parsing
  105. │ ├── ssr.py # JSON-LD / Next.js embedded-JSON extraction
  106. │ ├── normalization.py # stopword/phrase loading and headline normalization
  107. │ ├── similarity.py # cosine similarity over token lists
  108. │ ├── sources.py # load source URLs
  109. │ ├── stories.py # cluster headlines into stories
  110. │ └── dates.py # date parsing and window filtering
  111. ├── structs/
  112. │ ├── headline.py # Headline model (text, domain, published_at)
  113. │ └── story.py # Story model (sources, representative, latest_date)
  114. ├── resources/
  115. │ ├── sources.txt # one source URL per line
  116. │ ├── stopwords.txt # one stopword per line
  117. │ └── excluded_phrases.txt # boilerplate link text to ignore
  118. └── tests/ # unit tests
  119. ```