import { DOMParser } from "https://esm.sh/linkedom";
const NASA_API_KEY = 'vYg1cCNLVbcgNemMWuLEjoJsGOGbBXZjZjmwVwuV';
async function fetchNASAData() {
const today = new Date().toISOString().split('T')[0];
const apodResponse = await fetch(`https://api.nasa.gov/planetary/apod?api_key=${NASA_API_KEY}`);
const apodData = await apodResponse.json();
const epicResponse = await fetch(`https://api.nasa.gov/EPIC/api/natural?api_key=${NASA_API_KEY}`);
const epicData = await epicResponse.json();
const latestEpicImage = epicData[0];
const neowsResponse = await fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=${today}&end_date=${today}&api_key=${NASA_API_KEY}`);
const neowsData = await neowsResponse.json();
const marsWeatherResponse = await fetch(`https://api.nasa.gov/insight_weather/?api_key=${NASA_API_KEY}&feedtype=json&ver=1.0`);
const marsWeatherData = await marsWeatherResponse.json();
const eonetResponse = await fetch(`https://eonet.gsfc.nasa.gov/api/v3/events`);
const eonetData = await eonetResponse.json();
const issResponse = await fetch('http://api.open-notify.org/iss-now.json');
const issData = await issResponse.json();
const nasaLibraryResponse = await fetch(`https://images-api.nasa.gov/search?q=space&media_type=image`);
const nasaLibraryData = await nasaLibraryResponse.json();
const tleResponse = await fetch('https://www.celestrak.com/NORAD/elements/gp.php?GROUP=active&FORMAT=json');
const tleData = await tleResponse.json();
return {
date: today,
astronomyPictureOfTheDay: {
title: apodData.title,
explanation: apodData.explanation,
url: apodData.url,
mediaType: apodData.media_type,
},
earthPolychromaticImagingCamera: {
date: latestEpicImage.date,
caption: latestEpicImage.caption,
imageUrl: `https://epic.gsfc.nasa.gov/archive/natural/${latestEpicImage.date.split(' ')[0].replace(/-/g, '/')}/png/${latestEpicImage.image}.png`,
},
nearEarthObjects: {
totalObjectsToday: neowsData.element_count,
potentiallyHazardousAsteroids: neowsData.near_earth_objects[today].filter(obj => obj.is_potentially_hazardous_asteroid).length,
closestObject: neowsData.near_earth_objects[today].reduce((closest, current) =>
(parseFloat(current.close_approach_data[0].miss_distance.kilometers) < parseFloat(closest.close_approach_data[0].miss_distance.kilometers)) ? current : closest
),
},
marsWeather: marsWeatherData.sol_keys ? {
solKeys: marsWeatherData.sol_keys,
latestSol: marsWeatherData[marsWeatherData.sol_keys[marsWeatherData.sol_keys.length - 1]],
} : "Mars weather data currently unavailable",
earthEvents: {
totalEvents: eonetData.events.length,
recentEvents: eonetData.events.slice(0, 5).map(event => ({
title: event.title,
type: event.categories[0].title,
date: event.geometry[0].date
}))
},
internationalSpaceStation: {
currentLocation: {
latitude: issData.iss_position.latitude,
longitude: issData.iss_position.longitude,
},
timestamp: new Date(issData.timestamp * 1000).toISOString()
},
nasaImageLibrary: {
recentImages: nasaLibraryData.collection.items.slice(0, 5).map(item => ({
title: item.data[0].title,
description: item.data[0].description,
url: item.links[0].href
}))
},
satelliteData: {
totalTracked: tleData.length,
recentSatellites: tleData.slice(0, 5).map(sat => ({
name: sat.OBJECT_NAME,
noradCatId: sat.NORAD_CAT_ID,
objectType: sat.OBJECT_TYPE
}))