-
Notifications
You must be signed in to change notification settings - Fork 10
/
StoreInfoService.cs
370 lines (307 loc) · 13.8 KB
/
StoreInfoService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using CnGalWebSite.APIServer.DataReositories;
using CnGalWebSite.Core.Services;
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;
namespace CnGalWebSite.APIServer.Application.Stores
{
public class StoreInfoService : IStoreInfoService
{
private readonly IRepository<StoreInfo, long> _storeInfoRepository;
private readonly ILogger<StoreInfoService> _logger;
private readonly IHttpService _httpService;
private readonly IConfiguration _configuration;
public StoreInfoService(IRepository<StoreInfo, long> storeInfoRepository, ILogger<StoreInfoService> logger, IHttpService httpService, IConfiguration configuration)
{
_storeInfoRepository = storeInfoRepository;
_logger = logger;
_httpService = httpService;
_configuration = configuration;
}
public async Task<StoreInfoViewModel> Get(PublishPlatformType platformType, string platformName, string link, string name, int entryId)
{
var storeInfo = await _storeInfoRepository.GetAll().AsNoTracking()
.FirstOrDefaultAsync(s => s.PlatformType == platformType && s.PlatformName == platformName && s.Link == link && s.Name == name && s.EntryId == entryId);
if (storeInfo == null)
{
storeInfo = await Update(platformType, platformName, link,name, entryId);
_storeInfoRepository.Clear();
}
//复制数据
return new StoreInfoViewModel
{
PlatformType = storeInfo.PlatformType,
PlatformName = storeInfo.PlatformName,
Link = storeInfo.Link,
State = storeInfo.State,
CurrencyCode = storeInfo.CurrencyCode,
CutLowest = storeInfo.CutLowest,
CutNow = storeInfo.CutNow,
EstimationOwnersMax = storeInfo.EstimationOwnersMax,
EstimationOwnersMin = storeInfo.EstimationOwnersMin,
EvaluationCount = storeInfo.EvaluationCount,
OriginalPrice = storeInfo.OriginalPrice,
PlayTime = storeInfo.PlayTime,
PriceLowest = storeInfo.PriceLowest,
PriceNow = storeInfo.PriceNow,
RecommendationRate = storeInfo.RecommendationRate,
UpdateTime = storeInfo.UpdateTime,
UpdateType = storeInfo.UpdateType,
Name = storeInfo.Name,
};
}
public async Task BatchUpdate(int max)
{
var date = DateTime.Now.ToCstTime().Date;
var steams = await _storeInfoRepository.GetAll().AsNoTracking()
.Where(s => s.UpdateTime.Date < date && s.State != StoreState.Takedown && s.UpdateType == StoreUpdateType.Automatic && s.Entry.PubulishTime != null && s.Entry.PubulishTime < date)
.OrderByDescending(s => s.PriceNow).ThenByDescending(s => s.EntryId)
.Take(max)
.ToListAsync();
foreach (var item in steams)
{
await Update(item);
}
}
/// <summary>
/// 更新价格信息
/// </summary>
public async Task<StoreInfo> Update(PublishPlatformType platformType, string platformName, string link, string name, int entryId)
{
if (entryId == 0)
{
return new StoreInfo();
}
var storeInfo = await _storeInfoRepository.GetAll().FirstOrDefaultAsync(s => s.PlatformType == platformType && s.PlatformName == platformName && s.Link == link && s.Name == name&&s.EntryId==entryId);
if (storeInfo == null)
{
storeInfo = new StoreInfo
{
PlatformType = platformType,
PlatformName = platformName,
Name= name,
Link = link,
EntryId = entryId,
UpdateTime = DateTime.Now.ToCstTime()
};
await _storeInfoRepository.InsertAsync(storeInfo);
}
await Update(storeInfo);
return storeInfo;
}
public async Task Update(StoreInfo storeInfo)
{
switch (storeInfo.PlatformType)
{
case PublishPlatformType.Steam:
await UpdateFromSteam(storeInfo);
break;
default:
return;
}
//最后更新时间
storeInfo.UpdateTime = DateTime.Now.ToCstTime();
await _storeInfoRepository.UpdateAsync(storeInfo);
_logger.LogInformation("更新平台 - {platformType}, Id/链接 - {link}, 词条 - {id} 的商店信息", storeInfo.PlatformType == PublishPlatformType.Other ? storeInfo.PlatformName : storeInfo.PlatformType.GetDisplayName(),storeInfo.Link,storeInfo.EntryId);
}
#region Steam
/// <summary>
/// 更新Steam信息
/// </summary>
/// <param name="storeInfo"></param>
/// <returns></returns>
public async Task UpdateFromSteam(StoreInfo storeInfo)
{
//判断是否下架
if (storeInfo.State == StoreState.Takedown)
{
return;
}
//判断是否为免费游戏
//if (storeInfo.OriginalPrice != 0)
{
await UpdateSteamInforByRemoteAPI(storeInfo);
}
//获取附加信息
if (storeInfo.State== StoreState.OnSale)
{
await GetSteamAdditionInformationAsync(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>
/// <param name="steam"></param>
/// <returns></returns>
public async Task GetSteamAdditionInformationAsync(StoreInfo steam)
{
try
{
var content = await (await _httpService.GetClientAsync()).GetStringAsync(_configuration["SteamspyUrl"] + "api.php?request=appdetails&appid=" + steam.Link);
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;
}
var minutes = json["average_forever"].ToObject<int>();
if (minutes > 3000)
{
steam.PlayTime = 0;
}
else
{
steam.PlayTime = minutes;
}
var times = json["owners"].ToObject<string>().Split("..");
steam.EstimationOwnersMin = int.Parse(times[0].Replace(" ", "").Replace(",", ""));
steam.EstimationOwnersMax = int.Parse(times[1].Replace(" ", "").Replace(",", ""));
}
catch (Exception ex)
{
_logger.LogError(ex, "Id:{id} 获取Steam附加信息失败", steam.Link);
}
}
/// <summary>
/// 获取Steam价格
/// </summary>
/// <param name="steam"></param>
/// <returns></returns>
private async Task UpdateSteamInforByRemoteAPI(StoreInfo steam)
{
//获取信息
var jsonContent = await (await _httpService.GetClientAsync()).GetStringAsync("https://api.isthereanydeal.com/v01/game/overview/?key=" + _configuration["IsthereanydealAPIToken"] + "®ion=cn&country=CN&shop=steam&ids=app%2F" + steam.Link + "&allowed=steam");
var thirdResult = JObject.Parse(jsonContent);
var steamNowJson = new SteamNowJson();
var steamLowestJson = new SteamLowestJson();
if (thirdResult["data"]["app/" + steam.Link]["lowest"].Any())
{
steamLowestJson = thirdResult["data"]["app/" + steam.Link]["lowest"].ToObject<SteamLowestJson>();
}
JObject officialResult = null;
try
{
//尝试使用官方api获取信息
jsonContent = await (await _httpService.GetClientAsync()).GetStringAsync("https://store.steampowered.com/api/appdetails/?appids=" + steam.Link + "&cc=cn&filters=price_overview");
officialResult = JObject.Parse(jsonContent);
}
catch (Exception ex)
{
_logger.LogError(ex, "Id:{id} 获取Steam官方API数据失败", steam.Link);
}
if (officialResult != null && officialResult[steam.Link.ToString()]["success"].ToObject<bool>() == true)
{
if (officialResult[steam.Link.ToString()]["data"].Any())
{
var discount_percent = officialResult[steam.Link.ToString()]["data"]["price_overview"]["discount_percent"].ToObject<string>();
var final = officialResult[steam.Link.ToString()]["data"]["price_overview"]["final"].ToObject<string>();
var final_formatted = officialResult[steam.Link.ToString()]["data"]["price_overview"]["final_formatted"].ToObject<string>();
steamNowJson = new SteamNowJson
{
price = int.Parse(final)*0.01,
cut = int.Parse(discount_percent),
price_formatted = final_formatted
};
}
}
else
{
if (thirdResult["data"]["app/" + steam.Link]["price"].Any())
{
steamNowJson = thirdResult["data"]["app/" + steam.Link]["price"].ToObject<SteamNowJson>();
}
}
//更新数据 支持小数点 将真实价格*100储存 即1500表示15元
//当前价格
if (steamNowJson.price == null)
{
//判断是否 无法获取数据
if (steamLowestJson.price != null)
{
//已发布
steam.State = StoreState.OnSale;
steam.PriceNow =null;
steam.CutNow =null;
steam.CutLowest = steamLowestJson.cut;
steam.PriceLowest = steamLowestJson.price;
//计算原价
if (steam.CutLowest == 100)
{
steam.OriginalPrice = steam.PriceLowest;
}
steam.OriginalPrice = (int)(steam.PriceLowest / (1 - ((double)steam.CutLowest / 100)));
}
else
{
//未发布
steam.State = StoreState.NotPublished;
steam.PriceNow = null;
steam.CutNow = null;
steam.PriceLowest = null;
steam.CutLowest = null;
}
}
else
{
//已发布
steam.State = StoreState.OnSale;
steam.PriceNow = steamNowJson.price;
steam.CutNow = steamNowJson.cut;
steam.CutLowest = steamLowestJson.cut;
//计算原价
//当前价格 = 原价 * (1 - 折扣)
//原价 = 当前价格 / (1 - 折扣)
if (steam.CutNow == 100)
{
steam.OriginalPrice = steam.PriceNow;
}
steam.OriginalPrice = (int)(steam.PriceNow / (1 - ((double)steam.CutNow / 100)));
//计算史低价格
if (steamLowestJson.price != null)
{
steam.PriceLowest = steam.CutLowest >= 0 ? (int)(steam.OriginalPrice * (1 - ((double)steam.CutLowest / 100))) : steam.OriginalPrice;
//比较是否偏差较大
if (Math.Abs((steam.PriceLowest.Value / 100) - steamLowestJson.price.Value) > 2)
{
steam.PriceLowest = steamLowestJson.price;
}
}
else
{
steam.PriceLowest = null;
steam.CutLowest = null;
}
}
}
#endregion
}
}