Skip to content

Commit

Permalink
Merge pull request #51 from nwunderly/rework-default-date
Browse files Browse the repository at this point in the history
Rework default date
  • Loading branch information
JustinGOSSES authored Nov 20, 2020
2 parents 49e6a21 + 97aa47a commit 0d02048
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
48 changes: 42 additions & 6 deletions apod/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from bs4 import BeautifulSoup
from datetime import timedelta
import datetime
import requests
import logging
import json
Expand Down Expand Up @@ -53,8 +53,11 @@ def _get_last_url(data):

def _get_apod_chars(dt, thumbs):
media_type = 'image'
date_str = dt.strftime('%y%m%d')
apod_url = '%sap%s.html' % (BASE, date_str)
if dt:
date_str = dt.strftime('%y%m%d')
apod_url = '%sap%s.html' % (BASE, date_str)
else:
apod_url = '%sastropix.html' % BASE
LOG.debug('OPENING URL:' + apod_url)
res = requests.get(apod_url)

Expand Down Expand Up @@ -102,7 +105,10 @@ def _get_apod_chars(dt, thumbs):
props['media_type'] = media_type
if data:
props['url'] = _get_last_url(data)
props['date'] = dt.strftime('%Y-%m-%d')
if dt:
props['date'] = dt.strftime('%Y-%m-%d')
else:
props['date'] = _date(soup)

if hd_data:
props['hdurl'] = _get_last_url(hd_data)
Expand Down Expand Up @@ -247,6 +253,36 @@ def _explanation(soup):
return s


def _date(soup):
"""
Accepts a BeautifulSoup object for the APOD HTML page and returns the
date of the APOD image.
"""
LOG.debug('getting the date from soup data.')
_today = datetime.date.today()
for line in soup.text.split('\n'):
today_year = str(_today.year)
yesterday_year = str((_today-datetime.timedelta(days=1)).year)
# Looks for the first line that starts with the current year.
# This also checks yesterday's year so it doesn't break on January 1st at 00:00 UTC
# before apod.nasa.gov uploads a new image.
if line.startswith(today_year) or line.startswith(yesterday_year):
LOG.debug('found possible date match: ' + line)
# takes apart the date string and turns it into a datetime
try:
year, month, day = line.split()
year = int(year)
month = ['january', 'february', 'march', 'april',
'may', 'june', 'july', 'august',
'september', 'october', 'november', 'december'
].index(month.lower()) + 1
day = int(day)
return datetime.date(year=year, month=month, day=day).strftime('%Y-%m-%d')
except:
LOG.debug('unable to retrieve date from line: ' + line)
raise Exception('Date not found in soup data.')


def parse_apod(dt, use_default_today_date=False, thumbs=False):
"""
Accepts a date in '%Y-%m-%d' format. Returns the URL of the APOD image
Expand All @@ -265,9 +301,9 @@ def parse_apod(dt, use_default_today_date=False, thumbs=False):
# service (can happen because they are deployed in different
# timezones). Use the fallback of prior day's date

if use_default_today_date:
if use_default_today_date and dt:
# try to get the day before
dt = dt - timedelta(days=1)
dt = dt - datetime.timedelta(days=1)
return _get_apod_chars(dt, thumbs)
else:
# pass exception up the call stack
Expand Down
7 changes: 4 additions & 3 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ def _get_json_for_date(input_date, use_concept_tags, thumbs):
use_default_today_date = False
if not input_date:
# fall back to using today's date IF they didn't specify a date
input_date = datetime.strftime(datetime.today(), '%Y-%m-%d')
use_default_today_date = True
dt = input_date # None

# validate input date
dt = datetime.strptime(input_date, '%Y-%m-%d').date()
_validate_date(dt)
else:
dt = datetime.strptime(input_date, '%Y-%m-%d').date()
_validate_date(dt)

# get data
data = _apod_handler(dt, use_concept_tags, use_default_today_date, thumbs)
Expand Down

0 comments on commit 0d02048

Please sign in to comment.