-
Notifications
You must be signed in to change notification settings - Fork 0
/
t3n-news.js
101 lines (79 loc) · 2.6 KB
/
t3n-news.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
92
93
94
95
96
97
98
99
100
101
const widget = await createWidget();
if (!config.runsInWidget) {
widget.presentSmall();
}
Script.setWidget(widget);
Script.complete();
async function fetchData() {
let req = new Request("https://api.t3n.de");
req.method = "POST";
req.headers = {
"Content-Type": "application/json",
};
req.body = JSON.stringify({
operationName: "ScriptableLatestNews",
variables: {},
query:
"query ScriptableLatestNews {\n article {\n recentNews(limit: 4) {\n identifier\n title\n url\n imageUrl(width: 500, height: 500)\n }\n }\n}\n",
});
const { data } = await req.loadJSON();
const news = data.article.recentNews;
return news;
}
async function loadImage(url) {
const req = new Request(url);
const img = await req.loadImage();
return img;
}
async function createWidget() {
const widget = new ListWidget();
widget.backgroundColor = new Color("#f9423a");
try {
const logoImage = await loadImage(
"https://d1quwwdmdfumn6.cloudfront.net/t3n/2018/images/logos/t3n_logo_420x420.png"
);
const logoSize = config.widgetFamily === "large" ? 60 : 40;
const logo = widget.addImage(logoImage);
logo.imageSize = new Size(logoSize, logoSize);
const data = await fetchData();
const images = await Promise.all(
data.map((item) => loadImage(item.imageUrl))
);
data
.slice(0, config.widgetFamily === "large" ? undefined : 1)
.map(async (item, i) => {
widget.addSpacer();
const { title, url } = item;
if (i === 0) {
const image = images[i];
widget.backgroundImage = image;
const gradient = new LinearGradient();
gradient.locations = [0, 1];
gradient.colors = [
new Color("#f9423a", 0.8),
new Color("#f9423a", 1),
];
widget.backgroundGradient = gradient;
if (config.widgetFamily !== "large") widget.url = url;
}
const itemText = widget.addText(title);
itemText.font = Font.boldSystemFont(
config.widgetFamily !== "small" && i === 0 ? 18 : 14
);
itemText.textColor = Color.white();
if (config.widgetFamily === "large") itemText.url = url;
});
if (config.widgetFamily === "large") widget.url = "https://t3n.de";
widget.addSpacer();
return widget;
} catch (err) {
widget.addSpacer();
const errorText = widget.addText(
"Leider gab es ein Problem beim Darstellen des Widgets"
);
errorText.font = Font.boldSystemFont(14);
errorText.textColor = Color.white();
widget.addSpacer();
return widget;
}
}