Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework default date #51

Merged
merged 4 commits into from
Nov 20, 2020
Merged

Conversation

nwunderly
Copy link
Contributor

When the &date= parameter is left out of the query, the API currently defaults to datetime.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 specifying date, 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 the date 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.

This allows the date to be pulled from an APOD page.

I also replaced the request for today's date + fallback to yesterday with a request to astropix.html + date parsing.
date will be None if parameter is not specified, passing None down makes _get_apod_chars use astropix.html instead of the date-formatted page (this url will default to the most recent)
I realized this error handling seems to be needed for requesting random dates, so I left it in with the added check that dt must not be None (when checking if use_default_today_date is True)
@lgtm-com
Copy link

lgtm-com bot commented Oct 22, 2020

This pull request introduces 1 alert when merging 97aa47a into a434768 - view on LGTM.com

new alerts:

  • 1 for Except block handles 'BaseException'

@nwunderly nwunderly closed this Oct 22, 2020
@nwunderly nwunderly reopened this Oct 22, 2020
@JustinGOSSES JustinGOSSES self-assigned this Oct 30, 2020
@JustinGOSSES
Copy link
Contributor

Anyway this can mess up someone else's code that uses this API because the assumed behavior has changed??? Thoughts?

@nwunderly
Copy link
Contributor Author

I suppose it could. Looking at the source code, though, it seems the API's intended behavior was for it to do this, since it tries to fall back to the previous date if it fails to retrieve the current one.

I implemented this because it seemed that this functionality was intended, but not working properly.

@JustinGOSSES
Copy link
Contributor

Okay, I'll merge. It won't get deployed until later today or perhaps Monday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants