diff --git a/web/static/scripts/gallery.js b/web/static/scripts/gallery.js index ab2406d..146f7f0 100644 --- a/web/static/scripts/gallery.js +++ b/web/static/scripts/gallery.js @@ -1,33 +1,39 @@ +let prev_date = Number(new Date().toLocaleString().slice(0,2))-1; +let today = new Date(); +let endDate = new Date(); + +let ids = []; +let id = 0; + +const apiKEY = "1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et"; + +today.setDate(today.getDate() - 17); $(document).ready(function() { - let today = new Date(); - let date = today.setDate(today.getDate() - 31); - date = new Date(date); - - const apiKEY = "1gI9G84ZafKDEnrbydviGknReOGiVK9jqrQBE3et"; - $.ajax({ url: "https://api.nasa.gov/planetary/apod", type: "GET", data: { api_key: apiKEY, - start_date: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`, + start_date: `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`, end_date: `${new Date().toLocaleString().slice(6, 10)}-${new Date().toLocaleString().slice(3, 5)}-${new Date().toLocaleString().slice(0, 2)}`, }, success: function(data) { wallpaper(data); }, error: function() { - let prev_date = Number(new Date().toLocaleString().slice(0,2))-1; $.ajax({ - url: "https://api.nasa.gov/planetary/apod", - data: { - api_key: apiKEY, - start_date: `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`, - end_date: `${new Date().toLocaleString().slice(6,10)}-${new Date().toLocaleString().slice(3, 5)}-${prev_date}`, - }, - success: function (data) { - wallpaper(data); - } + url: "https://api.nasa.gov/planetary/apod", + data: { + api_key: apiKEY, + start_date: `${today.getFullYear()}-${today.getMonth()+1}-${today.getDate()}`, + end_date: `${new Date().toLocaleString().slice(6,10)}-${new Date().toLocaleString().slice(3, 5)}-${prev_date}`, + }, + success: function (data) { + wallpaper(data); + }, + error: function (data) { + console.error(data); + }, }); }, }); @@ -42,28 +48,35 @@ function wallpaper(data) { $(".preloader").hide(); data = data.reverse(); - for (let i = 0; i < data.length; i++) { - if (data[i]['media_type'] === "image") { + for (let i = 0; i < data.length; i++, id++) { + ids.push(data[i]); + + if (ids.filter((item) => item.url === data[i].url).length > 1) { + continue; + } + + if (ids[id]['media_type'] === "image") { $(".header-row").append(`
`); } else { $(".header-row").append(` -Author: ${data[id]['copyright']}
-Date of publication: ${data[id]['date']}
-Explanation: ${data[id]['explanation']}
+ +Author: ${ids[id]['copyright']}
+Date of publication: ${ids[id]['date']}
+Explanation: ${ids[id]['explanation']}
`; - title.innerHTML = `Author: ${data[id]['copyright']}
-Date of publication: ${data[id]['date']}
-Explanation: ${data[id]['explanation']}
+ +Author: ${ids[id]['copyright']}
+Date of publication: ${ids[id]['date']}
+Explanation: ${ids[id]['explanation']}
`; - title.innerHTML = `