-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjmaparser.js
337 lines (280 loc) · 8.76 KB
/
jmaparser.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'use strict';
const util = require('util');
const http = require('http');
const https = require('https');
const url = require('url');
const xmlparseAsync = util.promisify(require('xml2js').parseString);
// feedされたエントリを処理
exports.processEntriesAsync = async function(entries, slackInfo)
{
for(const entry of entries){
console.log(util.format('%s:%s\nLink:%s', entry.title, entry.content._ , entry.link.$.href));
await processXmlAsync(entry.link.$.href, slackInfo);
}
}
// XMLを処理する
function processXmlAsync(uri, slackInfo){
return new Promise(async (resolve, reject) => {
const message = await loadXmlAsync(uri, slackInfo);
if(message){
console.log(JSON.stringify(message));
const result = Promise.all(message.webhooks.map(async webhook =>{
try{
const response = await httpsPostAsync(webhook ,message);
return response;
}catch(error){
if(error instanceof Error){
console.log('http posting error:%s\n%s',error.message,error.stack);
}else{
console.log('http posting error:%s',error);
}
return null;
}
}));
resolve(result);
}else{
console.log('deprecaetd');
resolve(null);
}
});
}
// XMLを読み込んでSlackメッセージオブジェクトを作る
function loadXmlAsync(uri, slackInfo){
return new Promise(async (resolve, reject) => {
let body,message,xmlobj;
try{
body = await httpGetAsync(uri);
}catch(error){
if(error instanceof Error){
message = {'text' : util.format('cannot load XML "%s"\nError:%s\nTrace:%s',uri,error.message,error.stack)};
}else{
message = {'text' : util.format('cannot load XML "%s"\n%s',uri,error)};
}
message.webhooks = [slackInfo.error];
resolve(message);
return;
}
try{
xmlobj = await xmlparseAsync(body, {trim: true, explicitArray: false });
message = processObject(xmlobj);
if(message){
message.webhooks = slackInfo.notify;
}
}catch(error){
const dump = util.inspect(xmlobj,{ showHidden: true, depth: null });
if(error instanceof Error){
message = {'text' : util.format('cannot parse XML "%s"\nError:%s\nTrace:%s\n%s',uri,error.message,error.stack,dump)};
}else{
message = {'text' : util.format('cannot parse XML "%s"\n%s\n%s',uri,error,dump)};
}
message.webhooks = [slackInfo.error];
}
resolve(message);
});
}
// メッセージ種別に応じた処理を呼ぶ
function processObject(object){
const title = object && object.Report && object.Report.Head && object.Report.Head.Title;
switch(title){
case '震度速報':
return processSummary(object);
case '震源に関する情報':
return processEpicenter(object);
case '震源・震度情報':
return processDetail(object);
default:
console.log('unknown title:'+title);
return null;
}
}
// Slackメッセージアタッチメントを作成
function makeAttachment(object){
const message = {};
message.footer= object.Report.Control.EditorialOffice;
const reportDate = new Date(object.Report.Head.ReportDateTime);
message.ts = parseInt(reportDate.getTime()/1000);
const targetDate = (object.Report.Body.Earthquake) ?
new Date(object.Report.Body.Earthquake.OriginTime) :
new Date(object.Report.Head.TargetDateTime);
message.fields = [];
message.fields.push({
'title' : '発生時刻',
'value' : targetDate.toString()
});
message.actions = [{
'type' : 'button',
'text' : 'Yahoo!',
'url' : util.format('https://typhoon.yahoo.co.jp/weather/jp/earthquake/%s.html',object.Report.Head.EventID)
},{
'type' : 'button',
'text' : 'tenki.jp',
'url' : util.format('http://bousai.tenki.jp/bousai/earthquake/detail-%s.html',object.Report.Head.EventID)
}];
message.image_url = getTenkiJpMapImageURI(object.Report.Head.EventID);
return message;
}
// tenki.jpの震源画像URIを生成
function getTenkiJpMapImageURI(eventID){
const year = eventID.substring(0,4);
const month = eventID.substring(4,6);
const day = eventID.substring(6,8);
const hour = eventID.substring(8,10);
const minutes = eventID.substring(10,12);
const seconds = eventID.substring(12,14);
return util.format('https://earthquake.tenki.jp/static-images/earthquake/detail/%s/%s/%s/%s-%s-%s-%s-%s-%s-large.jpg',year,month,day,year,month,day,hour,minutes,seconds);
}
// Slackメッセージ概要を作成
function makeSlackMessage(message,object){
const msgType = object.Report.Control.Status === '通常' ? '' : '('+object.Report.Control.Status+')';
return {
'username' : util.format('%s(%s)',object.Report.Head.Title,object.Report.Head.InfoType),
'text' : msgType + object.Report.Head.Headline.Text,
'attachments' : Array.isArray(message) ? message : [message] ,
};
}
// 震度速報
function processSummary(object){
const message = makeAttachment(object);
const items = Array.isArray(object.Report.Head.Headline.Information.Item) ?
object.Report.Head.Headline.Information.Item :
[object.Report.Head.Headline.Information.Item] ;
items.forEach((item) => {
const areas = [];
const areaList = Array.isArray(item.Areas.Area) ? item.Areas.Area : [item.Areas.Area];
areaList.forEach((area) => {
areas.push(area.Name);
});
message.fields.push({
'title' : item.Kind.Name,
'value' : areas.join()
});
});
return makeSlackMessage(message,object);
}
// 震源に関する情報
function processEpicenter(object){
const message = makeAttachment(object);
loadEpicenter(message,object);
return makeSlackMessage(message,object);
}
// 震源情報の読み込み
function loadEpicenter(message,object)
{
const info = object.Report.Body.Earthquake;
// 座標はISO6709形式
const re= /([+-][\d\.]+?)([+-][\d\.]+?)([+-][\d\.]+?)\//;
const pos = re.exec(info.Hypocenter.Area['jmx_eb:Coordinate']._);
const link = util.format('https://www.google.com/maps?q=%s,%s',pos[1],pos[2]);
message.fields.push({
'title' : '震源',
'value' : util.format('%s\n<%s|%s>',
info.Hypocenter.Area.Name,
link,
info.Hypocenter.Area['jmx_eb:Coordinate'].$.description,
),
});
message.fields.push({
'title' : 'マグニチュード',
'value' : info['jmx_eb:Magnitude']._,
});
}
// 震源・震度に関する情報
function processDetail(object){
const message = makeAttachment(object);
//console.log('object:'+util.inspect(object,{ showHidden: true, depth: null }));
loadEpicenter(message,object);
loadIntensity(message,object);
return makeSlackMessage(message,object);
//console.log('message:'+util.inspect(msg,{ showHidden: true, depth: null }));
}
// 5,6の+/-を強弱に入れ替える
function convertIntensityNum(intensity)
{
switch(intensity){
case '6+':
return '6強';
case '6-':
return '6弱';
case '5+':
return '5強';
case '5-':
return '5弱';
default:
return intensity;
}
}
// 震度詳細の読み込み
function loadIntensity(message,object)
{
const intencityInfo = [];
const prefList = Array.isArray(object.Report.Body.Intensity.Observation.Pref) ?
object.Report.Body.Intensity.Observation.Pref :
[object.Report.Body.Intensity.Observation.Pref];
prefList.forEach( (pref) => {
const areas = [];
const areaList = Array.isArray(pref.Area) ? pref.Area : [pref.Area];
areaList.forEach((area) => {
const shortName = area.Name === pref.Name ? pref.Name : area.Name.replace(pref.Name,'');
areas.push(util.format('%s(最大震度 %s)',shortName, convertIntensityNum(area.MaxInt) ));
});
intencityInfo.push(util.format('*%s*:%s',pref.Name,areas.join()));
});
message.fields.push({
'title' : '各地の震度',
'value' : intencityInfo.join('\n'),
});
}
// asyncなhttp.get
function httpGetAsync(uri,encoding='utf8')
{
const client = uri.startsWith('https:') ? https : http;
return new Promise((resolve, reject) => {
client.get(uri,(res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
reject(new Error('statusCode=' + res.statusCode));
return;
}
let body = '';
res.setEncoding(encoding);
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve(body);
});
}).on('error',(error) => {
reject(error);
});
});
}
// asyncなhttps post
function httpsPostAsync(uri,object)
{
return new Promise((resolve, reject) => {
const data = JSON.stringify(object);
const options = url.parse(uri);
options.headers = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
};
options.method='POST';
const req = https.request(options,(res) => {
if (res.statusCode < 200 || res.statusCode >= 300) {
reject(new Error('statusCode=' + res.statusCode));
return;
}
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve(body);
});
}).on('error',(error) => {
reject(error);
});
req.write(data);
req.end();
});
}