2020-12-19 19:14:40 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Json;
|
2020-12-28 09:27:37 -05:00
|
|
|
using System.Text.RegularExpressions;
|
2020-12-19 19:14:40 -05:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AngleSharp.Dom;
|
|
|
|
using AngleSharp.Html.Parser;
|
2020-12-25 10:49:51 -05:00
|
|
|
using auto_creamapi.Models;
|
2020-12-19 19:14:40 -05:00
|
|
|
using auto_creamapi.Utils;
|
|
|
|
using NinjaNye.SearchExtensions;
|
|
|
|
using SteamStorefrontAPI;
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
namespace auto_creamapi.Services
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-25 10:49:51 -05:00
|
|
|
public interface ICacheService
|
|
|
|
{
|
|
|
|
public List<string> Languages { get; }
|
2020-12-28 09:27:37 -05:00
|
|
|
|
|
|
|
public Task Initialize();
|
|
|
|
|
|
|
|
//public Task UpdateCache();
|
2020-12-25 10:49:51 -05:00
|
|
|
public IEnumerable<SteamApp> GetListOfAppsByName(string name);
|
|
|
|
public SteamApp GetAppByName(string name);
|
|
|
|
public SteamApp GetAppById(int appid);
|
|
|
|
public Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CacheService : ICacheService
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
private const string CachePath = "steamapps.json";
|
|
|
|
private const string SteamUri = "https://api.steampowered.com/ISteamApps/GetAppList/v2/";
|
2020-12-28 09:27:37 -05:00
|
|
|
|
2020-12-19 19:14:40 -05:00
|
|
|
private const string UserAgent =
|
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
|
|
|
|
"Chrome/87.0.4280.88 Safari/537.36";
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
private const string SpecialCharsRegex = "[^0-9a-zA-Z]+";
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
private List<SteamApp> _cache = new List<SteamApp>();
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
/*private static readonly Lazy<CacheService> Lazy =
|
|
|
|
new Lazy<CacheService>(() => new CacheService());
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public static CacheService Instance => Lazy.Value;*/
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public CacheService()
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
Languages = Misc.DefaultLanguages;
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
/*public async void Initialize()
|
|
|
|
{
|
|
|
|
//Languages = _defaultLanguages;
|
|
|
|
await UpdateCache();
|
|
|
|
}*/
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public List<string> Languages { get; }
|
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
public async Task Initialize()
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Information("Updating cache...");
|
2020-12-25 10:49:51 -05:00
|
|
|
var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(CachePath)).TotalDays >= 1;
|
2020-12-19 19:14:40 -05:00
|
|
|
string cacheString;
|
|
|
|
if (updateNeeded)
|
|
|
|
{
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Information("Getting content from API...");
|
2020-12-19 19:14:40 -05:00
|
|
|
var client = new HttpClient();
|
|
|
|
var httpCall = client.GetAsync(SteamUri);
|
2020-12-28 09:27:37 -05:00
|
|
|
var response = await httpCall;
|
2020-12-19 19:14:40 -05:00
|
|
|
var readAsStringAsync = response.Content.ReadAsStringAsync();
|
2020-12-28 09:27:37 -05:00
|
|
|
var responseBody = await readAsStringAsync;
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Information("Got content from API successfully. Writing to file...");
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-28 09:27:37 -05:00
|
|
|
await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8);
|
2020-12-19 19:14:40 -05:00
|
|
|
cacheString = responseBody;
|
2020-12-23 13:13:52 -05:00
|
|
|
MyLogger.Log.Information("Cache written to file successfully.");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyLogger.Log.Information("Cache already up to date!");
|
2020-12-28 09:27:37 -05:00
|
|
|
// ReSharper disable once MethodHasAsyncOverload
|
2020-12-19 19:14:40 -05:00
|
|
|
cacheString = File.ReadAllText(CachePath);
|
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
|
2020-12-19 19:14:40 -05:00
|
|
|
var steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString);
|
|
|
|
_cache = steamApps.AppList.Apps;
|
|
|
|
MyLogger.Log.Information("Loaded cache into memory!");
|
|
|
|
}
|
|
|
|
|
2020-12-25 10:49:51 -05:00
|
|
|
public IEnumerable<SteamApp> GetListOfAppsByName(string name)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
var listOfAppsByName = _cache.Search(x => x.Name)
|
|
|
|
.SetCulture(StringComparison.OrdinalIgnoreCase)
|
|
|
|
.ContainingAll(name.Split(' '));
|
|
|
|
return listOfAppsByName;
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
public SteamApp GetAppByName(string name)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
MyLogger.Log.Information($"Trying to get app {name}");
|
2020-12-28 09:27:37 -05:00
|
|
|
var app = _cache.Find(x =>
|
|
|
|
Regex.Replace(x.Name, SpecialCharsRegex, "").ToLower()
|
|
|
|
.Equals(Regex.Replace(name, SpecialCharsRegex, "").ToLower()));
|
2020-12-19 19:14:40 -05:00
|
|
|
if (app != null) MyLogger.Log.Information($"Successfully got app {app}");
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
public SteamApp GetAppById(int appid)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
MyLogger.Log.Information($"Trying to get app with ID {appid}");
|
|
|
|
var app = _cache.Find(x => x.AppId.Equals(appid));
|
|
|
|
if (app != null) MyLogger.Log.Information($"Successfully got app {app}");
|
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
public async Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
MyLogger.Log.Information("Get DLC");
|
2020-12-23 06:34:31 -05:00
|
|
|
var dlcList = new List<SteamApp>();
|
|
|
|
if (steamApp != null)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2020-12-23 06:34:31 -05:00
|
|
|
var task = AppDetails.GetAsync(steamApp.AppId);
|
|
|
|
var steamAppDetails = await task;
|
|
|
|
steamAppDetails?.DLC.ForEach(x =>
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
|
|
|
var result = _cache.Find(y => y.AppId.Equals(x)) ??
|
2020-12-23 06:34:31 -05:00
|
|
|
new SteamApp {AppId = x, Name = $"Unknown DLC {x}"};
|
2020-12-19 19:14:40 -05:00
|
|
|
dlcList.Add(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
dlcList.ForEach(x => MyLogger.Log.Debug($"{x.AppId}={x.Name}"));
|
|
|
|
MyLogger.Log.Information("Got DLC successfully...");
|
|
|
|
|
|
|
|
// Get DLC from SteamDB
|
|
|
|
// Get Cloudflare cookie
|
|
|
|
// Scrape and parse HTML page
|
|
|
|
// Add missing to DLC list
|
|
|
|
if (useSteamDb)
|
|
|
|
{
|
2020-12-23 06:34:31 -05:00
|
|
|
var steamDbUri = new Uri($"https://steamdb.info/app/{steamApp.AppId}/dlc/");
|
2020-12-19 19:14:40 -05:00
|
|
|
|
|
|
|
/* var handler = new ClearanceHandler();
|
|
|
|
|
|
|
|
var client = new HttpClient(handler);
|
|
|
|
|
|
|
|
var content = client.GetStringAsync(steamDbUri).Result;
|
|
|
|
MyLogger.Log.Debug(content); */
|
|
|
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
|
|
|
|
|
|
|
|
MyLogger.Log.Information("Get SteamDB App");
|
|
|
|
var httpCall = client.GetAsync(steamDbUri);
|
|
|
|
var response = await httpCall;
|
|
|
|
MyLogger.Log.Debug(httpCall.Status.ToString());
|
|
|
|
MyLogger.Log.Debug(response.EnsureSuccessStatusCode().ToString());
|
|
|
|
|
|
|
|
var readAsStringAsync = response.Content.ReadAsStringAsync();
|
|
|
|
var responseBody = await readAsStringAsync;
|
|
|
|
MyLogger.Log.Debug(readAsStringAsync.Status.ToString());
|
|
|
|
|
|
|
|
var parser = new HtmlParser();
|
|
|
|
var doc = parser.ParseDocument(responseBody);
|
|
|
|
// Console.WriteLine(doc.DocumentElement.OuterHtml);
|
|
|
|
|
|
|
|
var query1 = doc.QuerySelector("#dlc");
|
|
|
|
if (query1 != null)
|
|
|
|
{
|
|
|
|
var query2 = query1.QuerySelectorAll(".app");
|
|
|
|
foreach (var element in query2)
|
|
|
|
{
|
|
|
|
var dlcId = element.GetAttribute("data-appid");
|
|
|
|
var dlcName = $"Unknown DLC {dlcId}";
|
|
|
|
var query3 = element.QuerySelectorAll("td");
|
2020-12-28 09:27:37 -05:00
|
|
|
if (query3 != null) dlcName = query3[1].Text().Replace("\n", "").Trim();
|
2020-12-19 19:14:40 -05:00
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
var dlcApp = new SteamApp {AppId = Convert.ToInt32(dlcId), Name = dlcName};
|
2020-12-19 19:14:40 -05:00
|
|
|
var i = dlcList.FindIndex(x => x.CompareId(dlcApp));
|
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dlcList.Add(dlcApp);
|
|
|
|
}
|
|
|
|
}
|
2020-12-25 10:49:51 -05:00
|
|
|
|
2020-12-19 19:14:40 -05:00
|
|
|
dlcList.ForEach(x => MyLogger.Log.Debug($"{x.AppId}={x.Name}"));
|
|
|
|
MyLogger.Log.Information("Got DLC from SteamDB successfully...");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyLogger.Log.Error("Could not get DLC from SteamDB1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-28 09:27:37 -05:00
|
|
|
MyLogger.Log.Error("Could not get DLC: Invalid Steam App");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return dlcList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|