From 963a0dc86588228a6f7f60ad991b9cda29d7d422 Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Fri, 16 Jul 2021 21:40:14 -0400 Subject: [PATCH] migrated in --- .gitignore | 16 +++++++ .vscode/launch.json | 27 ++++++++++++ .vscode/tasks.json | 42 ++++++++++++++++++ Program.cs | 105 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ WebhookData.cs | 9 ++++ config.cs | 9 ++++ config.example.json | 6 +++ rssberg.csproj | 15 +++++++ 9 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Program.cs create mode 100644 README.md create mode 100644 WebhookData.cs create mode 100644 config.cs create mode 100644 config.example.json create mode 100644 rssberg.csproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b80a9a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +config.json +doneIDs.txt + +# ---> VisualStudioCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +bin/ +obj/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e08183d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/net5.0/rssberg.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..be327db --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/rssberg.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/rssberg.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/rssberg.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..3c568d5 --- /dev/null +++ b/Program.cs @@ -0,0 +1,105 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using System.Xml; +using System.ServiceModel.Syndication; +using System.Linq; +using System.Collections.Generic; +using System.Net.Http; +using Newtonsoft.Json; +using Discord; + +namespace rssberg +{ + class Program + { + private const string archivePath = "doneIDs.txt"; + private const string configPath = "config.json"; + static readonly HttpClient client = new HttpClient(); + static List doneIDs; + + static void Main(string[] args) + { + if (!File.Exists(configPath)){ + File.CreateText(configPath); + var sampleCfg = new Config(); + sampleCfg.FeedPairs.Add("rss", "webhook"); + File.WriteAllText(configPath, JsonConvert.SerializeObject(sampleCfg)); + } + var cfg = JsonConvert.DeserializeObject(File.ReadAllText(configPath)); + + Console.WriteLine("rss reader alive"); + + if (!File.Exists(archivePath)) + { + File.CreateText(archivePath); + } + doneIDs = File.ReadAllLines(archivePath).ToList(); + + var postMetaTasks = new List(); + foreach (var kvp in cfg.FeedPairs.ToList()) + { + postMetaTasks.Add(rss2webhook(kvp.Key, kvp.Value)); + } + + Console.WriteLine($"rss reader collected {postMetaTasks.Count} post metatasks"); + Task.WaitAll(postMetaTasks.ToArray()); + while(doneIDs.Count > 5000){ + doneIDs.RemoveAt(0); + } + File.WriteAllLines(archivePath, doneIDs); + + Console.WriteLine("rss reader actually done"); + } + + private static async Task rss2webhook(string key, string value) + { + var postTasks = new List>(); + postTasks.Add(Task.Run(() => {return (ulong)1;})); //dummy task for debugging + var responseStream = await client.GetStreamAsync(key); + var wh = new Discord.Webhook.DiscordWebhookClient(value); + + using (XmlReader xr = XmlReader.Create(responseStream)) + { + var feed = SyndicationFeed.Load(xr); + foreach (var item in feed.Items) + { + var date = item.PublishDate; + if(item.LastUpdatedTime > item.PublishDate) + { + date = item.LastUpdatedTime; //imo the library should automatically set last updated to created, in this case + //but I think that would break with tradition in a painful way, so meh + } + if(!doneIDs.Contains(item.Id) && DateTime.UtcNow - date < TimeSpan.FromDays(5)) + { + // var sb = new StringBuilder(); + // using(XmlWriter xw = XmlWriter.Create(sb)){ + // item.Content.WriteTo(xw, "html", null); + // } + Console.WriteLine($"to post! {item.Id} {item.Title?.Text}"); + doneIDs.Add(item.Id); + postTasks.Add(wh.SendMessageAsync(item.Links?.ToList().FirstOrDefault()?.Uri.ToString())); + } + } + } + Task.WaitAll(postTasks.ToArray()); + } + static async Task> rssArticles(string rssURL) + { + var toReturn = new List(); + var responseStream = await client.GetStreamAsync(rssURL); + using (XmlReader xr = XmlReader.Create(responseStream)) + { + var feed = SyndicationFeed.Load(xr); + foreach (var item in feed.Items) + { + if(!doneIDs.Contains(item.Id)) + { + toReturn.Add(item); + } + } + } + return toReturn; + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..9fa6027 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# joe-rss + +if I were smart I'd make it configurable \ No newline at end of file diff --git a/WebhookData.cs b/WebhookData.cs new file mode 100644 index 0000000..31d4873 --- /dev/null +++ b/WebhookData.cs @@ -0,0 +1,9 @@ +namespace rssberg +{ + public class WebhookData + { + public string content {get;set;} + public string username {get;set;} = null; + public string avatar_url {get;set;} = null; + } +} \ No newline at end of file diff --git a/config.cs b/config.cs new file mode 100644 index 0000000..c546c4e --- /dev/null +++ b/config.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace rssberg +{ + public class Config + { + public Dictionary FeedPairs {get;set;} = new Dictionary(); + } +} \ No newline at end of file diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..bd59ec7 --- /dev/null +++ b/config.example.json @@ -0,0 +1,6 @@ +{ + "FeedPairs": + { + "https://reader.google.com/rss": "https://discord.com/api/webhooks/?" + } +} \ No newline at end of file diff --git a/rssberg.csproj b/rssberg.csproj new file mode 100644 index 0000000..0ec66a8 --- /dev/null +++ b/rssberg.csproj @@ -0,0 +1,15 @@ + + + + Exe + net5.0 + rssberg + + + + + + + + +