/// <summary> /// Create a IGDB API client based on a custom-created RestEase client. Adds required /// JSON serializer settings on top of any existing settings. /// </summary> /// <returns></returns> public IGDBClient(string clientId, string clientSecret, ITokenStore tokenStore) { if (tokenStore == null) { throw new ArgumentNullException(nameof(tokenStore), "A ITokenStore is required. Pass InMemoryTokenStore if you do not have a custom store implemented."); } _tokenManager = new TokenManager(tokenStore, new TwitchOAuthClient(clientId, clientSecret)); var api = new RestClient("https://api.igdb.com/v4", async(request, cancellationToken) => { var twitchToken = await _tokenManager.AcquireTokenAsync(); if (twitchToken?.AccessToken != null) { request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue( "Bearer", twitchToken.AccessToken); } }) { JsonSerializerSettings = DefaultJsonSerializerSettings }.For <IGDBApi>(); api.ClientId = clientId; _api = api; }
public static async void InitializeAsync(IServiceProvider serviceProvider) { Debug.AutoFlush = true; using (var context = new ApplicationDbContext( serviceProvider.GetRequiredService < DbContextOptions <ApplicationDbContext> >())) { // Look for any movies. IGDBApi client = Client.Create(""); //your IGDB API Key here if (context.Game.Any()) { return; // db has been seeded } GameJSON[] GameJSONs = new GameJSON[1000]; // Task.Run(async () => //{ var coverSmall = ""; var artworkImageId = ""; //loop through the API and add Games to Database for (int i = 32001; i <= 33000; i++) { try { GameJSONs = await client.QueryAsync <GameJSON>(Client.Endpoints.Games, query : "fields name,cover.*,genres.*,platforms.*,artworks.image_id,release_dates.*,involved_companies.*,aggregated_rating,storyline,summary,rating_count; where id =(" + i.ToString() + ");"); System.Diagnostics.Trace.WriteLine(client.ToString()); if (GameJSONs.FirstOrDefault() != null) { var genres = ""; var platforms = ""; var developer = ""; var bigCover = ""; if (coverSmall != null) { foreach (var item in GameJSONs) { if (item != null) { if (item.Artworks != null) { artworkImageId = item.Artworks.Values.First().ImageId; } else { artworkImageId = ""; } // coverSmall = ImageHelper.GetImageUrl(imageId: artworkImageId, size: ImageSize.CoverBig, retina: false); if (artworkImageId != "") { bigCover = ImageHelper.GetImageUrl(imageId: artworkImageId, size: ImageSize.ScreenshotBig, retina: false); } foreach (var item2 in item.Genres.Values) { genres = genres + item2.Name + "/"; } foreach (var item2 in item.Platforms.Values) { platforms += item2.Name + "/"; } //we need to trim the storyline and summary if higher than 4000 characters if (item.Storyline.Length > 4000) { game = new Game(item.Id, item.Name, DateTimeOffset.Parse(item.ReleaseDates.Values.First().Human), "http:" + item.Cover.Value.Url, genres, platforms, item.AggregatedRating, developer, "http:" + bigCover, item.Storyline.Substring(0, 4000), item.Summary, item.AggregatedRatingCount); } else if (item.Summary.Length > 4000) { game = new Game(item.Id, item.Name, DateTimeOffset.Parse(item.ReleaseDates.Values.First().Human), "http:" + item.Cover.Value.Url, genres, platforms, item.AggregatedRating, developer, "http:" + bigCover, item.Storyline, item.Summary.Substring(0, 4000), item.AggregatedRatingCount); } else { game = new Game(item.Id, item.Name, DateTimeOffset.Parse(item.ReleaseDates.Values.First().Human), "http:" + item.Cover.Value.Url, genres, platforms, item.AggregatedRating, developer, "http:" + bigCover, item.Storyline, item.Summary, item.AggregatedRatingCount); } //used for testing locally // Game.GetAllProperties(game); if (game != null) { games.Prepend(game); } try { await context.AddAsync(game); } catch (Exception e1) { System.Diagnostics.Trace.WriteLine(e1.ToString()); } } genres = ""; platforms = ""; } } } } catch (Exception e) { System.Diagnostics.Trace.WriteLine(client.ToString()); System.Diagnostics.Trace.WriteLine(e.ToString()); } }//end for context.SaveChanges(); } }