Skip to content

Commit

Permalink
Remove session requirement for GetAppDetails and GetStorePage
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Sep 28, 2024
1 parent 606f9d2 commit bb00ae1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 48 deletions.
37 changes: 0 additions & 37 deletions FreePackages/Data/SharedExternalResource.cs

This file was deleted.

15 changes: 9 additions & 6 deletions FreePackages/Handlers/PackageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Web.Responses;
using FreePackages.Localization;
Expand Down Expand Up @@ -197,10 +198,10 @@ private async static Task HandleProductInfo(List<SteamApps.PICSProductInfoCallba
// Add wanted apps to the queue
apps.ForEach(app => {
if (app.Type == EAppType.Beta) {
SharedExternalResource<HtmlDocumentResponse> storePageResource = new();
ArchiCacheable<HtmlDocumentResponse> storePageResource = new(async(_) => (true, await WebRequest.GetStorePage(app.Parent?.ID).ConfigureAwait(false)));
Handlers.Values.ToList().ForEach(x => Utilities.InBackground(async() => await x.HandlePlaytest(app, storePageResource).ConfigureAwait(false)));
} else {
SharedExternalResource<AppDetails> appDetailsResource = new();
ArchiCacheable<AppDetails> appDetailsResource = new(async(_) => (true, await WebRequest.GetAppDetails(app.ID).ConfigureAwait(false)));
Handlers.Values.ToList().ForEach(x => Utilities.InBackground(async() => await x.HandleFreeApp(app, appDetailsResource).ConfigureAwait(false)));
}
});
Expand Down Expand Up @@ -293,7 +294,7 @@ private async static Task HandleProductInfo(List<SteamApps.PICSProductInfoCallba
Handlers.Values.ToList().ForEach(x => x.BotCache.SaveChanges());
}

private async Task HandleFreeApp(FilterableApp app, SharedExternalResource<AppDetails> appDetailsResource) {
private async Task HandleFreeApp(FilterableApp app, ArchiCacheable<AppDetails> appDetailsResource) {
if (!BotCache.ChangedApps.Contains(app.ID)) {
return;
}
Expand All @@ -311,7 +312,8 @@ private async Task HandleFreeApp(FilterableApp app, SharedExternalResource<AppDe
return;
}

if (!PackageFilter.IsAppFreeAndValidOnStore(await appDetailsResource.Fetch(async() => await WebRequest.GetAppDetails(Bot, app.ID).ConfigureAwait(false)).ConfigureAwait(false))) {
(_, AppDetails? appDetails) = await appDetailsResource.GetValue().ConfigureAwait(false);
if (!PackageFilter.IsAppFreeAndValidOnStore(appDetails)) {
return;
}

Expand Down Expand Up @@ -345,7 +347,7 @@ private void HandleFreePackage(FilterablePackage package) {
}
}

private async Task HandlePlaytest(FilterableApp app, SharedExternalResource<HtmlDocumentResponse> storePageResource) {
private async Task HandlePlaytest(FilterableApp app, ArchiCacheable<HtmlDocumentResponse> storePageResource) {
if (!BotCache.ChangedApps.Contains(app.ID)) {
return;
}
Expand All @@ -367,7 +369,8 @@ private async Task HandlePlaytest(FilterableApp app, SharedExternalResource<Html
return;
}

if (!PackageFilter.IsPlaytestValidOnStore(await storePageResource.Fetch(async() => await WebRequest.GetStorePage(Bot, app.Parent.ID).ConfigureAwait(false)).ConfigureAwait(false))) {
(_, HtmlDocumentResponse? storePage) = await storePageResource.GetValue().ConfigureAwait(false);
if (!PackageFilter.IsPlaytestValidOnStore(storePage)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion FreePackages/Handlers/PackageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private async Task<EResult> ClaimFreeApp(uint appID) {
// When both GrantedApps and GrantedPackages are empty something went wrong, It could be we're rate limited or it could mean we just can't activate this app
if (response.GrantedApps.Count == 0 && response.GrantedPackages.Count == 0) {
// Only way to really get an idea of what might have went wrong is to check the store page
AppDetails? appDetails = await WebRequest.GetAppDetails(Bot, appID).ConfigureAwait(false);
AppDetails? appDetails = await WebRequest.GetAppDetails(appID).ConfigureAwait(false);
bool success = appDetails?.Success ?? false;
bool hasPackages = (appDetails?.Data?.Packages.Count ?? 0) != 0;
bool isFree = appDetails?.Data?.IsFree ?? false;
Expand Down
11 changes: 7 additions & 4 deletions FreePackages/WebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ internal static class WebRequest {
return userDataResponse?.Content;
}

internal static async Task<AppDetails?> GetAppDetails(Bot bot, uint appID) {
internal static async Task<AppDetails?> GetAppDetails(uint appID) {
ArgumentNullException.ThrowIfNull(ASF.WebBrowser);

// Reportedly, this API has a possibly reachable rate limit of 200 requests per 5 minutes
await AppDetailsSemaphore.WaitAsync().ConfigureAwait(false);
try {
Uri request = new(ArchiWebHandler.SteamStoreURL, String.Format("/api/appdetails/?appids={0}", appID));
ObjectResponse<Dictionary<uint, AppDetails>>? appDetailsResponse = await bot.ArchiWebHandler.UrlGetToJsonObjectWithSession<Dictionary<uint, AppDetails>>(request, maxTries: 1).ConfigureAwait(false);
ObjectResponse<Dictionary<uint, AppDetails>>? appDetailsResponse = await ASF.WebBrowser.UrlGetToJsonObject<Dictionary<uint, AppDetails>>(request, maxTries: 1).ConfigureAwait(false);

return appDetailsResponse?.Content?[appID];
} finally {
Expand Down Expand Up @@ -66,14 +68,15 @@ internal static class WebRequest {
return playtestAccessResponse?.Content;
}

internal static async Task<HtmlDocumentResponse?> GetStorePage(Bot bot, uint? appID) {
internal static async Task<HtmlDocumentResponse?> GetStorePage(uint? appID) {
ArgumentNullException.ThrowIfNull(appID);
ArgumentNullException.ThrowIfNull(ASF.WebBrowser);

await StorePageSemaphore.WaitAsync().ConfigureAwait(false);
try {
Uri request = new(ArchiWebHandler.SteamStoreURL, String.Format("/app/{0}", appID));

return await bot.ArchiWebHandler.UrlGetToHtmlDocumentWithSession(request, maxTries: 1, requestOptions: WebBrowser.ERequestOptions.ReturnRedirections);
return await ASF.WebBrowser.UrlGetToHtmlDocument(request, maxTries: 1, requestOptions: WebBrowser.ERequestOptions.ReturnRedirections);
} finally {
Utilities.InBackground(
async() => {
Expand Down

0 comments on commit bb00ae1

Please sign in to comment.