Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the
&date=
parameter is left out of the query, the API currently defaults todatetime.datetime.today().date()
, which is essentially "today UTC". This works, usually, but starting at 00:00 UTC until the next APOD image is uploaded, this API will 404 when requesting the current APOD image.I saw that the API has a fallback to yesterday's date if today doesn't work, but for some reason this fallback either isn't helping or isn't actually implemented in production. I chose to assume it was implemented in production, but wasn't helping. This was my fix for the issue, under that assumption.
I implemented the
_date
helper function so, when a user makes a request without specifyingdate
, instead of using the datetime module's implementation of "today", makes a request to https://apod.nasa.gov/apod/astropix.html, which will be the most recent valid APOD image. This essentially means the default is handled by apod.nasa.gov, so the API shouldn't 404 anymore when thedate
parameter is left out of the API query.My function processes the HTML returned by apod.nasa.gov and, similarly to how the other helper functions work, returns a datetime.date object created from the date detected on that page. This is the code that actually allows a request to be made to astropix.html instead of the standard formatted date string, while still returning the picture's upload date in the JSON data.