forked from RCVolus/obs-google-sheet-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (99 loc) · 3.61 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
const OBSWebSocket = require('obs-websocket-js');
const sheetLoader = require('./sheet-loader');
const config = require('./config.json');
const getChildren = sources => {
let items = sources;
sources.forEach(source => {
if (source.type === 'group') {
items = items.concat(getChildren(source.groupChildren));
}
});
return items;
}
const update = async (obs) => {
const data = await sheetLoader.loadData();
const range = config.range;
const startcell = range.split(":")[0].trim();
const startcol = startcell.match("[a-zA-Z]+");
//console.log("starting column is " + startcol);
const startrow = startcell.match("[0-9]+");
//console.log("starting row is " + startrow);
const rowoffset = startrow[0];
//console.log("row offset to array is " + rowoffset);
const coloffset = columnToNumber(startcol[0]);
//console.log("colum offset to array is " + coloffset);
const sceneList = await obs.send('GetSceneList');
await sceneList.scenes.forEach(async scene => {
// unfold group children
const allSources = getChildren(scene.sources);
// console.log(scene);
await allSources.forEach(async source => {
if (source.name.includes('|sheet')) {
const reference = source.name.split('|sheet')[1].trim();
let col = reference.match("[a-zA-Z]+");
let colnumber = columnToNumber(col[0]) - coloffset;
let row = reference.match("[0-9]+");
let rownumber = row[0] - rowoffset;
let cellvalue = data[colnumber][rownumber];
console.log("Value for cell in source is " + cellvalue)
if (cellvalue.length > 0) {
let color = null;
if (cellvalue.startsWith('?color')) {
const split = cellvalue.split(';');
cellvalue = split[1];
color = split[0].split('=')[1];
color = color.replace('#', '');
const color1 = color.substring(0, 2);
const color2 = color.substring(2, 4);
const color3 = color.substring(4, 6);
color = parseInt('ff' + color3 + color2 + color1, 16);
}
if (cellvalue.startsWith('?hide')) {
await obs.send("SetSceneItemRender", {
'scene-name': scene.name,
source: source.name,
render: false
});
} else if (cellvalue.startsWith('?show')) {
await obs.send("SetSceneItemRender", {
'scene-name': scene.name,
source: source.name,
render: true
});
}
// Update to OBS
await obs.send("SetTextGDIPlusProperties", {
source: source.name,
text: cellvalue,
color: color
});
console.log(`Updated: ${reference} to OBS: ${source.name}`);
} else {
console.log(`Field is empty idk`)
}
}
});
});
}
const main = async () => {
const obs = new OBSWebSocket();
await obs.connect({ address: 'localhost:4444' });
console.log('Connected to OBS!');
const updateWrapped = () => update(obs).catch(e => {
console.log("EXECUTION ERROR IN MAIN LOOP:");
console.log(e);
});
setInterval(updateWrapped, config.polling);
updateWrapped();
}
main().catch(e => {
console.log("EXECUTION ERROR:");
console.log(e);
});
function columnToNumber(str) {
var out = 0, len = str.length;
for (pos = 0; pos < len; pos++) {
out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
}
return out-1;
}