見出し画像

NASAのAPIを使ってみる

みなさん、こんにちは、こんばんは。S.Kと申します。

ふと面白いAPIはないか探してみたところ、NASAのAPIが公開されてたので、見てみました。氏名(FirstName/LastName)メールアドレス(Email)の情報が必要ですが、無料です。

一応、上記情報がなくても、制限はありますが、デモ用のキーでも触ることはできるようです。

リファレンス

NASAについて 

NASA 公式

NASA API リファレンス

API利用法

まずは、デモ用のキーを使います。IPアドレス毎に、時間や日単位で制限されてます。太字で強調しましたが、自分のAPIキーを取得した方が良いです。

DEMO_KEY Rate Limits
In documentation examples, the special DEMO_KEY api key is used.
This API key can be used for initially exploring APIs prior to signing up, but it has much lower rate limits, so you’re encouraged to signup for your own API key if you plan to use the API (signup is quick and easy). The rate limits for the DEMO_KEY are:
・Hourly Limit: 30 requests per IP address per hour
・Daily Limit: 50 requests per IP address per day

★デモ用のキーで利用

今回は・・・

にあるAstronomy Picture of the Day(APOD)の情報を取得するAPIを利用してみます。このサイトでは毎日異なる宇宙の画像を表示しています。

https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY

ウェブブラウザで上記入力してみてください。今日の日付(2021/02/12)だと以下のようなJSONが返ってきます。

{
	"copyright": "Mike Selby",
	"date": "2021-02-12",
	"explanation": "This gorgeous island universe lies about 85 million light-years distant in the southern constellation Fornax. Inhabited by young blue star clusters, the tightly wound spiral arms of NGC 1350 seem to join in a circle around the galaxy's large, bright nucleus, giving it the appearance of a cosmic eye. In fact, NGC 1350 is about 130,000 light-years across. That makes it as large or slightly larger than the Milky Way. For earth-based astronomers, NGC 1350 is seen on the outskirts of the Fornax cluster of galaxies, but its estimated distance suggests that it is not itself a cluster member. Of course, the bright spiky stars in the foreground of this telescopic field of view are members of our own spiral Milky Way galaxy.",
	"hdurl": "https://apod.nasa.gov/apod/image/2102/NGC1350_crop.jpg",
	"media_type": "image",
	"service_version": "v1",
	"title": "Spiral Galaxy NGC 1350",
	"url": "https://apod.nasa.gov/apod/image/2102/NGC1350_crop1024.jpg"
}

ちなみに上記JSON内のURLは、APODで公開されている画像のURLです。

画像2

copyright@Mike Selby

またexplanationに書かれているのもAPODに書かれているものになります。

Google翻訳より

この豪華な島の宇宙は、南の星座であるろ座から約8500万光年離れたところにあります。
若い青い星団が生息するNGC1350のきつく巻かれた渦巻腕は、銀河の大きくて明るい核の周りで円を描いて結合しているように見え、宇宙の目のように見えます。
実際、NGC1350は全体で約13万光年です。
それは天の川と同じかわずかに大きいです。
地球ベースの天文学者の場合、NGC 1350は、ろ座銀河団の郊外に見られますが、その推定距離は、それ自体が銀河団のメンバーではないことを示唆しています。もちろん、この望遠鏡の視野の前景にある明るいとがった星は、我々の渦巻き状の天の川銀河のメンバーです。

copyright@Mike Selby

APIのクエリとして、dateを指定できるので例えば昨年の2020年02月12日を指定してみます。

https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=2020-02-12
{
	"copyright": "Saeid Parchini",
	"date": "2020-02-12",
	"explanation": "What divides the north from the south? It all has to do with the spin of the Earth. On Earth's surface, the equator is the dividing line, but on Earth's sky, the dividing line is the Celestial Equator -- the equator's projection onto the sky.  You likely can't see the Earth's equator around you, but anyone with a clear night sky can find the Celestial Equator by watching stars move.  Just locate the dividing line between stars that arc north and stars that arc south.  Were you on Earth's equator, the Celestial Equator would go straight up and down.  In general, the angle between the Celestial Equator and the vertical is your latitude.  The featured image combines 325 photos taken every 30 seconds over 162 minutes. Taken soon after sunset earlier this month, moonlight illuminates a snowy and desolate scene in northwest Iran. The bright streak behind the lone tree is the planet Venus setting.  Almost Hyperspace: Random APOD Generator",
	"hdurl": "https://apod.nasa.gov/apod/image/2002/StarTrailsTree_Parchini_4000.jpg",
	"media_type": "image",
	"service_version": "v1",
	"title": "Star Trails of the North and South",
	"url": "https://apod.nasa.gov/apod/image/2002/StarTrailsTree_Parchini_1080.jpg"
}

copyright@Saeid Parchini

画像は各自確認していただきたいのですが、神秘的な写真でした。

★APIキー取得

NASA API リファレンスでメールアドレスを利用して登録します。

スクリーンショット 2021-02-12 21.13.25

こちらの場合の制限は1時間あたり1000リクエストです。
これを超えてしまうと一時的にブロックされてしまいます。
1時間後に利用可能となるようです。このリクエストじゃ足らないという方は別途連絡くださいとのこと。

Web Service Rate Limits

Limits are placed on the number of API requests you may make using your API key. Rate limits may vary by service, but the defaults are:

・Hourly Limit: 1,000 requests per hour

For each API key, these limits are applied across all api.nasa.gov API requests. Exceeding these limits will lead to your API key being temporarily blocked from making further requests. The block will automatically be lifted by waiting an hour. If you need higher rate limits, contact us.

また、現在の利用状況も知ることができるようです。
rolling basisの意味を知らなかったんですが、「随時」とか「つどつど」ってことですね。勉強になります。
以下の例で言ってるのは、
(1) 10:15AMに 500 リクエスト
(2) 10:25AMに 500 リクエスト
で1000リクエストを超えたので、API利用がブロックされます。
ですが、
11:15AMに、500 リクエスト((1)で利用した分)は解除されるということです。

How Do I See My Current Usage?

Your can check your current rate limit and usage details by inspecting the X-RateLimit-Limit and X-RateLimit-Remaining HTTP headers that are returned on every API response.
For example, if an API has the default hourly limit of 1,000 request, after making 2 requests, you will receive this HTTP header in the response of the second request:
X-RateLimit-Remaining: 998
The hourly counters for your API key reset on a rolling basis.

Example: If you made 500 requests at 10:15AM and 500 requests at 10:25AM, your API key would become temporarily blocked. This temporary block of your API key would cease at 11:15AM, at which point you could make 500 requests. At 11:25AM, you could then make another 500 requests.
Anyone can register for an api.nasa.gov key, which can be used to access data across federal agencies.

APIキーが入手できれば以下のように指定して利用ください。

https://api.nasa.gov/planetary/apod?api_key=ここに入手したAPIキー

このNASA APIを利用して何か面白いものができるといいですね。
以上です。また、別の記事でお会いしましょう。

余談(追記)

ここで公開されている火星の天気情報もNASAのAPIで取得できるようです。以下サンプル。

https://api.nasa.gov/insight_weather/?api_key=DEMO_KEY&feedtype=json&ver=1.0

関連記事


活動費、テキスト購入費に充てたいと思います。宜しくお願い致します。