Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dskleingeld/pods into main
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsk committed Feb 10, 2021
2 parents 32ac53f + 03a3f87 commit f87b046
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/feed/search/applepodcasts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/feed/search/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -81,14 +86,24 @@ pub struct SearchResult {
impl Search {
pub async fn search(&mut self, search_term: String, ignore_budget: bool)
-> Vec<SearchResult> {
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(..)){
Expand Down
2 changes: 1 addition & 1 deletion src/feed/search/podcastindex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 4 additions & 5 deletions src/page/podcasts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ pub struct Search {
}

impl Search {
pub fn do_search(&mut self) -> Command<crate::Message> {
pub fn do_search(&mut self, ignore_budget: bool) -> Command<crate::Message> {
// 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<crate::Message> {
pub fn input_changed(&mut self, pod_db: PodcastDb, input: String) -> Command<crate::Message> {
self.input_value = input;
if feed::valid_url(&self.input_value) {
let url = self.input_value.clone();
Command::perform(
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()
}
Expand Down

0 comments on commit f87b046

Please sign in to comment.