Last active
August 17, 2022 04:20
-
-
Save skysan87/586f3b0a52c4e7689a9f0d50dadb5eeb to your computer and use it in GitHub Desktop.
[JS][GAS]気象庁防災情報XMLフォーマット形式電文を取得するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 気象庁防災情報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