From a983871803f2cbd7f6b57b10972c89d0037f0c1c Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Thu, 9 Dec 2021 08:15:58 -0500 Subject: [PATCH] initial, seems to work --- .gitignore | 2 ++ .vscode/launch.json | 26 ++++++++++++++++++ .vscode/tasks.json | 42 ++++++++++++++++++++++++++++ Program.cs | 64 +++++++++++++++++++++++++++++++++++++++++++ mtg-rules-dler.csproj | 13 +++++++++ 5 files changed, 147 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Program.cs create mode 100644 mtg-rules-dler.csproj diff --git a/.gitignore b/.gitignore index 27ae6f1..0ba38dc 100644 --- a/.gitignore +++ b/.gitignore @@ -373,3 +373,5 @@ FodyWeavers.xsd # Local History for Visual Studio Code .history/ +MagicCompRules.txt +MagicCompRules.NoNums.txt \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8236e6a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "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/mtg-rules-dler.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" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4978f2a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/mtg-rules-dler.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/mtg-rules-dler.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/mtg-rules-dler.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..8408246 --- /dev/null +++ b/Program.cs @@ -0,0 +1,64 @@ +using System; +using System.IO; +using System.Net; +using System.Text.RegularExpressions; +using HtmlAgilityPack; + +namespace mtg_rules_dler +{ + class Program + { + static void Main(string[] args) + { + + HtmlWeb web = new HtmlWeb(); + + var htmlDoc = web.Load("https://magic.wizards.com/en/game-info/gameplay/rules-and-formats/rules"); + + var links = htmlDoc.DocumentNode.SelectNodes("//div[contains(@id,'comprehensive-rules')]//a"); + if (links == null || links.Count == 0) + { + Console.Error.WriteLine("can't find any links."); + return; + } + + var linkTarget = ""; + var found = false; + foreach (var link in links) + { + linkTarget = link.Attributes["href"]?.Value; + if (linkTarget?.EndsWith(".txt") == true) + { + found = true; + break; + } + } + if (!found) + { + Console.Error.WriteLine("couldn't find link to rules.txt"); + return; + } + + var request = WebRequest.Create(linkTarget); + var response = request.GetResponse(); + + var responseAsText = ""; + using (var dataStream = new StreamReader(response.GetResponseStream())) + { + responseAsText = dataStream.ReadToEnd(); + } + + if (File.Exists("MagicCompRules.txt")) + { + if (File.ReadAllText("MagicCompRules.txt") == responseAsText) + { + Console.WriteLine("no update."); + return; + } + } + File.WriteAllText("MagicCompRules.txt", responseAsText); + + File.WriteAllText("MagicCompRules.NoNums.txt", new Regex("\\b\\d+(\\.\\d+)?([\\w]+)?\\b").Replace(responseAsText, "#")); + } + } +} diff --git a/mtg-rules-dler.csproj b/mtg-rules-dler.csproj new file mode 100644 index 0000000..c485765 --- /dev/null +++ b/mtg-rules-dler.csproj @@ -0,0 +1,13 @@ + + + + Exe + net5.0 + mtg_rules_dler + + + + + + +