OpenWeatherを使ってみる

FreeのGet API keyを押してみる
ちなみに利用条件は、
- 60回/分、1,000,000回/月のリクエスト
- Current Weather
- 3-hour Forecast 5 days(3時間の予報を5日分)
5 day forecast is available at any location on the globe. It includes weather forecast data with 3-hour step. Forecast is available in JSON or XML format.
十分でしょう!

もろもろ入力してCreate Accountする
下3つのチェックはよくある「お便りを受け取りますか?」というやつ
I consent to receive communications from OpenWeather Group of Companies and their partners:
OpenWeather Group of Companies およびそのパートナーからの通信を受け取ることに同意します。

APIの利用目的を聞かれる。独習のためなのでEducation/Scienceとした

こんな内容のメールが届くのでVerify your emailをクリックしてverifyする

verifyするとOpenWeathreの画面が開くので、さっそくAPI Keysをクリックしてみる

API Keyが生成された。上の注意書きは、
You can generate as many API keys as needed for your subscription. We accumulate the total load from all of them.
サブスクリプションに必要な数だけAPIキーを生成することができます。それら全てから合計の負荷を蓄積します。
追加のkeyも生成できる?

API key nameに名前を入力してGenerateしたらkeyが追加された!
なんとなくDefaultのkeyは使いまわさない方がいいような気がする

APIのページからCurrent Weathr Dataを探す。freeプランで使えるAPIはこれのはず。
- Access current weather data for any location including over 200,000 cities
- We collect and process weather data from different sources such as global and local weather models, satellites, radars and a vast network of weather stations
- JSON, XML, and HTML formats
- Included in both free and paid subscriptions
- 20万以上の都市を含む、あらゆる場所の現在の気象データにアクセスできます。
- グローバルおよびローカル気象モデル、人工衛星、レーダー、気象観測所の広大なネットワークなど、さまざまなソースから気象データを収集し、処理します。
- JSON、XML、およびHTML形式
- 無料および有料のサブスクリプションに含まれます。
// 緯度と経度から
https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}
// 都市名から?
https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
City name, state code and country code divided by comma, Please, refer to ISO 3166 for the state codes or country codes.
You can specify the parameter not only in English. In this case, the API response should be returned in the same language as the language of requested location name if the location is in our predefined list of more than 200,000 locations.
都市名、州コード、国コードをカンマで区切ってください。州コード、国コードについてはISO3166を参照してください。
パラメータは英語だけでなく日本語も指定できます。この場合、20万件以上の定義済みリストに含まれている場所であれば、リクエストされた地名の言語と同じ言語でAPI応答が返されます。
ほほう・・・
APIを叩いたら401が返ってきた。
{"cod":401, "message": "Invalid API key. Please see https://openweathermap.org/faq#error401 for more info."}
FAQを見ると、
Your API key is not activated yet. Within the next couple of hours, it will be activated and ready to use.
あなたのAPIキーはまだ有効になっていません。数時間以内にアクティベートされ、使用できるようになる予定です。
前のめりすぎたっぽい。
東京都新宿区大久保
curl https://api.openweathermap.org/data/2.5/weather?zip=169-0072,JP&appid={API Key}&lang=jp
{
"coord": {
"lon": 139.7056,
"lat": 35.7018
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"base": "stations",
"main": {
"temp": 284.52,
"feels_like": 282.89,
"temp_min": 282.98,
"temp_max": 286.3,
"pressure": 1019,
"humidity": 45
},
"visibility": 10000,
"wind": {
"speed": 4.12,
"deg": 350
},
"clouds": {
"all": 75
},
"dt": 1675732660,
"sys": {
"type": 2,
"id": 2001249,
"country": "JP",
"sunrise": 1675719421,
"sunset": 1675757614
},
"timezone": 32400,
"id": 0,
"name": "Ookubo",
"cod": 200
}
地方の都市はけっこう対象外なのかも。札幌市中央区とか404だった
データが取れる都市名かzip codeの一覧がほしい
緯度経度、都市名、天気、アイコン、気温(華氏)、体感温度、風速、日の出日の入りまでもらえるのでけっこう遊べそう
TODO: 応答パラメータの詳細を調べる
応答パラメータの詳細を追記。
{
"coord": {
"lon": 139.7056, // 都市の地理的位置、経度
"lat": 35.7018 // 都市の地理的位置、緯度
},
"weather": [ // ココ、配列なので注意!
{
"id": 803, // 気象条件ID
"main": "Clouds", // 気象パラメータ群(雨、雪、異常気象など)
"description": "broken clouds", // グループ内の気象条件
"icon": "04d" // 天気アイコンID
}
],
"base": "stations", // 内部パラメータ
"main": {
"temp": 284.52, // 気温(華氏)
"feels_like": 282.89, // 体感温度
"temp_min": 282.98, // 現時点での最低気温。現在観測されている最低気温(大規模なメガロポリスや都市部内)。
"temp_max": 286.3, // 現時点での最高気温
"pressure": 1019, // 大気圧(sea_levelまたはgrnd_levelのデータがない場合は海面気圧)、hPa
"humidity": 45 // 湿度, %.
},
"visibility": 10000, // 視認性、メートル。視界の最大値は10km
"wind": {
"speed": 4.12, // 風速(m/s)
"deg": 350 // 風向き(度)
},
"clouds": {
"all": 75 // 曇り度, %.
},
"dt": 1675732660, // データ算出時刻、unix、UTC
"sys": {
"type": 2, // 内部パラメータ
"id": 2001249, // 内部パラメータ
"country": "JP", // 国コード
"sunrise": 1675719421, // 日の出時刻、Unix、UTC
"sunset": 1675757614 // 日没時刻、Unix、UTC
},
"timezone": 32400, // UTCからのシフト(秒
"id": 0, //
"name": "Ookubo",
"cod": 200 // 内部パラメータ
}
記事に起こしたためクローズ。