-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_quote-from-ZenQuotes-API.js
91 lines (73 loc) · 2.28 KB
/
widget_quote-from-ZenQuotes-API.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
91
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: gray; icon-glyph: smile-wink;
let widget = new ListWidget();
widget.backgroundColor = Color.black();
widget.useDefaultPadding();
const cacheKey = "cachedQuote";
const cacheDurationMinutes = 480;
// Load cache
let cachedData = Keychain.contains(cacheKey) ? JSON.parse(Keychain.get(cacheKey)) : null;
let quote = null;
if (await isConnectedToInternet()) {
if (isCacheUsable(cachedData)) {
// Use cached quote if still valid
quote = cachedData.quote;
} else {
// Fetch new quote if online and cache is invalid
quote = await fetchQuote();
// Cache the new quote and expiry date
Keychain.set(cacheKey, JSON.stringify({
quote: quote,
expiry: new Date(Date.now() + cacheDurationMinutes * 60 * 1000).toISOString()
}));
}
} else {
// Use cached quote if offline
quote = cachedData.quote;
}
let q = widget.addText(quote.q);
q.centerAlignText();
q.textColor = Color.white();
// http://iosfonts.com
q.font = new Font("IowanOldStyle-BoldItalic", 18);
q.minimumScaleFactor = 0.1;
q.textOpacity = 1;
widget.addSpacer(15);
let a = widget.addText(quote.a);
a.centerAlignText();
a.textColor = Color.white();
// http://iosfonts.com
a.font = new Font("Avenir Next", 12);
a.minimumScaleFactor = 0.1;
a.textOpacity = 0.8;
widget.url = `shortcuts://run-shortcut?` +
`name=${encodeURI("📥 Add to Inbox")}&` +
`input=${encodeURI(`"${quote.q.trim()}" — ${quote.a.trim()}`)}`;
config.runsInWidget ? Script.setWidget(widget) : widget.presentMedium();
Script.complete();
// ================
// Helper functions
// ================
// ZenQuotes API: https://zenquotes.io
async function fetchQuote() {
const response = await new Request("https://zenquotes.io/api/random").loadJSON();
return {
q: response[0].q,
a: response[0].a
};
}
function isCacheValid(expiry) {
return expiry.getTime() > new Date().getTime();
}
function isCacheUsable(cachedData) {
return cachedData && isCacheValid(new Date(cachedData.expiry));
}
async function isConnectedToInternet() {
try {
await new Request("https://www.google.com").load();
return true;
} catch {
return false;
}
}