From 8272fa93e68850e742e04cccde9f04a96e65f4bb Mon Sep 17 00:00:00 2001 From: dskleingeld <11743287+dskleingeld@users.noreply.github.com> Date: Wed, 10 Feb 2021 15:27:17 +0100 Subject: [PATCH] fixes #36 --- src/feed/search/applepodcasts.rs | 2 +- src/feed/search/mod.rs | 25 ++++++++++++++++++++----- src/feed/search/podcastindex.rs | 2 +- src/main.rs | 4 ++-- src/page/podcasts.rs | 9 ++++----- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/feed/search/applepodcasts.rs b/src/feed/search/applepodcasts.rs index 795dcd4..c3f67de 100644 --- a/src/feed/search/applepodcasts.rs +++ b/src/feed/search/applepodcasts.rs @@ -18,7 +18,7 @@ impl Default for Search { .expect("could not construct http client for podcast searching"), title: Regex::new(r#"collectionName":"(.+?)""#).unwrap(), url: Regex::new(r#"feedUrl":"(.+?)""#).unwrap(), - budget: ApiBudget::from(5), + budget: ApiBudget::from(20), } } } diff --git a/src/feed/search/mod.rs b/src/feed/search/mod.rs index 80d9b6a..d1dcbf8 100644 --- a/src/feed/search/mod.rs +++ b/src/feed/search/mod.rs @@ -28,6 +28,7 @@ pub enum Error { OutOfCalls, } +//TODO const generic for initial api budget when that stabilizes #[derive(Clone)] struct ApiBudget { max_per_min: u8, @@ -51,12 +52,16 @@ impl ApiBudget { let new = (0.8f32 * current + success as f32) as u8; let new = new.min(1); let new = new.max(self.max_per_min); + log::debug!("lowered api rate to: {}", new); self.current_per_min = new; } fn calls_in_last_minute(&self) -> usize { - self.called.iter() + dbg!(&self.called); + let calls = self.called.iter() .take_while(|t| t.elapsed() < Duration::from_secs(61)) - .count() + .count(); + log::trace!("calls in last minute: {}", calls); + calls } pub fn left(&self) -> u8 { self.current_per_min.saturating_sub(self.calls_in_last_minute() as u8) @@ -81,14 +86,24 @@ pub struct SearchResult { impl Search { pub async fn search(&mut self, search_term: String, ignore_budget: bool) -> Vec { + log::debug!("performing search for: {}", &search_term); let search_a = self.apple_podcasts.search(&search_term, ignore_budget); let search_b = self.podcast_index.search(&search_term, ignore_budget); let (res_a, res_b) = tokio::join!(search_a, search_b); match (res_a, res_b) { - (Err(_), Err(e2)) => {return Vec::new()},//TODO log the errs - (Err(_), Ok(b)) => {return b}, //TODO log the errs - (Ok(a), Err(_)) => {return a}, + (Err(e1), Err(e2)) => { + log::debug!("backends errored: {}, {}",e1,e2); + return Vec::new(); + } + (Err(e1), Ok(b)) => { + log::debug!("backend error: {}", e1); + return b; + } + (Ok(a), Err(e2)) => { + log::debug!("backend error: {}", e2); + return a; + } (Ok(mut a), Ok(mut b)) => { let mut result = HashSet::new(); for res in a.drain(..).chain(b.drain(..)){ diff --git a/src/feed/search/podcastindex.rs b/src/feed/search/podcastindex.rs index 23b2245..24cb1de 100644 --- a/src/feed/search/podcastindex.rs +++ b/src/feed/search/podcastindex.rs @@ -16,7 +16,7 @@ impl Default for Search { .build() .expect("could not construct http client for podcast searching"), title_url: Regex::new(r#""title":"(.+?)","url":"(.+?)","originalUrl":"#).unwrap(), - budget: ApiBudget::from(5), + budget: ApiBudget::from(20), } } } diff --git a/src/main.rs b/src/main.rs index c4cfa67..1090a31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,11 +192,11 @@ impl Application for App { Message::SearchSubmit => self .podcasts .search - .do_search(), + .do_search(true), Message::SearchInputChanged(input) => self .podcasts .search - .search_input_changed(self.pod_db.clone(), input), + .input_changed(self.pod_db.clone(), input), Message::SearchResults(r) => { self.podcasts.list.update_feedres(r); Command::none() diff --git a/src/page/podcasts.rs b/src/page/podcasts.rs index 48226cf..dd804a6 100644 --- a/src/page/podcasts.rs +++ b/src/page/podcasts.rs @@ -17,15 +17,15 @@ pub struct Search { } impl Search { - pub fn do_search(&mut self) -> Command { + pub fn do_search(&mut self, ignore_budget: bool) -> Command { // always do a web search if a search was submitted let term = self.input_value.clone(); let search = self.search.clone(); Command::perform( - async move {search.lock().await.search(term, true).await}, + async move {search.lock().await.search(term, ignore_budget).await}, |r| Message::SearchResults(r)) } - pub fn search_input_changed(&mut self, pod_db: PodcastDb, input: String) -> Command { + pub fn input_changed(&mut self, pod_db: PodcastDb, input: String) -> Command { self.input_value = input; if feed::valid_url(&self.input_value) { let url = self.input_value.clone(); @@ -33,8 +33,7 @@ impl Search { feed::add_podcast(pod_db, url), |(title, id)| Message::AddedPodcast(title,id)) } else if self.input_value.len() > 4 { - self.do_search(); - Command::none() + self.do_search(false) } else { Command::none() }