Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scriptable - iOS 14 widget to display Astronomy Picture Of the Day (APOD)
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: image;
// Author: Sylvain Gamel - https://Automatisez.net/
// Platform: iOS 14
// Application: Scriptable app
// ## Configuration
//
// API og APOD
//
let apiURL = "https://api.nasa.gov/planetary/apod";
let apiKey = "DEMO_KEY";
if ( Keychain.contains('nasa-api-key') ) {
apiKey = Keychain.get('nasa-api-key');
}
// Public URL
let apodNavURL = "https://apod.nasa.gov";
// ## Functions
// Basic widget creation
//
function createWidget() {
let widget = new ListWidget();
widget.spacing = 0;
// Avoid too frequest update, wait at least 6 hours
let now = Date.now();
let delay = 6 * 60 * 60 * 1000;
let nextUpdate = new Date(now + delay);
console.log("Next update: " + nextUpdate.toLocaleDateString() +
" " + nextUpdate.toLocaleTimeString());
widget.refreshAfterDate = nextUpdate;
return widget;
}
// Access APOD API to get image
// then update widget
//
async function loadPhoto(widget) {
let requestURI = `${apiURL}?api_key=${apiKey}`;
let request = new Request(requestURI);
console.log(`Request: ${requestURI}`);
let json = await request.loadJSON();
console.log(`Response: ${JSON.stringify(json)}`);
if ( json && json.url ) {
if ( json.media_type == 'image' ) {
let imgRequest = new Request(json.url);
let img = await imgRequest.loadImage();
widget.backgroundImage = img;
}
else if ( json.media_type == 'video' && json.url.match(/^https?:\/\/(www.)?youtube.com/) ) {
let ytID = json.url.match(/\/embed\/([^)]+)\?.*/, "/vi/$1/0.jpg")
let imgURL = `https://img.youtube.com/vi/${ ytID[1] }/0.jpg`;
console.log(imgURL);
let imgRequest = new Request(imgURL);
let img = await imgRequest.loadImage();
widget.backgroundImage = img;
let font = Font.blackRoundedSystemFont(10);
let text = widget.addText("Vidéo");
text.rightAlignText();
text.font = font;
text.textColor = Color.white();
widget.addSpacer(null);
}
else {
let font = Font.blackRoundedSystemFont(22);
let text = widget.addText("Pas d'image astro aujourd'hui c'est "+json.media_type);
text.centerAlignText();
text.font = font;
text.textColor = Color.white();
let bg = new LinearGradient();
bg.colors = [
new Color("#6666ff", 1.0),
new Color("#000033", 1.0)
];
bg.locations = [0, 1];
widget.backgroundGradient = bg;
}
widget.url = apodNavURL;
} else if ( json && json.error && json.error.message ) {
widget.addText(json.error.message);
} else {
widget.addText("Erreur. Pas de réponse.")
}
}
// -----
// create widget, load image and push widget
let widget = createWidget();
await loadPhoto(widget);
// check environment to just display widget content
// when running from Scriptable app
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentSmall();
}
// We must notify caller that script ended
Script.complete();
@automatisez

This comment has been minimized.

Copy link
Owner Author

@automatisez automatisez commented Sep 30, 2020

Scriptable widget - APOD

What is this?

This small Scriptable example script is used to create an iOS 14 widget using the APOD (Astronomy Picture Of the Day) JSON API.

In latest version it will properly display thumb for video media, as bellow:

widget-apod-small

Learn more

You may read my article, in french, on how this widget is built:
http://automatisez.net/ios/2020/09/24/image-astronomique-du-jour-en-widget.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.