output dir is per feed, also: keepcount

This commit is contained in:
Adam R Grey 2023-12-04 22:24:01 -05:00
parent f9d18c5baf
commit 4e2b15ecec
3 changed files with 27 additions and 7 deletions

View File

@ -3,8 +3,14 @@ namespace podcast_agent
using System.Xml.Linq; using System.Xml.Linq;
public class Configuration public class Configuration
{ {
public string OutputDir { get; set; } = "../";
public string UserAgent { get; set; } = "secret-chinese-spy-drone/0.1"; public string UserAgent { get; set; } = "secret-chinese-spy-drone/0.1";
public List<string> PodcastUrls { get; set; } public List<FeedCommand> FeedCommands { get; set; }
public class FeedCommand
{
public string Url { get; set; }
public string OutputDir { get; set; }
public uint? KeepCount { get; set; }
}
} }
} }

View File

@ -12,11 +12,11 @@
downloader.DefaultRequestHeaders.UserAgent.ParseAdd(conf.UserAgent); downloader.DefaultRequestHeaders.UserAgent.ParseAdd(conf.UserAgent);
XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd"; XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd";
foreach (var feed in conf.PodcastUrls) foreach (var feed in conf.FeedCommands)
{ {
try try
{ {
var xml = XDocument.Load(feed); var xml = XDocument.Load(feed.Url);
foreach (var item in xml.Descendants("item")) foreach (var item in xml.Descendants("item"))
{ {
@ -30,9 +30,24 @@
} }
Console.WriteLine($"{title}, {attachmentLink}"); 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)); 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) catch (Exception e)

View File

@ -10,6 +10,5 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="youtubedlsharp" Version="0.4.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>