Files
Nebula-Auth/SteamLibForked/Web/Scrappers/HTML/MarketGlobalInfoScrapper.cs
T
achiez 8ff960189e 1.5.6 Bug fixes and code clean-ups
- Update: Added "Copy Password" to Mafile context menu (if available)
- Update: "Save Password" now pre-fills the current password in the "Login Again" dialog when encryption is enabled
- Fix: Prevented overlapping confirmation cycles in AutoConfirmer
- Fix: Added a Snackbar notification when a confirmation cycle is skipped due to a too frequent timer interval
- Fix: Fixed a rare bug where the previous proxy was used instead of the current one during session refresh
- Fix: Corrected "Session Permanently Expired" message showing on unrelated errors (e.g. network issues)
- UI-Fix: Fixed visual glitch where proxy input appeared non-empty when switching to a mafile-specific proxy
- Cleanup: Refactored and cleaned up codebase, improved styling and formatting via ReSharper
2025-05-02 23:29:24 +03:00

81 lines
3.1 KiB
C#

using System.Text.RegularExpressions;
using HtmlAgilityPack;
using Newtonsoft.Json;
using SteamLib.Web.Models.GlobalMarketInfo;
namespace SteamLib.Web.Scrappers.HTML;
public static class MarketGlobalInfoScrapper
{
private static readonly Regex LoggedInRegex = new("var g_bLoggedIn = (.+);", RegexOptions.Compiled);
private static readonly Regex ReqBillingInfoRegex =
new("var g_bRequiresBillingInfo = (.+);", RegexOptions.Compiled);
private static readonly Regex CountryCodeRegex = new("var g_strCountryCode = \"(.+)\";", RegexOptions.Compiled);
private static readonly Regex HasBillingStates = new("var g_bHasBillingStates = (.+);", RegexOptions.Compiled);
private static readonly Regex LanguageRegex = new("var g_strLanguage = \"(.+)\";", RegexOptions.Compiled);
private static readonly Regex WalletInfoRegex = new("var g_rgWalletInfo = (.+);", RegexOptions.Compiled);
private static readonly Regex SessionIdRegex = new("g_sessionID = \"(.+)\";", RegexOptions.Compiled);
private static readonly Regex SteamIdRegex = new("g_steamID = \"(.+)\";", RegexOptions.Compiled);
public static GlobalInfoModel Scrap(string html)
{
var document = new HtmlDocument();
document.LoadHtml(html);
var scriptNodes = document.DocumentNode.SelectNodes("//*[@id=\"responsive_page_template_content\"]/script");
var index = scriptNodes.Count > 2
? 1
: 0; //If account is limited or market unavailable elements will be displaced
var scriptNode = scriptNodes[index];
var script = scriptNode.InnerText!;
var logged = GetBool(script, LoggedInRegex);
var hasBillingStates = GetBool(script, HasBillingStates);
var reqBillingInfo = GetBool(script, ReqBillingInfoRegex);
var country = CountryCodeRegex.Match(html).Groups[1].Value;
var language = LanguageRegex.Match(html).Groups[1].Value;
var walletInfoStr = WalletInfoRegex.Match(html).Groups[1].Value;
MarketWalletSchema? wallet = null;
var sessionScriptNode =
document.DocumentNode.SelectSingleNode("//div[@class='responsive_page_content']/script");
var sessionScript = sessionScriptNode.InnerText!;
var sessionId = SessionIdRegex.Match(sessionScript).Groups[1].Value;
long? steamId = null;
if (logged)
{
wallet = JsonConvert.DeserializeObject<MarketWalletSchema>(walletInfoStr);
var steamIdStr = SteamIdRegex.Match(sessionScript).Groups[1].Value;
steamId = long.Parse(new string(steamIdStr.Where(char.IsDigit).ToArray()));
}
return new GlobalInfoModel
{
CountryCode = country,
HasBillingStates = hasBillingStates,
IsLoggedIn = logged,
Language = language,
RequiresBillingInfo = reqBillingInfo,
WalletInfo = wallet,
SessionId = sessionId,
SteamId = steamId
};
}
private static bool GetBool(string script, Regex regex)
{
var value = regex.Match(script).Groups[1].Value;
return bool.Parse(value);
}
}