Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Nov 1, 2024
1 parent a55ca56 commit 6758d80
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 77 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ csharp_style_conditional_delegate_call = true

# 修饰符首选项
csharp_prefer_static_local_function = true
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async
csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async

# 代码块首选项
csharp_style_namespace_declarations=file_scoped:suggestion
csharp_prefer_braces = false
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_prefer_braces = true:warning
csharp_prefer_simple_using_statement = true

# 表达式级首选项
Expand Down
149 changes: 75 additions & 74 deletions ASFEnhance/Account/HtmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

namespace ASFEnhance.Account;

internal static class HtmlParser
static class HtmlParser
{
/// <summary>
/// 获取Cursor对象
/// 获取Cursor对象
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
Expand All @@ -24,7 +24,7 @@ internal static class HtmlParser
return null;
}

string content = response.Content.Body.InnerHtml;
var content = response.Content.Body.InnerHtml;
var match = RegexUtils.MatchHistortyCursor().Match(content);
if (!match.Success)
{
Expand All @@ -44,13 +44,14 @@ internal static class HtmlParser
}

/// <summary>
/// 解析历史记录条目
/// 解析历史记录条目
/// </summary>
/// <param name="tableElement"></param>
/// <param name="currencyRates"></param>
/// <param name="defaultCurrency"></param>
/// <returns></returns>
internal static HistoryParseResponse ParseHistory(IElement tableElement, Dictionary<string, decimal> currencyRates, string defaultCurrency)
internal static HistoryParseResponse ParseHistory(IElement tableElement, Dictionary<string, decimal> currencyRates,
string defaultCurrency)
{
var pattern = RegexUtils.MatchHistoryItem();

Expand All @@ -60,7 +61,7 @@ string ParseSymbol(string symbol1, string symbol2)
const char USD = '$';
const char RMB = '¥';

string currency = string.Empty;
var currency = string.Empty;

if (!string.IsNullOrEmpty(symbol1))
{
Expand All @@ -84,20 +85,18 @@ string ParseSymbol(string symbol1, string symbol2)
{
return "USD";
}
else if (symbol1.Contains(RMB) || symbol2.Contains(RMB))
{ //人民币和日元符号相同, 使用钱包默认货币单位
return defaultCurrency;
}
else

if (symbol1.Contains(RMB) || symbol2.Contains(RMB))
{
ASFLogger.LogGenericWarning(string.Format("检测货币符号失败, 使用默认货币单位 {0}", defaultCurrency));
//人民币和日元符号相同, 使用钱包默认货币单位
return defaultCurrency;
}

ASFLogger.LogGenericWarning($"检测货币符号失败, 使用默认货币单位 {defaultCurrency}");
return defaultCurrency;
}
else
{
return currency;
}

return currency;
}

// 识别货币数值
Expand All @@ -109,45 +108,49 @@ decimal ParseMoneyString(string strMoney)
{
return 0;
}
else
{
bool negative = match.Groups[1].Value == "-";
string symbol1 = match.Groups[2].Value.Trim();
string strPrice = match.Groups[3].Value;
string symbol2 = match.Groups[4].Value.Trim();

string currency = ParseSymbol(symbol1, symbol2);
var negative = match.Groups[1].Value == "-";
var symbol1 = match.Groups[2].Value.Trim();
var strPrice = match.Groups[3].Value;
var symbol2 = match.Groups[4].Value.Trim();

bool useDot = DotCurrency.Contains(currency);
var currency = ParseSymbol(symbol1, symbol2);

if (useDot)
{
strPrice = strPrice.Replace(".", ";").Replace(',', '.').Replace(';', ',');
}
// 获取当前文化信息
var currentCulture = CultureInfo.CurrentCulture;
var numberFormat = (NumberFormatInfo)currentCulture.NumberFormat.Clone();

if (decimal.TryParse(strPrice, NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, null, out decimal price))
{
if (currencyRates.TryGetValue(currency, out decimal rate))
{
return (negative ? -1 : 1) * (price / rate);
}
else
{
ASFLogger.LogGenericWarning(string.Format("无 {0} 货币的汇率", currency));
return (negative ? -1 : 1) * price;
}
}
else
// 根据货币符号设置小数点和千分位分隔符
if (DotCurrency.Contains(currency))
{
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencyGroupSeparator = ",";
}
else
{
numberFormat.CurrencyDecimalSeparator = ",";
numberFormat.CurrencyGroupSeparator = ".";
}

if (decimal.TryParse(strPrice, NumberStyles.Currency, numberFormat,
out var price))
{
if (currencyRates.TryGetValue(currency, out var rate))
{
ASFLogger.LogGenericWarning(string.Format("解析价格 {0} 失败", match.Groups[3].Value));
return 0;
return (negative ? -1 : 1) * (price / rate);
}

ASFLogger.LogGenericWarning($"无 {currency} 货币的汇率");
return (negative ? -1 : 1) * price;
}

ASFLogger.LogGenericWarning($"解析价格 {match.Groups[3].Value} 失败");
return 0;
}

HistoryParseResponse result = new();

IHtmlCollection<IElement> rows = tableElement.QuerySelectorAll("tr");
var rows = tableElement.QuerySelectorAll("tr");

