APOD API Reference¶
This page provides a breakdown of the aionasa APOD module.
Parameters for APOD API:
date: The date of the APOD image to retrieve. Defaults to ‘today’.
start_date: The first date to return when requesting a list of dates.
end_date: The last date to return when requesting a list of dates. Range is inclusive.
hd: Bool indicating whether to retrieve the URL for the high resolution image. Defaults to ‘False’. This is present for legacy purposes, it is always ignored by the service and high-resolution urls are returned regardless.
concept_tags: DISABLED FOR THIS ENDPOINT.
Client¶
- class aionasa.apod.APOD(api_key='DEMO_KEY', session=None, rate_limiter=<default_rate_limiter>)¶
Client for NASA Astronomy Picture of the Day API.
- Parameters
api_key (
str
) – NASA API key to be used by the client.session (
Optional[aiohttp.ClientSession]
) – Optional ClientSession to be used for requests made by this client. Creates a new session by default.rate_limiter (
Optional[RateLimiter]
) – Optional RateLimiter class to be used by this client. Uses the library’s internal global rate limiting by default.
- await batch_get(start_date: datetime.date, end_date: datetime.date, as_json: bool = False)¶
Retrieves multiple items from NASA’s APOD API. Returns a list of APOD entries.
- Parameters
start_date (
datetime.Date
) – The first date to return when requesting a range of dates.end_date (
datetime.Date
) – The last date to return when requesting a range of dates. Range is inclusive.as_json (
bool
) – Bool indicating whether to return a list of dicts containing the raw returned json data instead of the normalList[AstronomyPicture]
. Defaults toFalse
.
- Returns
A list of AstronomyPicture objects containing data returned by the API.
- Return type
List[AstronomyPicture]
- await get(date: Optional[datetime.date] = None, as_json: bool = False)¶
Retrieves a single item from NASA’s APOD API.
- Parameters
date (
datetime.Date
) – The date of the APOD image to retrieve. Defaults to'today'
.as_json (
bool
) – Bool indicating whether to return the raw returned json data instead of the normal AstronomyPicture object. Defaults toFalse
.
- Returns
An AstronomyPicture containing data returned by the API.
- Return type
Data Class¶
- class aionasa.apod.AstronomyPicture(client, date: datetime.date, json)¶
A class representing a single daily APOD picture. .. attribute:: client
The APOD client that was used to retrieve this data.
- type
- date¶
The date this image was uploaded to APOD.
- Type
datetime.Date
- copyright¶
The owner of this image, if it is not public domain.
- title¶
The APOD entry’s title.
- explanation¶
The explanation for this image, written by astronomers, from this image’s APOD page.
- url¶
The image url.
- hdurl¶
The HD image url, if available. Can be
None
.
- html_url¶
The url of the APOD HTML page. This is the page a user would find this image on. This data is not provided by the API. This attribute has been added by the library for ease of use.
- media_type¶
The type of media. Will pretty much always be
'image'
.
- service_version¶
The API service version. The API version is currently
'v1'
.
- await read(hdurl: bool = True)¶
Downloads the image associated with this AstronomyPicture.
- Parameters
hdurl (
bool
) – Indicates that the HD image should be downloaded, if possible.- Returns
The image, downloaded from the URL provided by the API.
- Return type
bytes
- await read_chunk(chunk_size: int, hdurl: bool = True)¶
Reads a chunk of the image associated with this AstronomyPicture.
- Parameters
chunk_size (
int
) – Number of bytes to read.hdurl (
bool
) – Indicates that the HD image should be downloaded, if possible.
- Returns
The chunked data. Will be None if the image has been completely read.
- Return type
bytes
- await save(path=None, hdurl: bool = True)¶
Downloads the image associated with this AstronomyPicture and saves to a file.
- Parameters
path – The file path at which to save the image. If
None
, saves the image to the working directory using the filename from the image url.hdurl (
bool
) – Indicates that the HD image should be downloaded, if possible.
Example Code¶
This is a simple script that will return the title, explanation, and url from the most recent Astronomy Picture of the Day page, then download and save the image.
import asyncio
from aionasa import APOD
async def main():
async with APOD() as apod:
apod_entry = await apod.get()
print(f'{apod_entry.title}\n'
f'{apod_entry.explanation}'
f'\n{apod_entry.hdurl}')
await apod_entry.save()
asyncio.run(main())
CLI Example¶
This command, like the above python script, will print data returned by the APOD API, then download and save the image.
python3 -m aionasa.apod --print --download .