-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (117 loc) · 3.56 KB
/
index.js
File metadata and controls
147 lines (117 loc) · 3.56 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
/* eslint-disable no-console */
const fs = require('fs').promises;
const path = require('path');
const tourRd = require('./module/TourRd');
const getTourId = async () => {
let listUrl = [];
let page = 0;
// Get Tour ID
try {
for (let i = 1; i <= 60; i++) {
const listUrlPage = await tourRd.getListTours(`vietnam?page=${i}`);
page = i;
listUrl = [...listUrl, ...listUrlPage];
console.log('done page:', i);
}
} catch (error) {
console.log(error);
} finally {
await fs.writeFile(`Tour-Url-${page}.json`, JSON.stringify(listUrl));
console.log('finished!!!');
}
};
// for testing TourRd
const getTour = async (tourId) => {
const tourData = await tourRd.getTourData(tourId);
// let quickGet = await tourRd.getDataTour('1842');
console.log(tourData);
};
const getTours = async (step, listUrl) => {
const data = [];
const reviews = [];
const errorList = [];
// listUrl.length
for (let i = 0; i < step; i++) {
try {
const { tourData, tourReviews } = await tourRd.getTourData(listUrl[i]);
data.push(tourData);
reviews.push(tourReviews);
console.log(
`🌟🌟🌟: Complete tour ${i + 1} : id ${listUrl[i]}! Remaining: ${
listUrl.length - i - 1
} 🌟🌟🌟`
);
// listUrl[i] = 'ok';
} catch (error) {
const errorMessage = `💥💥💥: Error on tour ${i + 1} : id ${
listUrl[i]
} !!!: 💥💥💥`;
// listUrl[i] = 'error';
console.log(errorMessage, error);
errorList.push({ tour: listUrl[i], error: error.toString() });
}
}
return { data, reviews, errorList };
};
const runBatch = async (from = 0, to = 0, step = 10) => {
const listUrl = JSON.parse(
await fs.readFile(
path.join(__dirname, 'File', 'Input', 'input.json'),
'utf8'
)
);
console.log(
`👉👉👉Total Tours need to get: ${listUrl.length - from} !!!👈👈👈\n`
);
if (!to) to = listUrl.length;
const timesRun = Math.ceil((to - from + 1) / step);
for (let j = 0; j < timesRun; j += 1) {
const startLoop = step * j + from;
const stopLoop = startLoop + step - 1;
console.log(`🥊🥊🥊Get tour from ${startLoop} to ${stopLoop}!!!🥊🥊🥊`);
const listUrlPart = listUrl.slice(startLoop, stopLoop + 1);
const { data, reviews, errorList } = await getTours(step, listUrlPart);
const writeData = fs.writeFile(
path.join(
__dirname,
'File',
'Data',
`Data from ${startLoop} to ${stopLoop} - ${data.length}.json`
),
JSON.stringify(data)
);
const writeReviews = fs.writeFile(
path.join(
__dirname,
'File',
'Review',
`Reviews from ${startLoop} to ${stopLoop} - ${reviews.length}.json`
),
JSON.stringify(reviews)
);
let writeError = '';
if (errorList.length)
writeError = await fs.writeFile(
path.join(
__dirname,
'File',
'Error',
`Error from ${startLoop} to ${stopLoop} - ${errorList.length}.json`
),
JSON.stringify(errorList)
);
await Promise.all([writeData, writeReviews, writeError]);
console.log(
`\n👍👍👍 finished from ${startLoop} to ${stopLoop}!!!😆😆😆 Total Remaining: ${
listUrl.length - stopLoop - 1
} 😅😅😅\n`
);
}
};
(async () => {
await tourRd.initialize();
await runBatch(0, 1, 1);
// await getTour('164411'); //188722
})();
// const file = await fs.readFile('filename.txt', 'utf8');
// await fs.writeFile('filename.txt', 'test');