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=", "");
}