Skip to content

Instantly share code, notes, and snippets.

@skysan87 skysan87/jmaMeteoInfo.gs
Last active Oct 22, 2018

Embed
What would you like to do?
[JS][GAS]気象庁防災情報XMLフォーマット形式電文を取得するスクリプト
// 気象庁防災情報XMLフォーマット形式電文の公開(PULL型)の高頻度フィード(随時)を取得
// see: http://www.data.jma.go.jp/developer/xmlpull.html
function pullMeteoHeadline() {
var url = 'http://www.data.jma.go.jp/developer/xml/feed/extra.xml';
var xml = UrlFetchApp.fetch(url).getContentText();
var xmlDoc = XmlService.parse(xml);
var rootDoc = xmlDoc.getRootElement();
var nsDefault = XmlService.getNamespace("", 'http://www.w3.org/2005/Atom');
var entries = rootDoc.getChildren("entry", nsDefault);
var length = entries.length;
var currentDate = new Date().getTime();
var title, updated, date, author, content;
var headlines = "";
// 最長で10分後に更新される
var past10Min = 10 * 60 * 1000;
// 10分以内に更新された東京都の気象警報のみ取得
for(var i=0; i < length; i++) {
title = entries[i].getChildText("title", nsDefault);
if(title != "気象特別警報・警報・注意報") continue;
updated = entries[i].getChildText("updated", nsDefault);
date = new Date(updated).getTime();
if(currentDate - date > past10Min) continue;
author = entries[i].getChild("author", nsDefault).getChildText("name", nsDefault);
if(author != "気象庁予報部") continue;
content = entries[i].getChildText("content", nsDefault);
if(content.indexOf('東京都') === -1) continue;
headlines += Utilities.formatString('[%s]\n%s\n%s\n', title, toLocalDate(updated), content);
}
if(headlines !=="")
{
Logger.log(headlines);
}
}
function toLocalDate(dateString)
{
var date = new Date(dateString);
var formattedDate = Utilities.formatDate( date, 'Asia/Tokyo', 'yyyy年M月d日 HH時');
return formattedDate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.