From c4f57e6526d21ced8a49834f93f9c3737cd2b9d5 Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 4 Jul 2025 21:15:29 +0300 Subject: [PATCH] initial commit --- .gitignore | 1 + Program.cs | 71 ++++++++++++++++++++++++++++++++++++ SteamCollectionParser.csproj | 16 ++++++++ SteamCollectionParser.sln | 16 ++++++++ 4 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 Program.cs create mode 100644 SteamCollectionParser.csproj create mode 100644 SteamCollectionParser.sln diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..e5af842 --- /dev/null +++ b/Program.cs @@ -0,0 +1,71 @@ +using Fizzler.Systems.HtmlAgilityPack; +using HtmlAgilityPack; +using DTLib.Console; +using File = DTLib.Filesystem.File; +using Directory = DTLib.Filesystem.Directory; + +List pageTexts=new List(); +string separator = "\n"; +Action? selectedTarget = null; +// file url separator +new LaunchArgumentParser( + new LaunchArgument(new []{"file"}, "loads local html page", LoadLocalPage, "file_path"), + new LaunchArgument(new []{"dir"}, "loads html pages from local directory", LoadLocalDir, "dir_path"), + new LaunchArgument(new []{"url"}, "downloads html page from a website", DownloadPage, "url"), + new LaunchArgument(new []{"col","collection"}, "parses workshop collection page", ()=>selectedTarget=ParseCollection), + new LaunchArgument(new []{"sub","subscriptions"}, "parses workshop subscriptions page", ()=>selectedTarget=ParseSubscriptions) +).ParseAndHandle(args); +if (selectedTarget is null) + throw new Exception("no action selected"); +if (pageTexts.Count==0) + throw new Exception("no page path set"); +foreach (string pageText in pageTexts) + selectedTarget(pageText); + +void LoadLocalPage(string path) +{ + pageTexts.Add(File.ReadAllText(path)); +} + +void LoadLocalDir(string path) +{ + foreach (var f in Directory.GetAllFiles(path)) + { + string text = File.ReadAllText(f); + if(text.StartsWith("")) + pageTexts.Add(text); + } +} +void DownloadPage(string collectionUrl) +{ + var http = new HttpClient(); + pageTexts.Add(http.GetStringAsync(collectionUrl).GetAwaiter().GetResult()); +} + +void ParseCollection(string pageText) +{ + var page = new HtmlDocument(); + page.LoadHtml(pageText); + var collectionItems = page.DocumentNode.QuerySelectorAll(".collectionItem"); + var ids = collectionItems.Select(n => n.Id.Remove(0, n.Id.LastIndexOf('_') + 1)); + string rezult = string.Join(separator, ids); + Console.WriteLine(rezult); +} + +void ParseSubscriptions(string pageText) +{ + var page = new HtmlDocument(); + page.LoadHtml(pageText); + var collectionItems = page.DocumentNode.QuerySelectorAll(".workshopItemSubscriptionDetails"); + var ids = collectionItems.Select(n => GetSubscriptionId(n)); + string rezult = string.Join(separator, ids); + Console.WriteLine(rezult); +} + +string GetSubscriptionId(HtmlNode n) +{ + HtmlNode a = n.ChildNodes.First(sn => sn.Name == "a"); + HtmlAttribute href = a.Attributes.First(a => a.Name == "href"); + string url = href.Value; + return url.Replace("https://steamcommunity.com/sharedfiles/filedetails/?id=", ""); +} \ No newline at end of file diff --git a/SteamCollectionParser.csproj b/SteamCollectionParser.csproj new file mode 100644 index 0000000..6d0e9f3 --- /dev/null +++ b/SteamCollectionParser.csproj @@ -0,0 +1,16 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + diff --git a/SteamCollectionParser.sln b/SteamCollectionParser.sln new file mode 100644 index 0000000..4320b09 --- /dev/null +++ b/SteamCollectionParser.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SteamCollectionParser", "SteamCollectionParser.csproj", "{78F88A9F-9B63-405A-B71A-79EBA263CAFA}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {78F88A9F-9B63-405A-B71A-79EBA263CAFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78F88A9F-9B63-405A-B71A-79EBA263CAFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78F88A9F-9B63-405A-B71A-79EBA263CAFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78F88A9F-9B63-405A-B71A-79EBA263CAFA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal