From b85ab007fd2b5f7d8a6bde98c6fcd4a094c6abe3 Mon Sep 17 00:00:00 2001 From: LittleFish-233 Date: Fri, 8 Sep 2023 14:58:37 +0800 Subject: [PATCH] =?UTF-8?q?steam=E8=AF=84=E6=B5=8B=E6=95=B0=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BB=8Eapi=E8=8E=B7=E5=8F=96=E5=88=99=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E4=BB=8E=E7=BD=91=E9=A1=B5=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/Stores/StoreInfoService.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/CnGalWebSite/CnGalWebSite.APIServer/Application/Stores/StoreInfoService.cs b/CnGalWebSite/CnGalWebSite.APIServer/Application/Stores/StoreInfoService.cs index 779d0b6e6..9316469e2 100644 --- a/CnGalWebSite/CnGalWebSite.APIServer/Application/Stores/StoreInfoService.cs +++ b/CnGalWebSite/CnGalWebSite.APIServer/Application/Stores/StoreInfoService.cs @@ -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; @@ -155,6 +156,32 @@ public async Task UpdateFromSteam(StoreInfo storeInfo) } } + public async Task 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, + }; + } + + /// /// 获取Steam附加信息 /// @@ -168,6 +195,12 @@ public async Task GetSteamAdditionInformationAsync(StoreInfo steam) var json = JObject.Parse(content); steam.EvaluationCount = json["positive"].ToObject() + json["negative"].ToObject(); + //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() * 100.0 / steam.EvaluationCount;