using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; using System.Collections.Generic; using galacticlip_app.Models; namespace galacticlip_app.Services { public class NasaMediaService { private readonly HttpClient _httpClient; private const string apiKey = ""; // Optional for this API, increase rate limits public NasaMediaService() { _httpClient = new HttpClient { BaseAddress = new Uri("https://images-api.nasa.gov/") }; } // Generate random keywords to search for images: too boring before with just one | CAN BE EXPANDED!!!!!!!@!@!@!@iuer4ytavb987y3r private static readonly List RandomKeywords = new List { "nebula", "galaxy", "star", "moon", "planet", "exploration", "astronomy", "rocket", "cosmos", "telescope", "asteroid", "comet", "black hole", "solar system", "exoplanet", "orbit", "spacecraft", "lunar", "eclipse", "milky way", "mars", "jupiter", "saturn", "venus", "mercury", "uranus", "neptune", "pluto", "earth", "satellite", "iss", "space station", "voyager", "apollo", "artemis", "hubble", "perseverance", "curiosity", "rover", "landing", "launch", "space shuttle", "spacewalk", "gravity", "cosmology", "interstellar", "pulsar", "supernova", "quasar", "big bang", "nasa", "observatory", "deep space", "light-year", "constellation" }; private static readonly Random _random = new Random(); public async Task> GetRandomImagesAsync() { var randomKeyword = RandomKeywords[_random.Next(RandomKeywords.Count)]; return await SearchMediaAsync(randomKeyword); } public async Task> SearchMediaAsync(string query) { var requestUrl = $"https://images-api.nasa.gov/search?q={Uri.EscapeDataString(query)}&media_type=image"; try { Console.WriteLine("Starting API Request"); // Debug statement var response = await _httpClient.GetStringAsync(requestUrl); Console.WriteLine("API Request Successful"); // Debug statement var nasaApiResponse = JsonConvert.DeserializeObject(response); Console.WriteLine("API Response Deserialized"); // Debug statement var mediaItems = new List(); foreach (var item in nasaApiResponse.Collection.Items) { if (item.Links != null && item.Links.Count > 0 && item.Data != null && item.Data.Count > 0) { mediaItems.Add(new NasaMediaItem { ThumbnailUrl = item.Links[0].Href, Title = item.Data[0].Title, Description = item.Data[0].Description, DateCreated = item.Data[0].DateCreated, Photographer = item.Data[0].Photographer, Keywords = item.Data[0].Keywords != null ? string.Join(", ", item.Data[0].Keywords) : string.Empty }); } } Console.WriteLine($"Images Retrieved: {mediaItems.Count}"); return mediaItems.Count > 10 ? mediaItems.GetRange(0, 10) : mediaItems; } catch (Exception ex) { Console.WriteLine($"Error fetching images: {ex.Message}"); return new List(); } } } }