Skip to main content

radio_cli/podcast/
mod.rs

1//! Public podcast subscriptions are independent of the radio catalog.
2mod commands;
3mod download;
4mod feed;
5mod storage;
6use crate::error::{DomainError, ErrorCode};
7pub use commands::{Action, DownloadAction, QueueAction, manage};
8pub use download::{download, download_path};
9pub use feed::{Preview, discover, fetch, search};
10use serde::{Deserialize, Serialize};
11
12pub(crate) fn error(detail: &str) -> anyhow::Error {
13    DomainError::new(ErrorCode::PodcastFailed)
14        .with("detail", detail)
15        .into()
16}
17#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
18#[serde(default)]
19pub struct Feed {
20    pub failures: u32,
21    pub revision: u64,
22    pub id: String,
23    pub url: String,
24    pub title: String,
25    pub author: String,
26    pub description: String,
27    pub image: String,
28    pub website: String,
29    pub subscribed: bool,
30    pub etag: Option<String>,
31    pub modified: Option<String>,
32    pub refreshed_at: Option<String>,
33    pub error: Option<String>,
34}
35#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
36pub struct Episode {
37    pub id: String,
38    pub feed_id: String,
39    pub guid: Option<String>,
40    pub title: String,
41    pub published: Option<String>,
42    pub description: String,
43    pub website: String,
44    pub url: String,
45    pub mime: String,
46    pub bytes: Option<u64>,
47    pub duration_ms: Option<u64>,
48    pub position_ms: u64,
49    pub observed_duration_ms: Option<u64>,
50    pub completed: bool,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54pub struct EpisodeMedia {
55    url: String,
56    bytes: Option<u64>,
57    duration_ms: Option<u64>,
58}
59
60#[derive(Debug, Serialize, Deserialize)]
61pub(crate) struct EpisodeProgress {
62    pub media: EpisodeMedia,
63    pub position_ms: u64,
64    pub observed_duration_ms: Option<u64>,
65    pub completed: bool,
66}
67
68impl Episode {
69    pub fn media(&self) -> EpisodeMedia {
70        EpisodeMedia {
71            url: self.url.clone(),
72            bytes: self.bytes,
73            duration_ms: self.duration_ms,
74        }
75    }
76
77    pub(crate) fn progress(&self) -> EpisodeProgress {
78        EpisodeProgress {
79            media: self.media(),
80            position_ms: self.position_ms,
81            observed_duration_ms: self.observed_duration_ms,
82            completed: self.completed,
83        }
84    }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
88#[serde(default)]
89pub struct Download {
90    pub generation: u64,
91    pub url: String,
92    pub media: Option<EpisodeMedia>,
93    pub episode_id: String,
94    pub state: String,
95    pub bytes: u64,
96    pub etag: Option<String>,
97    pub modified: Option<String>,
98    pub path: String,
99    pub error: Option<String>,
100}
101
102impl Download {
103    pub(crate) fn matches_media(&self, episode: &Episode) -> bool {
104        self.url == episode.url && self.media.as_ref() == Some(&episode.media())
105    }
106}
107
108pub(crate) use storage::migrate;
109pub(crate) use storage::register_functions;
110
111pub use commands::{parse_time, refresh, valid_speed};
112
113mod runtime;
114pub(crate) use runtime::Runtime;
115
116pub(crate) fn sanitize_output(value: &mut serde_json::Value) {
117    match value {
118        serde_json::Value::String(s) => *s = crate::clean_text(s),
119        serde_json::Value::Array(a) => a.iter_mut().for_each(sanitize_output),
120        serde_json::Value::Object(o) => o.values_mut().for_each(sanitize_output),
121        _ => {}
122    }
123}