-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (116 loc) · 4.16 KB
/
index.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
// europe - defunct coasters https://rcdb.com/r.htm?df=on&ol=25852&ot=2
// Mexico - operating coasters https://rcdb.com/r.htm?ex=on&ol=2&ot=2
// South America - operating coasters https://rcdb.com/r.htm?ex=on&ol=26723&ot=2
// Mexico - defunct coasters https://rcdb.com/r.htm?df=on&ol=2&ot=2
const axios = require('axios');
const cheerio = require('cheerio');
const converter = require('json-2-csv');
const fs = require('fs');
const urlParser = require('url');
const args = process.argv.slice(2);
const searchUrl = args[0].trim();
const filename = args[1].trim() || 'coasters';
let query;
let existing;
const getCoasterData = url => {
query = urlParser.parse(url, true).query;
existing = query.ex && query.ex === 'on' ? true: false;
const promise = axios.get(url);
const dataPromise = promise.then(res => {
return res;
})
.catch(e => {
console.log(e);
})
return dataPromise;
}
getCoasterData(searchUrl)
.then(data => {
const $ = cheerio.load(data.data);
const topTable = $('.t-list.t-top');
const tBodyTop = topTable.find('tbody');
const splitPages = tBodyTop.find('tr:nth-child(2)').text().split(' ');
const totalPages = splitPages[splitPages.length - 1].replace(')', '');
const nums = [...Array(parseInt(totalPages)).keys()];
const urls = [];
for (const i of nums) {
const status = existing ? 'ex' : 'df';
const url = 'https://rcdb.com' + `/r.htm?page=${i + 1}&ot=${query.ot}&ol=${query.ol}&${status}`;
urls.push(url);
}
axios.all(urls.map(url => {
return axios.get(url);
})).then(res => {
let coasterData = [];
for (const item of res) {
const $ = cheerio.load(item.data);
const dataTable = $('.stdtbl.rer table');
const rows = dataTable.find('tbody tr');
rows.each((i , el) => {
const opened = $(el).find('td:nth-child(7)');
let closed;
if (!existing) {
closed = $(el).find('td:nth-child(8)');
}
const getDateFromTd = td => {
const dateTime = td.find('time').attr('datetime');
let openedDate = '';
if (td.find('time').length === 1) {
if (td.text()) {
if (td.text() === 's') {
openedDate = `${dateTime}s`
} else {
openedDate = td.text() + dateTime;
}
} else {
openedDate = dateTime;
}
} else if (td.find('time').length === 2) {
let years = [];
td.find('time').each((i, el) => {
years.push($(el).attr('datetime'));
});
openedDate = years.join(' - ');
}
return openedDate;
};
// const dateTime = opened.find('time').attr('datetime');
// let openedDate = '';
// if (opened.find('time').length === 1) {
// if (opened.text()) {
// if (opened.text() === 's') {
// openedDate = `${dateTime}s`
// } else {
// openedDate = opened.text() + dateTime;
// }
// } else {
// openedDate = dateTime;
// }
// } else if (opened.find('time').length === 2) {
// let years = [];
// opened.find('time').each((i, el) => {
// years.push($(el).attr('datetime'));
// });
// openedDate = years.join(' - ');
// }
let obj = {
coaster: $(el).find('td:nth-child(2) a').text(),
park: $(el).find('td:nth-child(3) a').text(),
opened: getDateFromTd(opened)
}
if (!existing) {
obj.closed = getDateFromTd(closed);
}
coasterData.push(obj);
});
}
converter.json2csv(coasterData, (err, csv) => {
if (err) {
throw err;
}
console.log(csv);
fs.writeFileSync(`${filename}.csv`, csv);
});
});
}
)