-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (164 loc) · 6.69 KB
/
index.js
File metadata and controls
198 lines (164 loc) · 6.69 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
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
const express = require('express');
const axios = require('axios');
const path = require("path");
const engine = require('ejs-mate');
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "/public")));
app.engine('ejs', engine);
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.get('/', (req, res) => {
res.render("index.ejs");
});
async function getScore(quizId, accessToken) {
const url = 'https://api.tesseractonline.com/quizattempts/submit-quiz';
const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Authorization': accessToken,
'Origin': 'https://tesseractonline.com',
'Referer': 'https://tesseractonline.com/',
};
const payload = { quizId };
try {
const response = await axios.post(url, payload, { headers });
return response.data.payload.score;
} catch (error) {
console.error(`Error submitting quiz ${quizId}:`, error);
throw error;
}
}
async function attemptQuizApi(quizId, questionId, userAnswer, accessToken) {
const url = 'https://api.tesseractonline.com/quizquestionattempts/save-user-quiz-answer';
const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Authorization': accessToken,
'Origin': 'https://tesseractonline.com',
'Referer': 'https://tesseractonline.com/',
};
const payload = { quizId, questionId, userAnswer };
try {
const response = await axios.post(url, payload, { headers });
return await getScore(quizId, accessToken);
} catch (error) {
console.error(`Error attempting quiz ${quizId}, question ${questionId}:`, error);
throw error;
}
}
async function attemptQuiz(quizId, questionId, currentScore, accessToken) {
let score = currentScore;
const options = ['a', 'b', 'c', 'd'];
let i = 0;
while (score !== currentScore + 1) {
try {
score = await attemptQuizApi(quizId, questionId, options[i], accessToken);
if (score === currentScore + 1) {
console.log(`Option ${options[i]} locked for question ${questionId}`);
}
} catch (error) {
console.log(`Error with quiz ${quizId}, question ${questionId}, stopping attempts.`);
break;
}
i += 1;
}
return score;
}
async function attemptOneQuiz(quizId, accessToken) {
const url = `https://api.tesseractonline.com/quizattempts/create-quiz/${quizId}`;
const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
'Authorization': accessToken,
'Origin': 'https://tesseractonline.com',
'Referer': 'https://tesseractonline.com/',
};
try {
const response = await axios.get(url, { headers });
const data = response.data;
let currentScore = 0;
for (const question of data.payload.questions) {
currentScore = await attemptQuiz(data.payload.quizId, question.questionId, currentScore, accessToken);
}
return { quizId: data.payload.quizId, finalScore: currentScore };
} catch (error) {
console.log(`Error creating quiz ${quizId}: ${error}`);
throw error;
}
}
async function getUnitTopics(unitId, accessToken) {
const url = `https://api.tesseractonline.com/studentmaster/get-topics-unit/${unitId}`;
const headers = {
'Authorization': accessToken,
'Host': 'api.tesseractonline.com',
};
try {
const response = await axios.get(url, { headers });
const data = response.data;
return data.payload.topics
.filter(topic => topic.contentFlag) // Only include topics with contentFlag true
.map(topic => ({
topicId: topic.id,
topicName: topic.name
}));
} catch (error) {
console.log(`Error fetching topics for unit ${unitId}: ${error}`);
throw error;
}
}
async function resultQuiz(topicId, accessToken) {
const url = `https://api.tesseractonline.com/quizattempts/quiz-result/${topicId}`;
const headers = {
'Authorization': accessToken,
'Host': 'api.tesseractonline.com',
};
try {
const response = await axios.get(url, { headers });
const data = response.data;
return data.payload.badge === 1;
} catch (error) {
console.log(`Error fetching quiz result for topic ${topicId}: ${error}`);
throw error;
}
}
app.post('/', async (req, res) => {
const { accessToken, unitId, numUnits } = req.body;
if (!accessToken || !unitId || numUnits === undefined) {
return res.status(400).json({ error: 'Missing accessToken, unitId, or numUnits in request data.' });
}
let logs = [];
let topicsCount = 0; // Initialize topics counter
let processedTopics = 0; // Track the number of processed topics
try {
const unitIds = unitId.split(' ');
const numUnitsInt = parseInt(numUnits, 10); // Convert numUnits to an integer
for (const unit of unitIds) {
if (numUnitsInt !== 0 && processedTopics >= numUnitsInt) break; // Stop processing if the limit is reached and numUnits is not 0
const topics = await getUnitTopics(unit, accessToken);
topicsCount += topics.length; // Increment the counter by the number of topics in the current unit
for (const topic of topics) {
if (numUnitsInt !== 0 && processedTopics >= numUnitsInt) break; // Stop processing if the limit is reached and numUnits is not 0
console.log(`${topic.topicId}: ${topic.topicName}`);
const done = await resultQuiz(topic.topicId, accessToken);
if (done) {
console.log(`Quiz with id ${topic.topicId} is already done!`);
} else {
console.log(`Solving quiz ${topic.topicId}`);
await attemptOneQuiz(topic.topicId, accessToken);
console.log(`Quiz ${topic.topicId} is finished.`);
console.log('');
processedTopics += 1; // Increment the processed topics counter only for attempted quizzes
}
}
}
res.status(200).json({ logs, message: 'Submission complete', topicsCount }); // Include topicsCount in the response
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});