From 4e2b15ecec3665b0b1091b00eda0cc60361a40cc Mon Sep 17 00:00:00 2001 From: Adam R Grey Date: Mon, 4 Dec 2023 22:24:01 -0500 Subject: [PATCH] output dir is per feed, also: keepcount --- Configuration.cs | 10 ++++++++-- Program.cs | 23 +++++++++++++++++++---- podcast-agent.csproj | 1 - 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Configuration.cs b/Configuration.cs index 6bbf878..7ac26fa 100644 --- a/Configuration.cs +++ b/Configuration.cs @@ -3,8 +3,14 @@ namespace podcast_agent using System.Xml.Linq; public class Configuration { - public string OutputDir { get; set; } = "../"; public string UserAgent { get; set; } = "secret-chinese-spy-drone/0.1"; - public List PodcastUrls { get; set; } + public List FeedCommands { get; set; } + + public class FeedCommand + { + public string Url { get; set; } + public string OutputDir { get; set; } + public uint? KeepCount { get; set; } + } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index fe127cc..15293d4 100644 --- a/Program.cs +++ b/Program.cs @@ -12,11 +12,11 @@ downloader.DefaultRequestHeaders.UserAgent.ParseAdd(conf.UserAgent); XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd"; - foreach (var feed in conf.PodcastUrls) + foreach (var feed in conf.FeedCommands) { try { - var xml = XDocument.Load(feed); + var xml = XDocument.Load(feed.Url); foreach (var item in xml.Descendants("item")) { @@ -30,9 +30,24 @@ } Console.WriteLine($"{title}, {attachmentLink}"); - var downloadPath = Path.Combine(conf.OutputDir, title + ext); + var downloadPath = Path.Combine(feed.OutputDir, title + ext); var dlResult = (await downloader.GetAsync(attachmentLink)); - File.WriteAllBytes(downloadPath, await dlResult.Content.ReadAsByteArrayAsync()); + if(dlResult.IsSuccessStatusCode) + { + if(feed.KeepCount != null && feed.KeepCount > 0) + { + var files = Directory.GetFiles(feed.OutputDir, "*" + ext); + if(files.Length > feed.KeepCount -1) + { + files = files.Order().Take((int)feed.KeepCount -1).ToArray(); + foreach(var f in files) + { + File.Delete(f); + } + } + } + File.WriteAllBytes(downloadPath, await dlResult.Content.ReadAsByteArrayAsync()); + } } } catch (Exception e) diff --git a/podcast-agent.csproj b/podcast-agent.csproj index 2baebd1..e082a26 100644 --- a/podcast-agent.csproj +++ b/podcast-agent.csproj @@ -10,6 +10,5 @@ -