-
Notifications
You must be signed in to change notification settings - Fork 7
/
commander-articles.js
354 lines (300 loc) · 10.3 KB
/
commander-articles.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Commander Articles Widget
* A widget for displaying the latest articles from your choice of https://edhrec.com/ or https://commandersherald.com/
*
* SUPPORTS
* This script supports small and medium widgets.
*
* SETTINGS
* You can change the three values bellow this comment.
* preferredWebsite: either `edhrec` or `commandersherald` for the perfered website to fetch articles from.
* themeName: the name of the colour theme used in the widget. It can be EDHREC, CommandersHerald, Dracula-Purple, Dracula-Pink, Dracula-Red, Dracula-Orange, Dracula-Green, Dracula-Yellow, Dracula-Cyan, or Dracula-Gray. You can also create your own theme and use it or edit current theme colours.
* following: an array of objects to highlight matching articles for the medium widget. A highlighted article will have a different background color than the widget. The inner objects can have two properties. `author` to highlight articles from a certain author and `titlePattern` for a regex value that highlights articles where the title matches the pattern. One or both properties can be used at a time. If an object is empty, or has neither properties, all articles will be highlighted. The following is an example of the following array:
* [
* { author: "John Doe" },
* { titlePattern: /top 10/i },
* { author: "Jane Doe", titlePattern: /cedh/i },
* ]
*
* WIDGET PARAMETERS
* The widget parameter can be used to override the perfered website. The value can be `commandersherald` or `edhrec`. The parameter is useful when you want to have multiple widgets on your homescreen that each have articles from the different websites.
*/
const preferredWebsite = "edhrec";
const themeName = "EDHREC";
const following = [{ author: "John Doe", titlePattern: /top 10/i }];
const space = 10;
const draculaBase = {
font: "Menlo",
bg: new Color("#282a36"),
fg: new Color("#f8f8f2"),
muted: new Color("#6272A4"),
bgHighlight: new Color("#44475A"),
};
const themes = {
EDHREC: {
font: "",
bg: Color.dynamic(new Color("#f9fafb"), new Color("#121315")),
fg: Color.dynamic(new Color("#22262a"), new Color("#dfe3e7")),
accent: Color.dynamic(new Color("#1763b5"), new Color("#337ab7")),
accentInverse: Color.dynamic(new Color("#f9fafb"), new Color("#121315")),
muted: Color.dynamic(new Color("#999999"), new Color("#999999")),
bgHighlight: Color.dynamic(new Color("#ebeced"), new Color("#22262a")),
},
CommandersHerald: {
font: "",
bg: Color.dynamic(new Color("#fff"), new Color("#343a40")),
fg: Color.dynamic(new Color("#212529"), new Color("#f1f1f1")),
accent: Color.dynamic(new Color("#20c997"), new Color("#20c997")),
accentInverse: Color.dynamic(new Color("#fff"), new Color("#fff")),
muted: Color.dynamic(new Color("#6c757d"), new Color("#8a959e")),
bgHighlight: Color.dynamic(new Color("#ebeced"), new Color("#3d444d")),
},
"Dracula-Purple": {
...draculaBase,
accent: new Color("#bd93f9"),
accentInverse: new Color("#f8f8f2"),
},
"Dracula-Pink": {
...draculaBase,
accent: new Color("#ff79c6"),
accentInverse: new Color("#f8f8f2"),
},
"Dracula-Red": {
...draculaBase,
accent: new Color("#ff5555"),
accentInverse: new Color("#f8f8f2"),
},
"Dracula-Orange": {
...draculaBase,
accent: new Color("#ffb86c"),
accentInverse: new Color("#282a36"),
},
"Dracula-Green": {
...draculaBase,
accent: new Color("#50fa7b"),
accentInverse: new Color("#282a36"),
},
"Dracula-Yellow": {
...draculaBase,
accent: new Color("#f1fa8c"),
accentInverse: new Color("#282a36"),
},
"Dracula-Cyan": {
...draculaBase,
accent: new Color("#8be9fd"),
accentInverse: new Color("#282a36"),
},
"Dracula-Gray": {
...draculaBase,
accent: new Color("#6272a4"),
accentInverse: new Color("#f8f8f2"),
},
};
const theme = themes[themeName] || themes["EDHREC"];
let website = "https://edhrec.com/articles/feed/?cat=4";
if (
/commander'?s?herald/i.test(args.widgetParameter) ||
(preferredWebsite === "commandersherald" &&
!/edhrec/i.test(args.widgetParameter))
) {
website = "https://commandersherald.com/rss";
}
const xml = await new Request(website).loadString();
const data = parseXML(xml).rss.channel.item;
const widget = new ListWidget();
let date = new Date();
date.setHours(date.getHours() + 1);
widget.refreshAfterDate = date;
widget.url =
website === "https://commandersherald.com/rss"
? "https://commandersherald.com"
: "https://edhrec.com/";
widget.backgroundColor = theme.bg;
if (config.widgetFamily === "small") {
const bottomBarSize = 12;
widget.setPadding(0, 0, 0, 0);
widget.url = data[0].link;
const topBar = widget.addStack();
topBar.setPadding(space / 2, space, space / 2, space);
topBar.backgroundColor = theme.accent;
topBar.layoutHorizontally();
topBar.addSpacer();
const titleText = topBar.addText(data[0].title);
titleText.centerAlignText();
titleText.textColor = theme.accentInverse;
titleText.lineLimit = 2;
titleText.font = new Font(theme.font, 12);
titleText.minimumScaleFactor = 0.8;
topBar.addSpacer();
widget.addSpacer();
const middle = widget.addStack();
middle.setPadding(space / 2, space, space / 2, space);
const body = middle.addText(data[0].description.match(/<p>(.+?)<\/p>/)[1]);
body.centerAlignText();
body.font = new Font(theme.font, 14);
body.minimumScaleFactor = 0.6;
body.textColor = theme.fg;
widget.addSpacer();
const bottomBar = widget.addStack();
bottomBar.setPadding(0, space, space / 2, space);
bottomBar.layoutHorizontally();
bottomBar.centerAlignContent();
bottomBar.addSpacer();
const categoryIcon = bottomBar.addText("#");
categoryIcon.font = Font.boldSystemFont(bottomBarSize);
categoryIcon.textColor = theme.accent;
bottomBar.addSpacer(space / 2);
const categoryText = bottomBar.addText(Array.isArray(data[0].category) ? data[0].category[0] : data[0].category);
categoryText.lineLimit = 1;
categoryText.font = new Font(theme.font, bottomBarSize);
categoryText.minimumScaleFactor = 0.8;
categoryText.textColor = theme.fg;
bottomBar.addSpacer();
widget.presentSmall();
} else if (config.widgetFamily === "medium" || true) {
const bottomBarSize = 12;
const titleText = widget.addText(
website === "https://commandersherald.com/rss"
? "Commander's Herald"
: "EDHREC",
);
titleText.lineLimit = 1;
titleText.font = new Font(theme.font, 10);
titleText.minimumScaleFactor = 0.8;
titleText.textColor = theme.muted;
addDivider(widget);
for (let i = 0; i < 3; i++) {
const row = widget.addStack();
row.url = data[i].link;
row.layoutVertically();
row.setPadding(space / 3, space / 3, space / 3, space / 3);
row.cornerRadius = 5;
if (doesFollow(data[i])) {
row.backgroundColor = theme.bgHighlight;
}
const titleText = row.addText(data[i].title);
titleText.textColor = theme.fg;
titleText.lineLimit = 1;
titleText.font = new Font(theme.font, 12);
titleText.minimumScaleFactor = 0.8;
row.addSpacer(space / 4);
const bottomBar = row.addStack();
bottomBar.centerAlignContent();
bottomBar.layoutHorizontally();
const categoryIcon = bottomBar.addText("#");
categoryIcon.font = Font.boldSystemFont(bottomBarSize);
categoryIcon.textColor = theme.accent;
bottomBar.addSpacer(space / 2);
const categoryText = bottomBar.addText(Array.isArray(data[i].category) ? data[i].category[0] : data[i].category);
categoryText.lineLimit = 1;
categoryText.font = new Font(theme.font, bottomBarSize);
categoryText.minimumScaleFactor = 0.8;
categoryText.textColor = theme.muted;
bottomBar.addSpacer();
const date = bottomBar.addDate(new Date(data[i].pubDate));
date.textColor = theme.muted;
date.applyRelativeStyle();
date.font = new Font(theme.font, bottomBarSize);
date.minimumScaleFactor = 0.8;
if (i !== 2) {
addDivider(widget);
}
}
widget.presentMedium();
}
Script.setWidget(widget);
Script.complete();
function doesFollow(data) {
for (const pattern of following) {
if (pattern.author && data["dc:creator"] !== pattern.author) {
continue;
}
if (pattern.titlePattern && !pattern.titlePattern.test(data.title)) {
continue;
}
return true;
}
return false;
}
function addDivider(stack) {
stack.addSpacer(space / 4);
const bar = stack.addStack();
bar.backgroundColor = theme.accent;
bar.cornerRadius = 2;
bar.addSpacer();
const barHeightener = bar.addImage(
Image.fromData(
Data.fromBase64String(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
),
),
);
barHeightener.imageSize = new Size(1, 1);
bar.addSpacer();
stack.addSpacer(space / 4);
}
// See https://github.com/Normal-Tangerine8609/Scriptable-Widgets/blob/main/scripts/rss-parser.js
function parseXML(string) {
let main = {
isRoot: true,
name: "root",
children: [],
};
let target = main;
let goBack = {};
let parser = new XMLParser(string);
parser.didStartElement = (name, attrs) => {
let backTo = Symbol();
goBack[backTo] = target;
let newTarget = {
name,
attrs,
innerText: "",
children: [],
end: backTo,
};
target.children.push(newTarget);
target = newTarget;
};
parser.didEndElement = (name) => {
let sym = target.end;
delete target.end;
target = goBack[sym];
};
parser.foundCharacters = (text) => {
target.innerText +=
target.innerText === "" ? text.trim() : " " + text.trim();
};
parser.parseErrorOccurred = () => {
console.warn(
"A parse error occurred, ensure the document is formatted properly.",
);
};
parser.parse();
if (!main.isRoot) {
console.warn(
"A parse error occurred, ensure the document is formatted properly.",
);
}
delete main.isRoot;
return traverse(main);
function traverse(node) {
let newNode = {};
for (let child of node.children) {
let newChild = traverse(child);
if (child.children.length === 0) {
newChild = child.innerText;
}
if (newNode[child.name]) {
if (Array.isArray(newNode[child.name])) {
newNode[child.name].push(newChild);
} else {
newNode[child.name] = [newNode[child.name], newChild];
}
} else {
newNode[child.name] = newChild;
}
}
return newNode;
}
}