diff --git a/SGDBMetadata/ResponseModel.cs b/SGDBMetadata/ResponseModel.cs index e8010e2..47fb804 100644 --- a/SGDBMetadata/ResponseModel.cs +++ b/SGDBMetadata/ResponseModel.cs @@ -3,12 +3,16 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Web.UI; namespace SGDBMetadata { public class ResponseModel { public Boolean success { get; set; } + public int page { get; set; } + public int total { get; set; } + public int limit { get; set; } public List data; public List NewList diff --git a/SGDBMetadata/SGDBMetadata.cs b/SGDBMetadata/SGDBMetadata.cs index 0d5c0cf..b7ce35c 100644 --- a/SGDBMetadata/SGDBMetadata.cs +++ b/SGDBMetadata/SGDBMetadata.cs @@ -26,7 +26,7 @@ public class SGDBMetadata : MetadataPlugin public override string Name => "SteamGridDB"; - public SGDBMetadata(IPlayniteAPI api) : base(api) + public SGDBMetadata(IPlayniteAPI api) : base(api) { settings = new SGDBMetadataSettingsViewModel(this); Properties = new MetadataPluginProperties diff --git a/SGDBMetadata/SGDBMetadata.csproj b/SGDBMetadata/SGDBMetadata.csproj index 533f603..74e0172 100644 --- a/SGDBMetadata/SGDBMetadata.csproj +++ b/SGDBMetadata/SGDBMetadata.csproj @@ -32,20 +32,21 @@ 4 - - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll - - ..\packages\PlayniteSDK.6.0.0-preview4\lib\net462\Playnite.SDK.dll + + ..\packages\PlayniteSDK.6.11.0\lib\net462\Playnite.SDK.dll - - ..\packages\RestSharp.106.10.1\lib\net452\RestSharp.dll + + ..\packages\RestSharp.106.15.0\lib\net452\RestSharp.dll + diff --git a/SGDBMetadata/SGDBService.cs b/SGDBMetadata/SGDBService.cs index 6ceeac0..d768bc1 100644 --- a/SGDBMetadata/SGDBService.cs +++ b/SGDBMetadata/SGDBService.cs @@ -8,6 +8,9 @@ using RestSharp.Authenticators; using SGDBMetadata; using Playnite.SDK; +using System.Reflection; +using System.Net.Http; +using System.Threading; namespace SGDBMetadata { @@ -22,39 +25,57 @@ public SgdbServiceClient(SGDBMetadataSettings settings) { client = new RestClient(baseUrl); client.Authenticator = new JwtAuthenticator(settings.ApiKey); + this.settings = settings; } public RestClient RestClient { get; set; } - public T Execute(RestRequest request) where T : new() + public List Execute(RestRequest request, bool pagination = true) where T : new() { request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var logger = LogManager.GetLogger(); var fullUrl = client.BuildUri(request); logger.Info(fullUrl.ToString()); - var response = client.Execute(request); - if (response.ErrorException != null) + + var items = new List(); + int page = 0; + + while(true) { - const string message = "Error retrieving response. Check inner details for more info."; - var sgdbException = new Exception(message, response.ErrorException); - throw sgdbException; + request.AddOrUpdateParameter("page", page); + var response = client.Execute(request); + + if (response.ErrorException != null) + throw new Exception("Error retrieving response", response.ErrorException); + + logger.Info(response.Content); + + var json = Newtonsoft.Json.JsonConvert.DeserializeObject>(response.Content); + items.AddRange(json.data); + if (json.data.Count > 0 && pagination) + { + page++; + } else + { + break; + } + } - var content = response.Content; - logger.Info(content); - return Newtonsoft.Json.JsonConvert.DeserializeObject(content); + + return items; } - public ResponseModel getSGDBGames(string searchName) + public List getSGDBGames(string searchName) { var logger = LogManager.GetLogger(); logger.Info(searchName); var request = new RestRequest("search/autocomplete/{searchName}", Method.GET); request.AddParameter("searchName", searchName, ParameterType.UrlSegment); - return Execute>(request); + return Execute(request, false); } - public ResponseModel getSGDBGameGridByAppId(string platform, string gameId) + public List getSGDBGameGridByAppId(string platform, string gameId) { var request = new RestRequest("grids/{platform}/{gameId}", Method.GET); request.AddParameter("platform", platform, ParameterType.UrlSegment); @@ -70,10 +91,10 @@ public ResponseModel getSGDBGameGridByAppId(string platform, string g request.AddParameter("nsfw", settings.CoverNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.CoverHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameGridCover(int gameId) + public List getSGDBGameGridCover(int gameId) { var request = new RestRequest("grids/game/{id}", Method.GET); request.AddParameter("id", gameId, ParameterType.UrlSegment); @@ -87,10 +108,10 @@ public ResponseModel getSGDBGameGridCover(int gameId) } request.AddParameter("nsfw", settings.CoverNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.CoverHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameHero(int gameId) + public List getSGDBGameHero(int gameId) { var logger = LogManager.GetLogger(); logger.Info("getSGDBGameHero"); @@ -107,10 +128,10 @@ public ResponseModel getSGDBGameHero(int gameId) request.AddParameter("nsfw", settings.BackgroundNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.BackgroundHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameHeroByAppId(string platform, string gameId) + public List getSGDBGameHeroByAppId(string platform, string gameId) { var logger = LogManager.GetLogger(); logger.Info("getSGDBGameHeroByAppId"); @@ -128,20 +149,20 @@ public ResponseModel getSGDBGameHeroByAppId(string platform, string g request.AddParameter("nsfw", settings.BackgroundNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.BackgroundHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameLogo(int gameId) + public List getSGDBGameLogo(int gameId) { var request = new RestRequest("logos/game/{gameId}", Method.GET); request.AddParameter("gameId", gameId, ParameterType.UrlSegment); request.AddParameter("nsfw", settings.IconNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.IconHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameLogoByAppId(string platform, string gameId) + public List getSGDBGameLogoByAppId(string platform, string gameId) { var request = new RestRequest("logos/{platform}/{gameId}", Method.GET); request.AddParameter("platform", platform, ParameterType.UrlSegment); @@ -149,20 +170,20 @@ public ResponseModel getSGDBGameLogoByAppId(string platform, string request.AddParameter("nsfw", settings.IconNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.IconHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameIcon(int gameId) + public List getSGDBGameIcon(int gameId) { var request = new RestRequest("icons/game/{gameId}", Method.GET); request.AddParameter("gameId", gameId, ParameterType.UrlSegment); request.AddParameter("nsfw", settings.IconNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.IconHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } - public ResponseModel getSGDBGameIconByAppId(string platform, string gameId) + public List getSGDBGameIconByAppId(string platform, string gameId) { var request = new RestRequest("icons/{platform}/{gameId}", Method.GET); request.AddParameter("platform", platform, ParameterType.UrlSegment); @@ -170,7 +191,7 @@ public ResponseModel getSGDBGameIconByAppId(string platform, string request.AddParameter("nsfw", settings.IconNsfw, ParameterType.GetOrPost); request.AddParameter("humor", settings.IconHumor, ParameterType.GetOrPost); - return Execute>(request); + return Execute(request); } public SearchModel getGameSGDBFuzzySearch(string gameTitle) @@ -178,10 +199,10 @@ public SearchModel getGameSGDBFuzzySearch(string gameTitle) var logger = LogManager.GetLogger(); logger.Info(gameTitle); var gameListResponse = getSGDBGames(gameTitle); - if (gameListResponse.success) + if (gameListResponse.Count > 0) { - logger.Info(gameListResponse.data[0].name); - return gameListResponse.data[0]; //First element of search results, should probably implement fuzzysearchquery based on intentions + logger.Info(gameListResponse[0].name); + return gameListResponse[0]; //First element of search results, should probably implement fuzzysearchquery based on intentions } else { @@ -195,10 +216,10 @@ public List getGameListSGDB(string gameTitle) var logger = LogManager.GetLogger(); logger.Info(gameTitle); var gameListResponse = getSGDBGames(gameTitle); - if (gameListResponse.success) + if (gameListResponse.Count > 0) { - logger.Info(gameListResponse.data.ToString()); - return gameListResponse.data; //First element of search results, should probably implement fuzzysearchquery based on intentions + logger.Info(gameListResponse.ToString()); + return gameListResponse; //First element of search results, should probably implement fuzzysearchquery based on intentions } else { @@ -211,18 +232,18 @@ public string getCoverImageUrl(SearchModel gameSearchItem, string platform, stri { if (platform != null && gameId != null) { - ResponseModel grid = getSGDBGameGridByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if(grid.success && grid.data.Count > 0) + List grid = getSGDBGameGridByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if(grid.Count > 0) { - return grid.data[0].url; + return grid[0].url; } } else if (gameSearchItem != null) { - ResponseModel grid = getSGDBGameGridCover(gameSearchItem.id); - if (grid.success && grid.data.Count > 0) + List grid = getSGDBGameGridCover(gameSearchItem.id); + if (grid.Count > 0) { - return grid.data[0].url; + return grid[0].url; } } return "bad path"; @@ -232,12 +253,12 @@ public List getCoverImages(GenericItemOption searchSelection, string { if (platform != null && gameId != null) { - ResponseModel grid = getSGDBGameGridByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (grid.success && grid.data.Count > 0) + List grid = getSGDBGameGridByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (grid.Count > 0) { - return grid.data; + return grid; } - else if (grid.success && grid.data.Count == 0) + else if (grid.Count == 0) { return null; } @@ -249,12 +270,12 @@ public List getCoverImages(GenericItemOption searchSelection, string } else if (searchSelection != null) { - ResponseModel grid = getSGDBGameGridCover(int.Parse(searchSelection.Description)); - if (grid.success && grid.data.Count > 0) + List grid = getSGDBGameGridCover(int.Parse(searchSelection.Description)); + if (grid.Count > 0) { - return grid.data; + return grid; } - else if (grid.success && grid.data.Count == 0) + else if (grid.Count == 0) { return null; } @@ -279,18 +300,18 @@ public string getHeroImageUrl(SearchModel gameSearchItem, string platform, strin logger.Info(gameId); if (platform != null && gameId != null) { - ResponseModel hero = getSGDBGameHeroByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (hero.success && hero.data.Count > 0) + List hero = getSGDBGameHeroByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (hero.Count > 0) { - return hero.data[0].url; + return hero[0].url; } } else if (gameSearchItem != null) { - ResponseModel hero = getSGDBGameHero(gameSearchItem.id); - if (hero.success && hero.data.Count > 0) + List hero = getSGDBGameHero(gameSearchItem.id); + if (hero.Count > 0) { - return hero.data[0].url; + return hero[0].url; } } return "bad path"; @@ -305,12 +326,12 @@ public List getHeroImages(GenericItemOption searchSelection, string p logger.Info(gameId); if (platform != null && gameId != null) { - ResponseModel hero = getSGDBGameHeroByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (hero.success && hero.data.Count > 0) + List hero = getSGDBGameHeroByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (hero.Count > 0) { - return hero.data; + return hero; } - else if (hero.success && hero.data.Count == 0) + else if (hero.Count == 0) { return null; } @@ -322,12 +343,12 @@ public List getHeroImages(GenericItemOption searchSelection, string p } else if (searchSelection != null) { - ResponseModel hero = getSGDBGameHero(int.Parse(searchSelection.Description)); - if (hero.success && hero.data.Count > 0) + List hero = getSGDBGameHero(int.Parse(searchSelection.Description)); + if (hero.Count > 0) { - return hero.data; + return hero; } - else if (hero.success && hero.data.Count == 0) + else if (hero.Count == 0) { return null; } @@ -347,18 +368,18 @@ public string getLogoImageUrl(SearchModel gameSearchItem, string platform, strin { if (platform != null && gameId != null) { - ResponseModel logo = getSGDBGameLogoByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (logo.success && logo.data.Count > 0) + List logo = getSGDBGameLogoByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (logo.Count > 0) { - return logo.data[0].url; + return logo[0].url; } } else if (gameSearchItem != null) { - ResponseModel logo = getSGDBGameLogo(gameSearchItem.id); - if (logo.success && logo.data.Count > 0) + List logo = getSGDBGameLogo(gameSearchItem.id); + if (logo.Count > 0) { - return logo.data[0].url; + return logo[0].url; } } return "bad path"; @@ -368,12 +389,12 @@ public List getLogoImages(GenericItemOption searchSelection, string { if (platform != null && gameId != null) { - ResponseModel logo = getSGDBGameLogoByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (logo.success && logo.data.Count > 0) + List logo = getSGDBGameLogoByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (logo.Count > 0) { - return logo.data; + return logo; } - else if (logo.success && logo.data.Count == 0) + else if (logo.Count == 0) { return null; } @@ -385,12 +406,12 @@ public List getLogoImages(GenericItemOption searchSelection, string } else if (searchSelection != null) { - ResponseModel logo = getSGDBGameLogo(int.Parse(searchSelection.Description)); - if (logo.success && logo.data.Count > 0) + List logo = getSGDBGameLogo(int.Parse(searchSelection.Description)); + if (logo.Count > 0) { - return logo.data; + return logo; } - else if (logo.success && logo.data.Count == 0) + else if (logo.Count == 0) { return null; } @@ -410,18 +431,18 @@ public string getIconImageUrl(SearchModel gameSearchItem, string platform, strin { if (platform != null && gameId != null) { - ResponseModel icon = getSGDBGameIconByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (icon.success && icon.data.Count > 0) + List icon = getSGDBGameIconByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (icon.Count > 0) { - return icon.data[0].url; + return icon[0].url; } } else if (gameSearchItem != null) { - ResponseModel icon = getSGDBGameIcon(gameSearchItem.id); - if (icon.success && icon.data.Count > 0) + List icon = getSGDBGameIcon(gameSearchItem.id); + if (icon.Count > 0) { - return icon.data[0].url; + return icon[0].url; } } return "bad path"; @@ -431,12 +452,12 @@ public List getIconImages(GenericItemOption searchSelection, string { if (platform != null && gameId != null) { - ResponseModel icon = getSGDBGameIconByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions - if (icon.success && icon.data.Count > 0) + List icon = getSGDBGameIconByAppId(platform, gameId); //First element of search results, should probably implement fuzzysearchquery based on intentions + if (icon.Count > 0) { - return icon.data; + return icon; } - else if (icon.success && icon.data.Count == 0) + else if (icon.Count == 0) { return null; } @@ -448,12 +469,12 @@ public List getIconImages(GenericItemOption searchSelection, string } else if (searchSelection != null) { - ResponseModel icon = getSGDBGameIcon(int.Parse(searchSelection.Description)); - if (icon.success && icon.data.Count > 0) + List icon = getSGDBGameIcon(int.Parse(searchSelection.Description)); + if (icon.Count > 0) { - return icon.data; + return icon; } - else if (icon.success && icon.data.Count == 0) + else if (icon.Count == 0) { return null; } diff --git a/SGDBMetadata/app.config b/SGDBMetadata/app.config index e936cc1..b8e8f50 100644 --- a/SGDBMetadata/app.config +++ b/SGDBMetadata/app.config @@ -6,6 +6,22 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SGDBMetadata/extension.yaml b/SGDBMetadata/extension.yaml index fcd7324..311bac4 100644 --- a/SGDBMetadata/extension.yaml +++ b/SGDBMetadata/extension.yaml @@ -1,7 +1,7 @@ Id: SteamGridDB_Playnite_Metadata Name: SteamGridDB Metadata Provider Author: cooperate -Version: 1.2 +Version: 1.2.1 Module: SGDBMetadata.dll Type: MetadataProvider Icon: icon.png \ No newline at end of file diff --git a/SGDBMetadata/packages.config b/SGDBMetadata/packages.config index d5977db..f3cdb85 100644 --- a/SGDBMetadata/packages.config +++ b/SGDBMetadata/packages.config @@ -1,6 +1,6 @@  - - - + + + \ No newline at end of file