public SteamGameMetadata DownloadGameMetadata(int id) { var metadata = new SteamGameMetadata(); var productInfo = SteamApiClient.GetProductInfo(id).GetAwaiter().GetResult(); metadata.ProductDetails = productInfo; // Steam may return 429 if we put too many request for (int i = 0; i < 10; i++) { try { metadata.StoreDetails = WebApiClient.GetStoreAppDetail(id); break; } catch (WebException e) { if (i + 1 == 10) { throw; } if (e.Message.Contains("429")) { Thread.Sleep(2500); continue; } else { throw; } } } // Icon var iconRoot = @"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/{0}/{1}.ico"; var icon = productInfo["common"]["clienticon"]; var iconUrl = string.Empty; if (icon.Name != null) { iconUrl = string.Format(iconRoot, id, icon.Value); } else { var newIcon = productInfo["common"]["icon"]; if (newIcon.Name != null) { iconRoot = @"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/{0}/{1}.jpg"; iconUrl = string.Format(iconRoot, id, newIcon.Value); } } // There might be no icon assigned to game if (!string.IsNullOrEmpty(iconUrl)) { var iconName = Path.GetFileName(new Uri(iconUrl).AbsolutePath); var iconData = Web.DownloadData(iconUrl); metadata.Icon = new FileDefinition( string.Format("images/steam/{0}/{1}", id.ToString(), iconName), iconName, iconData ); } // Image var imageRoot = @"http://cdn.akamai.steamstatic.com/steam/apps/{0}/header.jpg"; var imageUrl = string.Format(imageRoot, id); byte[] imageData = null; try { imageData = Web.DownloadData(imageUrl); } catch (WebException e) { var response = (HttpWebResponse)e.Response; if (response.StatusCode == HttpStatusCode.NotFound) { imageRoot = @"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/{0}/{1}.jpg"; var image = productInfo["common"]["logo"]; if (image.Name != null) { imageUrl = string.Format(imageRoot, id, image.Value); imageData = Web.DownloadData(imageUrl); } } else { throw; } } if (imageData != null) { var imageName = Path.GetFileName(new Uri(imageUrl).AbsolutePath); metadata.Image = new FileDefinition( string.Format("images/steam/{0}/{1}", id.ToString(), imageName), imageName, imageData ); } // Background Image metadata.BackgroundImage = string.Format(@"https://steamcdn-a.akamaihd.net/steam/apps/{0}/page_bg_generated_v6b.jpg", id); return(metadata); }
public StoreAppDetailsResult.AppDetails GetStoreData(int appId) { var stringData = string.Empty; // First try to get cached data try { stringData = playniteServices?.GetSteamStoreData(appId); } catch (Exception e) { logger.Error(e, "Failed to get Steam store cache data."); } // If no cache then download on client and push to cache if (string.IsNullOrEmpty(stringData)) { // Steam may return 429 if we put too many request for (int i = 0; i < 10; i++) { try { stringData = WebApiClient.GetRawStoreAppDetail(appId); logger.Debug($"Steam store data got from live server {appId}"); try { playniteServices?.PostSteamStoreData(appId, stringData); } catch (Exception e) { logger.Error(e, $"Failed to post steam store data to cache {appId}"); } break; } catch (WebException e) { if (i + 1 == 10) { throw; } if (e.Message.Contains("429")) { Thread.Sleep(2500); continue; } else { throw; } } } } else { logger.Debug($"Steam store data got from cache {appId}"); } if (!string.IsNullOrEmpty(stringData)) { var response = WebApiClient.ParseStoreData(appId, stringData); if (response.success != true) { return(null); } return(response.data); } return(null); }