Files
arr/crates/arr-daemon/src/tv_grab.rs
T
Miguel PalhasandClaude Sonnet 5 473792d5de
ci / web (pull_request) Successful in 1m16s
e2e / e2e (pull_request) Successful in 2m59s
ci / rust (pull_request) Successful in 2m41s
fix(db): scale TV size bands to episode size
Migration 0005 seeded TV policies with the movie policy's bands
(2160p floor 8 GiB, 1080p floor 3 GiB), sized for a whole film. A
typical 1080p single-episode WEB-DL (1-2 GiB) sat below that floor
and was hard-filtered, starving the per-episode grab path from #38.

Closes #95

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 02:11:09 +01:00

1099 lines
39 KiB
Rust

//! The TV grab pipeline: season packs versus per-episode grabs. See
//! DESIGN.md §13 phase 6 and `arr_core::grabbing`.
//!
//! The operator's rule: a fully released season with nothing on disk takes
//! one season-pack torrent; an airing season grabs episodes individually as
//! they appear. A pack that hard-failed post-probe blacklists only that
//! release (§6.3) — the season falls back to per-episode instead of being
//! blacklisted outright, and a pack overlapping episodes already on disk is
//! never chosen (§14 defers pack re-grabs entirely).
//!
//! Everything is recomputed from domain rows every tick (§8): an episode
//! with a file or a live grab — its own or its season's — is not a gap.
use std::path::PathBuf;
use arr_core::grabbing::{season_grab_mode, SeasonGrabFacts, SeasonGrabMode};
use arr_core::Language;
use arr_db::{Blacklist, Db, TitlePolicy};
use arr_dl::TransmissionClient;
use arr_indexer::{ProwlarrClient, SearchRelease, TvSelector, TvTarget};
use arr_parse::EpisodeClaim;
use crate::grab::{
backoff_elapsed, record_episode_search, store_episode_release, Eligible, GrabError, GrabScope,
GrabTarget, Grabber, SeedingRules,
};
use crate::indexers::IndexerDirectory;
use crate::reconcile::{Action, ActionFuture, Outcome};
/// How many seasons one tick may search. Same budget reasoning as the movie
/// lane: one search costs one call per indexer (§6.2).
const SEASONS_PER_TICK: usize = 3;
/// How many per-episode searches one tick may run across all seasons.
const EPISODE_SEARCHES_PER_TICK: usize = 5;
/// Grabs season packs or single episodes for every season with wanted,
/// missing episodes.
#[derive(Debug)]
pub struct TvGrabAction {
prowlarr: ProwlarrClient,
grabber: Grabber,
indexers: IndexerDirectory,
}
impl TvGrabAction {
#[must_use]
pub fn new(
prowlarr: ProwlarrClient,
transmission: TransmissionClient,
download_dir: PathBuf,
seeding: SeedingRules,
) -> Self {
Self {
indexers: IndexerDirectory::new(prowlarr.clone()),
prowlarr,
grabber: Grabber::new(transmission, download_dir, seeding),
}
}
async fn tick(&self, database: &Db) -> Result<Vec<Outcome>, GrabError> {
let mut outcomes = self.grabber.track_sent_grabs(database).await?;
let seasons = pending_seasons(database).await?;
if seasons.is_empty() {
return Ok(outcomes);
}
let blacklist = Blacklist::load(database.pool()).await?;
let mut searched_seasons = 0;
let mut episode_searches = 0;
for season in seasons {
if searched_seasons >= SEASONS_PER_TICK || episode_searches >= EPISODE_SEARCHES_PER_TICK
{
break;
}
match self
.grab_season(
database,
&season,
&blacklist,
EPISODE_SEARCHES_PER_TICK - episode_searches,
)
.await
{
Ok(Some(work)) => {
searched_seasons += 1;
episode_searches += work.episode_searches;
outcomes.extend(work.outcomes);
}
Ok(None) => {}
// One season's failure must not cost the rest of the tick.
Err(error) => tracing::error!(
season_id = season.season_id,
series = season.series_title,
%error,
"tv grab failed"
),
}
}
Ok(outcomes)
}
/// Search and grab for one season. `None` means the season was skipped
/// without spending an indexer call — nothing due, or no language yet.
async fn grab_season(
&self,
database: &Db,
season: &PendingSeason,
blacklist: &Blacklist,
episode_budget: usize,
) -> Result<Option<SeasonWork>, GrabError> {
// §5.2: without the title's original language there is nothing to
// evaluate a release against, and guessing is worse than waiting.
let Some(original_language) = season.original_language.as_deref() else {
tracing::warn!(
series = season.series_title,
"no original language yet; not searching"
);
return Ok(None);
};
let original_language = arr_db::policy::language(original_language);
let episodes = season_episodes(database, season.season_id).await?;
let gaps: Vec<&SeasonEpisode> = episodes
.iter()
.filter(|episode| episode.wanted && !episode.has_file && !episode.in_flight)
.collect();
let Some(first_gap) = gaps.first() else {
return Ok(None);
};
let Some(loaded) = database.episode_policy(first_gap.id).await? else {
return Ok(None);
};
let now = std::time::SystemTime::now();
let air_dates: Vec<Option<std::time::SystemTime>> = episodes
.iter()
.map(|episode| air_date_time(episode.air_date.as_deref()))
.collect();
let mode = season_grab_mode(&SeasonGrabFacts {
air_dates: &air_dates,
now,
any_episode_on_disk: episodes.iter().any(|episode| episode.has_file),
pack_hard_failed: pack_hard_failed(database, season.season_id).await?,
});
let aired_due_gaps: Vec<&SeasonEpisode> = gaps
.iter()
.copied()
.filter(|episode| {
air_date_time(episode.air_date.as_deref()).is_some_and(|date| date <= now)
&& backoff_elapsed(episode.search_attempts, episode.last_searched_at.as_deref())
})
.collect();
if aired_due_gaps.is_empty() {
return Ok(None);
}
match mode {
SeasonGrabMode::SeasonPack => self
.grab_pack_or_fall_back(
database,
season,
&episodes,
&gaps,
&aired_due_gaps,
&loaded,
&original_language,
blacklist,
)
.await
.map(Some),
SeasonGrabMode::PerEpisode => self
.grab_episodes(
database,
season,
&episodes,
&aired_due_gaps,
&loaded,
&original_language,
blacklist,
episode_budget,
)
.await
.map(Some),
}
}
/// One `t=tvsearch` for the whole season; the pack wins when one is
/// eligible, otherwise the same result set feeds per-episode selection —
/// "prefers the pack" is a preference, not a veto on episodes.
#[allow(clippy::too_many_arguments)]
async fn grab_pack_or_fall_back(
&self,
database: &Db,
season: &PendingSeason,
episodes: &[SeasonEpisode],
gaps: &[&SeasonEpisode],
due: &[&SeasonEpisode],
loaded: &TitlePolicy,
original_language: &Language,
blacklist: &Blacklist,
) -> Result<SeasonWork, GrabError> {
let candidates = self
.search(
database,
season,
episodes,
TvSelector::Season {
season: season.season_number_u32(),
},
loaded,
original_language,
blacklist,
)
.await?;
let mut work = SeasonWork::searched();
let gap_ids: Vec<i64> = gaps.iter().map(|episode| episode.id).collect();
if let Some(winner) = best_candidate(
candidates.iter().filter(|candidate| {
candidate
.claim
.as_ref()
.is_some_and(|claim| is_this_seasons_pack(claim, season.season_number_u32()))
}),
blacklist,
) {
let outcome = self
.grabber
.send_winner(
database,
&GrabTarget {
scope: GrabScope::Season {
season_id: season.season_id,
episode_ids: gap_ids,
},
title: &season.series_title,
counts_as_attempt: true,
},
loaded,
blacklist,
winner.clone(),
)
.await?;
work.outcomes.extend(outcome);
return Ok(work);
}
// No eligible pack: fill episodes from the same results, and record
// the attempt on whatever stays open so the season backs off (§6.2).
let mut unfilled = Vec::new();
for episode in due {
let covering = candidates.iter().filter(|candidate| {
candidate.claim.as_ref().is_some_and(|claim| {
is_single_episode(claim, season.season_number_u32(), episode.number_u32())
})
});
if let Some(winner) = best_candidate(covering, blacklist) {
let outcome = self
.grabber
.send_winner(
database,
&GrabTarget {
scope: GrabScope::Episode {
episode_id: episode.id,
},
title: &season.series_title,
counts_as_attempt: true,
},
loaded,
blacklist,
winner.clone(),
)
.await?;
work.outcomes.extend(outcome);
} else {
unfilled.push(episode.id);
}
}
if !unfilled.is_empty() {
record_episode_search(database, &unfilled).await?;
tracing::info!(
season_id = season.season_id,
series = season.series_title,
episodes = unfilled.len(),
"no eligible release for season or its episodes"
);
}
Ok(work)
}
/// One `t=tvsearch` per due episode (§6.2: one call per wanted item).
#[allow(clippy::too_many_arguments)]
async fn grab_episodes(
&self,
database: &Db,
season: &PendingSeason,
episodes: &[SeasonEpisode],
due: &[&SeasonEpisode],
loaded: &TitlePolicy,
original_language: &Language,
blacklist: &Blacklist,
budget: usize,
) -> Result<SeasonWork, GrabError> {
let mut work = SeasonWork::searched();
for episode in due.iter().take(budget) {
work.episode_searches += 1;
let candidates = self
.search(
database,
season,
episodes,
TvSelector::Episode {
season: season.season_number_u32(),
episode: episode.number_u32(),
},
loaded,
original_language,
blacklist,
)
.await?;
let covering = candidates.iter().filter(|candidate| {
candidate.claim.as_ref().is_some_and(|claim| {
is_single_episode(claim, season.season_number_u32(), episode.number_u32())
})
});
if let Some(winner) = best_candidate(covering, blacklist) {
let outcome = self
.grabber
.send_winner(
database,
&GrabTarget {
scope: GrabScope::Episode {
episode_id: episode.id,
},
title: &season.series_title,
counts_as_attempt: true,
},
loaded,
blacklist,
winner.clone(),
)
.await?;
work.outcomes.extend(outcome);
} else {
record_episode_search(database, &[episode.id]).await?;
tracing::info!(
episode_id = episode.id,
series = season.series_title,
"no eligible release for episode"
);
}
}
Ok(work)
}
/// Search every indexer with its own widened or narrowed request (§6.1),
/// cache each result with its verdict, and return the candidates with
/// what they claim to cover.
#[allow(clippy::too_many_arguments)]
async fn search(
&self,
database: &Db,
season: &PendingSeason,
episodes: &[SeasonEpisode],
selector: TvSelector,
loaded: &TitlePolicy,
original_language: &Language,
blacklist: &Blacklist,
) -> Result<Vec<TvCandidate>, GrabError> {
let target = TvTarget {
// The series table carries no TVDB id yet; `tv_request` falls
// back to a text search built from the title and the tag.
tvdb_id: None,
title: season.series_title.clone(),
selector,
};
let indexers = self.indexers.indexers().await?;
let mut releases: Vec<SearchRelease> = Vec::new();
let mut any_searchable = false;
for indexer in &indexers {
let Some(request) = indexer.capabilities.tv_request(&target) else {
continue;
};
any_searchable = true;
match self.prowlarr.search_indexer(indexer.id, &request).await {
Ok(found) => releases.extend(found),
Err(error) => {
tracing::warn!(indexer_id = indexer.id, %error, "indexer tv search failed");
}
}
}
if !any_searchable {
tracing::warn!("no indexer accepts a TV or text search; nothing can be grabbed");
}
let mut candidates = Vec::new();
for release in releases {
let claim = arr_parse::parse(&release.name).episode;
let covered: Vec<i64> = match &claim {
Some(claim) => episodes
.iter()
.filter(|episode| match claim {
EpisodeClaim::Episodes { .. } => {
claim.covers(season.season_number_u32(), episode.number_u32())
}
EpisodeClaim::Season { .. } | EpisodeClaim::Seasons { .. } => {
claim.covers_season(season.season_number_u32())
}
EpisodeClaim::Daily { .. } => false,
})
.map(|episode| episode.id)
.collect(),
None => Vec::new(),
};
if covered.is_empty() {
continue;
}
let stored = store_episode_release(
database,
&covered,
&release,
&loaded.policy,
&loaded.overrides,
original_language,
blacklist,
)
.await?;
candidates.push(TvCandidate {
claim,
eligible: stored,
});
}
Ok(candidates)
}
}
impl Action for TvGrabAction {
fn name(&self) -> &'static str {
"tv-grab"
}
fn run<'a>(&'a self, database: &'a Db) -> ActionFuture<'a> {
Box::pin(async move { self.tick(database).await.map_err(Into::into) })
}
}
/// What one season's turn produced.
#[derive(Debug, Default)]
struct SeasonWork {
outcomes: Vec<Outcome>,
episode_searches: usize,
}
impl SeasonWork {
fn searched() -> Self {
Self::default()
}
}
/// A stored search result and what its name claims to hold.
#[derive(Debug)]
struct TvCandidate {
claim: Option<EpisodeClaim>,
eligible: Option<Eligible>,
}
/// A season with at least one wanted, missing episode and no live grab.
#[derive(Debug)]
struct PendingSeason {
season_id: i64,
season_number: i64,
series_title: String,
original_language: Option<String>,
}
impl PendingSeason {
fn season_number_u32(&self) -> u32 {
u32::try_from(self.season_number).unwrap_or_default()
}
}
/// One episode row with the derived facts selection needs.
#[derive(Debug)]
struct SeasonEpisode {
id: i64,
number: i64,
air_date: Option<String>,
wanted: bool,
search_attempts: i64,
last_searched_at: Option<String>,
has_file: bool,
in_flight: bool,
}
impl SeasonEpisode {
fn number_u32(&self) -> u32 {
u32::try_from(self.number).unwrap_or_default()
}
}
/// The gap, straight out of the domain rows (§8). A season already being
/// downloaded as a pack is not a gap, nor is one whose every open episode is
/// individually in flight.
async fn pending_seasons(database: &Db) -> Result<Vec<PendingSeason>, GrabError> {
let rows = sqlx::query!(
r#"
SELECT se.id AS "season_id!: i64",
se.number AS "season_number!: i64",
s.title AS "series_title!: String",
s.original_language
FROM seasons se
JOIN series s ON s.id = se.series_id
WHERE s.blocked = 0
AND EXISTS (
SELECT 1 FROM episodes e
WHERE e.season_id = se.id
AND e.wanted = 1
AND NOT EXISTS (
SELECT 1 FROM media_files f
WHERE f.owner_kind = 'episode' AND f.owner_id = e.id
)
AND NOT EXISTS (
SELECT 1 FROM grabs g
WHERE g.target_kind = 'episode' AND g.target_id = e.id
AND g.state IN ('sent', 'downloaded', 'imported')
)
)
AND NOT EXISTS (
SELECT 1 FROM grabs g
WHERE g.target_kind = 'season' AND g.target_id = se.id
AND g.state IN ('sent', 'downloaded', 'imported')
)
ORDER BY se.id
"#
)
.fetch_all(database.pool())
.await?;
Ok(rows
.into_iter()
.map(|row| PendingSeason {
season_id: row.season_id,
season_number: row.season_number,
series_title: row.series_title,
original_language: row.original_language,
})
.collect())
}
async fn season_episodes(database: &Db, season_id: i64) -> Result<Vec<SeasonEpisode>, GrabError> {
let rows = sqlx::query!(
r#"
SELECT e.id AS "id!: i64",
e.number AS "number!: i64",
e.air_date,
e.wanted AS "wanted!: bool",
e.search_attempts AS "search_attempts!: i64",
e.last_searched_at,
EXISTS (
SELECT 1 FROM media_files f
WHERE f.owner_kind = 'episode' AND f.owner_id = e.id
) AS "has_file!: bool",
EXISTS (
SELECT 1 FROM grabs g
WHERE g.target_kind = 'episode' AND g.target_id = e.id
AND g.state IN ('sent', 'downloaded', 'imported')
) AS "in_flight!: bool"
FROM episodes e
WHERE e.season_id = ?
ORDER BY e.number
"#,
season_id
)
.fetch_all(database.pool())
.await?;
Ok(rows
.into_iter()
.map(|row| SeasonEpisode {
id: row.id,
number: row.number,
air_date: row.air_date,
wanted: row.wanted,
search_attempts: row.search_attempts,
last_searched_at: row.last_searched_at,
has_file: row.has_file,
in_flight: row.in_flight,
})
.collect())
}
/// Whether a season-pack grab for this season already hard-failed — the
/// fall-back-to-per-episode signal.
async fn pack_hard_failed(database: &Db, season_id: i64) -> Result<bool, GrabError> {
let failed = sqlx::query_scalar!(
r#"SELECT EXISTS (
SELECT 1 FROM grabs
WHERE target_kind = 'season' AND target_id = ? AND state = 'failed'
) AS "failed!: bool""#,
season_id
)
.fetch_one(database.pool())
.await?;
Ok(failed)
}
/// A pack for exactly this season. Multi-season packs are never selected
/// automatically: they drag other seasons in with them.
fn is_this_seasons_pack(claim: &EpisodeClaim, season: u32) -> bool {
match claim {
EpisodeClaim::Season { season: claimed } => *claimed == season,
EpisodeClaim::Seasons { first, last } => first == last && *first == season,
EpisodeClaim::Episodes { .. } | EpisodeClaim::Daily { .. } => false,
}
}
/// A release holding exactly this one episode. Multi-episode files are left
/// to manual grabs: two episodes automatically picking the same torrent
/// would fight over one `grabs` row.
fn is_single_episode(claim: &EpisodeClaim, season: u32, episode: u32) -> bool {
matches!(
claim,
EpisodeClaim::Episodes { episodes, .. } if episodes.len() == 1
) && claim.covers(season, episode)
}
/// The eligible view of a candidate set, in the same total order targeted
/// movie search uses, minus anything the blacklist knows (§6.3).
fn best_candidate<'a>(
candidates: impl Iterator<Item = &'a TvCandidate>,
blacklist: &Blacklist,
) -> Option<&'a Eligible> {
let mut eligible: Vec<&Eligible> = candidates
.filter_map(|candidate| candidate.eligible.as_ref())
.collect();
eligible.sort_by(|left, right| {
right
.score
.cmp(&left.score)
.then_with(|| left.indexer_id.cmp(&right.indexer_id))
.then_with(|| left.guid.cmp(&right.guid))
});
eligible
.into_iter()
.find(|candidate| !blacklist.blocks_candidate(&candidate.name, &candidate.download_url))
}
/// An `air_date` as TMDB writes it (`YYYY-MM-DD`), or a full timestamp if
/// one ever arrives that way. Unknown is unaired: it cannot complete a
/// season and cannot be searched for.
fn air_date_time(value: Option<&str>) -> Option<std::time::SystemTime> {
let value = value?;
let timestamp = if let Ok(date) = value.parse::<chrono::NaiveDate>() {
date.and_time(chrono::NaiveTime::MIN).and_utc().timestamp()
} else {
value
.parse::<chrono::DateTime<chrono::Utc>>()
.ok()?
.timestamp()
};
let seconds = u64::try_from(timestamp.abs()).ok()?;
if timestamp < 0 {
std::time::UNIX_EPOCH.checked_sub(std::time::Duration::from_secs(seconds))
} else {
std::time::UNIX_EPOCH.checked_add(std::time::Duration::from_secs(seconds))
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use serde_json::{json, Value};
use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};
use crate::grab::SeedingLimits;
use super::*;
/// A Transmission that dedupes on the infohash, like the real one.
#[derive(Clone, Debug, Default)]
struct FakeTransmission {
torrents: Arc<Mutex<Vec<FakeTorrent>>>,
}
#[derive(Clone, Debug)]
struct FakeTorrent {
id: i64,
hash: String,
source: String,
progress: f64,
}
impl FakeTransmission {
fn torrents(&self) -> Vec<FakeTorrent> {
self.torrents.lock().unwrap().clone()
}
fn add(&self, arguments: &Value) -> ResponseTemplate {
let source = arguments["filename"]
.as_str()
.unwrap_or_default()
.to_owned();
let mut torrents = self.torrents.lock().unwrap();
if let Some(existing) = torrents.iter().find(|torrent| torrent.source == source) {
return success(&json!({"torrent-duplicate": {
"id": existing.id, "name": existing.source, "hashString": existing.hash
}}));
}
let id = i64::try_from(torrents.len()).unwrap() + 1;
let hash = format!("{:040x}", id * 7);
torrents.push(FakeTorrent {
id,
hash: hash.clone(),
source: source.clone(),
progress: 0.0,
});
success(&json!({"torrent-added": {"id": id, "name": source, "hashString": hash}}))
}
}
impl Respond for FakeTransmission {
fn respond(&self, request: &Request) -> ResponseTemplate {
let body: Value = serde_json::from_slice(&request.body).unwrap();
match body["method"].as_str().unwrap_or_default() {
"torrent-add" => self.add(&body["arguments"]),
"torrent-get" => {
let torrents: Vec<Value> = self
.torrents()
.into_iter()
.map(|torrent| {
json!({
"id": torrent.id, "name": torrent.source,
"hashString": torrent.hash, "status": 4,
"percentDone": torrent.progress,
"downloadDir": "/downloads", "labels": []
})
})
.collect();
success(&json!({"torrents": torrents}))
}
_ => success(&json!({})),
}
}
}
fn success(arguments: &Value) -> ResponseTemplate {
ResponseTemplate::new(200).set_body_json(json!({
"result": "success", "arguments": arguments
}))
}
/// A season pack and its three episodes, all eligible under the seeded
/// TV main policy (§5.5 bands: 2160p floor 3 GiB, per #95).
const TV_RSS: &str = r#"<rss><channel>
<item>
<title>Fallout.S01.2160p.WEB-DL.DDP5.1.Atmos</title>
<guid>pack</guid><link>https://tracker/pack.torrent</link>
<size>85899345920</size>
<torznab:attr xmlns:torznab="http://torznab.com/schemas/2015/feed" name="seeders" value="120"/>
</item>
<item>
<title>Fallout.S01E01.2160p.WEB-DL.DDP5.1</title>
<guid>e01</guid><link>https://tracker/e01.torrent</link>
<size>10737418240</size>
<torznab:attr xmlns:torznab="http://torznab.com/schemas/2015/feed" name="seeders" value="80"/>
</item>
<item>
<title>Fallout.S01E02.2160p.WEB-DL.DDP5.1</title>
<guid>e02</guid><link>https://tracker/e02.torrent</link>
<size>10737418240</size>
<torznab:attr xmlns:torznab="http://torznab.com/schemas/2015/feed" name="seeders" value="80"/>
</item>
<item>
<title>Fallout.S01E03.2160p.WEB-DL.DDP5.1</title>
<guid>e03</guid><link>https://tracker/e03.torrent</link>
<size>10737418240</size>
<torznab:attr xmlns:torznab="http://torznab.com/schemas/2015/feed" name="seeders" value="80"/>
</item>
</channel></rss>"#;
async fn prowlarr() -> MockServer {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/v1/indexer"))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!([{"id": 7, "name": "tracker", "enable": true}])),
)
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/7/api"))
.and(query_param("t", "caps"))
.respond_with(ResponseTemplate::new(200).set_body_string(
r#"<caps><searching><search available="yes" supportedParams="q"/></searching></caps>"#,
))
.mount(&server)
.await;
Mock::given(method("GET"))
.and(path("/7/api"))
.and(query_param("t", "search"))
.respond_with(ResponseTemplate::new(200).set_body_string(TV_RSS))
.mount(&server)
.await;
server
}
async fn transmission() -> (MockServer, FakeTransmission) {
let server = MockServer::start().await;
let fake = FakeTransmission::default();
Mock::given(method("POST"))
.respond_with(fake.clone())
.mount(&server)
.await;
(server, fake)
}
/// Fallout S01 with three wanted episodes at the given air dates.
async fn wanted_season(air_dates: &[&str]) -> (tempfile::TempDir, Db, i64) {
let dir = tempfile::tempdir().unwrap();
let database = Db::connect(dir.path().join("arr.db")).await.unwrap();
database.migrate().await.unwrap();
sqlx::query(
"INSERT INTO series (tmdb_id, title, year, original_language, root_id)
SELECT 106379, 'Fallout', 2024, 'en', id
FROM roots WHERE kind = 'tv' AND audience = 'main'",
)
.execute(database.pool())
.await
.unwrap();
let season_id: i64 = sqlx::query_scalar(
"INSERT INTO seasons (series_id, number) VALUES (1, 1) RETURNING id",
)
.fetch_one(database.pool())
.await
.unwrap();
for (index, air_date) in air_dates.iter().enumerate() {
sqlx::query(
"INSERT INTO episodes (season_id, number, title, air_date, wanted)
VALUES (?, ?, ?, ?, 1)",
)
.bind(season_id)
.bind(i64::try_from(index).unwrap() + 1)
.bind(format!("Episode {}", index + 1))
.bind(air_date)
.execute(database.pool())
.await
.unwrap();
}
(dir, database, season_id)
}
fn action(prowlarr: &MockServer, transmission: &MockServer) -> TvGrabAction {
TvGrabAction::new(
ProwlarrClient::new(prowlarr.uri(), "key").unwrap(),
TransmissionClient::new(&transmission.uri()).unwrap(),
PathBuf::from("/mnt/media/transmission/complete"),
SeedingRules::new(
SeedingLimits {
ratio: 1.5,
idle_minutes: 60,
},
HashMap::new(),
),
)
}
async fn grabs(database: &Db) -> Vec<(String, i64, String)> {
sqlx::query_as::<_, (String, i64, String)>(
"SELECT target_kind, target_id, state FROM grabs ORDER BY id",
)
.fetch_all(database.pool())
.await
.unwrap()
}
async fn episode_states(database: &Db) -> Vec<String> {
sqlx::query_scalar::<_, String>("SELECT state FROM episodes ORDER BY number")
.fetch_all(database.pool())
.await
.unwrap()
}
/// The first acceptance case: a completed season with nothing on disk
/// takes the pack — one torrent, one season grab, every episode
/// downloading.
#[tokio::test]
async fn a_completed_season_takes_one_season_pack() {
let (_dir, database, season_id) =
wanted_season(&["2024-04-11", "2024-04-11", "2024-04-11"]).await;
let indexer = prowlarr().await;
let (downloader, fake) = transmission().await;
let outcomes = action(&indexer, &downloader).tick(&database).await.unwrap();
assert_eq!(outcomes.len(), 1);
assert_eq!(fake.torrents().len(), 1);
assert!(fake.torrents()[0].source.ends_with("pack.torrent"));
assert_eq!(
grabs(&database).await,
vec![("season".to_owned(), season_id, "sent".to_owned())]
);
assert_eq!(
episode_states(&database).await,
vec!["downloading", "downloading", "downloading"]
);
}
/// The second acceptance case: an airing season grabs episodes
/// individually as they appear — never the pack, never an unaired
/// episode.
#[tokio::test]
async fn an_airing_season_grabs_aired_episodes_individually() {
let (_dir, database, _season_id) =
wanted_season(&["2024-04-11", "2024-04-18", "2999-01-01"]).await;
let indexer = prowlarr().await;
let (downloader, fake) = transmission().await;
action(&indexer, &downloader).tick(&database).await.unwrap();
let sources: Vec<String> = fake
.torrents()
.into_iter()
.map(|torrent| torrent.source)
.collect();
assert_eq!(sources.len(), 2, "{sources:?}");
assert!(sources[0].ends_with("e01.torrent"));
assert!(sources[1].ends_with("e02.torrent"));
let grabs = grabs(&database).await;
assert_eq!(grabs.len(), 2);
assert!(grabs
.iter()
.all(|(kind, _, state)| kind == "episode" && state == "sent"));
assert_eq!(
episode_states(&database).await,
vec!["downloading", "downloading", "missing"]
);
}
/// The third acceptance case, selection side: a hard-failed pack put the
/// release on the blacklist and the season falls back to per-episode —
/// the pack is not tried again and the episodes are not written off.
#[tokio::test]
async fn a_hard_failed_pack_falls_back_to_per_episode() {
let (_dir, database, season_id) =
wanted_season(&["2024-04-11", "2024-04-11", "2024-04-11"]).await;
// What the import tick left behind: a failed season grab and the
// pack release on the blacklist (§6.3).
let release_id: i64 = sqlx::query_scalar(
"INSERT INTO releases (indexer_id, guid, name, size, download_url, parsed, verdict)
VALUES (7, 'pack', 'Fallout.S01.2160p.WEB-DL.DDP5.1.Atmos', 85899345920,
'https://tracker/pack.torrent', '{}', 'eligible')
RETURNING id",
)
.fetch_one(database.pool())
.await
.unwrap();
sqlx::query(
"INSERT INTO grabs (release_id, target_kind, target_id, infohash, state)
VALUES (?, 'season', ?, 'dead', 'failed')",
)
.bind(release_id)
.bind(season_id)
.execute(database.pool())
.await
.unwrap();
arr_db::blacklist::add(
database.pool(),
Some("dead"),
"Fallout.S01.2160p.WEB-DL.DDP5.1.Atmos",
"dolby_vision_profile",
)
.await
.unwrap();
let indexer = prowlarr().await;
let (downloader, fake) = transmission().await;
action(&indexer, &downloader).tick(&database).await.unwrap();
let sources: Vec<String> = fake
.torrents()
.into_iter()
.map(|torrent| torrent.source)
.collect();
assert_eq!(sources.len(), 3, "{sources:?}");
assert!(sources
.iter()
.all(|source| !source.ends_with("pack.torrent")));
let grabs = grabs(&database).await;
assert!(grabs
.iter()
.filter(|(_, _, state)| state == "sent")
.all(|(kind, _, _)| kind == "episode"));
assert_eq!(
episode_states(&database).await,
vec!["downloading", "downloading", "downloading"]
);
}
/// §14: a season with an episode already on disk never grabs the pack,
/// even when fully released. The remaining episodes come individually.
#[tokio::test]
async fn a_partially_present_season_grabs_the_rest_per_episode() {
let (_dir, database, _season_id) =
wanted_season(&["2024-04-11", "2024-04-11", "2024-04-11"]).await;
sqlx::query(
"INSERT INTO media_files (owner_kind, owner_id, path, size)
SELECT 'episode', id, '/library/e01.mkv', 1 FROM episodes WHERE number = 1",
)
.execute(database.pool())
.await
.unwrap();
sqlx::query("UPDATE episodes SET state = 'available' WHERE number = 1")
.execute(database.pool())
.await
.unwrap();
let indexer = prowlarr().await;
let (downloader, fake) = transmission().await;
action(&indexer, &downloader).tick(&database).await.unwrap();
let sources: Vec<String> = fake
.torrents()
.into_iter()
.map(|torrent| torrent.source)
.collect();
assert_eq!(sources.len(), 2, "{sources:?}");
assert!(sources[0].ends_with("e02.torrent"));
assert!(sources[1].ends_with("e03.torrent"));
}
/// An empty result backs the whole season off together (§6.2) instead of
/// hammering the tracker every 30 s.
#[tokio::test]
async fn a_fruitless_season_search_records_the_attempt() {
let (_dir, database, _season_id) = wanted_season(&["2024-04-11", "2024-04-11"]).await;
let indexer = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/v1/indexer"))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(json!([{"id": 7, "name": "tracker", "enable": true}])),
)
.mount(&indexer)
.await;
Mock::given(method("GET"))
.and(path("/7/api"))
.and(query_param("t", "caps"))
.respond_with(ResponseTemplate::new(200).set_body_string(
r#"<caps><searching><search available="yes" supportedParams="q"/></searching></caps>"#,
))
.mount(&indexer)
.await;
Mock::given(method("GET"))
.and(path("/7/api"))
.and(query_param("t", "search"))
.respond_with(
ResponseTemplate::new(200).set_body_string("<rss><channel></channel></rss>"),
)
.mount(&indexer)
.await;
let (downloader, fake) = transmission().await;
let action = action(&indexer, &downloader);
action.tick(&database).await.unwrap();
assert!(fake.torrents().is_empty());
let attempts: Vec<i64> =
sqlx::query_scalar("SELECT search_attempts FROM episodes ORDER BY number")
.fetch_all(database.pool())
.await
.unwrap();
assert_eq!(attempts, vec![1, 1]);
// Inside the backoff window nothing is searched again.
let searches_after_first: usize = indexer
.received_requests()
.await
.unwrap()
.iter()
.filter(|request| request.url.path() == "/7/api")
.count();
action.tick(&database).await.unwrap();
let searches_after_second: usize = indexer
.received_requests()
.await
.unwrap()
.iter()
.filter(|request| request.url.path() == "/7/api")
.count();
assert_eq!(searches_after_first, searches_after_second);
}
}