Debian Codegraph @

podcasts-data/src/discovery/search.rs

raw symbols SCIP
1 // platform.rs
2 //
3 // Copyright 2022-2024 nee <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use crate::dbqueries;
21 use crate::discovery::data::*;
22 use crate::discovery::fyyd;
23 use crate::discovery::itunes;
24 use anyhow::Result;
25 use thiserror::Error;
26 use tokio::join;
27
28 #[derive(Error, Debug)]
29 pub enum SearchError {
30 #[error("Network Error: {0}")]
31 ReqwestError(#[from] reqwest::Error),
32 #[error("Other Error: {0}")]
33 AnyhowError(#[from] anyhow::Error),
34 #[error("No Search Platform was selected.")]
35 NoSearchPlatformsSelected,
36 }
37
38 /// Sends a http search to all platforms that are active in the settings.
39 /// It joins all results into a Vector and tries to filter out duplicates.
40 /// Results are sorted as they are returned by the Search platforms.
41 pub async fn search(query: &str) -> Result<Vec<FoundPodcast>, SearchError> {
42 // This looks like it could be abstracted more,
43 // but traits with async fns are impossible to deal with.
44 let settings = dbqueries::get_discovery_settings();
45
46 let fyyd_on = *settings.get("fyyd.de").unwrap_or(&false);
47 let itunes_on = *settings.get("itunes.apple.com").unwrap_or(&false);
48
49 if !fyyd_on && !itunes_on {
50 return Err(SearchError::NoSearchPlatformsSelected);
51 }
52
53 let fyyd = fyyd::search(query, fyyd_on);
54 let itunes = itunes::search(query, itunes_on);
55 let (fyyd, itunes) = join!(fyyd, itunes);
56
57 let fyyd: Vec<FoundPodcast> = fyyd.map_err(|e| error!("fyyd {e}")).unwrap_or_default();
58 let itunes: Vec<FoundPodcast> = itunes.map_err(|e| error!("itunes {e}")).unwrap_or_default();
59
60 trace!("combining {fyyd:#?} with {itunes:#?}");
61 let mut merged = fyyd;
62 merge_results(&mut merged, itunes);
63 Ok(merged)
64 }
65
66 fn merge_results(merged: &mut Vec<FoundPodcast>, other_results: Vec<FoundPodcast>) {
67 for p in other_results.into_iter() {
68 if let Some(existing) = merged.iter_mut().find(|p2| p.eq(*p2)) {
69 existing.combine(p);
70 } else {
71 merged.push(p);
72 }
73 }
74 }
75
76 #[cfg(test)]
77 mod tests {
78 use super::*;
79
80 #[test]
81 fn merge() -> Result<()> {
82 let itunes = vec![FoundPodcast {
83 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
84 title: "Corner Späti".to_string(),
85 author: "The Späti Boys".to_string(),
86 description: "".to_string(),
87 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg"
88 .to_string(),
89 episode_count: Some(347),
90 last_publication: Some(
91 chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00")
92 .unwrap()
93 .with_timezone(&chrono::Local),
94 ),
95 }];
96 let fyyd = vec![FoundPodcast {
97 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
98 title: "Corner Späti".to_string(),
99 author: "The Späti Boys".to_string(),
100 description: "Weekly discussions of a deteriorating world all from the comfort of your local smoke-filled Spätkauf.\nhttps://www.patreon.com/cornerspaeti\nhttps://www.operationglad.io/start\n".to_string(),
101 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg".to_string(),
102 episode_count: Some(348),
103 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00").unwrap().with_timezone(&chrono::Local))
104 }];
105 let expected: Vec<FoundPodcast> = vec![
106 FoundPodcast {
107 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
108 title: "Corner Späti".to_string(),
109 author: "The Späti Boys".to_string(),
110 description: "Weekly discussions of a deteriorating world all from the comfort of your local smoke-filled Spätkauf.\nhttps://www.patreon.com/cornerspaeti\nhttps://www.operationglad.io/start\n".to_string(),
111 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg".to_string(),
112 episode_count: Some(348),
113 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00").unwrap().with_timezone(&chrono::Local))
114 }
115 ];
116 let itunes2 = itunes.clone();
117 let mut merged = itunes;
118 merge_results(&mut merged, fyyd);
119 assert_eq!(expected, merged);
120 merge_results(&mut merged, itunes2);
121 assert_eq!(expected, merged);
122 Ok(())
123 }
124
125 #[test]
126 fn merge_nones() -> Result<()> {
127 let itunes = vec![FoundPodcast {
128 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
129 title: "Corner Späti".to_string(),
130 author: "The Späti Boys".to_string(),
131 description: "".to_string(),
132 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg"
133 .to_string(),
134 episode_count: None,
135 last_publication: None,
136 }];
137 let fyyd = vec![FoundPodcast {
138 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
139 title: "Corner Späti".to_string(),
140 author: "The Späti Boys".to_string(),
141 description: "Weekly discussions of a deteriorating world all from the comfort of your local smoke-filled Spätkauf.\nhttps://www.patreon.com/cornerspaeti\nhttps://www.operationglad.io/start\n".to_string(),
142 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg".to_string(),
143 episode_count: Some(348),
144 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00").unwrap().with_timezone(&chrono::Local))
145 }];
146 let expected: Vec<FoundPodcast> = vec![
147 FoundPodcast {
148 feed: "https://feeds.fireside.fm/cornerspaeti/rss".to_string(),
149 title: "Corner Späti".to_string(),
150 author: "The Späti Boys".to_string(),
151 description: "Weekly discussions of a deteriorating world all from the comfort of your local smoke-filled Spätkauf.\nhttps://www.patreon.com/cornerspaeti\nhttps://www.operationglad.io/start\n".to_string(),
152 art: "https://img-1.fyyd.de/pd/small/77182e877ac679f9414148fab3dddb74782c2.jpg".to_string(),
153 episode_count: Some(348),
154 last_publication: Some(chrono::DateTime::parse_from_rfc3339("2024-03-07T10:00:00+01:00").unwrap().with_timezone(&chrono::Local))
155 }
156 ];
157 let itunes2 = itunes.clone();
158 let mut merged = itunes;
159 merge_results(&mut merged, fyyd);
160 assert_eq!(expected, merged);
161 merge_results(&mut merged, itunes2);
162 assert_eq!(expected, merged);
163 Ok(())
164 }
165 }