From 151fd484afe0258fe755083a127eb1a28b996595 Mon Sep 17 00:00:00 2001 From: Jeff Hughes Date: Mon, 3 May 2021 19:24:20 -0400 Subject: [PATCH] Add timestamp to filenames of downloaded files --- src/downloads.rs | 8 +++++++- src/main_controller.rs | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/downloads.rs b/src/downloads.rs index cd28fe4..2328bb4 100644 --- a/src/downloads.rs +++ b/src/downloads.rs @@ -2,6 +2,7 @@ use std::fs::File; use std::path::{Path, PathBuf}; use std::sync::mpsc::Sender; +use chrono::{DateTime, Utc}; use sanitize_filename::{sanitize_with_options, Options}; use crate::threadpool::Threadpool; @@ -25,6 +26,7 @@ pub struct EpData { pub pod_id: i64, pub title: String, pub url: String, + pub pubdate: Option>, pub file_path: Option, } @@ -87,12 +89,16 @@ fn download_file(ep_data: EpData, dest: PathBuf, mut max_retries: usize) -> Down _ => "mp3", // assume .mp3 unless we figure out otherwise }; - let file_name = sanitize_with_options(&ep_data.title, Options { + let mut file_name = sanitize_with_options(&ep_data.title, Options { truncate: true, windows: true, // for simplicity, we'll just use Windows-friendly paths for everyone replacement: "", }); + if let Some(pubdate) = ep_data.pubdate { + file_name = format!("{}_{}", file_name, pubdate.format("%Y%m%d_%H%M%S")); + } + let mut file_path = dest; file_path.push(format!("{}.{}", file_name, ext)); diff --git a/src/main_controller.rs b/src/main_controller.rs index 89aef6e..34bf88b 100644 --- a/src/main_controller.rs +++ b/src/main_controller.rs @@ -450,6 +450,7 @@ impl MainController { pod_id: ep.pod_id, title: ep.title.clone(), url: ep.url.clone(), + pubdate: ep.pubdate, file_path: None, }, ep.path.is_none(), @@ -469,6 +470,7 @@ impl MainController { pod_id: ep.pod_id, title: ep.title.clone(), url: ep.url.clone(), + pubdate: ep.pubdate, file_path: None, }) } else {