Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Apr 7, 2024
1 parent bdecf60 commit ec1d794
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::collections::HashMap;
use std::io::Write;
use std::path::{Path, PathBuf};

//use anyhow::Result;

/// Represents a [`PodcastConfig`] value that is either enabled, disabled, or we defer to the
/// global config.
#[derive(Clone, Copy, Debug, Default)]
Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ async fn main() {
return;
}

eprintln!("Checking for new episodes...");
let mp = (!args.quiet).then_some(MultiProgress::new());

let podcasts = {
Expand All @@ -78,17 +77,21 @@ async fn main() {
};

// Longest podcast name is used for formatting.
let Some(longest_name) = podcasts
let longest_name = match podcasts
.iter()
.map(|podcast| podcast.name().chars().count())
.max()
else {
eprintln!("no podcasts configured");
std::process::exit(1);
{
Some(len) => len,
None => {
eprintln!("no podcasts configured");
std::process::exit(1);
}
};

let mut futures = vec![];

eprintln!("Checking for new episodes...");
for podcast in podcasts {
let future = tokio::task::spawn(async move { podcast.sync(longest_name).await });
futures.push(future);
Expand Down
35 changes: 17 additions & 18 deletions src/podcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,6 @@ impl Podcast {
}
}

fn finish_with_msg(&self, msg: String) {
if let Some(pb) = &self.progress_bar {
pb.finish_with_message(msg);
}
}

pub async fn download_episode<'a>(&self, episode: Episode<'a>) -> DownloadedEpisode<'a> {
let partial_path = {
let file_name = format!("{}.partial", episode.guid);
Expand Down Expand Up @@ -512,25 +506,33 @@ impl Podcast {
&self,
episode: &DownloadedEpisode,
) -> Option<tokio::task::JoinHandle<()>> {
if let Some(script_path) = self.config.download_hook.clone() {
let path = episode.path.clone();
let handle = tokio::task::spawn_blocking(move || {
let _ = std::process::Command::new(script_path).arg(path).output();
});
let script_path = self.config.download_hook.clone()?;
let path = episode.path.clone();

return Some(handle);
}
let handle = tokio::task::spawn_blocking(move || {
let _ = std::process::Command::new(script_path).arg(path).output();
});

None
Some(handle)
}

fn mark_complete(&self) {
if let Some(pb) = &self.progress_bar {
self.set_template("{msg}");
let msg = format!("✅ {}", &self.name);
pb.finish_with_message(msg);
}
}

pub async fn sync(&self, longest_podcast_name: usize) -> Vec<PathBuf> {
self.set_download_style();

let episodes = self.pending_episodes();
let episode_qty = episodes.len();

let mut downloaded = vec![];
let mut hook_handles = vec![];

for (index, episode) in episodes.into_iter().enumerate() {
self.show_download_info(&episode, index, longest_podcast_name, episode_qty);
let mut downloaded_episode = self.download_episode(episode).await;
Expand All @@ -545,10 +547,7 @@ impl Podcast {
futures::future::join_all(hook_handles).await;
}

self.set_template("{msg}");
let msg = format!("✅ {}", &self.name);
self.finish_with_msg(msg);

self.mark_complete();
downloaded.into_iter().map(|episode| episode.path).collect()
}
}
Expand Down

0 comments on commit ec1d794

Please sign in to comment.