Advertisement
Guest User

nasa.js

a guest
Jun 5th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const apiKey = "OfzgBPXMt4Ao8IW5gKBFIePSEzUtHzhsIQV5M8oS";
  2. const apodUrl = "https://api.nasa.gov/planetary/apod";
  3. const afeedUrl = "https://api.nasa.gov/neo/rest/v1/feed";
  4.  
  5.  
  6. function handleAFeedData({element_count, near_earth_objects}, afeedElement, afeedTable){
  7.    
  8.  
  9.     afeedElement.innerHTML = Object.keys(near_earth_objects).map(date=>{
  10.         return near_earth_objects[date].map(asteroid=>{
  11.             const id        = asteroid.id;
  12.             const name      = asteroid.name;
  13.             const dangerous = asteroid.is_potentially_hazardous_asteroid;
  14.             const magnitude = asteroid.absolute_magnitude_h;
  15.             const min       = asteroid.estimated_diameter.meters.estimated_diameter_min;
  16.             const max       = asteroid.estimated_diameter.meters.estimated_diameter_max;
  17.             const close_approach_data = asteroid.close_approach_data.shift();
  18.             const miss_distance = close_approach_data.miss_distance.kilometers;
  19.  
  20.             return `<tr>
  21.                 <td>${id}</td>
  22.                 <td>${name}</td>
  23.                 <td>${dangerous ? "YES" : "NO"}</td>
  24.                 <td>${magnitude}</td>
  25.                 <td>${min}</td>
  26.                 <td>${max}</td>
  27.                 <td>${miss_distance}</td>
  28.                 <td>${date}</td>
  29.             </tr>`
  30.  
  31.         }).join("");
  32.  
  33.     }).join("");
  34.  
  35.     if(afeedElement.innerHTML === ""){
  36.         afeedTable.className = "striped hide"
  37.     } else{
  38.         afeedTable.className = "striped";
  39.     }
  40.  
  41.  
  42. }
  43.  
  44. function contentLoaded(){
  45.     const apodElement = document.getElementById("apod");
  46.     const startElement = document.getElementById("start");
  47.     const afeedElement = document.getElementById("afeed");
  48.     const afeedTable   = document.getElementById("afeedTable");
  49.  
  50.     /** Apod */
  51.  
  52.     fetch(`${apodUrl}?api_key=${apiKey}`)
  53.     .then(res=>res.json())
  54.     .then(data=>{
  55.  
  56.         let media = "";
  57.         if(data.media_type === "image"){
  58.             media = `<img class="responsive-img" src="${data.hdurl}">`
  59.         } else {
  60.             media = `<div class="video-container">
  61.                         <iframe src="${dat.hdurl}" width="560" height="315"></iframe>
  62.                     </div>`
  63.         }
  64.  
  65.         apodElement.innerHTML = (`
  66.             <div class="card-image">
  67.                 ${media}
  68.                 <span class="card-title">${data.title}</span>
  69.             </div>
  70.             <div class="card-content">
  71.                 <p>
  72.                     ${data.explanation}
  73.                 </p>
  74.                 <p>${(new Date(data.date)).toDateString()}</p>
  75.                 <p>© ${data.copyright}</p>
  76.             </div>
  77.             <div class="card-action">
  78.                 <a target="_blank" href="https://www.nasa.gov/">Find more @ Nasa</a>
  79.             </div>
  80.         `)
  81.     }).catch(handleError);
  82.  
  83.     /** Asteroid Feed */
  84.     const elems = document.querySelectorAll('.datepicker');
  85.     const instances = M.Datepicker.init(elems, {
  86.         autoClose: true,
  87.         defaultDate: new Date(),
  88.         format: 'yyyy-mm-dd'
  89.     });
  90.  
  91.     start.addEventListener("change", function(){
  92.  
  93.         fetch(`${afeedUrl}?start_date=${this.value}&api_key=${apiKey}`)
  94.         .then(res=>res.json())
  95.         .then(data=>handleAFeedData(data, afeedElement, afeedTable))
  96.         .catch(handleError);
  97.  
  98.         console.log(this.value);
  99.     })
  100.  
  101. }
  102.  
  103.  
  104. function handleError(error){
  105.     console.warn(error.message);
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. window.addEventListener("DOMContentLoaded", contentLoaded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement