-
Notifications
You must be signed in to change notification settings - Fork 0
/
парсинг_api.js
78 lines (60 loc) · 1.69 KB
/
парсинг_api.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
const getData = require('./api.js');
const fs = require("fs");
let stream = fs.createWriteStream('./data/result.txt', {flags: "a"});
let wordsListTextBasic = '';
let wordsListBasic;
let wordsListIterable = [];
let wordsListBasicLength;
const geoId = [];
let current = 0; //можно менять
//значение это количество строк в result.txt
//пустая строка в конце не считается
let readStream = fs.createReadStream('./data/read.txt');
readStream.on('data', (data)=>{
wordsListTextBasic+=data;
});
readStream.on('end', async ()=>{
wordsListBasic = wordsListTextBasic.split('\r\n');
wordsListTextBasic = '';
wordsListBasicLength = wordsListBasic.length;
if (current !== 0) {
wordsListBasic = wordsListBasic.slice(current);
}
let i=0;
let y = 0;
for (let item of wordsListBasic) {
if (i==10) {
i=0;
y++;
}
if (i == 0) {
wordsListIterable[y] = [item];
} else {
wordsListIterable[y][i] = item;
}
i++;
}
wordsListIterable[Symbol.asyncIterator] = function() {
let o1 = {
current: 0,
last: wordsListIterable.length-1,
async next() {
if (this.current <= this.last) {
let data = await getData(wordsListIterable[this.current++], geoId);
return {done: false, value: data}
} else {
return {done: true}
}
}
}
return o1;
}
for await (let data of wordsListIterable) {
console.log(current+=data.length, 'готово');
if (current<wordsListBasicLength) {
stream.write(`${data.string}\n`);
} else {
stream.end(`${data.string}`);
}
}
});