-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (52 loc) · 1.72 KB
/
index.js
File metadata and controls
59 lines (52 loc) · 1.72 KB
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
import axios from "axios";
import XLSX from "xlsx";
import dotenv from "dotenv";
dotenv.config();
if (process.argv.length != 3) {
console.log(
"Export a Kahoot! with a given UUID.\nCreates an .xlsx file with the same name as the Kahoot! title in ./kahoots/"
);
console.log("Usage: npm start <Kahoot UUID>");
process.exit()
}
const endpoint = "https://create.kahoot.it/rest";
function authenticate(username, password) {
return axios.post(`${endpoint}/authenticate`, {
grant_type: "password",
username,
password
});
}
function getKahoot(uuid) {
return axios.get(`${endpoint}/kahoots/${uuid}`);
}
let wb = XLSX.readFile("./KahootQuizTemplate.xlsx");
let jsSheet = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
authenticate(process.env.KAHOOT_EMAIL, process.env.KAHOOT_PASSWORD)
.then(res => {
axios.defaults.headers.common[
"Authorization"
] = `Bearer ${res.data.access_token}`;
// getKahoot("8062ad52-49af-485d-93fc-5534925b087f")
getKahoot(process.argv[2])
.then(res => {
res.data.questions.forEach((q, i) => {
let obj = {
__EMPTY: i + 1,
__EMPTY_1: q.question
};
let correct = [];
q.choices.forEach((choice, i) => {
obj[`__EMPTY_${i + 2}`] = choice.answer;
if (choice.correct) correct.push(i + 1);
});
obj["__EMPTY_6"] = Math.floor(q.time / 1000);
obj["__EMPTY_7"] = correct.join(",");
jsSheet[i + 6] = obj;
});
wb.Sheets[wb.SheetNames[0]] = XLSX.utils.json_to_sheet(jsSheet);
XLSX.writeFile(wb, `./kahoots/${res.data.title}.xlsx`);
})
.catch(err => console.error(err));
})
.catch(err => console.error(err));