Skip to content

Commit

Permalink
Fixed up WeatherScraper.py to better handle error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
hyakuhei committed Oct 6, 2015
1 parent 4908bea commit 69252fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ RUN apt-get update && apt-get install -y \
RUN adduser --disabled-password --gecos '' stormforce
WORKDIR /home/stormforce
RUN git clone https://github.com/hyakuhei/Stormforce.git
WORKDIR /home/stormforce/
RUN chown stormforce:stormforce *
WORKDIR /home/stormforce/Stormforce
RUN pip install -r requirements.txt
RUN chown stormforce:stormforce *

# anchor user operations
RUN su - stormforce
Expand Down
27 changes: 13 additions & 14 deletions WeatherScraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def wwoLocationLookup(self,lat,lon):

locData = ret.json()
wwoLocations = {}
if "search_api" not in locData:
return None, {}

for loc in locData['search_api']['result']:
name = loc['areaName'][0]['value']
wwoLocations[name] = {
Expand Down Expand Up @@ -152,7 +155,7 @@ def getConditions(self,lat,lon):
else:
logger.debug("Retreived Maritime Conditions")


response = {"hasWeather":False}
data = ret.json()
if "weather" not in data["data"]:
Expand All @@ -170,18 +173,18 @@ def getConditions(self,lat,lon):
response['swellDir']=weather['swellDir']
response['swellPeriod']=weather['swellPeriod_secs']
response['waterTemp']=weather['waterTemp_C']

response['hasWeather']=False
except Exception as e:
logger.debug("WWO Maritime Weather is not formatted as expected")
logger.debug(ret.json())

#First off, we try to get the nearest location from GeoNames because their API has lots of capacity
#If that doesn't work we'll try to fetch it from WWO

tides = None
data = None

# Get a lookup from Geonames
# Attempt to scrape just that from tide forecast
# If that fails, get a bunch of places from WWO
Expand All @@ -197,8 +200,8 @@ def getConditions(self,lat,lon):
except Exception as e:
logger.error("WWO Lookup for {} failed".format(closestLocationName))
logger.error(e)
#if geonames couldn't find data _or_ the tide lookup failed

#if geonames couldn't find data _or_ the tide lookup failed
if closestLocationName == None or data == None:
logger.debug("Falling back to WWO lookup")
closestLocationName, wwodata = self.wwoLocationLookup(lat=lat,lon=lon)
Expand All @@ -208,15 +211,11 @@ def getConditions(self,lat,lon):
data, establishedLocation = self.ts.scrapeTideForecast(alternates,bestguess=closestLocationName)
except Exception as e:
logger.error(e)

if data == None:
errordata = {}
if closestLocationName != None:
errordata['error'] = "No Tide Data"
errordata['location'] = closestLocationName
else:
errordat['error'] = "No Location Match"

response['error'] = "No Tide Data"
response['loctation'] = closestLocationName
return response

response['location'] = establishedLocation

Expand Down

0 comments on commit 69252fe

Please sign in to comment.