-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
59 lines (50 loc) · 1.46 KB
/
index.ts
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 {
closeStream,
createStream,
getProgress,
saveProgress,
writeToFile,
} from "./fileLogger";
import puppeteer, { Page } from "puppeteer";
let page: Page;
(async () => {
const browser = await puppeteer.launch();
page = await browser.newPage();
await page.goto(getProgress());
await scrape(5000, 1000);
saveProgress(page.url());
browser.close();
})();
const scrape = async (nrOfWords: number, maxFileSize: number) => {
for (let i = 0; i < nrOfWords / maxFileSize; i++) {
await iterateWords(maxFileSize);
}
};
const iterateWords = async (nrOfWords: number) => {
const stream = createStream();
for (let i = 0; i < nrOfWords; i++) {
const transcription = await getWord();
writeToFile(stream, transcription);
await navigateNextWord();
await page.waitForSelector(".translation");
}
closeStream(stream);
};
const getWord = async () => {
const word = await page.$eval(".translation", (el) =>
el.innerHTML.toString()
);
const foundPosition = word.indexOf(
'<span class="found">Търсената дума е намерена</span>'
);
const transcription = word
.substring(0, foundPosition)
.split("<br>")
.map((w) => w.trim())
.filter((w) => !!w.length);
console.log(transcription);
return transcription;
};
const navigateNextWord = async () => {
await page.click("#wordsList > a:nth-child(3)");
};