forked from andresberrios/duolingo-stories-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.js
160 lines (147 loc) · 4.32 KB
/
runner.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
wrapper().catch(e => {
console.error("RUNNER ERROR");
console.error(e);
});
async function wrapper() {
$ = artoo.$;
let currentStory = null;
artoo.ajaxSniffer.after((req, res) => {
if (req.url.startsWith("https://stories.duolingo.com/api2/stories/")) {
console.log("Request:", req, res);
currentStory = res.data.elements;
}
});
while (true) {
const stories = $("a.story .title");
const index = Math.floor(Math.random() * stories.length);
const story = $(stories[index]);
console.log(`Clicking on a story: ${story.text()}`);
story.click();
await wait(2000);
let prevStep = null;
for (const step of currentStory) {
await runStep(step, prevStep);
prevStep = step;
}
console.log("Finished a story!");
await continueUntilCatalog();
}
}
async function continueUntilCatalog() {
const $ = artoo.$;
await tryUntilDone(() => {
const catalog = $("a.story .title");
if (catalog.length) {
return true;
} else {
const button = $(".continue:not([disabled])");
button.click();
}
});
}
async function wait(base, extra) {
await new Promise(r => setTimeout(r, getRandomTime(base, extra)));
}
function getRandomTime(base, extra = 1000) {
return base + Math.random() * extra;
}
async function runStep(step, prevStep) {
console.log("Running:", step);
if (step.type === "LINE") {
if (prevStep && prevStep.type === "CHALLENGE_PROMPT") {
console.log("Awaiting challenge...");
} else {
console.log(`Clicking continue...`);
await clickContinue();
}
} else if (step.type === "MULTIPLE_CHOICE") {
const phrase = step.answers[step.correctAnswerIndex].text;
console.log(`Clicking phrase "${phrase}"`);
await clickAnswer(phrase);
await clickContinue();
} else if (step.type === "CHALLENGE_PROMPT") {
console.log("Entering challenge mode");
} else if (step.type === "SELECT_PHRASE") {
const phrase = step.answers[step.correctAnswerIndex];
console.log(`Clicking phrase "${phrase}"`);
await clickAnswer(phrase);
await clickContinue();
} else if (step.type === "POINT_TO_PHRASE") {
const options = step.transcriptParts.filter(p => p.selectable);
const phrase = options[step.correctAnswerIndex].text;
console.log(`Clicking phrase "${phrase}"`);
await clickAnswer(phrase);
await clickContinue();
} else if (step.type === "ARRANGE") {
const phrases = step.phraseOrder.map(
index => step.selectablePhrases[index]
);
for (const phrase of phrases) {
console.log(`Clicking phrase "${phrase}"`);
await clickAnswer(phrase);
}
await clickContinue();
} else if (step.type === "MATCH") {
for (const { phrase, translation } of step.fallbackHints) {
console.log(`Clicking pair "${phrase}" - "${translation}"`);
await clickAnswer(phrase);
await clickAnswer(translation);
}
await clickContinue();
}
}
async function findAnswers() {
const $ = artoo.$;
return tryUntilDone(() => {
let answers = $(".challenge-container li");
if (!answers.length) {
answers = $(".challenge-container .tappable-phrase");
}
if (!answers.length) {
answers = $(".challenge-container .phrase-bank .phrase");
}
if (answers.length) {
return answers;
}
});
}
async function findAnswer(text) {
const $ = artoo.$;
const answers = await findAnswers();
console.log(answers);
const answer = answers.filter(function () {
const selected = $(this).find(".match-selected").length !== 0;
return !selected && $(this).text() === text;
});
return $(answer[0]);
}
async function clickAnswer(text) {
const answer = await findAnswer(text);
const target = answer.children()[0] || answer;
artoo.$(target).click();
}
async function clickContinue() {
await click(".continue:not([disabled])");
}
async function tryUntilDone(callback) {
return new Promise(resolve => {
const id = setInterval(() => {
const result = callback();
if (result) {
clearInterval(id);
resolve(result);
}
}, getRandomTime(250, 1000));
});
}
async function click(selector) {
await tryUntilDone(() => {
const button = artoo.$(selector);
if (button.length) {
button.click();
return true;
} else {
console.log("Could not find selector, retrying...", selector);
}
});
}