| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
pub async fn search(query: &str) -> Result<Vec<FoundPodcast>, SearchError> { |
| 42 |
|
| 43 |
|
| 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 |
} |