Skip to content

Commit

Permalink
Changed response format slightly so that a lack of maritime conditions
Browse files Browse the repository at this point in the history
information doesn't break the output.
  • Loading branch information
robert authored and robert committed Oct 5, 2015
1 parent 3d7787f commit 4908bea
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions WeatherScraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ def wwoLocationLookup(self,lat,lon):
return closestLocationName, wwoLocations

def getConditions(self,lat,lon):
logger.debug("Attempting to get conditions at {},{}".format(lat,lon))
"""
Get approximate location from WWO
If that location includes tidal information, return it all pretty
If not, it might be a position in the UK, grok the BBC tides page using the location from WWO for a town/port name
"""

#Get general tidal weather at that Lat and Lon
logger.debug("Attempting to get conditions at {},{}".format(lat,lon))
waveUrl = self.wwo_marineurl.format(key=self.wwo_apikey,lat=lat,lon=lon)

ret = requests.get(waveUrl)
Expand All @@ -152,43 +152,71 @@ def getConditions(self,lat,lon):
else:
logger.debug("Retreived Maritime Conditions")


response = {"hasWeather":False}
data = ret.json()
weather = data['data']['weather'][0]['hourly'][0]

response = {}
response['windDirection16pt']=weather['winddir16Point']
response['windDirection']=weather['winddirDegree']
response['windSpeedKnots']=self.milesToKnots(weather['windspeedMiles'])
response['windBeaufort']=self.milesToBeaufort(weather['windspeedMiles'])
response['swellHeight']=weather['swellHeight_m']
response['waveHeight']=weather['sigHeight_m']
#response['swellDir16pt']=weather['swellDir16Point']
response['swellDir']=weather['swellDir']
response['swellPeriod']=weather['swellPeriod_secs']
response['waterTemp']=weather['waterTemp_C']

if "weather" not in data["data"]:
logger.debug("WWO Maritime Weather Lookup Failed");
else:
try:
weather = data['data']['weather'][0]['hourly'][0]
response['windDirection16pt']=weather['winddir16Point']
response['windDirection']=weather['winddirDegree']
response['windSpeedKnots']=self.milesToKnots(weather['windspeedMiles'])
response['windBeaufort']=self.milesToBeaufort(weather['windspeedMiles'])
response['swellHeight']=weather['swellHeight_m']
response['waveHeight']=weather['sigHeight_m']
#response['swellDir16pt']=weather['swellDir16Point']
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
# Scrape tide forecast for each of them

closestLocationName = self.geonameLookup(lat=lat,lon=lon)
alternates = [closestLocationName]
if closestLocationName == None:
logger.debug("geonameLookup failed.")
if closestLocationName != None:
logger.debug("Geonames lookup returned {}".format(closestLocationName))
try:
logger.debug("Attempting to find tidal data for any of {}".format(alternates))
data, establishedLocation = self.ts.scrapeTideForecast(alternates,bestguess=closestLocationName)
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 closestLocationName == None or data == None:
logger.debug("Falling back to WWO lookup")
closestLocationName, wwodata = self.wwoLocationLookup(lat=lat,lon=lon)
alternates = wwodata.keys()

if closestLocationName == None:
logger.debug("wwodata lookup failed too!")

try:
logger.debug("Attempting to find tidal data for any of {}".format(alternates))
data, establishedLocation = self.ts.scrapeTideForecast(alternates,bestguess=closestLocationName)
except Exception as e:
logger.error(e)
return {'error':'error'}
try:
logger.debug("Attempting to find tidal data for any of {}".format(alternates))
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['location'] = establishedLocation

Expand Down

0 comments on commit 4908bea

Please sign in to comment.