Skip to content

Instantly share code, notes, and snippets.

@basictomonokai
Created December 14, 2018 05:11
Show Gist options
  • Save basictomonokai/44b0b86a1530b94fd57015d9f315c9e2 to your computer and use it in GitHub Desktop.
Save basictomonokai/44b0b86a1530b94fd57015d9f315c9e2 to your computer and use it in GitHub Desktop.
テスト用MD

【地震情報配信】なまずが素晴らしいと思う件

※カレンダーが空いてたのでショボイですが書きました

0.初めに

私はエンジニアではないただのドシロウトです。

  • ドシロウトなのでAPIの外の人になります。
  • ドシロウトなのでプログラミングの能力は低いです。
  • ドシロウトなので簡単に利用出来て、使い方も簡単なAPIが好きです。
  • せっかくAPIを利用したプログラムを作ったら長く使いたいです。

この記事では国産でとても長くAPIを提供されていて登録不要で使い方も簡単な地震情報配信APIをご紹介します。(ご存知でしょうが…(^_^;))

1.【地震情報配信】なまず

他にも地震情報を配信してるAPIはあると思いますが5年以上継続しているすばらしいサイトです。

【地震情報配信】なまず - PCからモバイルまでサポートするリアルタイム地震情報サービス http://nmzu.jp/

検索した限りでは2013年2月に以下のネット記事でも紹介されてました。

Bing Maps上に地震の震源地を表示するプログラムを作る https://thinkit.co.jp/story/2013/02/20/3971

APIエンドポイント: http://api.nmzu.jp/history.xml

APIエンドポイントを見て頂ければわかる通り直近の地震情報が時系列で並んだ以下の項目を含んだXMLが返されます。

項目:地震情報のURL、発生日、緯度、経度、地域名、震度、マグニチュード

2.サンプル

ドシロウトらしく世界一簡単な言語BASIC!でのサンプルを載せておきます。

BASIC!はAndroidで動くBASICインタープリタです。詳細は以下をどうぞ

AndroidでBASICを使う( BASIC プログラミング ) https://qiita.com/basictomonokai/items/25bcb21ec69c00e9ad23

私がBASIC!のプログラムで作成した画面は以下。

BASIC!アプリ

Androidのスマホやタブレットで見れるのでとても便利です。

3.サンプルプログラムのコード

BASIC!プログラムのコードのAPI取得部分が以下です。

REM 地震情報取得

! API呼出
console.title "地震情報取得中…"
graburl eqwk$,"http://api.nmzu.jp/history.xml"

! 改行で取得したデータを分割
split eqa$[],eqwk$,"\n"
array.length leqa,eqa$[]        

! 行ごとの処理
outall$=""
for i=1 to leqa
   msx$="地震情報処理中… ( "+str$(int(i/leqa*100))+" %)"
   console.title msx$

! URLを抽出
   if IS_IN("<url>",eqa$[i])>0 then
     equrl$=replace$(eqa$[i],"<url>","")
     equrl$=replace$(equrl$,"</url>","")
     equrl$=replace$(equrl$,"\t","")
   endif

! 発生日時を抽出
   if IS_IN("<detection_date>",eqa$[i])>0 then
     eqdat$=replace$(eqa$[i],"<detection_date>","")
     eqdat$=replace$(eqdat$,"</detection_date>","")
     eqdat$=replace$(eqdat$,"\t","")
   endif

! 緯度経度を抽出
   if IS_IN("<latitude>",eqa$[i])>0 then
     eqlat$=replace$(eqa$[i],"<latitude>","")
     eqlat$=replace$(eqlat$,"</latitude>","")
     eqlat$=replace$(eqlat$,"\t","")
   endif

   if IS_IN("<longitude>",eqa$[i])>0 then
     eqlon$=replace$(eqa$[i],"<longitude>","")
     eqlon$=replace$(eqlon$,"</longitude>","")
     eqlon$=replace$(eqlon$,"\t","")
   endif

! 地域名を抽出
   if IS_IN("<name>",eqa$[i])>0 then
     eqnam$=replace$(eqa$[i],"<name>","")
     eqnam$=replace$(eqnam$,"</name>","")
     eqnam$=replace$(eqnam$,"\t","")
   endif

! 震度を抽出
   if IS_IN("<intensity_no>",eqa$[i])>0 then
     eqint$=replace$(eqa$[i],"<intensity_no>","")
     eqint$=replace$(eqint$,"</intensity_no>","")
     eqint$=replace$(eqint$,"\t","")
   endif

! マグニチュードを抽出
   if IS_IN("<magnitude>",eqa$[i])>0 then
     eqmag$=replace$(eqa$[i],"<magnitude>","")
     eqmag$=replace$(eqmag$,"</magnitude>","")
     eqmag$=replace$(eqmag$,"\t","")
   endif

! 1件分まとめ
   if IS_IN("</entry>",eqa$[i])>0 then
     outall$=outall$+"発生日時:"+eqdat$+","
     outall$=outall$+"地域:"+eqnam$+","
     outall$=outall$+"震度:"+eqint$+","
     outall$=outall$+"url:"+equrl$+","
     outall$=outall$+"緯度:"+eqlat$+","
     outall$=outall$+"経度:"+eqlon$+","
     outall$=outall$+"マグニチュード:"+eqmag$+"\n"
   endif

next i

array.delete eqa$[]

とても簡単だと思います。

4.まとめ

地震情報のような災害系のAPIはいつ発生するかわからないので情報提供の継続性が重要だと思います。

「なまず」さんはその意味で素晴らしく今後も続けて頂けるといいなぁと思っています。

以 上

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment