podcast-agent/Program.cs

94 lines
4.0 KiB
C#
Raw Normal View History

2023-12-04 21:31:31 -05:00
namespace podcast_agent
2023-12-04 20:39:43 -05:00
{
2023-12-04 21:31:31 -05:00
using System.Xml.Linq;
2023-12-04 21:58:05 -05:00
using Newtonsoft.Json;
2023-12-04 21:31:31 -05:00
2023-12-04 20:39:43 -05:00
class Program
{
static async Task Main(string[] args)
{
2023-12-04 21:58:05 -05:00
var conf = Configure();
2023-12-04 21:31:31 -05:00
var downloader = new HttpClient();
2023-12-04 21:58:05 -05:00
downloader.DefaultRequestHeaders.UserAgent.ParseAdd(conf.UserAgent);
2023-12-04 21:31:31 -05:00
XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd";
foreach (var feed in conf.FeedCommands)
2023-12-04 21:31:31 -05:00
{
2023-12-04 21:58:05 -05:00
try
2023-12-04 21:31:31 -05:00
{
var xml = XDocument.Load(feed.Url);
2023-12-04 21:58:05 -05:00
foreach (var item in xml.Descendants("item"))
{
var title = item.Element("title")?.Value;
var ext = ".wav";
var attachmentLink = item.Element("enclosure")?.Attribute("url")?.Value.ToString();
var probableFilename = attachmentLink.Substring(0, attachmentLink.IndexOf('?'));
if (probableFilename.LastIndexOf('.') > 0)
{
ext = probableFilename.Substring(probableFilename.LastIndexOf('.'));
}
2023-12-06 13:48:15 -05:00
foreach(char c in "'\":\\?/")
{
title = title.Replace(c, ' ');
}
2023-12-04 21:58:05 -05:00
Console.WriteLine($"{title}, {attachmentLink}");
var downloadPath = Path.Combine(feed.OutputDir, title + ext);
2023-12-04 21:58:05 -05:00
var dlResult = (await downloader.GetAsync(attachmentLink));
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());
}
2023-12-04 21:58:05 -05:00
}
2023-12-04 21:31:31 -05:00
}
2023-12-04 21:58:05 -05:00
catch (Exception e)
{
Console.Error.WriteLine($"failed attempting to dl {feed} -");
Console.Error.WriteLine(e);
}
}
}
static Configuration Configure(string configurationPath = "appsettings.json")
{
if (!File.Exists(configurationPath))
{
Console.Error.WriteLine($"could not find configuration at {configurationPath}! copying sample to that spot.");
File.Copy("sample-appsettings.json", configurationPath); //and you know what, if that explodes at the OS level, the OS should give you an error
return null;
}
var fileContents = File.ReadAllText(configurationPath);
if (string.IsNullOrWhiteSpace(fileContents))
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
}
var conf = JsonConvert.DeserializeObject<Configuration>(fileContents);
2023-12-04 21:31:31 -05:00
2023-12-04 21:58:05 -05:00
if (conf == null)
{
Console.Error.WriteLine($"configuration file at {configurationPath} was empty! overwriting with sample settings.");
File.Copy("sample-appsettings.json", configurationPath, true);
return null;
2023-12-04 21:31:31 -05:00
}
2023-12-04 21:58:05 -05:00
return conf;
2023-12-04 20:39:43 -05:00
}
}
}