Last active
March 14, 2021 08:47
-
-
Save kenshinji/31aadcc4f47d505ce5c0496f88aa1120 to your computer and use it in GitHub Desktop.
Scriptable script for getting sunset/sunrise & moonset/moonrise time for your location
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; | |
// Script for getting sunset/sunrise & moonset/moonrise time for your location. | |
const w = new ListWidget() | |
w.backgroundColor = new Color("#222222") | |
// apiKey from https://ipgeolocation.io/ | |
const apiKey = '' | |
// first we need to get current location | |
let {latitude, longitude} = await Location.current() | |
latitude = latitude.toFixed(4) | |
longitude = longitude.toFixed(4) | |
// then we call the sunset/sunrise API | |
const resp = await get({url:`https://api.ipgeolocation.io/astronomy?apiKey=${apiKey}&lat=${latitude}&long=${longitude}`}) | |
const {sunrise, sunset, moonrise, moonset} = resp | |
// display in the UI | |
// sunrise time | |
const stack1 =w.addStack() | |
stack1.centerAlignContent() | |
const sunriseLabel = stack1.addText('sunrise') | |
sunriseLabel.textColor = Color.white() | |
stack1.addSpacer(5) | |
const sunriseTime = stack1.addText(sunrise) | |
sunriseTime.textColor = Color.white() | |
stack1.addSpacer(5) | |
// sunset time | |
const stack2 =w.addStack() | |
stack2.centerAlignContent() | |
const sunsetLabel = stack2.addText('sunset') | |
sunsetLabel.textColor = Color.white() | |
stack2.addSpacer(5) | |
const sunsetTime = stack2.addText(sunset) | |
sunsetTime.textColor = Color.white() | |
stack2.addSpacer(5) | |
// moonrise time | |
const stack3 =w.addStack() | |
stack3.centerAlignContent() | |
const moonriseLabel = stack3.addText('moonrise') | |
moonriseLabel.textColor = Color.white() | |
stack3.addSpacer(5) | |
const moonriseTime = stack3.addText(moonrise) | |
moonrise.textColor = Color.white() | |
stack3.addSpacer(5) | |
// moonset time | |
const stack4 =w.addStack() | |
stack4.centerAlignContent() | |
const moonsetLabel = stack4.addText('moonset') | |
moonsetLabel.textColor = Color.white() | |
stack4.addSpacer(5) | |
const moonsetTime = stack4.addText(moonset) | |
moonsetTime.textColor = Color.white() | |
stack4.addSpacer(5) | |
Script.setWidget(w) | |
Script.complete() | |
w.presentSmall() | |
async function get(opts) { | |
const request = new Request(opts.url) | |
request.headers = { | |
...opts.headers, | |
...this.defaultHeaders | |
} | |
var result=await request.loadJSON() | |
return result | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment