2019-07-07 14:56:13 +00:00
|
|
|
|
using System;
|
2019-07-14 00:02:45 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
2019-07-28 20:19:29 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-28 17:15:00 +00:00
|
|
|
|
using Nett;
|
2019-07-07 14:56:13 +00:00
|
|
|
|
|
2019-07-28 16:24:21 +00:00
|
|
|
|
namespace PolyFeed
|
2019-07-07 14:56:13 +00:00
|
|
|
|
{
|
2019-07-14 00:02:45 +00:00
|
|
|
|
internal class Settings
|
2019-07-07 14:56:13 +00:00
|
|
|
|
{
|
2019-07-14 00:02:45 +00:00
|
|
|
|
public readonly string ProgramName = "PolyFeed";
|
|
|
|
|
public readonly string Description = "creates Atom feeds from websites that don't support it";
|
2019-07-28 17:15:00 +00:00
|
|
|
|
|
|
|
|
|
public string ConfigFilepath = "feed.toml";
|
|
|
|
|
public string OutputFilepath = "feed.atom";
|
2019-07-14 00:02:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
private static Settings settings = new Settings();
|
|
|
|
|
|
|
|
|
|
public static int Main(string[] args)
|
2019-07-07 14:56:13 +00:00
|
|
|
|
{
|
2019-07-14 00:02:45 +00:00
|
|
|
|
|
|
|
|
|
///// 1: Parse arguments /////
|
|
|
|
|
List<string> extras = new List<string>();
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!args[i].StartsWith("-"))
|
|
|
|
|
{
|
|
|
|
|
extras.Add(args[i]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (args[i])
|
|
|
|
|
{
|
|
|
|
|
case "-h":
|
|
|
|
|
case "--help":
|
2019-07-28 17:15:00 +00:00
|
|
|
|
Console.WriteLine($"{settings.ProgramName}, {GetProgramVersion()}");
|
2019-07-14 00:02:45 +00:00
|
|
|
|
Console.WriteLine(" By Starbeamrainbowlabs");
|
|
|
|
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine($"This program {settings.Description}.");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Usage:");
|
|
|
|
|
Console.WriteLine($" ./{Path.GetFileName(Assembly.GetExecutingAssembly().Location)} [arguments]");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Options:");
|
2019-07-28 17:15:00 +00:00
|
|
|
|
Console.WriteLine(" -h --help Displays this message");
|
|
|
|
|
Console.WriteLine(" -v --version Outputs the version number of this program");
|
|
|
|
|
Console.WriteLine(" -c --config Specifies the location of the feed configuration file to use to generate a feed (default: feed.toml)");
|
|
|
|
|
Console.WriteLine(" -o --output Specifies the location to write the output feed to (default: feed.atom)");
|
2019-07-14 00:02:45 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case "-v":
|
|
|
|
|
case "--version":
|
2019-07-28 17:15:00 +00:00
|
|
|
|
Console.WriteLine($"{settings.ProgramName}\t{GetProgramVersion()}");
|
2019-07-14 00:02:45 +00:00
|
|
|
|
return 0;
|
2019-07-28 17:15:00 +00:00
|
|
|
|
|
|
|
|
|
case "-c":
|
|
|
|
|
case "--config":
|
|
|
|
|
settings.ConfigFilepath = args[++i];
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "-o":
|
|
|
|
|
case "--output":
|
|
|
|
|
settings.OutputFilepath = args[++i];
|
|
|
|
|
break;
|
2019-07-14 00:02:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///// 2: Acquire environment variables /////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// 3: Run program /////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 20:19:29 +00:00
|
|
|
|
private static async Task<string> run()
|
2019-07-28 17:15:00 +00:00
|
|
|
|
{
|
|
|
|
|
FeedSource feedSource = new FeedSource();
|
|
|
|
|
TomlTable config = Toml.ReadFile(settings.ConfigFilepath, TomlSettings.Create());
|
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<string, TomlObject> item in config) {
|
2019-07-28 20:19:29 +00:00
|
|
|
|
string key = Regex.Replace(
|
|
|
|
|
item.Key,
|
|
|
|
|
@"(^|_)[A-Za-z0-9]",
|
|
|
|
|
(match) => match.Value.Replace("_", "").ToUpper()
|
|
|
|
|
);
|
2019-07-28 17:15:00 +00:00
|
|
|
|
string value = item.Value.Get<TomlString>().Value;
|
|
|
|
|
feedSource.GetType().GetProperty(value).SetValue(
|
|
|
|
|
feedSource,
|
|
|
|
|
value
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-07-28 20:19:29 +00:00
|
|
|
|
|
|
|
|
|
FeedBuilder feedBuilder = new FeedBuilder();
|
|
|
|
|
await feedBuilder.AddSource(feedSource);
|
|
|
|
|
return await feedBuilder.Render();
|
2019-07-28 17:15:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:02:45 +00:00
|
|
|
|
|
|
|
|
|
#region Helper Methods
|
|
|
|
|
|
2019-07-28 17:15:00 +00:00
|
|
|
|
public static string GetProgramVersion()
|
2019-07-14 00:02:45 +00:00
|
|
|
|
{
|
|
|
|
|
Version version = Assembly.GetExecutingAssembly().GetName().Version;
|
|
|
|
|
return $"{version.Major}.{version.Minor}";
|
2019-07-07 14:56:13 +00:00
|
|
|
|
}
|
2019-07-14 00:02:45 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2019-07-07 14:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|