Created
December 22, 2020 04:47
-
-
Save PtruckStar/9469bd907ee3664b346fb5009b729b6d to your computer and use it in GitHub Desktop.
fetch sunrise sunset in scriptable app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: orange; icon-glyph: magic; | |
// made by @morinagapltynm | |
async function getSunData() { | |
let sunData = {} | |
for(s = 0; s < 2; s++){ | |
let date = today | |
if(s>0)date.setDate(date.getDate()+1); | |
let req = new Request("https://api.sunrise-sunset.org/json?lat="+LAT+"&lng="+LON+"&formatted=0&date="+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()).loadJSON() | |
let raw = await req | |
if(s==0) { | |
sunData.sunrise = raw.results.sunrise | |
sunData.sunset = raw.results.sunset | |
} else { | |
sunData.tomorrow = raw.results.sunrise | |
} | |
} | |
// what you got | |
console.log(sunData) | |
return sunData | |
} | |
// usage | |
var today = new Date() | |
var LAT = //latitude | |
var LON = //longitude | |
var sunData = await getSunData() | |
var sunrise = sunData.sunrise | |
var sunset = sunData.sunset | |
var tomorrow = sunData.tomorrow | |
const sun = (today > sunrise && dates < sunset) ? sunset : (today > sunset) ? tomorrow : sunrise | |
console.log(sun) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment