A feed is the same as last time by its items, not its bytes (#503)

The quiet rule (#242) hashed the raw body, so feeds rewritten at every
read with nothing new (Substack's lastBuildDate, Patreon's signed file
links) never matched and stayed hourly: 792 fetches a day for one Patreon
creator's shows, about 240 MB for five blogs. The hash is now of the
feed's title, artwork without its query, description and ttl, and each
item's guid, title, date and file addresses without their queries. An
OPML still hashes its bytes. The stored hashes are all of the old kind,
so every feed is read once after the deploy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqkrVCCH8YcP2Y3i8VNYWA
This commit is contained in:
2026-11-29 07:41:15 -05:00
parent e2e8105880
commit 99ce2dbc03
2 changed files with 64 additions and 15 deletions

View File

@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- A feed whose items have not changed is checked less often even when its server rewrites it at every read, as Substack's build time and Patreon's signed file links do: it is judged by its items, not its bytes. An item's text edited with nothing else changed shows at the next full read, at most four hours on (#503).
- An item its feed gives no title is named as it is stored, by the opening of its text or else its file's name, so search, Signals, the iOS widget and CarPlay show the same name the page does; the untitled items already stored are named once at start (#502). - An item its feed gives no title is named as it is stored, by the opening of its text or else its file's name, so search, Signals, the iOS widget and CarPlay show the same name the page does; the untitled items already stored are named once at start (#502).
- Torrents go to qBittorrent through its Web API, set in `[torrent]` (`qbittorrent_url`, `username`, `password`, `save_path`), which downloads and seeds them until the episode is deleted; ipx no longer runs a BitTorrent client of its own or listens on 6881, and `port_range` is gone. Without a qBittorrent set, torrents are held back as when they are turned off (#494, #495). - Torrents go to qBittorrent through its Web API, set in `[torrent]` (`qbittorrent_url`, `username`, `password`, `save_path`), which downloads and seeds them until the episode is deleted; ipx no longer runs a BitTorrent client of its own or listens on 6881, and `port_range` is gone. Without a qBittorrent set, torrents are held back as when they are turned off (#494, #495).
- The server's command is `ipxd`, not `ipx`: `ipxd daemon`, `ipxd status`, `ipxd user add` and the rest, in the container as `docker exec iPX ipxd ...`. `ipx` is now the reader in a terminal (#437). - The server's command is `ipxd`, not `ipx`: `ipxd daemon`, `ipxd status`, `ipxd user add` and the rest, in the container as `docker exec iPX ipxd ...`. `ipx` is now the reader in a terminal (#437).

View File

@@ -229,6 +229,38 @@ pub(crate) fn quiet(usual: u64, same_for: Option<u64>) -> u64 {
} }
} }
fn sha256(bytes: &[u8]) -> String {
use sha2::Digest as _;
sha2::Sha256::digest(bytes).iter().map(|b| format!("{b:02x}")).collect()
}
/// What a feed's "the same as last time" is decided on (#242): its items and what it calls
/// itself, not its bytes. Substack stamps a new build time on every read and Patreon signs its
/// file links afresh, so their bytes were never the same and they stayed hourly with nothing
/// new (#503). An item's text edited with nothing else changed waits for the next full read.
fn identity(p: &feed::ParsedFeed) -> Vec<u8> {
let bare = |u: &str| u.split(['?', '#']).next().unwrap_or_default().to_owned();
let mut out = String::new();
let mut put = |s: &str| {
out.push_str(s);
out.push('\0');
};
put(p.title.as_deref().unwrap_or_default());
put(&bare(p.image.as_deref().unwrap_or_default()));
put(p.description.as_deref().unwrap_or_default());
put(&p.ttl_mins.map(|t| t.to_string()).unwrap_or_default());
for e in &p.entries {
put(&e.guid);
put(e.title.as_deref().unwrap_or_default());
put(&e.published.map(|t| t.to_string()).unwrap_or_default());
for enc in &e.enclosures {
put(&bare(&enc.url));
}
put("\x1e");
}
out.into_bytes()
}
pub(crate) fn backoff(usual: u64, failing_for: Option<u64>) -> u64 { pub(crate) fn backoff(usual: u64, failing_for: Option<u64>) -> u64 {
const CEILING: u64 = 86_400; const CEILING: u64 = 86_400;
match failing_for { match failing_for {
@@ -354,26 +386,27 @@ pub(crate) async fn scan_one(
return Ok(Outcome::Empty); return Ok(Outcome::Empty);
} }
// The same body as last time, from a server that ignores "not modified": nothing to read, // A subscribed OPML is a list of feeds, not a feed. The original matched on a ".opml"
// URL; sniffing the body also catches one served from a URL without that extension.
let opml = feed::is_opml(&bytes);
let parsed = if opml { None } else { Some(feed::parse(&bytes)?) };
// The same feed as last time, from a server that ignores "not modified": nothing to read,
// as a 304 would have said (#242). Not when something is missing that a read would fill, as // as a 304 would have said (#242). Not when something is missing that a read would fill, as
// above, nor when someone asked for a full check. // above, nor when someone asked for a full check.
let hash = { let hash = sha256(&match &parsed {
use sha2::Digest as _; Some(p) => identity(p),
sha2::Sha256::digest(&bytes).iter().map(|b| format!("{b:02x}")).collect::<String>() None => bytes.to_vec(),
}; });
if !force && state.body_hash.as_deref() == Some(hash.as_str()) && stored.entries > 0 && stored.image.is_some() { if !force && state.body_hash.as_deref() == Some(hash.as_str()) && stored.entries > 0 && stored.image.is_some() {
ctx.db.touch_feed(id, &feed_cfg.url).await?; ctx.db.touch_feed(id, &feed_cfg.url).await?;
return Ok(Outcome::NotModified); return Ok(Outcome::NotModified);
} }
// A subscribed OPML is a list of feeds, not a feed. The original matched on a ".opml" let Some(mut parsed) = parsed else {
// URL; sniffing the body also catches one served from a URL without that extension.
if feed::is_opml(&bytes) {
ctx.db.touch_feed(id, &feed_cfg.url).await?; ctx.db.touch_feed(id, &feed_cfg.url).await?;
return lists::sync_opml(ctx, id, &bytes).await; return lists::sync_opml(ctx, id, &bytes).await;
} };
let mut parsed = feed::parse(&bytes)?;
// Artwork on http moves to https where its host serves it (#110), before it is compared // Artwork on http moves to https where its host serves it (#110), before it is compared
// with what is stored, which is then the https address too. // with what is stored, which is then the https address too.
let mut secure = std::collections::HashMap::new(); let mut secure = std::collections::HashMap::new();
@@ -964,6 +997,22 @@ mod tests {
assert_eq!(same(&set), hour, "someone's own schedule wins"); assert_eq!(same(&set), hour, "someone's own schedule wins");
} }
#[test]
fn a_feed_is_the_same_by_its_items_not_its_build_time_or_signed_links() {
let feed = |built: &str, token: &str, title: &str| {
let xml = format!(
"<rss><channel><title>Show</title><lastBuildDate>{built}</lastBuildDate>\
<item><guid>1</guid><title>{title}</title>\
<enclosure url=\"https://c.example/a.mp3?token={token}\" type=\"audio/mpeg\"/></item>\
</channel></rss>"
);
sha256(&identity(&feed::parse(xml.as_bytes()).unwrap()))
};
let first = feed("Mon, 01 Jan 2024 00:00:00 GMT", "a", "One");
assert_eq!(first, feed("Tue, 02 Jan 2024 00:00:00 GMT", "b", "One"), "rebuilt and re-signed, the same");
assert_ne!(first, feed("Mon, 01 Jan 2024 00:00:00 GMT", "a", "Two"), "an item renamed is a change");
}
#[tokio::test] #[tokio::test]
async fn the_same_body_again_is_not_modified_unless_a_check_is_forced() { async fn the_same_body_again_is_not_modified_unless_a_check_is_forced() {
let base = empty_or_feed_server().await; let base = empty_or_feed_server().await;
@@ -983,10 +1032,9 @@ mod tests {
matches!(scan_one(&ctx, "same", &cfg, &state, true, None).await, Ok(Outcome::Feed(_))), matches!(scan_one(&ctx, "same", &cfg, &state, true, None).await, Ok(Outcome::Feed(_))),
"a forced check reads it" "a forced check reads it"
); );
// Changed: read, and the same from now. // Only an item's text edited: the same feed, read at the next forced or changed read (#503).
let moved = config::Feed { url: format!("{base}/fixed"), ..feed() }; let edited = config::Feed { url: format!("{base}/fixed"), ..feed() };
assert!(matches!(scan_one(&ctx, "same", &moved, &state, false, None).await, Ok(Outcome::Feed(_)))); assert!(matches!(scan_one(&ctx, "same", &edited, &state, false, None).await, Ok(Outcome::NotModified)));
assert_ne!(ctx.db.http_state("same").await.unwrap().body_hash, state.body_hash);
} }
#[test] #[test]