Advertisement
Guest User

SteamAPI Image Loading

a guest
Nov 8th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. public partial class MainPage : Page
  2.     {
  3.         private readonly string _apiKey = "";
  4.        
  5.         SettingsManager settingsManager = new SettingsManager();
  6.         AppSettings settings;
  7.  
  8.         public string SteamPath;
  9.  
  10.         public ObservableCollection<App> Apps { get; set; }
  11.  
  12.         public MainPage()
  13.         {
  14.             InitializeComponent();
  15.             settings = settingsManager.LoadSettings();
  16.             SteamPath = settings.steampath;
  17.  
  18.             Apps = new ObservableCollection<App>();
  19.             DataContext = this;
  20.         }
  21.  
  22.         private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  23.         {
  24.             FrameApp.FrameMain.Navigate(new SettingsPage());
  25.         }
  26.  
  27.         private async void TestBtn_Click(object sender, RoutedEventArgs e)
  28.         {
  29.             if (!SteamPath.Contains("Steam") || SteamPath == "" || SteamPath == String.Empty || SteamPath == null)
  30.             {
  31.                 MessageBox.Show("Incorrect path to the Steam folder!");
  32.                 FrameApp.FrameMain.Navigate(new SettingsPage());
  33.             }
  34.             else
  35.             {
  36.                 Debug.WriteLine($"### Getting the list of games started ###");
  37.                 await GetAllApps();
  38.                 Debug.WriteLine($"### End of getting the list of games ###");
  39.             }
  40.  
  41.         }
  42.  
  43.         private async void SearchTBX_KeyDown(object sender, KeyEventArgs e)
  44.         {
  45.             if (e.Key == Key.Enter)
  46.             {
  47.                 string searchTerm = SearchTBX.Text;
  48.                 await SearchGamesAsync(searchTerm);
  49.             }
  50.         }
  51.  
  52.         public async Task<AppDetails> GetAppDetails(int appId)
  53.         {
  54.             using (var httpClient = new HttpClient())
  55.             {
  56.                 try
  57.                 {
  58.                     var response = await httpClient.GetStringAsync($"https://store.steampowered.com/api/appdetails/?appids={appId}");
  59.                     var appDetails = JsonConvert.DeserializeObject<Dictionary<string, AppDetails>>(response);
  60.  
  61.                     if (appDetails.ContainsKey(appId.ToString()) && appDetails[appId.ToString()].Success)
  62.                     {
  63.                         return appDetails[appId.ToString()];
  64.                     }
  65.                     else
  66.                     {
  67.                         Debug.WriteLine($"Error when receiving data for a game with AppID {appId};\n {appDetails.Count}\n{response.ToString()}");
  68.                         return new AppDetails { Success = false, Data = null };
  69.                     }
  70.                 }
  71.                 catch (Exception ex)
  72.                 {
  73.                     Debug.WriteLine($"Error receiving data: {ex.Message}");
  74.                     return new AppDetails { Success = false, Data = null };
  75.                 }
  76.             }
  77.         }
  78.  
  79.         public async Task SearchGamesAsync(string searchTerm)
  80.         {
  81.             using (var httpClient = new HttpClient())
  82.             {
  83.                 try
  84.                 {
  85.                     var response = await httpClient.GetStringAsync($"http://api.steampowered.com/ISteamApps/GetAppList/v2/");
  86.                     var steamApiResponse = JsonConvert.DeserializeObject<SteamApiResponse>(response);
  87.                     var apps = steamApiResponse.AppList.Apps;
  88.  
  89.                     var filteredGames = apps.Where(app => app.AppId.ToString().ToLower().Contains(searchTerm.ToLower())).ToList();
  90.                     var tasks = filteredGames.Select(app => ProcessGameAsync(app)).ToList();
  91.  
  92.                     await Task.WhenAll(tasks);
  93.  
  94.                     GamesDG.ItemsSource = filteredGames;
  95.                 }
  96.                 catch (Exception ex)
  97.                 {
  98.                     Debug.WriteLine($"Error receiving data: {ex.Message}");
  99.                 }
  100.             }
  101.         }
  102.  
  103.         private async Task ProcessGameAsync(App app)
  104.         {
  105.             var appDetails = await GetAppDetails(app.AppId);
  106.             if (appDetails.Success)
  107.             {
  108.                 app.header_image = appDetails.Data?.header_image;
  109.                 MessageBox.Show($"Added an image for\nGame: {app.Name} ({app.AppId});\n Image: {app.header_image}");
  110.             }
  111.         }
  112.     }
  113.  
  114.     public class SteamApiResponse
  115.     {
  116.         public AppList AppList { get; set; }
  117.     }
  118.  
  119.     public class AppList
  120.     {
  121.         public List<App> Apps { get; set; }
  122.     }
  123.  
  124.     public class AppDetails
  125.     {
  126.         public bool Success { get; set; }
  127.         public App Data { get; set; }
  128.     }
  129.  
  130.     public class App
  131.     {
  132.         public int AppId { get; set; }
  133.         public string Name { get; set; }
  134.         public string header_image { get; set; }
  135.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement