---
title: "Web data"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Web data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)
source("cache-hook.R")
```

`textpress` is organized around four actions -- fetch, read, process, search. This vignette covers the first two. `fetch_rss()` retrieves recent entries from publisher-supported RSS and Atom feeds; `read_urls()` scrapes their content into a node-level data frame while retaining that provenance. Together they turn selected news sources into an analysis-ready corpus in a few lines.

## Select, fetch, and read

Select the Pew Research Center feed from the bundled `rss_politics` catalog, add The Conversation's US politics feed, use `fetch_rss()` to retrieve their recent entries, then keep entries about the 2026 midterms before `read_urls()` scrapes and parses the selected articles. The filtering below is ordinary R filtering, not a query sent to a search service.

Both sources allow reuse with attribution (The Conversation publishes under CC BY-ND 4.0; Pew Research Center under its terms of use), so the article text can be saved with this vignette. Entries were retrieved in November 2026; the table below links to every article.

```{r libs, message=FALSE, warning=FALSE}
library(textpress)
library(dplyr)
library(DT)
```

```{r web-urls, cache_rds = c("feeds", "web_urls")}
feeds <- c(
  textpress::rss_politics |>
    filter(source == "Pew Research Center") |>
    pull(url),
  "https://theconversation.com/us/politics/articles.atom"
)

web_urls <- textpress::fetch_rss(feeds) |>
  filter(grepl(
    "midterm|election|voter|Congress",
    paste(title, description),
    ignore.case = TRUE
  ))
```



Scrape and parse the URLs returned above into a node-level data frame with `$text` and `$meta` components.

```{r web-text, cache_rds = "web_text_list"}
web_text_list <- web_urls |>
  textpress::read_urls(cores = 4)
```

Build a text snippet from the first 15 words of each article, join to metadata, and display as an interactive table.

```{r}
snippets <- web_text_list$text |>
  group_by(doc_id) |>
  summarise(text = {
    words <- unlist(strsplit(paste(text, collapse = " "), "\\s+"))
    paste(paste(words[seq_len(min(15, length(words)))], collapse = " "), "...")
  }, .groups = "drop")

metas_dt <- web_text_list$meta |>
  filter(!is.na(h1_title) & nzchar(trimws(h1_title))) |>
  left_join(snippets, by = "doc_id") |>
  arrange(desc(date)) |>
  mutate(
    title_link = paste0(
      '<a href="', url, '" target="_blank">', h1_title, '</a>'
    )
  )

DT::datatable(
  metas_dt |> select(date, source, title_link, text),
  options = list(columnDefs = list(
    list(targets = 2, orderable = FALSE)
  )),
  escape   = FALSE,
  rownames = FALSE
)
```

## Summary

`fetch_rss()` and `read_urls()` are source-to-corpus entry points for a `textpress` pipeline. `fetch_rss()` returns publisher-provided article URLs with feed metadata; `read_urls()` scrapes and parses them into `$text` (one row per node) and `$meta` (one row per URL, including discovery provenance).
