Skip to content

Commit

Permalink
new DownloadedEpisode type
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Apr 7, 2024
1 parent 7faad41 commit bdecf60
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
9 changes: 9 additions & 0 deletions src/episode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fs::File;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct Episode<'a> {
pub title: &'a str,
Expand Down Expand Up @@ -33,3 +36,9 @@ impl<'a> Episode<'a> {
self.raw.get(tag).unwrap().as_str()
}
}

pub struct DownloadedEpisode<'a> {
pub inner: Episode<'a>,
pub file: File,
pub path: PathBuf,
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct Args {
#[tokio::main]
async fn main() {
let args = Args::parse();
let config_path = args.config.unwrap_or_else(|| crate::utils::config_toml());
let config_path = args.config.unwrap_or_else(crate::utils::config_toml);

let should_sync =
args.import.is_none() && args.export.is_none() && args.add.is_empty() && !args.tutorial;
Expand Down
69 changes: 44 additions & 25 deletions src/podcast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::{Config, GlobalConfig, PodcastConfig};

use crate::config::DownloadMode;
use crate::episode::DownloadedEpisode;
use crate::episode::Episode;
use crate::utils::current_unix;
use crate::utils::get_guid;
Expand Down Expand Up @@ -414,7 +415,7 @@ impl Podcast {
}
}

pub async fn download_episode(&self, episode: &Episode<'_>) -> PathBuf {
pub async fn download_episode<'a>(&self, episode: Episode<'a>) -> DownloadedEpisode<'a> {
let partial_path = {
let file_name = format!("{}.partial", episode.guid);
self.download_folder().join(file_name)
Expand Down Expand Up @@ -484,41 +485,59 @@ impl Podcast {

std::fs::rename(partial_path, &path).unwrap();

path
DownloadedEpisode {
inner: episode,
file,
path,
}
}

async fn normalize_episode(&self, episode: &Episode<'_>, file_path: &Path) -> PathBuf {
async fn normalize_episode(&self, episode: &mut DownloadedEpisode<'_>) {
let file_path = &episode.path;
let mp3_tags = (file_path.extension().unwrap() == "mp3").then_some(
crate::tags::set_mp3_tags(&self.channel, &episode, &file_path, &self.config.id3_tags)
.await,
crate::tags::set_mp3_tags(
&self.channel,
&episode.inner,
file_path,
&self.config.id3_tags,
)
.await,
);

self.rename_file(&file_path, mp3_tags.as_ref(), episode)
let new_path = self.rename_file(&file_path, mp3_tags.as_ref(), &episode.inner);
episode.path = new_path;
}

fn run_download_hook(
&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();
});

return Some(handle);
}

None
}

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 file_paths = vec![];
let mut downloaded = vec![];
let mut hook_handles = vec![];
for (index, episode) in episodes.iter().enumerate() {
self.show_download_info(episode, index, longest_podcast_name, episodes.len());

let file_path = self.download_episode(episode).await;
let file_path = self.normalize_episode(episode, &file_path).await;

self.mark_downloaded(episode);
file_paths.push(file_path.clone());

if let Some(script_path) = self.config.download_hook.clone() {
let handle = tokio::task::spawn_blocking(move || {
std::process::Command::new(script_path)
.arg(&file_path)
.output()
});
hook_handles.push(handle);
}
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;
self.normalize_episode(&mut downloaded_episode).await;
self.mark_downloaded(&downloaded_episode.inner);
hook_handles.extend(self.run_download_hook(&downloaded_episode));
downloaded.push(downloaded_episode);
}

if !hook_handles.is_empty() {
Expand All @@ -530,7 +549,7 @@ impl Podcast {
let msg = format!("✅ {}", &self.name);
self.finish_with_msg(msg);

file_paths
downloaded.into_iter().map(|episode| episode.path).collect()
}
}

Expand Down

0 comments on commit bdecf60

Please sign in to comment.