Skip to main content

radio_cli/podcast/
feed.rs

1use super::*;
2use crate::config::Config;
3use anyhow::Result;
4use quick_xml::{NsReader, events::Event, name::ResolveResult};
5use reqwest::{
6    Url,
7    blocking::{Client, Response},
8};
9use scraper::{Html, Selector};
10use std::{
11    io::Read,
12    sync::{
13        Arc,
14        atomic::{AtomicBool, Ordering},
15    },
16    time::Duration,
17};
18const LIMIT: u64 = 10 * 1024 * 1024;
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct Preview {
21    pub feed: Feed,
22    pub episodes: Vec<Episode>,
23}
24pub(crate) fn url(value: &str, base: Option<&Url>) -> Result<Url> {
25    let mut u = match base {
26        Some(b) => b.join(value),
27        None => Url::parse(value),
28    }
29    .map_err(|_| error(crate::t!("podcast_invalid_url")))?;
30    if !matches!(u.scheme(), "http" | "https")
31        || u.host_str().is_none()
32        || !u.username().is_empty()
33        || u.password().is_some()
34    {
35        return Err(error(crate::t!("podcast_invalid_url")));
36    }
37    u.set_fragment(None);
38    Ok(u)
39}
40pub(crate) fn client(config: &Config) -> Result<Client> {
41    Client::builder()
42        .user_agent(crate::USER_AGENT)
43        .connect_timeout(Duration::from_secs(config.settings.connect_timeout_secs))
44        .timeout(Duration::from_secs(config.settings.request_timeout_secs))
45        .redirect(reqwest::redirect::Policy::custom(|a| {
46            if a.previous().len() >= 5
47                || !matches!(a.url().scheme(), "http" | "https")
48                || !a.url().username().is_empty()
49                || a.url().password().is_some()
50            {
51                a.stop()
52            } else {
53                a.follow()
54            }
55        }))
56        .build()
57        .map_err(network_error)
58}
59pub(crate) fn body(mut r: Response, stop: &AtomicBool) -> Result<(Url, Vec<u8>)> {
60    if !r.status().is_success() {
61        return Err(error(&format!(
62            "{} {}",
63            crate::t!("podcast_http"),
64            r.status().as_u16()
65        )));
66    }
67    let base = r.url().clone();
68    let mut data = Vec::new();
69    let mut buf = [0; 16384];
70    loop {
71        if stop.load(Ordering::Relaxed) {
72            return Err(DomainError::new(ErrorCode::Cancelled).into());
73        }
74        let n = r
75            .read(&mut buf)
76            .map_err(|e| error(&format!("{}: {}", crate::t!("podcast_network"), e.kind())))?;
77        if n == 0 {
78            break;
79        }
80        if data.len() as u64 + n as u64 > LIMIT {
81            return Err(error(crate::t!("podcast_limit")));
82        }
83        data.extend_from_slice(&buf[..n]);
84    }
85    Ok((base, data))
86}
87pub fn fetch(
88    config: &Config,
89    value: &str,
90    old: Option<&Feed>,
91    stop: &Arc<AtomicBool>,
92) -> Result<Option<Preview>> {
93    let client = Client::builder()
94        .user_agent(crate::USER_AGENT)
95        .connect_timeout(Duration::from_secs(config.settings.connect_timeout_secs))
96        .timeout(Duration::from_secs(config.settings.request_timeout_secs))
97        .redirect(reqwest::redirect::Policy::none())
98        .build()
99        .map_err(network_error)?;
100    let mut current = url(value, None)?;
101    let mut canonical = current.clone();
102    let mut visited = Vec::new();
103    for _ in 0..=5 {
104        if stop.load(Ordering::Relaxed) {
105            return Err(DomainError::new(ErrorCode::Cancelled).into());
106        }
107        if visited.contains(&current) {
108            return Err(error(crate::t!("podcast_invalid_url")));
109        }
110        visited.push(current.clone());
111        let mut req = client.get(current.clone());
112        // Validators are only reusable for the URL that supplied them.
113        if let Some(f) = old.filter(|f| f.url == current.as_str()) {
114            if let Some(v) = &f.etag {
115                req = req.header("If-None-Match", v);
116            }
117            if let Some(v) = &f.modified {
118                req = req.header("If-Modified-Since", v);
119            }
120        }
121        let r = req.send().map_err(network_error)?;
122        if r.status() == 304 && old.is_some() {
123            return Ok(None);
124        }
125        if matches!(r.status().as_u16(), 301 | 302 | 303 | 307 | 308) {
126            let next = r
127                .headers()
128                .get("location")
129                .and_then(|v| v.to_str().ok())
130                .ok_or_else(|| error(crate::t!("podcast_invalid_url")))?;
131            let next = url(next, Some(&current))?;
132            // Do not persist a permanent redirect reached through a temporary URL.
133            if matches!(r.status().as_u16(), 301 | 308) && canonical == current {
134                canonical = next.clone();
135            }
136            current = next;
137            continue;
138        }
139        let etag = r
140            .headers()
141            .get("etag")
142            .and_then(|v| v.to_str().ok())
143            .map(str::to_owned);
144        let modified = r
145            .headers()
146            .get("last-modified")
147            .and_then(|v| v.to_str().ok())
148            .map(str::to_owned);
149        let (base, data) = body(r, stop)?;
150        let mut p = parse(&data, &base)?;
151        if let Some(next) = p.feed.new_feed_url.as_deref() {
152            let next = url(next, Some(&base))?;
153            if next != current {
154                canonical = next.clone();
155                current = next;
156                continue;
157            }
158        }
159        p.feed.url = canonical.to_string();
160        if canonical == current {
161            p.feed.etag = etag;
162            p.feed.modified = modified;
163        }
164        p.feed.refreshed_at = Some(chrono::Utc::now().to_rfc3339());
165        return Ok(Some(p));
166    }
167    Err(error(crate::t!("podcast_limit")))
168}
169
170pub(crate) fn network_error(e: reqwest::Error) -> anyhow::Error {
171    // reqwest can include signed URLs; remove URLs before exposing diagnostics.
172    error(&format!(
173        "{}: {}",
174        crate::t!("podcast_network"),
175        e.without_url()
176    ))
177}
178
179fn plain(value: &str) -> String {
180    Html::parse_fragment(value)
181        .root_element()
182        .text()
183        .collect::<Vec<_>>()
184        .join(" ")
185        .split_whitespace()
186        .collect::<Vec<_>>()
187        .join(" ")
188}
189fn notes(value: &str) -> String {
190    fn walk(element: scraper::ElementRef<'_>, output: &mut String) {
191        let name = element.value().name();
192        if matches!(name, "script" | "style") {
193            return;
194        }
195        let block = matches!(
196            name,
197            "p" | "div" | "li" | "ul" | "ol" | "h1" | "h2" | "h3" | "br"
198        );
199        if block && !output.ends_with('\n') {
200            output.push('\n');
201        }
202        if name == "li" {
203            output.push_str("• ");
204        }
205        for child in element.children() {
206            if let Some(text) = child.value().as_text() {
207                output.push_str(&text.split_whitespace().collect::<Vec<_>>().join(" "));
208                output.push(' ');
209            } else if let Some(child) = scraper::ElementRef::wrap(child) {
210                walk(child, output);
211            }
212        }
213        if name == "a"
214            && let Some(href) = element
215                .value()
216                .attr("href")
217                .filter(|u| u.starts_with("https://") || u.starts_with("http://"))
218        {
219            output.push_str(&format!(" ({href}) "));
220        }
221        if block && !output.ends_with('\n') {
222            output.push('\n');
223        }
224    }
225    let html = Html::parse_fragment(value);
226    let mut output = String::new();
227    walk(html.root_element(), &mut output);
228    output
229        .lines()
230        .map(str::trim)
231        .filter(|s| !s.is_empty())
232        .collect::<Vec<_>>()
233        .join("\n\n")
234}
235fn duration(value: &str) -> Option<u64> {
236    super::parse_time(value.trim()).ok()
237}
238fn published(value: &str) -> Option<String> {
239    let strict = |s: &str| {
240        chrono::DateTime::parse_from_rfc2822(s)
241            .or_else(|_| chrono::DateTime::parse_from_rfc3339(s))
242            .ok()
243    };
244    if let Some(d) = strict(value) {
245        return Some(d.to_utc().to_rfc3339());
246    }
247    let value = value.split_once(',').map_or(value, |(_, rest)| rest.trim());
248    let mut parts: Vec<String> = value.split_whitespace().map(str::to_owned).collect();
249    if let Some(zone) = parts.last_mut() {
250        *zone = match zone.as_str() {
251            "UTC" => "+0000".into(),
252            "CEST" => "+0200".into(),
253            z if (z.starts_with('+') || z.starts_with('-')) && z.len() == 6 => z.replace(':', ""),
254            z => z.into(),
255        };
256    }
257    strict(&parts.join(" ")).map(|d| d.to_utc().to_rfc3339())
258}
259
260struct Node {
261    name: String,
262    text: String,
263    base: Url,
264    html: bool,
265    updated: Option<String>,
266}
267#[derive(Clone, Copy, PartialEq, Eq)]
268enum Scope {
269    Feed,
270    Episode,
271    Other,
272}
273fn scope(stack: &[Node]) -> Scope {
274    match stack {
275        [feed] if feed.name == "feed" => Scope::Feed,
276        [rss, channel] if rss.name == "rss" && channel.name == "channel" => Scope::Feed,
277        [feed, entry] if feed.name == "feed" && entry.name == "entry" => Scope::Episode,
278        [rss, channel, item]
279            if rss.name == "rss" && channel.name == "channel" && item.name == "item" =>
280        {
281            Scope::Episode
282        }
283        _ => Scope::Other,
284    }
285}
286pub(crate) fn parse(data: &[u8], base: &Url) -> Result<Preview> {
287    if data.len() as u64 > LIMIT {
288        return Err(error(crate::t!("podcast_limit")));
289    }
290    if data.starts_with(&[0xff, 0xfe]) || data.starts_with(&[0xfe, 0xff]) {
291        return Err(error(crate::t!("podcast_invalid_feed")));
292    }
293    let invalid = || error(crate::t!("podcast_invalid_feed"));
294    let mut reader = NsReader::from_reader(data);
295    reader.config_mut().check_end_names = true;
296    let mut stack: Vec<Node> = vec![];
297    let mut f = Feed {
298        url: base.to_string(),
299        ..Default::default()
300    };
301    let mut episodes = vec![];
302    let mut current: Option<Episode> = None;
303    let mut count = 0;
304    let mut root = String::new();
305    loop {
306        match reader.read_event().map_err(|_| invalid())? {
307            Event::Start(e) | Event::Empty(e) => {
308                let empty =
309                    data.get(reader.buffer_position().saturating_sub(2) as usize) == Some(&b'/');
310                let name = e.local_name().as_ref().to_owned();
311                if stack.is_empty() {
312                    if !root.is_empty() || !matches!(name.as_str(), "rss" | "feed") {
313                        return Err(invalid());
314                    }
315                    root = name.clone();
316                }
317                if stack.len() >= 64 {
318                    return Err(error(crate::t!("podcast_limit")));
319                }
320                let parent_base = stack.last().map(|n| &n.base).unwrap_or(base);
321                let mut attrs = std::collections::HashMap::new();
322                for a in e.attributes() {
323                    let a = a.map_err(|_| invalid())?;
324                    attrs.insert(
325                        a.key.as_ref().to_owned(),
326                        a.normalized_value(quick_xml::XmlVersion::Implicit1_0)
327                            .map_err(|_| invalid())?
328                            .into_owned(),
329                    );
330                }
331                let node_base = if let Some(b) = attrs.get("xml:base") {
332                    url(b, Some(parent_base))?
333                } else {
334                    parent_base.clone()
335                };
336                let attr_url = |key: &str| {
337                    attrs
338                        .get(key)
339                        .and_then(|v| url(v, Some(&node_base)).ok())
340                        .map(|u| u.to_string())
341                };
342                let parent_scope = scope(&stack);
343                if parent_scope == Scope::Feed
344                    && ((root == "feed" && name == "entry") || (root == "rss" && name == "item"))
345                {
346                    count += 1;
347                    if count > 10000 || current.is_some() {
348                        return Err(error(crate::t!("podcast_limit")));
349                    }
350                    current = Some(Episode::default());
351                }
352                if let Some(ep) = current.as_mut() {
353                    let ns = reader.resolver().resolve_element(e.name()).0;
354                    if name == "chapters"
355                        && parent_scope == Scope::Episode
356                        && matches!(ns, ResolveResult::Bound(ns) if ns.as_ref() == "https://podcastindex.org/namespace/1.0")
357                    {
358                        ep.chapters_url = attr_url("url");
359                    } else if name == "chapter"
360                        && matches!(ns, ResolveResult::Bound(ns) if ns.as_ref() == "http://podlove.org/simple-chapters")
361                        && let Some(position_ms) =
362                            attrs.get("start").and_then(|v| super::parse_time(v).ok())
363                    {
364                        ep.chapters.push(Chapter {
365                            title: crate::clean_text(
366                                attrs.get("title").map(String::as_str).unwrap_or(""),
367                            ),
368                            position_ms,
369                        });
370                    }
371                }
372                if parent_scope == Scope::Episode
373                    && name == "transcript"
374                    && matches!(reader.resolver().resolve_element(e.name()).0, ResolveResult::Bound(ns) if ns.as_ref() == "https://podcastindex.org/namespace/1.0")
375                    && let Some(ep) = current.as_mut()
376                    && let Some(url) = attr_url("url")
377                {
378                    ep.transcripts.push(Transcript {
379                        url,
380                        mime: attrs.get("type").cloned().unwrap_or_default(),
381                        language: attrs.get("language").cloned(),
382                    });
383                }
384                if parent_scope == Scope::Episode
385                    && (name == "enclosure"
386                        || (name == "link" && attrs.get("rel").is_some_and(|r| r == "enclosure")))
387                {
388                    if let Some(ep) = current.as_mut() {
389                        let mime = attrs.get("type").cloned().unwrap_or_default();
390                        if ep.url.is_empty()
391                            && (mime.is_empty()
392                                || mime.starts_with("audio/")
393                                || mime == "application/octet-stream"
394                                || mime == "application/ogg")
395                            && let Some(u) = attr_url(if name == "link" { "href" } else { "url" })
396                        {
397                            ep.url = u;
398                            ep.mime = mime;
399                            ep.bytes = attrs.get("length").and_then(|v| v.parse().ok());
400                        }
401                    }
402                } else if parent_scope == Scope::Feed
403                    && name == "image"
404                    && let Some(u) = attr_url("href")
405                {
406                    f.image = u;
407                } else if matches!(parent_scope, Scope::Feed | Scope::Episode)
408                    && name == "link"
409                    && attrs.get("rel").is_none_or(|v| v == "alternate")
410                    && let Some(u) = attr_url("href")
411                {
412                    if let Some(e) = current.as_mut() {
413                        e.website = u;
414                    } else {
415                        f.website = u;
416                    }
417                }
418                stack.push(Node {
419                    name,
420                    text: String::new(),
421                    base: node_base,
422                    // XHTML tags are consumed by the XML reader. Only HTML
423                    // needs another parsing pass after XML entity decoding.
424                    html: root != "feed"
425                        || attrs
426                            .get("type")
427                            .is_some_and(|t| matches!(t.as_str(), "html" | "text/html")),
428                    updated: None,
429                });
430                if !empty {
431                    continue;
432                }
433                finish(&mut stack, &mut f, &mut current, &mut episodes);
434            }
435            Event::End(_) => finish(&mut stack, &mut f, &mut current, &mut episodes),
436            Event::Text(t) => {
437                if let Some(n) = stack.last_mut() {
438                    n.text.push_str(t.as_ref());
439                }
440            }
441            Event::CData(t) => {
442                if let Some(n) = stack.last_mut() {
443                    n.text.push_str(t.as_ref());
444                }
445            }
446            Event::GeneralRef(t) => {
447                if let Some(n) = stack.last_mut() {
448                    let t = t.as_ref();
449                    let v = quick_xml::escape::unescape(&format!("&{t};"))
450                        .map_err(|_| invalid())?
451                        .into_owned();
452                    n.text.push_str(&v);
453                }
454            }
455            Event::DocType(_) => return Err(invalid()),
456            Event::Eof => break,
457            _ => {}
458        }
459    }
460    if !stack.is_empty() || root.is_empty() || f.title.is_empty() || episodes.is_empty() {
461        return Err(invalid());
462    }
463    Ok(Preview { feed: f, episodes })
464}
465fn finish(
466    stack: &mut Vec<Node>,
467    f: &mut Feed,
468    current: &mut Option<Episode>,
469    episodes: &mut Vec<Episode>,
470) {
471    let Some(n) = stack.pop() else { return };
472    let text = n.text.trim();
473    let display_text = || {
474        if n.html {
475            plain(text)
476        } else {
477            text.split_whitespace().collect::<Vec<_>>().join(" ")
478        }
479    };
480    let parent = stack.last().map(|n| n.name.as_str()).unwrap_or("");
481    let parent_scope = scope(stack);
482    let resolved = || {
483        url(text, Some(&n.base))
484            .map(|u| u.to_string())
485            .unwrap_or_default()
486    };
487    if let Some(e) = current.as_mut() {
488        if parent_scope == Scope::Episode {
489            match n.name.as_str() {
490                "title" => e.title = display_text(),
491                "guid" | "id" if !text.is_empty() => e.guid = Some(text.into()),
492                "description" | "summary" | "content" => {
493                    e.description = if n.html { notes(text) } else { text.to_owned() }
494                }
495                "pubDate" | "published" => {
496                    e.published = published(text);
497                }
498                "updated" => {
499                    stack.last_mut().unwrap().updated = chrono::DateTime::parse_from_rfc3339(text)
500                        .ok()
501                        .map(|d| d.to_utc().to_rfc3339());
502                }
503                "link" if !text.is_empty() => e.website = resolved(),
504                "duration" => e.duration_ms = duration(text),
505                _ => {}
506            }
507        }
508        if parent_scope == Scope::Feed && matches!(n.name.as_str(), "item" | "entry") {
509            let mut e = current.take().unwrap();
510            e.published = e.published.or(n.updated);
511            if !e.url.is_empty() {
512                e.source_order = Some(episodes.len() as u64);
513                episodes.push(e);
514            }
515        }
516    } else {
517        let nested_feed_field = stack
518            .split_last()
519            .is_some_and(|(_, ancestors)| scope(ancestors) == Scope::Feed);
520        match n.name.as_str() {
521            "new-feed-url" if parent_scope == Scope::Feed => f.new_feed_url = Some(resolved()),
522            "title" if parent_scope == Scope::Feed => f.title = display_text(),
523            "author" | "managingEditor" if parent_scope == Scope::Feed => f.author = display_text(),
524            "name" if parent == "author" && nested_feed_field => f.author = display_text(),
525            "description" | "subtitle" if parent_scope == Scope::Feed => {
526                f.description = if n.html { notes(text) } else { text.to_owned() }
527            }
528            "link" if parent_scope == Scope::Feed && !text.is_empty() => f.website = resolved(),
529            "url" if parent == "image" && nested_feed_field && f.image.is_empty() => {
530                f.image = resolved()
531            }
532            "logo" | "icon" if parent_scope == Scope::Feed => f.image = resolved(),
533            _ => {}
534        }
535    }
536    if let Some(parent) = stack.last_mut().filter(|p| {
537        !matches!(
538            p.name.as_str(),
539            "rss" | "channel" | "feed" | "item" | "entry"
540        )
541    }) && parent.text.len().saturating_add(n.text.len()) <= 1024 * 1024
542    {
543        parent.text.push_str(&n.text);
544    }
545}
546pub(crate) fn is_apple_url(value: &str) -> bool {
547    url(value, None).is_ok_and(|u| {
548        matches!(
549            u.host_str(),
550            Some("podcasts.apple.com" | "itunes.apple.com")
551        )
552    })
553}
554pub fn discover(config: &Config, value: &str, stop: &Arc<AtomicBool>) -> Result<Vec<String>> {
555    let u = url(value, None)?;
556    if matches!(
557        u.host_str(),
558        Some("podcasts.apple.com" | "itunes.apple.com")
559    ) && let Some(id) = u
560        .path_segments()
561        .and_then(|s| {
562            s.filter_map(|p| p.strip_prefix("id").map(str::to_owned))
563                .next_back()
564        })
565        .or_else(|| {
566            u.query_pairs()
567                .find(|(k, _)| k == "id")
568                .map(|(_, v)| v.into_owned())
569        })
570    {
571        let country = u
572            .path_segments()
573            .and_then(|mut p| p.next())
574            .filter(|p| p.len() == 2 && p.bytes().all(|c| c.is_ascii_alphabetic()));
575        let mut args = vec![("id", id.as_str())];
576        if let Some(country) = country {
577            args.push(("country", country));
578        }
579        return apple(config, "lookup", &args, stop).map(|v| {
580            v.into_iter()
581                .filter_map(|v| {
582                    v["feedUrl"]
583                        .as_str()
584                        .and_then(|s| url(s, None).ok())
585                        .map(|u| u.to_string())
586                })
587                .collect()
588        });
589    }
590    let (base, data) = body(
591        client(config)?
592            .get(u.clone())
593            .send()
594            .map_err(network_error)?,
595        stop,
596    )?;
597    if parse(&data, &base).is_ok() {
598        return Ok(vec![u.to_string()]);
599    }
600    let html = Html::parse_document(&String::from_utf8_lossy(&data));
601    let mut found = vec![];
602    let base = html
603        .select(&Selector::parse("head base[href]").unwrap())
604        .next()
605        .and_then(|e| url(e.value().attr("href")?, Some(&base)).ok())
606        .unwrap_or(base);
607    for e in html.select(&Selector::parse("head link[href]").unwrap()) {
608        let rel = e.value().attr("rel").unwrap_or("");
609        let mime = e.value().attr("type").unwrap_or("").to_ascii_lowercase();
610        if rel
611            .split_whitespace()
612            .any(|r| r.eq_ignore_ascii_case("alternate") || r.eq_ignore_ascii_case("feed"))
613            && matches!(
614                mime.as_str(),
615                "application/rss+xml" | "application/atom+xml"
616            )
617            && let Some(u) = e
618                .value()
619                .attr("href")
620                .and_then(|s| url(s, Some(&base)).ok())
621            && !found.contains(&u.to_string())
622        {
623            found.push(u.to_string());
624        }
625    }
626    Ok(found)
627}
628fn apple(
629    config: &Config,
630    endpoint: &str,
631    args: &[(&str, &str)],
632    stop: &Arc<AtomicBool>,
633) -> Result<Vec<serde_json::Value>> {
634    use std::sync::{Mutex, OnceLock};
635    use std::time::Instant;
636    type Cache = std::collections::HashMap<String, (Instant, Vec<serde_json::Value>)>;
637    static CACHE: OnceLock<Mutex<Cache>> = OnceLock::new();
638    static LAST: Mutex<Option<Instant>> = Mutex::new(None);
639    let key = format!("{endpoint}{args:?}");
640    let cache = CACHE.get_or_init(Mutex::default);
641    if let Some((at, v)) = cache.lock().unwrap().get(&key)
642        && at.elapsed() < Duration::from_secs(600)
643    {
644        return Ok(v.clone());
645    }
646    {
647        let mut last = LAST.lock().unwrap();
648        if let Some(at) = *last {
649            let wait = (at + Duration::from_secs(3)).saturating_duration_since(Instant::now());
650            if !wait.is_zero() {
651                return Err(error(&format!(
652                    "{} ({}s)",
653                    crate::t!("podcast_rate_limited"),
654                    wait.as_secs().saturating_add(1)
655                )));
656            }
657        }
658        *last = Some(Instant::now());
659    }
660    let r = client(config)?
661        .get(format!("https://itunes.apple.com/{endpoint}"))
662        .query(args)
663        .send()
664        .map_err(network_error)?;
665    if r.status() == 429 {
666        let seconds = r
667            .headers()
668            .get("retry-after")
669            .and_then(|v| v.to_str().ok())
670            .and_then(|v| v.parse::<u64>().ok())
671            .unwrap_or(60)
672            .min(3600);
673        *LAST.lock().unwrap() = Some(Instant::now() + Duration::from_secs(seconds));
674        return Err(error(crate::t!("podcast_rate_limited")));
675    }
676    let (_, data) = body(r, stop)?;
677    let value: serde_json::Value =
678        serde_json::from_slice(&data).map_err(|_| error(crate::t!("podcast_invalid_feed")))?;
679    let rows = value["results"].as_array().cloned().unwrap_or_default();
680    let mut c = cache.lock().unwrap();
681    if c.len() > 100 {
682        c.clear();
683    }
684    c.insert(key, (Instant::now(), rows.clone()));
685    Ok(rows)
686}
687pub fn search(
688    config: &Config,
689    query: &str,
690    country: &str,
691    limit: usize,
692    stop: &Arc<AtomicBool>,
693) -> Result<Vec<serde_json::Value>> {
694    apple(
695        config,
696        "search",
697        &[
698            ("term", query),
699            ("media", "podcast"),
700            ("entity", "podcast"),
701            ("country", country),
702            ("limit", &limit.to_string()),
703        ],
704        stop,
705    )
706}
707
708#[cfg(test)]
709mod tests {
710    use super::*;
711    fn parse_xml(xml: &str) -> Result<Preview> {
712        parse(
713            xml.as_bytes(),
714            &Url::parse("https://example.test/a/feed").unwrap(),
715        )
716    }
717    #[test]
718    fn transcript_namespace_aliases_and_relative_urls_are_preserved() {
719        let p = parse_xml(r#"<rss xmlns:p="https://podcastindex.org/namespace/1.0" xmlns:other="https://example.test"><channel><title>Show</title><item><title>Episode</title><enclosure url="audio.mp3"/><p:transcript url="captions.vtt" type="text/vtt" language="zh"/><other:transcript url="wrong.srt" type="application/x-subrip"/></item></channel></rss>"#).unwrap();
720        assert_eq!(p.episodes[0].transcripts.len(), 1);
721        assert!(p.episodes[0].transcripts[0].url.ends_with("/captions.vtt"));
722        assert_eq!(p.episodes[0].transcripts[0].language.as_deref(), Some("zh"));
723    }
724
725    #[test]
726    fn atom_source_metadata_stays_separate_from_entry_identity_and_content() {
727        for source_first in [false, true] {
728            let source = r#"<source xml:base="https://source.test/">
729                <id>shared-source</id><title>Source title</title>
730                <updated>2026-02-01T00:00:00Z</updated>
731                <link rel="alternate" href="source"/>
732                <link rel="enclosure" href="source.mp3" type="audio/mpeg"/>
733                <logo>source.jpg</logo><author><name>Source author</name></author>
734                </source>"#;
735            let entries = ["one", "two"].map(|id| {
736                let fields = format!(
737                    r#"<id>{id}</id><title>Episode {id}</title>
738                    <published>2026-01-01T00:00:00Z</published>
739                    <summary>Episode description</summary>
740                    <link rel="alternate" href="{id}"/>
741                    <link rel="enclosure" href="{id}.mp3" type="audio/mpeg"/>"#
742                );
743                if source_first {
744                    format!("<entry>{source}{fields}</entry>")
745                } else {
746                    format!("<entry>{fields}{source}</entry>")
747                }
748            });
749            let p = parse_xml(&format!(
750                r#"<feed xmlns="http://www.w3.org/2005/Atom">
751                <title>Show</title><author><name>Show author</name></author>
752                <logo>show.jpg</logo>{}</feed>"#,
753                entries.join("")
754            ))
755            .unwrap();
756            assert_eq!(p.feed.author, "Show author");
757            assert_eq!(p.feed.image, "https://example.test/a/show.jpg");
758            for (episode, id) in p.episodes.iter().zip(["one", "two"]) {
759                assert_eq!(episode.guid.as_deref(), Some(id));
760                assert_eq!(episode.title, format!("Episode {id}"));
761                assert_eq!(episode.description, "Episode description");
762                assert_eq!(
763                    episode.published.as_deref(),
764                    Some("2026-01-01T00:00:00+00:00")
765                );
766                assert_eq!(episode.website, format!("https://example.test/a/{id}"));
767                assert_eq!(episode.url, format!("https://example.test/a/{id}.mp3"));
768            }
769            let dir = tempfile::tempdir().unwrap();
770            let store = crate::store::Store::open(&dir.path().join("radio.db")).unwrap();
771            let feed = store.podcast_subscribe(&p, &p.feed.url).unwrap();
772            assert_eq!(store.podcast_episodes(&feed.id).unwrap().len(), 2);
773        }
774    }
775    #[test]
776    fn rss_episode_images_never_replace_or_supply_show_artwork() {
777        for show_image in ["", r#"<itunes:image href="show.jpg"/>"#] {
778            for show_first in [false, true] {
779                let episodes = r#"<item><title>One</title><enclosure url="one.mp3"/>
780                    <itunes:image href="one.jpg"/></item>
781                    <item><title>Two</title><enclosure url="two.mp3"/>
782                    <itunes:image href="two.jpg"/></item>"#;
783                let content = if show_first {
784                    format!("{show_image}{episodes}")
785                } else {
786                    format!("{episodes}{show_image}")
787                };
788                let p = parse_xml(&format!(
789                    r#"<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
790                    <channel><title>Show</title>{content}</channel></rss>"#
791                ))
792                .unwrap();
793                assert_eq!(p.episodes.len(), 2);
794                assert_eq!(
795                    p.feed.image,
796                    if show_image.is_empty() {
797                        ""
798                    } else {
799                        "https://example.test/a/show.jpg"
800                    }
801                );
802            }
803        }
804        let p = parse_xml(
805            r#"<rss><channel><title>Show</title><image>
806            <title>Image title</title><url>rss.jpg</url><link>https://image.test/</link>
807            </image><link>https://show.test/</link>
808            <item><title>Episode</title><enclosure url="one.mp3"/></item>
809            </channel></rss>"#,
810        )
811        .unwrap();
812        assert_eq!(p.feed.title, "Show");
813        assert_eq!(p.feed.website, "https://show.test/");
814        assert_eq!(p.feed.image, "https://example.test/a/rss.jpg");
815    }
816    #[test]
817    fn rss_metadata_entities_cdata_and_relative_enclosures() {
818        let p=parse_xml(r#"<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"><channel><title>节目 &amp; 测试</title><itunes:author>作者</itunes:author><item><title><![CDATA[第 <b>一</b> 集]]></title><guid>opaque</guid><itunes:duration>1:02:03</itunes:duration><enclosure url="../audio.m4a?a=1&amp;b=2" type="audio/mp4" length="123"/></item></channel></rss>"#).unwrap();
819        assert_eq!(p.feed.title, "节目 & 测试");
820        assert_eq!(p.feed.author, "作者");
821        assert_eq!(p.episodes[0].title, "第 一 集");
822        assert_eq!(p.episodes[0].duration_ms, Some(3723000));
823        assert_eq!(p.episodes[0].bytes, Some(123));
824        assert_eq!(p.episodes[0].url, "https://example.test/audio.m4a?a=1&b=2");
825    }
826    #[test]
827    fn atom_enclosures_respect_xml_base_and_unknown_duration() {
828        let p=parse_xml(r#"<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://media.test/"><title>Atom</title><entry><id>x</id><title>one</title><link rel="enclosure" href="one.mp3" type="audio/mpeg"/></entry></feed>"#).unwrap();
829        assert_eq!(p.episodes[0].url, "https://media.test/one.mp3");
830        assert_eq!(p.episodes[0].duration_ms, None);
831    }
832    #[test]
833    fn atom_publication_dates_prefer_published_independently_of_tag_order() {
834        let published = "<published>2026-01-01T00:00:00Z</published>";
835        let updated = "<updated>2026-03-01T00:00:00Z</updated>";
836        for (dates, expected) in [
837            (format!("{published}{updated}"), "2026-01-01T00:00:00+00:00"),
838            (format!("{updated}{published}"), "2026-01-01T00:00:00+00:00"),
839            (published.into(), "2026-01-01T00:00:00+00:00"),
840            (updated.into(), "2026-03-01T00:00:00+00:00"),
841            (
842                format!("{updated}<published>invalid</published>"),
843                "2026-03-01T00:00:00+00:00",
844            ),
845            (
846                format!("<published>invalid</published>{updated}"),
847                "2026-03-01T00:00:00+00:00",
848            ),
849            (
850                format!("{published}<updated>invalid</updated>"),
851                "2026-01-01T00:00:00+00:00",
852            ),
853        ] {
854            let p = parse_xml(&format!(
855                r#"<feed xmlns="http://www.w3.org/2005/Atom"><title>Show</title>
856                <entry><id>one</id><title>One</title>{dates}<link rel="enclosure" href="one.mp3"/></entry>
857                <entry><id>two</id><title>Two</title><published>2026-02-01T00:00:00Z</published>
858                <link rel="enclosure" href="two.mp3"/></entry></feed>"#
859            )).unwrap();
860            assert_eq!(
861                p.episodes[0].published.as_deref(),
862                Some(expected),
863                "{dates}"
864            );
865            let dir = tempfile::tempdir().unwrap();
866            let store = crate::store::Store::open(&dir.path().join("radio.db")).unwrap();
867            let feed = store.podcast_subscribe(&p, &p.feed.url).unwrap();
868            let (page, total) = store
869                .podcast_episodes_page(
870                    &feed.id,
871                    "",
872                    &crate::cli::Page {
873                        offset: 0,
874                        limit: 1,
875                        all: false,
876                    },
877                )
878                .unwrap();
879            assert_eq!(total, 2);
880            assert_eq!(
881                page[0].guid.as_deref(),
882                Some(if expected.contains("01-01") {
883                    "two"
884                } else {
885                    "one"
886                })
887            );
888        }
889    }
890    #[test]
891    fn atom_text_constructs_preserve_literal_markup() {
892        for (kind, content) in [
893            ("", "Show &lt;test&gt; &amp;lt;literal&amp;gt;"),
894            (
895                r#" type="text""#,
896                "Show &lt;test&gt; &amp;lt;literal&amp;gt;",
897            ),
898            (r#" type="text""#, "<![CDATA[Show <test> &lt;literal&gt;]]>"),
899            (
900                r#" type="html""#,
901                "&lt;b&gt;Show&lt;/b&gt; &amp;lt;test&amp;gt; &amp;amp;lt;literal&amp;amp;gt;",
902            ),
903            (
904                r#" type="xhtml""#,
905                r#"<div xmlns="http://www.w3.org/1999/xhtml"><b>Show</b> &lt;test&gt; &amp;lt;literal&amp;gt;</div>"#,
906            ),
907        ] {
908            let p = parse_xml(&format!(
909                r#"<feed xmlns="http://www.w3.org/2005/Atom">
910                <title{kind}>{content}</title><subtitle{kind}>{content}</subtitle>
911                <entry><id>one</id><title{kind}>{content}</title>
912                <summary{kind}>{content}</summary>
913                <link rel="enclosure" href="one.mp3" type="audio/mpeg"/>
914                </entry></feed>"#,
915            ))
916            .unwrap();
917            let expected = "Show <test> &lt;literal&gt;";
918            assert_eq!(p.feed.title, expected, "{kind}: {content}");
919            assert_eq!(p.feed.description, expected);
920            assert_eq!(p.episodes[0].title, expected);
921            assert_eq!(p.episodes[0].description, expected);
922        }
923    }
924    #[test]
925    fn malformed_external_entities_and_missing_audio_are_rejected() {
926        for xml in [
927            "<!DOCTYPE rss SYSTEM 'http://bad.test/dtd'><rss/>",
928            "<rss><channel><title>x</title><item><enclosure url='file:///etc/passwd'/></item></channel></rss>",
929            "<rss><channel><title>x</title><item><enclosure url='https://example.test/a'/></channel>",
930            "<html><title>x</title></html>",
931        ] {
932            assert!(parse_xml(xml).is_err(), "{xml}");
933        }
934        let xml = format!("<rss>{}</rss>", "<a>".repeat(65));
935        assert!(parse_xml(&xml).is_err());
936    }
937}
938
939#[cfg(test)]
940mod tolerant_date_tests {
941    use super::*;
942    #[test]
943    fn tolerant_dates_preserve_offsets_without_inventing_timezones() {
944        for s in [
945            "Tue, 01 Jan 2024 10:00:00 +0800",
946            "Monday, 01 Jan 2024 10:00:00 GMT",
947            "Mon, 01 Jan 2024 10:00:00 UTC",
948            "Mon, 01 Jan 2024 10:00:00 CEST",
949            "Mon, 01 Jan 2024 10:00:00 +08:00",
950        ] {
951            assert!(published(s).is_some(), "{s}");
952        }
953        assert!(published("2024-01-01T10:00:00").is_none());
954        assert_eq!(duration("01:02.5"), Some(62500));
955        assert_eq!(duration("NaN"), None);
956        assert_eq!(duration("-1"), None);
957    }
958}
959
960#[cfg(test)]
961mod chapter_tests {
962    use super::*;
963    #[test]
964    fn chapters_and_ogg_use_their_declared_namespaces_and_media_times() {
965        let p = parse(br#"<rss xmlns:p="https://podcastindex.org/namespace/1.0" xmlns:c="http://podlove.org/simple-chapters"><channel><title>Show</title><item><title>Episode</title><enclosure url="audio.ogg" type="application/ogg"/><p:chapters url="chapters.json" type="application/json+chapters"/><c:chapters><c:chapter start="00:01.5" title="Intro"/></c:chapters></item></channel></rss>"#, &Url::parse("https://chapters.test/feed").unwrap()).unwrap();
966        assert_eq!(p.episodes[0].chapters[0].position_ms, 1500);
967        assert_eq!(
968            p.episodes[0].chapters_url.as_deref(),
969            Some("https://chapters.test/chapters.json")
970        );
971    }
972}