public KeyValue GetAppInfo(int appId) { KeyValue data = null; var stringData = string.Empty; // First try to get cached data try { stringData = playniteServices?.GetSteamAppInfoData(appId); } catch (Exception e) { logger.Error(e, $"Failed to get Steam appinfo cache data {appId}."); } // If no cache then download on client and push to cache if (string.IsNullOrEmpty(stringData)) { data = SteamApiClient.GetProductInfo(appId).GetAwaiter().GetResult(); logger.Debug($"Steam appinfo got from live server {appId}"); try { if (playniteServices != null) { using (var str = new MemoryStream()) { data.SaveToStream(str, false); using (var reader = new StreamReader(str, Encoding.UTF8)) { str.Seek(0, SeekOrigin.Begin); stringData = reader.ReadToEnd(); } } playniteServices.PostSteamAppInfoData(appId, stringData); } } catch (Exception e) { logger.Error(e, $"Failed to post steam appinfo data to cache {appId}"); } } else { logger.Debug($"Steam appinfo data got from cache {appId}"); } if (data != null) { return(data); } else if (!string.IsNullOrEmpty(stringData)) { return(KeyValue.LoadFromString(stringData)); } return(null); }
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); }