気象庁天気予報のAPIを使ってみた

Bonjour!!

 

以前作ったその日の降水確率を朝7時に知らせてくれるLineBotが突然動かなくなりました

朝AM7:00に名古屋の降水確率を知らせるLine Bot

ちょっと調べてみるとどうやらサイトのレイアウトが変わったようです。

前回はphpqueryを利用してサイト上の情報をスクレイピングしているので、

レイアウトが変更されるとどうしてもそのたびに修正が必要になってきますね。

 

そんな中気象庁の天気予報にAPIが用意されたという情報を見かけました

気象庁公式の天気予報API(?)が発見 ~Twitterの開発者界隈に喜びの声が満ちる

 

 

気象庁の中の人によるとオフィシャルなAPIではないようですが、利用に関しては

ルールに則ってすれば問題内容なので、さっそく使ってみました。

 

 

 

以下のエンドポイントにアクセスするとJSON形式で情報が表示されます。

(forecastのあとの数字を変えると別の地域の予報を取得できます。)

名古屋の天気→https://www.jma.go.jp/bosai/forecast/data/forecast/230000.json

 

 

そのままだととても見づらいので、整形してくれるサイトの力を借りて見やすい形にします。

  1. [
  2.     {
  3.         “publishingOffice”: “名古屋地方気象台”,
  4.         “reportDatetime”: “2021-04-19T17:00:00+09:00”,
  5.         “timeSeries”: [
  6.             {
  7.                 “timeDefines”: [
  8.                     “2021-04-19T17:00:00+09:00”,
  9.                     “2021-04-20T00:00:00+09:00”,
  10.                     “2021-04-21T00:00:00+09:00”
  11.                 ],
  12.                 “areas”: [
  13.                     {
  14.                         “area”: {
  15.                             “name”: “西部”,
  16.                             “code”: “230010”
  17.                         },
  18.                         “weatherCodes”: [
  19.                             “100”,
  20.                             “100”,
  21.                             “100”
  22.                         ],
  23.                         “weathers”: [
  24.                             “晴れ”,
  25.                             “晴れ”,
  26.                             “晴れ”
  27.                         ],
  28.                         “winds”: [
  29.                             “北西の風”,
  30.                             “北西の風”,
  31.                             “北西の風 後 西の風”
  32.                         ],
  33.                         “waves”: [
  34.                             “0.5メートル”,
  35.                             “0.5メートル”,
  36.                             “0.5メートル”
  37.                         ]
  38.                     },
  39.                     {
  40.                         “area”: {
  41.                             “name”: “東部”,
  42.                             “code”: “230020”
  43.                         },
  44.                         “weatherCodes”: [
  45.                             “100”,
  46.                             “100”,
  47.                             “100”
  48.                         ],
  49.                         “weathers”: [
  50.                             “晴れ”,
  51.                             “晴れ”,
  52.                             “晴れ”
  53.                         ],
  54.                         “winds”: [
  55.                             “北西の風 東三河南部 では はじめ 北西の風 やや強く”,
  56.                             “北西の風”,
  57.                             “北西の風 後 西の風”
  58.                         ],
  59.                         “waves”: [
  60.                             “1.5メートル ただし 内海 では 0.5メートル”,
  61.                             “1.5メートル ただし 内海 では 0.5メートル”,
  62.                             “1.5メートル ただし 内海 では 0.5メートル”
  63.                         ]
  64.                     }
  65.                 ]
  66.             },
  67.             {
  68.                 “timeDefines”: [
  69.                     “2021-04-19T18:00:00+09:00”,
  70.                     “2021-04-20T00:00:00+09:00”,
  71.                     “2021-04-20T06:00:00+09:00”,
  72.                     “2021-04-20T12:00:00+09:00”,
  73.                     “2021-04-20T18:00:00+09:00”
  74.                 ],
  75.                 “areas”: [
  76.                     {
  77.                         “area”: {
  78.                             “name”: “西部”,
  79.                             “code”: “230010”
  80.                         },
  81.                         “pops”: [
  82.                             “0”,
  83.                             “0”,
  84.                             “0”,
  85.                             “0”,
  86.                             “0”
  87.                         ]
  88.                     },
  89.                     {
  90.                         “area”: {
  91.                             “name”: “東部”,
  92.                             “code”: “230020”
  93.                         },
  94.                         “pops”: [
  95.                             “0”,
  96.                             “0”,
  97.                             “0”,
  98.                             “0”,
  99.                             “0”
  100.                         ]
  101.                     }
  102.                 ]
  103.             },
  104.             {
  105.                 “timeDefines”: [
  106.                     “2021-04-20T00:00:00+09:00”,
  107.                     “2021-04-20T09:00:00+09:00”
  108.                 ],
  109.                 “areas”: [
  110.                     {
  111.                         “area”: {
  112.                             “name”: “名古屋”,
  113.                             “code”: “51106”
  114.                         },
  115.                         “temps”: [
  116.                             “8”,
  117.                             “25”
  118.                         ]
  119.                     },
  120.                     {
  121.                         “area”: {
  122.                             “name”: “豊橋”,
  123.                             “code”: “51331”
  124.                         },
  125.                         “temps”: [
  126.                             “9”,
  127.                             “22”
  128.                         ]
  129.                     }
  130.                 ]
  131.             }
  132.         ]
  133.     },
  134.     {
  135.         “publishingOffice”: “名古屋地方気象台”,
  136.         “reportDatetime”: “2021-04-19T17:00:00+09:00”,
  137.         “timeSeries”: [
  138.             {
  139.                 “timeDefines”: [
  140.                     “2021-04-20T00:00:00+09:00”,
  141.                     “2021-04-21T00:00:00+09:00”,
  142.                     “2021-04-22T00:00:00+09:00”,
  143.                     “2021-04-23T00:00:00+09:00”,
  144.                     “2021-04-24T00:00:00+09:00”,
  145.                     “2021-04-25T00:00:00+09:00”,
  146.                     “2021-04-26T00:00:00+09:00”
  147.                 ],
  148.                 “areas”: [
  149.                     {
  150.                         “area”: {
  151.                             “name”: “愛知県”,
  152.                             “code”: “230000”
  153.                         },
  154.                         “weatherCodes”: [
  155.                             “100”,
  156.                             “100”,
  157.                             “101”,
  158.                             “101”,
  159.                             “201”,
  160.                             “201”,
  161.                             “201”
  162.                         ],
  163.                         “pops”: [
  164.                             “”,
  165.                             “0”,
  166.                             “10”,
  167.                             “20”,
  168.                             “30”,
  169.                             “30”,
  170.                             “30”
  171.                         ],
  172.                         “reliabilities”: [
  173.                             “”,
  174.                             “”,
  175.                             “A”,
  176.                             “A”,
  177.                             “A”,
  178.                             “A”,
  179.                             “A”
  180.                         ]
  181.                     }
  182.                 ]
  183.             },
  184.             {
  185.                 “timeDefines”: [
  186.                     “2021-04-20T00:00:00+09:00”,
  187.                     “2021-04-21T00:00:00+09:00”,
  188.                     “2021-04-22T00:00:00+09:00”,
  189.                     “2021-04-23T00:00:00+09:00”,
  190.                     “2021-04-24T00:00:00+09:00”,
  191.                     “2021-04-25T00:00:00+09:00”,
  192.                     “2021-04-26T00:00:00+09:00”
  193.                 ],
  194.                 “areas”: [
  195.                     {
  196.                         “area”: {
  197.                             “name”: “名古屋”,
  198.                             “code”: “51106”
  199.                         },
  200.                         “tempsMin”: [
  201.                             “”,
  202.                             “9”,
  203.                             “11”,
  204.                             “13”,
  205.                             “14”,
  206.                             “13”,
  207.                             “13”
  208.                         ],
  209.                         “tempsMinUpper”: [
  210.                             “”,
  211.                             “13”,
  212.                             “13”,
  213.                             “14”,
  214.                             “16”,
  215.                             “15”,
  216.                             “15”
  217.                         ],
  218.                         “tempsMinLower”: [
  219.                             “”,
  220.                             “8”,
  221.                             “9”,
  222.                             “11”,
  223.                             “12”,
  224.                             “10”,
  225.                             “10”
  226.                         ],
  227.                         “tempsMax”: [
  228.                             “”,
  229.                             “26”,
  230.                             “27”,
  231.                             “25”,
  232.                             “22”,
  233.                             “23”,
  234.                             “23”
  235.                         ],
  236.                         “tempsMaxUpper”: [
  237.                             “”,
  238.                             “28”,
  239.                             “29”,
  240.                             “27”,
  241.                             “26”,
  242.                             “26”,
  243.                             “26”
  244.                         ],
  245.                         “tempsMaxLower”: [
  246.                             “”,
  247.                             “24”,
  248.                             “25”,
  249.                             “22”,
  250.                             “19”,
  251.                             “20”,
  252.                             “20”
  253.                         ]
  254.                     }
  255.                 ]
  256.             }
  257.         ],
  258.         “tempAverage”: {
  259.             “areas”: [
  260.                 {
  261.                     “area”: {
  262.                         “name”: “名古屋”,
  263.                         “code”: “51106”
  264.                     },
  265.                     “min”: “11.0”,
  266.                     “max”: “21.2”
  267.                 }
  268.             ]
  269.         },
  270.         “precipAverage”: {
  271.             “areas”: [
  272.                 {
  273.                     “area”: {
  274.                         “name”: “名古屋”,
  275.                         “code”: “51106”
  276.                     },
  277.                     “min”: “12.8”,
  278.                     “max”: “34.7”
  279.                 }
  280.             ]
  281.         }
  282.     }
  283. ]

 

3日分の天気予報の情報が含まれています。

今回のボットは当日の3つの時間帯での降水確率を取得したいので、以下のようになります。

  1. $rain_day[0] = $arr[0][“timeSeries”][1][“areas”][0][“pops”][0]; /*6-12*/
  2. $rain_day[1] = $arr[0][“timeSeries”][1][“areas”][0][“pops”][1];     /*12-18*/
  3. $rain_day[2] = $arr[0][“timeSeries”][1][“areas”][0][“pops”][2];   /*18-24*/

 

他にも最大気温や最低気温といった情報もとってこれます。

 

 

これで気象庁がこのJSONを基にページを作っている限りはレイアウト変更があってもBotが途切れることはなくなりました。

 

ではAu revoir!!!