-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotion quotes.js
203 lines (167 loc) · 5.76 KB
/
notion quotes.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
//////////////////////////////////////////////////////////////////////
//Notion settings
const databaseID = `YOUR_DATABASE_ID`;
const token = `YOUR_SECRET_TOKEN`;
const notionVersion = '2022-02-22';
const notionApi = `https://api.notion.com/v1/databases/${databaseID}`;
//Notion column names
const quoteCol = 'Quote';
const createdDateCol = 'Created Date';
const ratingCol = 'Rating';
const authorCol = 'Author';
//////////////////////////////////////////////////////////////////////
//read random quote from Notion page
let quote = await readNotionQuote();
let text = extractPlainText(quote.text);
let rating = quote.rating;
let clickUrl = quote.url;
console.log(text);
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Runs inside a widget so add it to the homescreen widget
Script.setWidget(widget);
} else {
// Show the medium widget inside the app
widget.presentMedium();
}
Script.complete();
//////////////////////////////////////////////////////////////////////
async function createWidget() {
// Create new empty ListWidget instance
let listwidget = new ListWidget();
// Set new background color
listwidget.backgroundColor = new Color("#1C1C1E");
let nextRefresh = Date.now() + 1000 * 60 * 120; // add 2 hours
var nextDate = new Date(Date.now());
let hour = nextDate.getHours();
console.log('Hour: ' + hour);
//don't refresh during the night, next day is better
if (hour < 7 || 21 < hour) {
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(8);
tomorrow.setMinutes(0);
tomorrow.setMilliseconds(0);
nextRefresh = tomorrow.getTime();
console.log('Next day refresh');
}
console.log('Next refresh: ' + new Date(nextRefresh));
listwidget.refreshAfterDate = new Date(nextRefresh);
listwidget.spacing = 6;
let linkStack = listwidget.addStack();
linkStack.centerAlignContent();
linkStack.url = clickUrl;
let heading = linkStack.addText(text);
// Add widget heading
heading.centerAlignText();
heading.font = Font.lightSystemFont(15);
heading.textColor = new Color("#ffffff");
//add rating
let ratingStr = '';
for (let i = 0; i < rating; i++) {
ratingStr = ratingStr + '★';
}
let ratingGui = listwidget.addText(ratingStr);
ratingGui.centerAlignText();
ratingGui.font = Font.lightSystemFont(5);
ratingGui.textColor = new Color("#ffff00");
quote.author.trim();
if (quote.author.length > 0){
let authorGui = listwidget.addText(quote.author);
authorGui.centerAlignText();
authorGui.font = Font.italicSystemFont(10);
authorGui.textColor = new Color("#aaaaaa");
}
// Return the created widget
return listwidget;
}
function addDateText(stack, text) {
let dateText = stack.addText(text);
dateText.centerAlignText();
dateText.font = Font.semiboldSystemFont(20);
dateText.textColor = new Color("#ffffff");
}
//read qote from notion page
async function readNotionQuote() {
let reqNotion = new Request(notionApi + '/query');
reqNotion.method = 'post';
reqNotion.headers = {
"Authorization": `Bearer ${token}`,
"Notion-Version": notionVersion
};
let resNotion = await reqNotion.loadJSON();
let quotesObjs = convertNotionQuotes(resNotion);
if (quotesObjs.length == 0)
return null;
//load previous random numbers
let fmCloud = FileManager.iCloud(); // iCloud
let docFile = fmCloud.documentsDirectory() + '/quoteLastElems.txt';
let lastUsedIndexes = [];
if (fmCloud.fileExists(docFile)) {
let randomTexts = fmCloud.readString(docFile);
randomTexts = randomTexts.trim();
let numberTexts = randomTexts.split(' ');
numberTexts.forEach(n => lastUsedIndexes.push(Number(n)));
console.log('Previous random numbers: ' + lastUsedIndexes);
}
let maxRand = 0;
quotesObjs.forEach(qo => maxRand += qo.rating);
console.log('Max rand:' + maxRand);
//find "free" random number
let useIndex = Math.floor(Math.random() * quotesObjs.length);
while (lastUsedIndexes.length < quotesObjs.length) {
let randomNumber = Math.floor(Math.random() * maxRand);
useIndex = convertRandomToQuoteIndex(randomNumber, maxRand, quotesObjs);
if (!lastUsedIndexes.includes(useIndex))
break;
}
lastUsedIndexes.push(useIndex);
while (lastUsedIndexes.length > 20)
lastUsedIndexes.shift();
let lastUsedIndexesText = '';
lastUsedIndexes.forEach(lui => lastUsedIndexesText += lui.toString() + ' ');
fmCloud.writeString(docFile, lastUsedIndexesText);
console.log('Using ' + useIndex + ': ' + quotesObjs[useIndex]);
return quotesObjs[useIndex];
}
function convertRandomToQuoteIndex(randNum, maxRand, quotesObjs){
let useIndex = 0;
let currentTotal = 0;
while (randNum < maxRand) {
if (useIndex >= quotesObjs.length - 1)
break;
let currRating = quotesObjs[useIndex].rating;
if (currentTotal + currRating > randNum)
break;
useIndex++;
currentTotal += currRating;
}
return useIndex;
}
function convertNotionQuotes(jsonNotion) {
let quoteObjects = [];
jsonNotion.results.forEach(element => {
let newQuoteObj = {};
let props = element['properties'];
let text = props[quoteCol]['rich_text'];
if (text.length == 0)
return;
newQuoteObj.text = text;
let createdDate = props[createdDateCol]['created_time'];
newQuoteObj.date = new Date(createdDate);
newQuoteObj.rating = props[ratingCol]['select']['name'].length / 2;
newQuoteObj.url = element.url;
newQuoteObj.author = extractPlainText(props[authorCol]['title']);
quoteObjects.push(newQuoteObj);
});
quoteObjects.sort((a, b) => (a.date > b.date) ? 1 : -1);
return quoteObjects;
}
function extractPlainText(texts) {
if (texts.length == 0)
return "";
let finalText = "";
texts.forEach(t => finalText += t.plain_text);
return finalText;
}