aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2025-11-22 07:52:55 +0300
committerA Farzat <a@farzat.xyz>2025-11-22 07:52:55 +0300
commita4836871247da314110d1e42344e3239eadbdd3e (patch)
treee304c645f23d3d34e5673794b160dabec7872050
parentdb713f8e215b2c349e4eaf0fa4791b876125148b (diff)
downloadsimple-rss-podcast-downloader-a4836871247da314110d1e42344e3239eadbdd3e.tar.gz
simple-rss-podcast-downloader-a4836871247da314110d1e42344e3239eadbdd3e.zip
Add optional numbering of downloaded episodes
-rw-r--r--src/lib.rs16
-rw-r--r--src/main.rs10
2 files changed, 21 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 78ddfe4..74e37b2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -13,19 +13,29 @@ pub fn parse_feed(xml: &str) -> Result<Channel, rss::Error> {
}
/// Extract the audio URLs from the given channel
-pub fn get_audio_urls(channel: &Channel) -> Vec<&str> {
+pub fn get_audio_urls(channel: &Channel) -> Vec<(usize, &str)> {
channel
.items()
.iter()
.rev()
- .filter_map(|item| item.enclosure().map(|e| e.url()))
+ .enumerate()
+ .filter_map(|(i, item)| item.enclosure().map(|e| (i + 1, e.url())))
.collect()
}
/// Download the given audio file to the supplied directory
-pub fn download_file(url: &str, output_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
+pub fn download_file(
+ url: &str,
+ output_dir: &str,
+ prefix: Option<&str>,
+) -> Result<(), Box<dyn std::error::Error>> {
let mut response = reqwest::blocking::get(url)?;
let filename = url.split('/').next_back().unwrap_or("episode.mp3");
+ let filename = if let Some(p) = prefix {
+ format!("{}-{}", p, filename)
+ } else {
+ filename.to_string()
+ };
let mut path = PathBuf::from(output_dir);
path.push(filename);
let mut file = File::create(path)?;
diff --git a/src/main.rs b/src/main.rs
index a2ac775..50c59e3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -18,9 +18,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let xml = fetch_feed(&args.feed_url)?;
let channel = parse_feed(&xml)?;
- for url in get_audio_urls(&channel) {
+ let pad = channel.items().len().to_string().len();
+ for (i, url) in get_audio_urls(&channel) {
+ let prefix = if args.numbered {
+ Some(format!("{:0width$}", i, width = pad))
+ } else {
+ None
+ };
println!("Downloading file: {}", url);
- download_file(url, &args.output_dir)?;
+ download_file(url, &args.output_dir, prefix.as_deref())?;
}
Ok(())