2020-12-19 19:14:40 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-02-15 10:51:50 -05:00
|
|
|
using System.Diagnostics;
|
2020-12-19 19:14:40 -05:00
|
|
|
using System.IO;
|
2021-01-01 10:27:42 -05:00
|
|
|
using System.Linq;
|
2020-12-19 19:14:40 -05:00
|
|
|
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
|
|
|
|
{
|
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);
|
2021-01-04 10:14:10 -05:00
|
|
|
public Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb, bool ignoreUnknown);
|
2020-12-25 10:49:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-12-23 16:55:26 -05:00
|
|
|
private HashSet<SteamApp> _cache = [];
|
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)
|
|
|
|
{
|
2021-01-04 09:43:27 -05:00
|
|
|
cacheString = await UpdateCache().ConfigureAwait(false);
|
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);
|
|
|
|
}
|
|
|
|
var steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString);
|
2021-01-01 10:27:42 -05:00
|
|
|
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
|
2020-12-19 19:14:40 -05:00
|
|
|
MyLogger.Log.Information("Loaded cache into memory!");
|
|
|
|
}
|
|
|
|
|
2021-01-04 09:43:27 -05:00
|
|
|
private static async Task<string> UpdateCache()
|
|
|
|
{
|
|
|
|
MyLogger.Log.Information("Getting content from API...");
|
|
|
|
var client = new HttpClient();
|
|
|
|
var httpCall = client.GetAsync(SteamUri);
|
|
|
|
var response = await httpCall.ConfigureAwait(false);
|
|
|
|
var readAsStringAsync = response.Content.ReadAsStringAsync();
|
2021-01-13 07:04:35 -05:00
|
|
|
var responseBody = await readAsStringAsync.ConfigureAwait(false);
|
2021-01-04 09:43:27 -05:00
|
|
|
MyLogger.Log.Information("Got content from API successfully. Writing to file...");
|
|
|
|
|
2021-01-13 07:04:35 -05:00
|
|
|
await File.WriteAllTextAsync(CachePath, responseBody, Encoding.UTF8).ConfigureAwait(false);
|
2021-01-04 09:43:27 -05:00
|
|
|
var cacheString = responseBody;
|
|
|
|
MyLogger.Log.Information("Cache written to file successfully.");
|
|
|
|
return cacheString;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2021-02-14 09:02:02 -05:00
|
|
|
MyLogger.Log.Information("Trying to get app {Name}", name);
|
2021-01-01 10:27:42 -05:00
|
|
|
var comparableName = Regex.Replace(name, Misc.SpecialCharsRegex, "").ToLower();
|
|
|
|
var app = _cache.FirstOrDefault(x => x.CompareName(comparableName));
|
2021-02-14 09:02:02 -05:00
|
|
|
if (app != null) MyLogger.Log.Information("Successfully got app {App}", app);
|
2020-12-19 19:14:40 -05:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2020-12-23 06:34:31 -05:00
|
|
|
public SteamApp GetAppById(int appid)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:02:02 -05:00
|
|
|
MyLogger.Log.Information("Trying to get app with ID {AppId}", appid);
|
2021-01-01 10:27:42 -05:00
|
|
|
var app = _cache.FirstOrDefault(x => x.AppId.Equals(appid));
|
2021-02-14 09:02:02 -05:00
|
|
|
if (app != null) MyLogger.Log.Information("Successfully got app {App}", app);
|
2020-12-19 19:14:40 -05:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:14:10 -05:00
|
|
|
public async Task<List<SteamApp>> GetListOfDlc(SteamApp steamApp, bool useSteamDb, bool ignoreUnknown)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Debug("Start: GetListOfDlc");
|
2020-12-23 06:34:31 -05:00
|
|
|
var dlcList = new List<SteamApp>();
|
2021-02-14 09:19:45 -05:00
|
|
|
try
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
if (steamApp != null)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
var steamAppDetails = await AppDetails.GetAsync(steamApp.AppId).ConfigureAwait(false);
|
|
|
|
if (steamAppDetails != null)
|
2021-01-04 06:27:48 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Debug("Type for Steam App {Name}: \"{Type}\"", steamApp.Name,
|
|
|
|
steamAppDetails.Type);
|
2023-12-23 16:55:26 -05:00
|
|
|
if (steamAppDetails.Type == "game" || steamAppDetails.Type == "demo")
|
2021-01-04 06:27:48 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
steamAppDetails.DLC.ForEach(x =>
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
var result = _cache.FirstOrDefault(y => y.AppId.Equals(x));
|
|
|
|
if (result == null) return;
|
|
|
|
var dlcDetails = AppDetails.GetAsync(x).Result;
|
|
|
|
dlcList.Add(dlcDetails != null
|
2023-12-23 16:55:26 -05:00
|
|
|
? new SteamApp { AppId = dlcDetails.SteamAppId, Name = dlcDetails.Name }
|
|
|
|
: new SteamApp { AppId = x, Name = $"Unknown DLC {x}" });
|
2021-02-14 09:19:45 -05:00
|
|
|
});
|
2021-01-04 06:27:48 -05:00
|
|
|
|
2021-02-14 09:19:45 -05:00
|
|
|
dlcList.ForEach(x => MyLogger.Log.Debug("{AppId}={Name}", x.AppId, x.Name));
|
|
|
|
MyLogger.Log.Information("Got DLC successfully...");
|
|
|
|
|
2023-12-24 05:51:18 -05:00
|
|
|
// Return if Steam DB is deactivated
|
2021-02-14 09:19:45 -05:00
|
|
|
if (!useSteamDb) return dlcList;
|
|
|
|
|
2023-12-24 05:51:18 -05:00
|
|
|
string steamDbUrl = $"https://steamdb.info/app/{steamApp.AppId}/dlc/";
|
2021-02-14 09:19:45 -05:00
|
|
|
|
|
|
|
var client = new HttpClient();
|
2023-12-24 05:51:18 -05:00
|
|
|
string archiveJson = await client.GetStringAsync($"https://archive.org/wayback/available?url={steamDbUrl}");
|
|
|
|
var archiveResult = JsonSerializer.Deserialize<AvailableArchive>(archiveJson);
|
|
|
|
|
|
|
|
if (archiveResult == null || archiveResult.ArchivedSnapshots.Closest.Status != "200")
|
|
|
|
{
|
|
|
|
return dlcList;
|
|
|
|
}
|
|
|
|
|
|
|
|
//language=regex
|
|
|
|
const string pattern = @"^(https?:\/\/web\.archive\.org\/web\/\d+)(\/.+)$";
|
|
|
|
const string substitution = "$1id_$2";
|
|
|
|
const RegexOptions options = RegexOptions.Multiline;
|
|
|
|
|
|
|
|
Regex regex = new(pattern, options);
|
|
|
|
string newUrl = regex.Replace(archiveResult.ArchivedSnapshots.Closest.Url, substitution);
|
|
|
|
|
|
|
|
//client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
|
2021-02-14 09:19:45 -05:00
|
|
|
|
|
|
|
MyLogger.Log.Information("Get SteamDB App");
|
2023-12-24 05:51:18 -05:00
|
|
|
var httpCall = client.GetAsync(newUrl);
|
2021-02-14 09:19:45 -05:00
|
|
|
var response = await httpCall.ConfigureAwait(false);
|
|
|
|
MyLogger.Log.Debug("{Status}", httpCall.Status.ToString());
|
2023-12-23 16:55:26 -05:00
|
|
|
MyLogger.Log.Debug("{Boolean}", response.IsSuccessStatusCode.ToString());
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2021-02-14 09:19:45 -05:00
|
|
|
|
|
|
|
var readAsStringAsync = response.Content.ReadAsStringAsync();
|
|
|
|
var responseBody = await readAsStringAsync.ConfigureAwait(false);
|
|
|
|
MyLogger.Log.Debug("{Status}", 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)
|
2021-01-05 06:30:34 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
var dlcId = element.GetAttribute("data-appid");
|
|
|
|
var query3 = element.QuerySelectorAll("td");
|
|
|
|
var dlcName = query3 == null
|
|
|
|
? $"Unknown DLC {dlcId}"
|
|
|
|
: query3[1].Text().Replace("\n", "").Trim();
|
|
|
|
|
|
|
|
if (ignoreUnknown && dlcName.Contains("SteamDB Unknown App"))
|
2021-01-04 06:27:48 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Information("Skipping SteamDB Unknown App {DlcId}", dlcId);
|
2021-01-04 06:27:48 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-23 16:55:26 -05:00
|
|
|
var dlcApp = new SteamApp { AppId = Convert.ToInt32(dlcId), Name = dlcName };
|
2021-02-14 09:19:45 -05:00
|
|
|
var i = dlcList.FindIndex(x => x.AppId.Equals(dlcApp.AppId));
|
|
|
|
if (i > -1)
|
|
|
|
{
|
|
|
|
if (dlcList[i].Name.Contains("Unknown DLC")) dlcList[i] = dlcApp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dlcList.Add(dlcApp);
|
|
|
|
}
|
2021-01-04 06:27:48 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 09:19:45 -05:00
|
|
|
|
|
|
|
dlcList.ForEach(x => MyLogger.Log.Debug("{AppId}={Name}", x.AppId, x.Name));
|
|
|
|
MyLogger.Log.Information("Got DLC from SteamDB successfully...");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MyLogger.Log.Error("Could not get DLC from SteamDB!");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
2021-01-05 06:30:34 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Error("Could not get DLC: Steam App is not of type: \"Game\"");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Error("Could not get DLC: Could not get Steam App details");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 06:27:48 -05:00
|
|
|
else
|
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Error("Could not get DLC: Invalid Steam App");
|
2021-01-04 06:27:48 -05:00
|
|
|
}
|
2021-02-14 09:19:45 -05:00
|
|
|
|
|
|
|
//return dlcList;
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
2021-02-14 09:19:45 -05:00
|
|
|
catch (Exception e)
|
2020-12-19 19:14:40 -05:00
|
|
|
{
|
2021-02-14 09:19:45 -05:00
|
|
|
MyLogger.Log.Error("Could not get DLC!");
|
2021-02-15 10:51:50 -05:00
|
|
|
MyLogger.Log.Debug(e.Demystify(), "Exception thrown!");
|
2020-12-19 19:14:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return dlcList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|