Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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