-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (73 loc) · 2.17 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
const axios = require("axios");
const cheerio = require("cheerio");
// console.log("start script")
//meta data
const url = "http://www.koeri.boun.edu.tr/scripts/lst0.asp";
const template = {
tarih: "",
saat: "",
enlem: "",
boylam: "",
derinlikkm: "",
md: "",
ml: "",
yer: "",
cozumniteligi: "",
};
//helpers
// /\(.*\)|\{.*\}|\[.*\]/
const isUpperAlphaOrBrackets = (str) => str.match(/^[A-Z]+$|\(.*\)/) && true;
const isIlksel = (str) => (str.match(/.*(sel)/) ? true : false);
const isRevize = (str) => str.match(/^[A-Z]+[0-9]+$/) && true;
//run
axios
.get(url) // Layer of request
.then((response) => {
//layer of htmlParse
const $ = cheerio.load(response.data);
return $("pre").html().split("\n").slice(7); // We dont need the first 7 lines.
})
.then((chunk) =>
chunk.reduce((acc, val) => {
// split and trim all rows
return [
...acc,
val.split("\n") && val.split(" ").filter((i) => i !== ""),
];
}, [])
)
.then((data) =>
data.map((item) => {
//concat data and template
if (Array.isArray(item)) {
return {
...template,
[Object.keys(template)[0]]: item[0],
[Object.keys(template)[1]]: item[1],
[Object.keys(template)[2]]: item[2],
[Object.keys(template)[3]]: item[3],
[Object.keys(template)[4]]: item[4],
[Object.keys(template)[5]]: item[5],
[Object.keys(template)[6]]: item[6],
[Object.keys(template)[7]]: item.reduce((acc, item) => {
if (isUpperAlphaOrBrackets(item)) return acc.concat(" ", item);
return acc;
}, ""),
[Object.keys(template)[8]]: item.reduce((acc, val) => {
if (isIlksel(val)) return acc.concat("İlksel");
if (isRevize(val)) return acc.concat(item.slice(item.indexOf(val)));
return acc;
}, ""),
};
}
return [...item];
}, [])
)
.then((result) => {
const [_, ml] = process.argv.slice(2);
return ml ?
result.filter((x) => parseFloat(x.ml) >= ml)
: result
})
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(err));