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

Formatting and Pytests #63

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 29 additions & 14 deletions apod/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
# Create urllib3 Pool Manager
http = urllib3.PoolManager()

# function for getting video thumbnails
def _get_thumbs(data):
"""
Function for getting video thumbnails
"""
global video_thumb
if "youtube" in data or "youtu.be" in data:
# get ID from YouTube URL
Expand All @@ -48,21 +50,36 @@ def _get_thumbs(data):

return video_thumb

# function that returns only last URL if there are multiple URLs stacked together

def _get_last_url(data):
"""
Function that returns only last URL if there are multiple URLs stacked together
"""
regex = re.compile("(?:.(?!http[s]?://))+$")
return regex.findall(data)[0]

def _get_apod_chars(dt, thumbs):
media_type = 'image'

def _format_url(dt):
"""
Returns url for APOD page
"""
if dt:
date_str = dt.strftime('%y%m%d')
apod_url = '%sap%s.html' % (BASE, date_str)
else:
apod_url = '%sastropix.html' % BASE
return apod_url


def _get_apod_chars(dt, thumbs):
"""
Gets data from APOD page
"""
media_type = 'image'
apod_url = _format_url(dt)
LOG.debug('OPENING URL:' + apod_url)
res = requests.get(apod_url)

if res.status_code == 404:
return None
# LOG.error(f'No APOD entry for URL: {apod_url}')
Expand Down Expand Up @@ -97,10 +114,8 @@ def _get_apod_chars(dt, thumbs):
media_type = 'other'
data = ''

props = {}
props = {'explanation': _explanation(soup), 'title': _title(soup)}

props['explanation'] = _explanation(soup)
props['title'] = _title(soup)
copyright_text = _copyright(soup)
if copyright_text:
props['copyright'] = copyright_text
Expand Down Expand Up @@ -132,7 +147,7 @@ def _title(soup):
try:
# Handler for later APOD entries
number_of_center_elements = len(soup.find_all('center'))
if(number_of_center_elements == 2):
if (number_of_center_elements == 2):
center_selection = soup.find_all('center')[0]
bold_selection = center_selection.find_all('b')[0]
title = bold_selection.text.strip(' ')
Expand All @@ -148,7 +163,7 @@ def _title(soup):
title = title.encode('latin1').decode('cp1252')
except Exception as ex:
LOG.error(str(ex))

return title
except Exception:
# Handler for early APOD entries
Expand Down Expand Up @@ -264,7 +279,7 @@ def _date(soup):
_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)
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.
Expand All @@ -285,7 +300,7 @@ def _date(soup):
raise Exception('Date not found in soup data.')


def parse_apod(dt, use_default_today_date=False, thumbs=False):
def _image_url(dt, use_default_today_date=False, thumbs=False):
"""
Accepts a date in '%Y-%m-%d' format. Returns the URL of the APOD image
of that day, noting that
Expand All @@ -294,8 +309,8 @@ def parse_apod(dt, use_default_today_date=False, thumbs=False):
LOG.debug('apod chars called date:' + str(dt))

try:
return _get_apod_chars(dt, thumbs)

data = _get_apod_chars(dt, thumbs)
return data['url']
except Exception as ex:

# handle edge case where the service local time
Expand Down
16 changes: 10 additions & 6 deletions apod_parser/apod_object_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from PIL import Image


def get_data(api_key):
raw_response = requests.get(f'https://api.nasa.gov/planetary/apod?api_key={api_key}').text
response = json.loads(raw_response)
Expand All @@ -14,9 +15,9 @@ def get_date(response):
return date


def get_explaination(response):
explaination = response['explanation']
return explaination
def get_explanation(response):
explanation = response['explanation']
return explanation


def get_hdurl(response):
Expand All @@ -28,7 +29,8 @@ def get_media_type(response):
media_type = response['media_type']
return media_type

def get_service_version(response):

def get_service_version(response):
service_version = response['service_version']
return service_version

Expand All @@ -37,16 +39,18 @@ def get_title(response):
service_version = response['title']
return service_version


def get_url(response):
url = response['url']
return url


def download_image(url, date):
if os.path.isfile(f'{date}.png') == False:
if not os.path.isfile(f'{date}.png'):
raw_image = requests.get(url).content
with open(f'{date}.jpg', 'wb') as file:
file.write(raw_image)

else:
return FileExistsError

Expand Down
18 changes: 10 additions & 8 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
@author=JustinGOSSES @email=justin.c.gosses@nasa.gov
"""
import sys

sys.path.insert(0, "../lib")
### justin edit
# justin edit
sys.path.insert(1, ".")

from datetime import datetime, date
Expand All @@ -24,8 +25,8 @@
from apod.utility import parse_apod, get_concepts
import logging

#### added by justin for EB
#from wsgiref.simple_server import make_server
# added by justin for EB
# from wsgiref.simple_server import make_server

application = Flask(__name__)
CORS(application, resources={r"/*": {"expose_headers": ["X-RateLimit-Limit","X-RateLimit-Remaining"]} })
Expand All @@ -44,9 +45,9 @@
try:
with open('alchemy_api.key', 'r') as f:
ALCHEMY_API_KEY = f.read()
#except FileNotFoundError:
# except FileNotFoundError:
except IOError:
LOG.info('WARNING: NO alchemy_api.key found, concept_tagging is NOT supported')
LOG.info('WARNING: NO alchemy_api.key found, concept_tagging is NOT supported')


def _abort(code, msg, usage=True):
Expand Down Expand Up @@ -92,7 +93,7 @@ def _apod_handler(dt, use_concept_tags=False, use_default_today_date=False, thum
served through the API.
"""
try:

page_props = parse_apod(dt, use_default_today_date, thumbs)
if not page_props:
return None
Expand Down Expand Up @@ -167,7 +168,7 @@ def _get_json_for_random_dates(count, use_concept_tags, thumbs):
for date_ordinal in random_date_ordinals:
dt = date.fromordinal(date_ordinal)
data = _apod_handler(dt, use_concept_tags, date_ordinal == today_ordinal, thumbs)

# Handle case where no data is available
if not data:
continue
Expand Down Expand Up @@ -214,7 +215,7 @@ def _get_json_for_date_range(start_date, end_date, use_concept_tags, thumbs):
while start_ordinal <= end_ordinal:
# get data
dt = date.fromordinal(start_ordinal)

data = _apod_handler(dt, use_concept_tags, start_ordinal == today_ordinal, thumbs)

# Handle case where no data is available
Expand Down Expand Up @@ -245,6 +246,7 @@ def home():
methodname=APOD_METHOD_NAME,
usage=_usage(joinstr='", "', prestr='"') + '"')


@application.route('/static/<asset_path>')
def serve_static(asset_path):
return current_app.send_static_file(asset_path)
Expand Down
19 changes: 0 additions & 19 deletions requirements.txt

This file was deleted.

Loading