| 1 |
// pipeline.rs |
| 2 |
// |
| 3 |
// Copyright 2017 Jordan Petridis <[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 |
// FIXME: |
| 21 |
//! Docs. |
| 22 |
|
| 23 |
use crate::Source; |
| 24 |
use crate::downloader::client_builder; |
| 25 |
use crate::errors::DataError; |
| 26 |
|
| 27 |
/// The pipline to be run for indexing and updating a Podcast feed that originates from |
| 28 |
/// `Source.uri`. |
| 29 |
/// |
| 30 |
/// Messy temp diagram: |
| 31 |
/// Source -> GET Request -> Update Etags -> Check Status -> Parse `xml/Rss` -> |
| 32 |
/// Convert `rss::Channel` into `Feed` -> Index Podcast -> Index Episodes. |
| 33 |
pub async fn pipeline<S>(sources: S) -> Result<(), reqwest::Error> |
| 34 |
where |
| 35 |
S: IntoIterator<Item = Source>, |
| 36 |
{ |
| 37 |
let client = client_builder().build()?; |
| 38 |
|
| 39 |
let handles: Vec<_> = sources |
| 40 |
.into_iter() |
| 41 |
.map(|source| async { |
| 42 |
let uri = source.uri().to_string(); |
| 43 |
match source.into_feed(&client).await { |
| 44 |
Ok(feed) => match feed.index() { |
| 45 |
Ok(_) => (), |
| 46 |
Err(err) => error!( |
| 47 |
"Error while indexing content feed into the database: {} - {}", |
| 48 |
uri, err |
| 49 |
), |
| 50 |
}, |
| 51 |
// Avoid spamming the stderr when it's not an actual error |
| 52 |
Err(DataError::FeedNotModified(_)) => (), |
| 53 |
Err(err) => error!( |
| 54 |
"Error while fetching the latest xml feed: {} - {}", |
| 55 |
uri, err |
| 56 |
), |
| 57 |
} |
| 58 |
}) |
| 59 |
.collect(); |
| 60 |
futures_util::future::join_all(handles).await; |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(test)] |
| 65 |
mod tests { |
| 66 |
use super::*; |
| 67 |
use crate::Source; |
| 68 |
use crate::database::truncate_db; |
| 69 |
use crate::dbqueries; |
| 70 |
|
| 71 |
// (path, url) tuples. |
| 72 |
const URLS: &[&str] = &[ |
| 73 |
"https://web.archive.org/web/20180120083840if_/https://feeds.feedburner.\ |
| 74 |
com/InterceptedWithJeremyScahill", |
| 75 |
"https://web.archive.org/web/20180120110314if_/https://feeds.feedburner.com/linuxunplugged", |
| 76 |
"https://web.archive.org/web/20180120110727if_/https://rss.acast.com/thetipoff", |
| 77 |
"https://web.archive.org/web/20180120104957if_/https://rss.art19.com/steal-the-stars", |
| 78 |
"https://web.archive.org/web/20180120104741if_/https://www.greaterthancode.\ |
| 79 |
com/feed/podcast", |
| 80 |
]; |
| 81 |
|
| 82 |
#[test] |
| 83 |
/// Insert feeds and update/index them. |
| 84 |
fn test_pipeline() -> Result<(), DataError> { |
| 85 |
truncate_db()?; |
| 86 |
let bad_url = "https://gitlab.gnome.org/World/podcasts.atom"; |
| 87 |
// if a stream returns error/None it stops |
| 88 |
// bad we want to parse all feeds regardless if one fails |
| 89 |
Source::from_url(bad_url)?; |
| 90 |
|
| 91 |
URLS.iter().for_each(|url| { |
| 92 |
// Index the urls into the source table. |
| 93 |
Source::from_url(url).unwrap(); |
| 94 |
}); |
| 95 |
|
| 96 |
let sources = dbqueries::get_sources()?; |
| 97 |
let rt = tokio::runtime::Runtime::new()?; |
| 98 |
rt.block_on(pipeline(sources))?; |
| 99 |
|
| 100 |
let sources = dbqueries::get_sources()?; |
| 101 |
// Run again to cover Unique constrains errors. |
| 102 |
rt.block_on(pipeline(sources))?; |
| 103 |
|
| 104 |
// Assert the index rows equal the controlled results |
| 105 |
assert_eq!(dbqueries::get_sources()?.len(), 6); |
| 106 |
assert_eq!(dbqueries::get_podcasts()?.len(), 5); |
| 107 |
assert_eq!(dbqueries::get_episodes()?.len(), 354); |
| 108 |
Ok(()) |
| 109 |
} |
| 110 |
} |