-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
634 lines (573 loc) · 22.2 KB
/
index.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
fs = require('fs');
const { Client } = require("@notionhq/client");
const { makeConsoleLogger } = require('@notionhq/client/build/src/logging');
const { exit } = require('process');
const { start } = require('repl');
const verbose = true;
const onlyDebug = true;
// all options are optional
const bgg_options = {
timeout: 10000, // timeout of 10s (5s is the default)
// see https://github.com/cujojs/rest/blob/master/docs/interceptors.md#module-rest/interceptor/retry
retry: {
initial: 100,
multiplier: 2,
max: 15e3
}
}
const bgg = require('bgg')(bgg_options);
const secret_token = process.env.NOTION_TOKEN ? process.env.NOTION_TOKEN : fs.readFileSync('.config/notion_token');
const database_id = process.env.DATABASE_ID ? process.env.DATABASE_ID : fs.readFileSync('.config/database_id');
// Initializing a client
const notion = new Client({
auth: secret_token,
});
async function bgg_searchGame(queryText, exact=false) {
// Apparently BGG api can't handle ":" even if encoded as "%3A"
const qs = queryText.replace(":", " ");
let res = await bgg('search', {'query': qs, 'exact': 1, 'type': 'boardgame,boardgameexpansion,boardgameaccessory'});
if (exact) return res;
if (res.items && res.items.total && res.items.total > 0) return res;
res = await bgg('search', {'query': qs, 'exact': 0, 'type': 'boardgame,boardgameexpansion,boardgameaccessory'});
// Remove weird duplicate results
if (Array.isArray(res.items.item)) {
final = [];
ids = [];
for (const entry of res.items.item) {
if (ids.includes(entry.id)) continue;
final.push(entry);
ids.push(entry.id);
}
res.total = final.length;
if (final.length == 1) {
res.items.item = final.pop();
} else {
res.items.item = final;
}
}
return res;
}
// Convert and append bgg data into a more friendly-to-use internal format
function bgg_processItem(item) {
if (Array.isArray(item.name)) {
item.primaryName = item.name.filter(n => n.type === "primary").map(n=>n.value).join(" / ");
} else {
item.primaryName = item.name.value;
}
const flatten_properties = ["yearpublished", "minplayers", "maxplayers", "playingtime", "minplaytime","maxplaytime","minage"];
for (const prop of flatten_properties) {
if (item[prop] && item[prop].value !== undefined) {
item[prop] = item[prop].value;
}
}
item.details = {};
for (const linkItem of item.link) {
if (!item.details[linkItem.type]) item.details[linkItem.type] = [];
item.details[linkItem.type].push(linkItem.value);
}
item.constructed_bgg_url = 'https://www.boardgamegeek.com/boardgame/'+item.id;
if (item.image) {
item.image = item.image.replace('(','(').replace(')',')');
}
console.log(item.defals);
return item;
/* Returning object:
{
"type": "boardgame",
"id": 205398,
"thumbnail": "https://...",
"image": "https://...",
"name": [ { "type": "primary", "sortindex": 1, "value": "Citadels" }, { "type": "alternate", "sortindex": 1, "value": "Citadela DeLuxe" } ],
"description": "In Citadels, players...",
"yearpublished": 2016,
"minplayers": 2,
"maxplayers": 8,
"poll": [...],
"playingtime": 60,
"minplaytime": 30,
"maxplaytime": 60,
"minage": 10,
"link": [...],
"primaryName": "Citadels",
"constructed_bgg_url": "https://...",
"details": {
"boardgamecategory": [...],
"boardgamemechanic": [...],
"boardgamefamily": [...],
"boardgameimplementation": [...],
"boardgamedesigner": [...],
"boardgameartist": [...],
"boardgamepublisher": [...],
}
} */
}
async function bgg_multiGameDetails(gameIds) {
res = await bgg('thing', {'id': gameIds.join(","), "versions": 0, "videos": 0, "stats": 0, "comments": 0, "ratingcomments": 0});
const nitems = [];
for (const item of res.items.item) {
nitems.push(bgg_processItem(item));
}
return nitems;
}
async function bgg_gameDetails(gameId) {
const res = await bgg('thing', {'id': gameId});
return bgg_processItem(res.items.item);
}
function groupString(items, max=1, placeholder="") {
if (!items || !Array.isArray(items)) return placeholder;
const c = items;
let str = items.slice(0, max).join(", ");
if (c > max) str = str+' + '+(publisherCount-3)+" more";
return str;
}
function bgg_detailString(det) {
const publisherString = groupString(det.details.boardgamepublisher, 3, "?");
const designerString = groupString(det.details.boardgamedesigner, 3, "?");
return "["+det.id+": "+det.primaryName+" ("+det.yearpublished+") "+det.minplayers+"-"+det.maxplayers+"ply "+det.playingtime+"min by "+designerString+" from "+publisherString+"]";
}
function dumpj(data) {
console.log(JSON.stringify(data,null,2));
}
function notion_item_debug(notionData) {
return notionData.properties["DEBUG_THIS"] && notionData.properties["DEBUG_THIS"].checkbox && notionData.properties["DEBUG_THIS"].checkbox == true;
}
class NotionItem {
get data() {
return this.itemData;
}
constructor(itemData) {
this.itemData = itemData;
this.associations = {};
}
setContainer(container) {
this.container = container;
}
associate(notionKey, accessInterface, interfaceParameters, notionType) {
this.associations[notionKey] = { accessInterface, interfaceParameters };
this.notionDataType = notionType; // this would be better to have interpreted from original data some way
}
hasData(notionKey) {
const props = this.itemData.properties[notionKey];
if (!props) return false; // if empty, will not exist
if (this.notionType) {
if (props[this.notionType]) {
if (this.notionType == 'number' && props.number !== 0) return true; // count 0 as missing value(!)
if (this.notionType == 'url' && props.url !== "") return true;
if (this.notionType == 'rich_text' && props.rich_text.length > 0) return true;
}
} else { // Type not set, just check each
if (props.number && props.number !== 0) return true;
if (props.url && props.url !== "") return true;
if (props.rich_text && props.rich_text.length > 0) return true;
}
}
async sync(forceSync=false) {
if (this.complete && !forceSync) return;
for (const [notionKey, assoc] of Object.entries(this.associations)) {
if (this.hasData(notionKey)) continue; // Skip if already has data
try {
newData = await assoc.accessInterface.fetch({ key: assoc.interfaceParameters, bgg_id: this.bgg_id });
} catch (err) {
if (err instanceof DataUnavailableError) {
console.log("Note: failed to find data for",notionKey);
} else {
throw err;
}
}
}
}
doDebug() {
if (this.debug) {
console.log("___ DEBUG FOR:", this.name, this.bgg_id, this.id)
//dumpj(queueItem.notion_data);
dumpj(this.data.properties["Display name"]);
//dumpj(this.data.properties["Expansion for"]);
dumpj(this.data.properties["Expanded by"]);
dumpj(this.data.properties["Minor expansion list"]);
//dumpj(this.data.properties["Root game name"]);
}
}
hasName() {
const gameTitleObject = this.itemData.properties["Name"].title;
if (gameTitleObject.length === 0) return false;
const name = this.name;
if (name == '' || name == 'undefined') return false;
return true;
}
get complete() {
return this.itemData.properties["data_complete"] && this.itemData.properties["data_complete"].checkbox;
}
get lautapeliopas_url() {
const propertyData = this.itemData.properties["Lautapeliopas"];
if (!propertyData) return false;
return propertyData.url;
}
get debug() {
return notion_item_debug(this.itemData);
}
get category() {
const propertyData = this.itemData.properties["Game category"];
if (!propertyData) return "default";
return propertyData.select.name;
}
get alternateNames() {
const propertyData = this.itemData.properties["Alternate names"];
if (!propertyData) return null;
return propertyData.rich_text.map(i => i.plain_text).join("");
}
get displayName() {
const propertyData = this.itemData.properties["Display name"];
if (!propertyData) return null;
return propertyData.formula.string;
}
get name() {
const gameTitleObject = this.itemData.properties["Name"].title;
if (gameTitleObject.length > 0) {
return gameTitleObject[0].plain_text;
} else {
return "";
}
}
isExpansion() {
const propertyData = this.itemData.properties["Expansion for"];
if (propertyData.relation.length > 0) return true;
return false;
}
isMinorExpansion() {
const propertyData = this.itemData.properties["Is minor expansion"];
return propertyData.checkbox;
}
hasExpansions() {
const propertyData = this.itemData.properties["Expanded by"];
if (propertyData.relation.length > 0) return true;
return false;
}
get expansions() {
const propertyData = this.itemData.properties["Expanded by"];
if (propertyData.relation.length == 0) return [];
const items = propertyData.relation.map(i => this.container.getItem(i.id));
return items;
}
get rootItem() {
const propertyData = this.itemData.properties["Expansion for"];
if (propertyData.relation.length == 0) return null;
const rootGameId = propertyData.relation[0].id;
console.log("root game id:", rootGameId);
const ri = this.container.getItem(rootGameId);
console.log("root game:", ri);
return ri;
}
get minorExpansionList() {
const propertyData = this.itemData.properties["Minor expansion list"];
//if (propertyData.rollup.array.length == 0) return null;
return propertyData.rollup.array.map(i => i.formula['string']).filter(i => i !== "");
}
get expansionRootName() {
const propertyData = this.itemData.properties["Root game name"];
if (propertyData.rollup.array.length == 0) return null;
return propertyData.rollup.array[0].title[0].plain_text;
//return this.rootItem ? this.rootItem.name : null;
}
get queryTitle() {
if (this.isExpansion() && !this.name.includes(this.expansionRootName)) {
const rootname = this.expansionRootName;
return rootname ? rootname+": "+this.name : this.name;
} else {
return this.name;
}
}
get bgg_id() {
const propertyData = this.itemData.properties["bgg_id"];
if (!propertyData) return null;
return propertyData.number;
}
get id() {
return this.itemData.id;
}
}
class NotionContainer {
items = [];
include(item) {
const itemId = item.id;
item.container = this;
if (!this.hasItem(item.id)) {
this.items.push(item);
}
item.doDebug();
}
getItem(id) {
for (const item of this.items) {
if (item.id == id) return item;
}
}
hasItem(id) {
for (const item of this.items) {
if (item.id == id) return True;
}
return false;
}
sort(sortFunction) {
this.items.sort(sortFunction);
//noop
}
doDebug() {
for (const item of this.items) {
item.doDebug();
}
}
}
class DataUnavailableError extends Error {
constructor(message) {
super(message);
this.name = "DataUnavailableError";
}
}
class BGGInterface {
constructor() {
this.fetchedData = {};
}
async fetch(params) {
const bggKey = params.key;
// Fetch data or use cache
if (!this.fetchedData[bggKey]) this.fetchedData[bggKey] = await bgg_gameDetails(params.bgg_id);
const bggData = this.fetchedData[bggKey];
if (!bggData[bggKey] && bggData[bggKey] !== 0) throw new DataUnavailableError("Could not find data for "+bggKey);
return bggData[bggKey];
}
}
class LautapeliopasInterface {
async fetch() {
}
}
const notionItems = new NotionContainer();
async function doThings() {
currentTime = new Date();
if (verbose) { console.log("Accessing notion database "+database_id+"..."); }
const notionResult = await notion.databases.query({
database_id: database_id
});
if (verbose) { console.log("Notion database access complete."); }
// Collect items, and check for missing BGG links
detailsQueue = [];
if (notionResult.object == 'list') {
if (verbose) console.log("Count of items:", notionResult.results.length);
for (const gameItem of notionResult.results) {
if (onlyDebug && (!gameItem.properties || !gameItem.properties.DEBUG_THIS || !gameItem.properties.DEBUG_THIS.checkbox)) continue;
game = new NotionItem(gameItem);
notionItems.include(game);
const gameTitleObject = gameItem.properties["Name"].title;
if (game.hasName()) {
const gameTitle = game.name;
if (verbose) console.log("*** Processing game:", gameTitle);
if (gameItem.properties.bgg_id && gameItem.properties.bgg_id.number) {
const bggId = gameItem.properties.bgg_id.number;
if (verbose) console.log("Game "+gameTitle+" already has bgg_id:", bggId);
detailsQueue.push({ gameTitle: gameTitle, bgg_id: bggId, notion_id: gameItem.id, notion_data: gameItem });
continue;
}
if (!gameItem.properties.bgg_id || !gameItem.properties.bgg_id.number) {
if (gameItem.properties.bgg_potential_matches && gameItem.properties.bgg_potential_matches.rich_text.length > 0) {
if (verbose) console.log("Already has potential matches, skipping");
continue;
}
queryString = game.queryTitle;
if (verbose) console.log("Performing a BGG search for: "+queryString);
let bggResult = await bgg_searchGame(queryString);
if (Array.isArray(bggResult.items.item)) {
if (verbose) console.log("Found "+bggResult.items.total+" potential matches");
const potentialIds = bggResult.items.item.map(item => item.id);
const bggDetails = await bgg_multiGameDetails(potentialIds);
const potentialStrings = bggDetails.map(det => bgg_detailString(det));
let finalPotentialString = potentialStrings.join("\n");
if (finalPotentialString.length > 2000) finalPotentialString = finalPotentialString.slice(0,1997) + "...";
if (verbose) console.log("Updating potential matches to Notion...");
await notion.pages.update({
page_id: gameItem.id,
properties: {
bgg_potential_matches: { rich_text: [ { type: "text", text: { content: finalPotentialString } } ] },
//api_sync_notes: { rich_text: [ { type: "text", text: { content: "Synced. "+Math.floor(Math.random()*1000) } } ] },
api_latest_sync: { date: { start: currentTime.toISOString() } }
}
});
//console.log(bggResult);
//console.log(JSON.stringify(bggResult,null,2));
} else if (bggResult.items.total === 0) {
console.log("No matches found for "+gameTitle);
} else { // Single match
const composedName = bggResult.items.item.name.value + " ("+bggResult.items.item.yearpublished.value+")"
const bggId = bggResult.items.item.id;
if (verbose) console.log("Found single match:", composedName, "with id", bggId);
if (verbose) console.log("Updating bgg id to Notion...");
await notion.pages.update({
page_id: gameItem.id,
properties: {
bgg_id: { number: bggId },
//api_sync_notes: { rich_text: [ { type: "text", text: { content: "Synced. "+Math.floor(Math.random()*1000) } } ] },
api_latest_sync: { date: { start: currentTime.toISOString() } }
}
});
detailsQueue.push({ gameTitle: gameTitle, bgg_id: bggId, notion_id: gameItem.id, notionData: gameItem });
}
}
}
}
}
bggInterface = new BGGInterface();
lpoInterface = new LautapeliopasInterface();
// Check every item for details data
console.log("Proceeding to check details.")
for (const queueItem of detailsQueue) {
if (verbose) console.log("*** Processing details for game",queueItem.gameTitle, "/", queueItem.bgg_id);
if (!queueItem.notionData) {
// fetch from notion
continue;
}
bggData = queueItem.bgg_data;
toUpdate = {};
sync = async (notionKey, bggKey, notionType) => {
const props = queueItem.notionData.properties[notionKey];
if (props) { // if empty, will not exist
if (props.number && props.number !== 0) return; // Is already set
if (props.url && props.url !== "") return; // Is already set
if (props.rich_text && props.rich_text.length > 0) return; // Is already set
}
if (!bggData) {
if (verbose) console.log("Missing data for at least "+notionKey+" - fetching from BGG")
bggData = await bgg_gameDetails(queueItem.bgg_id); // Fetch data
}
if (!bggData[bggKey] && bggData[bggKey] !== 0) return;
const newContent = bggData[bggKey];
if (notionType === "number") {
toUpdate[notionKey] = { number: newContent }
} else if (notionType === "url") {
toUpdate[notionKey] = { url: newContent }
} else if (notionType === "rich_text") {
toUpdate[notionKey] = { rich_text: [ { type: "text", text: { content: newContent } } ] };
} else {
console.log(" BGG KEY "+bggKey+" FAILS"); dumpj(bggData);
}
}
if (notion_item_debug(queueItem.notionData)) {
//dumpj(queueItem.notion_data);
//dumpj(queueItem.notion_data.properties["Display name"]);
//dumpj(queueItem.notion_data.properties["Expansion for"]);
//game = notionItems.getItem(queueItem.notion_id)
}
if (queueItem.complete) continue; // data marked as complete, skipping
queueItem.notionData.associate("Players min", bggInterface, "minplayers", "number");
queueItem.notionData.associate("Players max", bggInterface, "maxplayers", "number");
queueItem.notionData.associate("Playtime min", bggInterface, "minplaytime", "number");
queueItem.notionData.associate("Playtime max", bggInterface, "maxplaytime", "number");
queueItem.notionData.associate("Age", "minage", bggInterface, "number");
queueItem.notionData.associate("Published year", bggInterface, "yearpublished", "number");
queueItem.notionData.associate("bgg_name", bggInterface, "primaryName", "rich_text");
queueItem.notionData.associate("BGG", bggInterface, "constructed_bgg_url", "url");
queueItem.notionData.associate("bgg_image_url", bggInterface, "image", "url");
queueItem.notionData.associate("Lautapeliopas", lpoInterface, "image", "url");
await queueItem.sync();
/*
await sync("Players min", "minplayers", "number");
await sync("Players max", "maxplayers", "number");
await sync("Playtime min", "minplaytime", "number");
await sync("Playtime max", "maxplaytime", "number");
await sync("Age", "minage", "number");
await sync("Published year", "yearpublished", "number");
await sync("bgg_name", "primaryName", "rich_text");
await sync("BGG", "constructed_bgg_url", "url");
await sync("bgg_image_url", "image", "url");
*/
// to sync: BGG, bgg_name
if (Object.keys(toUpdate).length > 0) {
if (verbose) console.log("Updating to Notion: "+Object.keys(toUpdate).join(", "));
toUpdate["api_latest_sync"] = { date: { start: currentTime.toISOString() } };
await notion.pages.update({
page_id: queueItem.notion_id,
properties: toUpdate
});
}
}
notionItems.doDebug();
}
function exportTitle(item) {
let add = item.displayName;
if (item.isExpansion() && item.displayName.startsWith(item.expansionRootName)) {
add = item.displayName.substring(item.expansionRootName.length);
if (add.startsWith(':')) add = add.substring(1).trim();
}
if (item.lautapeliopas_url) {
add = '<a href="'+item.lautapeliopas_url+'">'+add+'</a>';
}
if (item.minorExpansionList.length > 0) {
// Note: since minorExpansionList is calculated from rollup of a formula resultin in string
// it is not possible to get a backreference.
// To do a backreference with links, one could instead use something like:
// item.expansions.filter(i => i.isMinorExpansion).map(i => i.lautapeliopas_url ? "a href..." : i.displayName)
add += " ("+item.minorExpansionList.map(str => '+ '+str).join(" ")+")";
}
return add;
}
function doExport() {
notionItems.sort((a,b) => a.name.toUpperCase() < b.name.toUpperCase() ? -1 : +1);
let out = "";
const outCat = {};
const knownCategories = ["default", "Party game", "Children's game"];
for (const item of notionItems.items) {
if (item.isExpansion()) continue;
let add = exportTitle(item) + "<br />\n";
const cat = knownCategories.includes(item.category) ? item.category : "default";
if (!outCat[cat]) outCat[cat] = [];
startLetter = item.name.slice(0,1);
if (item.hasExpansions()) {
const exps = item.expansions.filter(i=>!i.isMinorExpansion()).sort((a,b) => a.name.toUpperCase() < b.name.toUpperCase() ? -1 : +1);
for (const exp of exps) {
add = add + ' lisäosa: '+ exportTitle(exp) + "<br />\n";
}
}
outCat[cat].push({ add: add, name: item.name, id: item.id, startLetter: startLetter });
}
for (const category of knownCategories) {
let prevStartLetter = "";
out += "\n\n<!-- Category: "+category+" -->\n<p>\n\n"
if (!outCat[category]) continue;
for (const item of outCat[category]) {
if (category === 'default' && prevStartLetter != item.startLetter) {
out += "\n<!-- "+item.startLetter+" -->\n\n<br/>\n";
prevStartLetter = item.startLetter;
}
out += item.add;
}
out += "\n\n</p>\n\n"
}
try {
let content = fs.readFileSync('export-src/pre');
out = content + out;
} catch (err) {
console.log("No export-src/pre found - ignoring");
}
try {
let content = fs.readFileSync('export-src/post');
out = out + content;
} catch (err) {
console.log("No export-src/post found - ignoring");
}
try {
fs.writeFileSync("export/index.html", out);
} catch (err) {
console.warn("Failed to write to export/index.html");
}
}
doThings().then(() => {
console.log("Main fetch and update done.");
console.log("Proceed to export.");
doExport();
console.log("Export done.");
});
console.log("Main loop exit.");
/*
var bgg = require('bgg')(options);
bgg('search', {'query': 'Terraforming Mars', 'exact': 1})
.then(function(results){
console.log(results);
console.log(JSON.stringify(results,null,2));
});
*/