foreach (var row in rows)
{
Expand All @@ -161,19 +164,19 @@ decimal ParseMoneyString(string strMoney)
var whtTotal = row?.QuerySelector("td.wht_total");
var whtChange = row?.QuerySelector("td.wht_wallet_change.wallet_column");

bool isRefund = whtType?.ClassName?.Contains("wht_refunded") ?? false;
var isRefund = whtType?.ClassName?.Contains("wht_refunded") ?? false;

string strItem = whtItem?.Text().Trim().Replace("\t", "") ?? "";
string strType = whtType?.Text().Trim().Replace("\t", "") ?? "";
string strTotal = whtTotal?.Text().Replace("资金", "").Trim().Replace("\t", "") ?? "";
string strChange = whtChange?.Text().Trim().Replace("\t", "") ?? "";
var strItem = whtItem?.Text().Trim().Replace("\t", "") ?? "";
var strType = whtType?.Text().Trim().Replace("\t", "") ?? "";
var strTotal = whtTotal?.Text().Replace("资金", "").Trim().Replace("\t", "") ?? "";
var strChange = whtChange?.Text().Trim().Replace("\t", "") ?? "";

if (!string.IsNullOrEmpty(strType))
{
// 排除退款和转换货币
if (!string.IsNullOrEmpty(strType) && !strType.StartsWith("转换") && !strType.StartsWith("退款"))
{
int total = (int)(ParseMoneyString(strTotal) * 100);
var total = (int)(ParseMoneyString(strTotal) * 100);

int walletChange;
int walletChangeAbs;
Expand All @@ -186,6 +189,7 @@ decimal ParseMoneyString(string strMoney)
{
walletChange = (int)(ParseMoneyString(strChange) * 100);
}

walletChangeAbs = Math.Abs(walletChange);

if (total == 0)
Expand Down Expand Up @@ -275,7 +279,7 @@ decimal ParseMoneyString(string strMoney)
}

/// <summary>
/// 解析Sub页
/// 解析Sub页
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
Expand Down Expand Up @@ -307,7 +311,7 @@ decimal ParseMoneyString(string strMoney)
var match = matchSubId.Match(link);
if (match.Success)
{
string strId = match.Groups[1].Value;
var strId = match.Groups[1].Value;
_ = uint.TryParse(strId, out subId);
}

Expand All @@ -322,30 +326,25 @@ decimal ParseMoneyString(string strMoney)
}

var typeEle = ele.SelectSingleNode<IElement>(".//td[3]");
string? typeStr = typeEle?.TextContent.Trim();
var typeStr = typeEle?.TextContent.Trim();

LicenseType licenseType = typeStr switch
var licenseType = typeStr switch
{
"零售" => LicenseType.Retail,
"免费赠送" => LicenseType.Complimentary,
"Steam 商店" => LicenseType.SteamStore,
"礼物/玩家通行证" => LicenseType.GiftOrGuestPass,
_ => LicenseType.Unknown,
_ => LicenseType.Unknown
};

result.Add(new LicensesData
{
Type = licenseType,
Name = name,
PackageId = subId,
});
result.Add(new LicensesData { Type = licenseType, Name = name, PackageId = subId });
}

return result;
}

/// <summary>
/// 解析电子邮件偏好
/// 解析电子邮件偏好
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
Expand All @@ -362,7 +361,7 @@ decimal ParseMoneyString(string strMoney)

foreach (var ele in inputEles)
{
bool check = ele.HasAttribute("checked");
var check = ele.HasAttribute("checked");

switch (ele.Id)
{
Expand Down Expand Up @@ -395,16 +394,14 @@ decimal ParseMoneyString(string strMoney)
break;
case "opt_out_all":
break;
default:
break;
}
}

return result;
}

/// <summary>
/// 解析通知偏好
/// 解析通知偏好
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
Expand All @@ -422,6 +419,7 @@ decimal ParseMoneyString(string strMoney)
{
return null;
}

try
{
var optionsList = payload.ToJsonObject<List<NotificationPayload>>();
Expand Down Expand Up @@ -465,6 +463,7 @@ decimal ParseMoneyString(string strMoney)
break;
}
}

return result;
}
catch (Exception ex)
Expand All @@ -475,7 +474,7 @@ decimal ParseMoneyString(string strMoney)
}

/// <summary>
/// 解析礼物页面
/// 解析礼物页面
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
Expand All @@ -485,6 +484,7 @@ decimal ParseMoneyString(string strMoney)
{
return null;
}

var result = new HashSet<ulong>();

var eleAcceptBtns = response.Content.QuerySelectorAll(".gift_controls_buttons>div[id$='init']");
Expand All @@ -506,7 +506,7 @@ decimal ParseMoneyString(string strMoney)
}

/// <summary>
/// 解析账号邮箱
/// 解析账号邮箱
/// </summary>
/// <param name="document"></param>
/// <returns></returns>
Expand All @@ -517,7 +517,9 @@ decimal ParseMoneyString(string strMoney)
return null;
}

var eleEmail = document.QuerySelector("#main_content div.account_setting_sub_block:nth-child(1) > div:nth-child(2) span.account_data_field");
var eleEmail =
document.QuerySelector(
"#main_content div.account_setting_sub_block:nth-child(1) > div:nth-child(2) span.account_data_field");
return eleEmail?.TextContent;
}

Expand All @@ -537,11 +539,10 @@ decimal ParseMoneyString(string strMoney)
{
sb.AppendLine(ele.TextContent.Trim());
}

return sb.ToString();
}
else
{
return Langs.NoBanRecords;
}

return Langs.NoBanRecords;
}
}
}

0 comments on commit 6758d80

Please sign in to comment.