forked from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.js
148 lines (132 loc) · 3.81 KB
/
backend.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
const { request } = require('obsidian')
const toLoad = ["character", "world", "prompt", "plot", "ttrpg"]
function loadConfig(filename) {
const yaml = require('js-yaml')
const fs = require("fs")
return yaml.load(fs.readFileSync(filename, "utf-8"))
}
async function updateQuestions(config) {
let outData = {}
let base = `https://docs.google.com/spreadsheets/d/${config.sheetsId}/export?gid=`
for (const key in config.sheets) {
let url = base + config.sheets[key] + "&format=csv"
console.log(`Fetching ${key}.csv! {${url}}`);
const body = await request(url)
outData[key] = body
}
console.log("questions updated");
return outData
}
function questionInit(data) {
const {parse} = require('csv/sync');
let cats = {}
let loaded = {}
// console.log(data["plot"]);
for (const item of toLoad) {
// console.log(`Item: ${item}, data: ${data[item]}`);
const records = parse(data[item], {from:1})
console.log(records);
let obj = []
for (const record of records) {
obj.push([])
for (const sub of record) {
if (sub == "") {continue}
obj.at(-1).push(sub)
}
}
obj.shift()
loaded[item] = obj
}
const categoryRecords = parse(data.categories, {
from: 2,
relax_column_count: true
})
let currentType = ""
for (const item of categoryRecords) {
// console.log(item);
if (item[2] == "") {
let type = item.shift()
switch (type) {
case "World Questions":
currentType = "world"
break;
case "Character Questions":
currentType = "character"
break;
case "Writing Prompts":
currentType = "prompt"
break;
case "Plot Questions":
currentType = "plot"
break;
case "TTRPG":
currentType = "ttrpg"
break;
default:
// throw "fuck shit"
}
continue
}
let category = item.shift()
if (category == "") {
category = item.shift()
}
else { item.shift() }
let desc = item.shift()
if (cats[currentType]) {
cats[currentType][category] = desc
}
else {
cats[currentType] = {}
cats[currentType][category] = desc
}
}
console.log("questions init");
// console.log(cats);
return {...loaded, cats}
}
function arrayChoose(array) {
const idx = Math.floor(Math.random() * array.length)
// console.log(idx)
return array[idx]
}
function thingPicker(questions, filters) {
if (!filters) {
return arrayChoose(questions)
}
if (filters[0] == "") {
return arrayChoose(questions)
}
let pos = []
let neg = []
for (const filter of filters) {
if (filter.at(0) == "-") {neg.push(filter.slice(1))}
else { pos.push(filter) }
}
let filteredQs = []
outer: for (const item of questions) {
for (const negFilter of neg) {
if (item.includes(negFilter)) {
continue outer
}
}
if (pos.length < 1) {
filteredQs.push(item)
}
let results = []
for (const posFilter of pos) {
results.push(item.includes(posFilter))
}
if (results.every((val) => val)) {
filteredQs.push(item)
}
}
return arrayChoose(filteredQs)
}
module.exports = {
loadConfig,
updateQuestions,
questionInit,
thingPicker,
toLoad
}