-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext Rocket Launch.js
90 lines (76 loc) · 2.8 KB
/
Next Rocket Launch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-blue; icon-glyph: space-shuttle;
// Function to get data from API
async function getData() {
const url = "https://lldev.thespacedevs.com/2.2.0/launch/upcoming";
const newRequest = await new Request(url);
const response = await newRequest.loadJSON();
return response;
};
// Define variables from API response
const data = await getData();
// Get background image
let image = await new Request("https://media.wired.com/photos/5bbadf1c40061e2cf09198a9/master/w_2240,c_limit/SpaceX-1047301226a.jpg").loadImage();
// Get launch details
var launchDetails = data.results;
// Get mission name
var missionDetails = launchDetails["0"]["mission"];
var missionName = missionDetails.name;
// Get type of rocket
var rocketDetails = launchDetails["0"]["rocket"];
var rocketType = rocketDetails.configuration.name;
// Get launch time
var launchTimestamp = launchDetails["0"]["net"];
var launchDate = new Date(launchTimestamp);
var formatDate = {
month: "numeric",
day: "numeric",
year: "2-digit"
};
var formatTime = {
hour: "numeric",
minute: "numeric",
hour12: false
};
var dateString = Intl.DateTimeFormat("en", formatDate).format(launchDate);
var timeString = Intl.DateTimeFormat("en", formatTime).format(launchDate);
var launchDateTime = dateString + " at " + timeString;
// Function to create and customize widget UI
function createWidget(data) {
const widget = new ListWidget();
// Set background image to widget
widget.backgroundImage = image;
// Add header displaying mission name
const title = widget.addStack();
let mission = title.addText(missionName);
mission.textColor = Color.white();
mission.font = Font.semiboldSystemFont(14);
mission.centerAlignText();
// Add footer displaying rocket type
const footer1 = widget.addStack();
footer1.layoutVertically();
footer1.addSpacer();
let launchRocket = footer1.addText(rocketType);
launchRocket.textColor = Color.white();
launchRocket.font = Font.semiboldSystemFont(12);
// Add footer displaying launch date and time
const footer2 = widget.addStack();
footer2.layoutVertically();
let launchCountdown = footer2.addText(launchDateTime);
launchCountdown.textColor = Color.white();
launchCountdown.font = Font.semiboldSystemFont(12);
// Return customized widget UI
return widget;
};
// Display widget
let widget = createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Run inside a widget when added to the home screen
Script.setWidget(widget);
} else {
// Otherwise show the small widget preview inside the Scriptable app
widget.presentSmall();
}
Script.complete();