diff options
| author | A Farzat <a@farzat.xyz> | 2025-11-21 13:00:21 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2025-11-21 13:00:21 +0300 |
| commit | c41059e398f5499376e41b898aed35ed5054d50a (patch) | |
| tree | 2644eed662d236bac6cbb99a9debb14dce577d30 | |
| parent | 6227b533cebd6c54df183289f17ad380d9539106 (diff) | |
| download | simple-rss-podcast-downloader-c41059e398f5499376e41b898aed35ed5054d50a.tar.gz simple-rss-podcast-downloader-c41059e398f5499376e41b898aed35ed5054d50a.zip | |
Add file download functionality
| -rw-r--r-- | src/lib.rs | 13 | ||||
| -rw-r--r-- | src/main.rs | 11 |
2 files changed, 19 insertions, 5 deletions
@@ -1,4 +1,6 @@ use std::env; +use std::fs::File; +use std::path::PathBuf; use rss::Channel; /// Parse CLI arguments and return (feed_url, output_dir) @@ -35,3 +37,14 @@ pub fn get_audio_urls(channel: &Channel) -> Vec<&str> { } audio_urls } + +/// Download the given audio file to the supplied directory +pub fn download_file(url: &str, output_dir: &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 mut path = PathBuf::from(output_dir); + path.push(filename); + let mut file = File::create(path)?; + std::io::copy(&mut response, &mut file)?; + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 82fb97e..7e28a52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,16 @@ use simple_rss_podcast_downloader::*; -fn main() { +fn main() -> Result<(), Box<dyn std::error::Error>> { let (feed_url, output_dir) = parse_args(); println!("Feed RSS feed from: {}", feed_url); - let xml = fetch_feed(&feed_url).expect("Error fetching feed"); - let channel = parse_feed(&xml).expect("Error parsing feed"); + let xml = fetch_feed(&feed_url)?; + let channel = parse_feed(&xml)?; for url in get_audio_urls(&channel) { - println!("Audio URL: {}", url) + println!("Downloading file: {}", url); + download_file(url, &output_dir)?; } - println!("Output directory: {}", output_dir); + Ok(()) } |
