-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
141 lines (121 loc) · 4.47 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const twilio = require('twilio');
const fs = require('fs');
require('dotenv').config();
const emptyChar = '⠀';
const client = new twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
fetch("http://udim.koeri.boun.edu.tr/zeqmap/xmlt/son24saat.xml")
.then(res => res.text())
.then(res => evalRes(res))
.catch(err => console.log(err));
let comparer = (otherArray) => {
return function(current) {
return otherArray.filter(function(other) {
return other.date === current.date
}).length == 0;
}
}
let evalRes = (res) => {
const $ = cheerio.load(res, { xmlMode: true });
const earthquakesDOM = getEarthquakesDOM($);
const networkEarthquakes = createEarthquakesArray($, earthquakesDOM);
readLocalFileEarthquakes().then(strLocalEarthquakes => {
const localEarthquakes = JSON.parse(strLocalEarthquakes);
const newEarthquakes = networkEarthquakes.filter(comparer(localEarthquakes));
console.log('newEarthquakes', newEarthquakes);
const earthquakes = getEarthquakesBySelectedCriteria(newEarthquakes);
console.log('earthquakes', earthquakes);
const smsText = createSmsText(earthquakes);
writeEarthquakesToFile(networkEarthquakes);
if (smsText.length === 0) {
console.log('Earthquake not happened');
return;
}
writeSmsToFile(smsText);
sendSmsToRecievers(smsText);
})
.catch(err => console.log(err));
}
let getEarthquakesDOM = ($) => {
return $('earhquake');
}
let createEarthquakesArray = ($, earthquakesDOM) => {
let earthquakes = [];
earthquakesDOM.each((index, earthquake) => {
earthquakes.push(createEarthQuakeObj($, earthquake));
});
return earthquakes;
}
let createEarthQuakeObj = ($, earthquake) => {
const date = $(earthquake).attr("name").trim();
const location = $(earthquake).attr("lokasyon").replace(/\s\s+/g, ' ').trim();
const lat = $(earthquake).attr("lat").trim();
const lng = $(earthquake).attr("lng").trim();
const mag = $(earthquake).attr("mag").trim();
const depth = $(earthquake).attr("Depth").trim();
return { date, location, lat, lng, mag, depth };
}
let writeEarthquakesToFile = (earthquakes) => {
fs.writeFile('previousEarthquakes.json', JSON.stringify(earthquakes), function(err) {
if (err) return console.log(err);
console.log('Written earthquakes.json');
});
}
const readFile = async filePath => {
try {
const data = await fs.promises.readFile(filePath, 'utf8');
return data;
} catch (err) {
console.log(err);
}
}
let readLocalFileEarthquakes = () => {
return readFile('previousEarthquakes.json');
}
const getEarthquakesBySelectedCriteria = newEarthquakes => {
const foundEarthquakes = [];
const cities = process.env.CITIES_DELIMITED_WITH_SEMICOLON.split(';');
const minMagnitude = process.env.MIN_MAGNITUDE;
newEarthquakes.forEach(earthquake => {
cities.forEach(city => {
if ((city === "*" || earthquake.location.includes(city)) && earthquake.mag >= minMagnitude) {
foundEarthquakes.push(earthquake);
}
});
});
return foundEarthquakes;
}
let createSmsText = (earthquakes) => {
let smsTextArray = [];
earthquakes.forEach(earthquake => {
smsTextArray.push(createSmsLine(earthquake));
});
if (smsTextArray.length === 0) return "";
return `${emptyChar}\n${emptyChar}\n${smsTextArray.join('\n')}\n${emptyChar}\n${emptyChar}`;
}
let createSmsLine = (earthquake) => {
let date = earthquake.date;
if (date.includes(' ')) {
date = date.split(' ')[1];
}
return `💢 ${earthquake.mag} ${date} ${earthquake.location}`;
}
let writeSmsToFile = (sms) => {
fs.writeFile('sms.txt', sms, function(err) {
if (err) return console.log(err);
console.log('Written sms.txt');
});
}
let sendSmsToRecievers = (smsText) => {
const receivers = process.env.MSISDN_RECEIVERS_DELIMITED_WITH_SEMICOLON;
receivers.split(';').forEach(receiver => {
client.messages.create({
to: receiver,
from: process.env.MSISDN_SENDER,
body: smsText
})
.then(message => console.log('Sent', 'SID', message.sid))
.catch(error => console.log('Sending error', error));
});
}