Skip to content

Commit

Permalink
steam评测数无法从api获取则直接从网页获取
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleFish-233 committed Sep 8, 2023
1 parent 345cc8b commit b85ab00
Showing 1 changed file with 33 additions and 0 deletions.
Expand Up @@ -3,6 +3,7 @@
using CnGalWebSite.DataModel.Helper;
using CnGalWebSite.DataModel.Model;
using CnGalWebSite.DataModel.ViewModel.Steam;
using HtmlAgilityPack;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -155,6 +156,32 @@ public async Task UpdateFromSteam(StoreInfo storeInfo)
}
}

public async Task<SteamEvaluation> GetSteamEvaluationAsync(string id)
{

var content = await (await _httpService.GetClientAsync()).GetStringAsync("https://store.steampowered.com/app/" + id);

var document = new HtmlDocument();
document.LoadHtml(content);

var node = document.GetElementbyId("userReviews");
var text = node.ChildNodes.Count > 3
? node.ChildNodes[3].ChildNodes[3].ChildNodes[5].InnerText
: node.ChildNodes[1].ChildNodes[3].ChildNodes[5].InnerText;
var countStr = ToolHelper.MidStrEx(text, "the ", " ").Replace(",", "");
var rateStr = ToolHelper.MidStrEx(text, " ", "% ");

int.TryParse(countStr, out int evaluationCount);
int.TryParse(rateStr, out int recommendationRate);

return new SteamEvaluation
{
EvaluationCount = evaluationCount,
RecommendationRate = recommendationRate,
};
}


/// <summary>
/// 获取Steam附加信息
/// </summary>
Expand All @@ -168,6 +195,12 @@ public async Task GetSteamAdditionInformationAsync(StoreInfo steam)
var json = JObject.Parse(content);

steam.EvaluationCount = json["positive"].ToObject<int>() + json["negative"].ToObject<int>();
//if (steam.EvaluationCount == 0)
{
var re = await GetSteamEvaluationAsync(steam.Link);
steam.EvaluationCount = re.EvaluationCount;
steam.RecommendationRate = re.RecommendationRate;
}
if (steam.EvaluationCount != 0)
{
steam.RecommendationRate = json["positive"].ToObject<int>() * 100.0 / steam.EvaluationCount;
Expand Down

0 comments on commit b85ab00

Please sign in to comment.