Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 5 additions & 4 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 Expand Up @@ -298,7 +299,7 @@ def page_not_found(e):
"""
Return a custom 404 error.
"""
LOG.info('Invalid page request: ' + e)
LOG.info('Invalid page request: ' + str(e))
return _abort(404, 'Sorry, Nothing at this URL.', usage=True)


Expand Down