-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
129 lines (101 loc) · 3.19 KB
/
main.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
require('dotenv').config({ path: __dirname + '/.env'})
var nodemailer = require("nodemailer");
const https = require('https')
var builder = require('xmlbuilder');
fs = require('fs');
const options = {
hostname: 'ocean.weather.gov',
port: 443,
path: '/gulf_stream_latest.txt',
method: 'GET'
}
var today = new Date();
var todayString = today.toISOString().split('T')[0];
var timeString = today.toISOString();
var elementNumber = "1";
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
let body = [];
res.on('data', d => {
body.push(d);
}).on('end', () => {
body = Buffer.concat(body).toString();
var parts = body.split(':');
var obj = builder.create('gpx', { encoding: 'utf-8' })
.att('version', '1.1')
.att('xmlns', 'http://www.topografix.com/GPX/1/1');
var trk = obj.ele('trk');
var name = trk.ele('name', 'Gulf Stream Track ' + todayString);
var trkseg = trk.ele('trkseg');
let northWallwpts = parts[1].split('GULF');
northWallwpts = northWallwpts[0].split(' ');
for(var i = 0; i < northWallwpts.length; i++){
var match = /\r|\n/.exec(northWallwpts[i]);
if (!match && northWallwpts[i] != " " && northWallwpts[i] != null){
let latLong = northWallwpts[i].split('N');
// add each waypoint
var trkpt = trkseg.ele('trkpt');
trkpt.att('lat', latLong[0]);
trkpt.att('lon', '-' + latLong[1].replace("W", ""));
var trkptname = trkpt.ele('ele', elementNumber);
var time = trkpt.ele('time', new Date().toISOString());
}
}
var trkseg = trk.ele('trkseg');
let southWallwpts = parts[2].split('2. ');
// console.log(southWallwpts[0]);
southWallwpts = southWallwpts[0].split(' ');
for(var i = 0; i < southWallwpts.length; i++){
var match = /\r|\n/.exec(southWallwpts[i]);
if (!match && southWallwpts[i] != " " && southWallwpts[i] != null){
let latLong =southWallwpts[i].split('N');
// add each waypoint
var trkpt = trkseg.ele('trkpt');
trkpt.att('lat', latLong[0]);
trkpt.att('lon', '-' + latLong[1].replace("W", ""));
var trkptname = trkpt.ele('ele', elementNumber);
var time = trkpt.ele('time', new Date().toISOString());
}
}
var xml = obj.end({ pretty: true});
// console.log(xml);
fs.writeFile( __dirname + '/GulfStream.GPX', xml, function (err) {
if (err) {
console.log(err)
}
});
})
})
req.on('error', error => {
console.error(error)
})
req.end(data => {
var sender = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.USERNAME,
pass: process.env.PASSWORD
}
});
var mail = {
from: process.env.USERNAME,
to: process.env.TOEMAIL,
subject: "Updated Gulstream track",
text: "Attached Gulfstream updates",
attachments: [
{
filename: 'GulfStream.' + todayString + '.GPX',
path: __dirname + '/GulfStream.GPX',
cid: 'uniq-mailtrap.png'
}
]
};
sender.sendMail(mail, function(error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent successfully: "
+ info.response);
}
});
})