migrated in

This commit is contained in:
Adam R. Grey 2021-07-16 21:40:14 -04:00
commit 963a0dc865
9 changed files with 232 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -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/

27
.vscode/launch.json vendored Normal file
View File

@ -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}"
}
]
}

42
.vscode/tasks.json vendored Normal file
View File

@ -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"
}
]
}

105
Program.cs Normal file
View File

@ -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<string> 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<Config>(File.ReadAllText(configPath));
Console.WriteLine("rss reader alive");
if (!File.Exists(archivePath))
{
File.CreateText(archivePath);
}
doneIDs = File.ReadAllLines(archivePath).ToList();
var postMetaTasks = new List<Task>();
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<Task<ulong>>();
postTasks.Add(Task.Run<ulong>(() => {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<List<SyndicationItem>> rssArticles(string rssURL)
{
var toReturn = new List<SyndicationItem>();
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;
}
}
}

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# joe-rss
if I were smart I'd make it configurable

9
WebhookData.cs Normal file
View File

@ -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;
}
}

9
config.cs Normal file
View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace rssberg
{
public class Config
{
public Dictionary<string, string> FeedPairs {get;set;} = new Dictionary<string, string>();
}
}

6
config.example.json Normal file
View File

@ -0,0 +1,6 @@
{
"FeedPairs":
{
"https://reader.google.com/rss": "https://discord.com/api/webhooks/?"
}
}

15
rssberg.csproj Normal file
View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>rssberg</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="discord.net" Version="2.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.ServiceModel.Syndication" Version="5.0.0" />
</ItemGroup>
</Project>