baro-circular/Program.cs

99 lines
3.7 KiB
C#
Raw Normal View History

2023-04-04 01:57:59 -04:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml;
using HtmlAgilityPack;
using Newtonsoft.Json;
using PuppeteerSharp;
namespace baros_circular
{
class Program
{
private const string archivePath = "doneIDs.txt";
private const string configPath = "config.json";
private static HttpClient webClient = new HttpClient();
static async Task Main(string[] args)
{
if(baroHere() == false) return;
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true,
ExecutablePath = "/usr/bin/chromium",
UserDataDir = "../chromiumData",
DefaultViewport = new ViewPortOptions() { Width = 1920, Height = 1080 }
});
var page = await browser.NewPageAsync();
await page.GoToAsync("https://warframe.fandom.com/wiki/Baro_Ki'Teer/Current_PC_Items");
var content = await page.GetContentAsync();
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(content);
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
var targetNode = htmlDoc.DocumentNode.SelectSingleNode("//div[contains(@id, 'gallery-0')]");
purgeSiblingsUp(targetNode, ref htmlBody);
File.WriteAllText("debug.html", htmlDoc.DocumentNode.OuterHtml);
await page.SetContentAsync(htmlDoc.DocumentNode.OuterHtml);
Console.WriteLine("going to wait 30 seconds for loading images");
await page.WaitForTimeoutAsync(30000);
var targetElement = (await page.XPathAsync("//div[contains(@id, 'gallery-0')]"))?.FirstOrDefault();
await targetElement.ScreenshotAsync("./diamondular.png");
await PostToDiscord();
}
private static bool baroHere()
{
var ws = webClient.GetAsync("https://content.warframe.com/dynamic/worldState.php").Result;
var wsText = "";
using(StreamReader sr = new StreamReader(ws.Content.ReadAsStream()))
{
wsText = sr.ReadToEnd();
}
var worldState = JsonConvert.DeserializeObject<Worldstate>(wsText);
//baro shows up twice if he's at "tennoconhub2", but fortunately he doesn't have a manifest
//return worldState?.VoidTraders?.Where(bkt => bkt.Manifest?.Count() > 0)?.Count() > 0;
Console.WriteLine("we're just going to say here's here, for test purpposes");
return true;
}
private static async Task PostToDiscord()
{
ByteArrayContent fileContent = new ByteArrayContent(File.ReadAllBytes("./diamondular.png"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "File",
FileName = "diamondular.png"
};
var content = new MultipartFormDataContent();
content.Add(fileContent);
await webClient.PostAsync(
"[REDACTED]" //your webhook here
content);
}
private static void purgeSiblingsUp(HtmlNode childNode, ref HtmlNode parentNode)
{
var immediateParent = childNode.ParentNode;
immediateParent.RemoveAllChildren();
immediateParent.AppendChild(childNode);
if(immediateParent == parentNode)
{
return;
}
purgeSiblingsUp(immediateParent, ref parentNode);
}
}
}