def fetch_weather(token): logger.debug("Fetching weather data for token '{0}'".format(str(token))) url = settings.get('mock', token) \ if settings.getboolean('app', 'debug') and settings.has_section('mock') \ else settings.get('weather', token) logger.debug("WEATHER - URL fetched for weather data was '{0}'".format(str(url))) if url.startswith('http'): logger.debug("WEATHER - URL '{0}' has http, attempting http request".format(str(url))) try: weather = json.loads(requests.get(url).content) except (requests.exceptions.missingSchema, ValueError) as e: # json.loads throws ValueError on invalid json string logger.error("Exception {0} caught while trying to fetch weather. Message: {1}".format(e.__class__, e.message)) flask.abort(400, e.message) else: logger.debug("WEATHER - URL '{0}' had no http, attempting to open file".format(str(url))) try: with open(url, 'rb') as f: weather = json.load(f) except IOError as e: logger.error("Exception {0} caught while trying to fetch weather. Message: {1}".format(e.__class__, e.message)) flask.abort(500, e.message) return weather
def get_weather_current(): logger.debug("Received call to Current Weather controller from {0}".format(str(flask.request.remote_addr))) weather = fetch_weather('weather') try: result = { "city" : weather['name'], "country" : weather['sys']['country'], # Use ISO 8601 standard for representing datetime as string, instead of the unix timestamp returned by OpenWeatherAPI # TODO: Handle case where API returns correct format, or different formats. "time" : datetime.datetime.fromtimestamp(int(weather['dt'])).strftime('%Y-%m-%d %H:%M:%S'), "weather" : weather['weather'], "wind" : weather['wind'], "cloudpercent" : weather['clouds']['all'], "temp" : { "current" : weather['main']['temp'], "max" : weather['main']['temp_max'], "min" : weather['main']['temp_min'] }, "sun" : { "rise" : weather['sys']['sunrise'], "set" : weather['sys']['sunset'] } } except KeyError as e: logger.error("KeyError received on key {0}".format(e.message)) flask.abort(400, "KeyError received on key '{0}'. This probably happened because the structure of the API response is different than expected.".format(e.message)) return flask.jsonify(result)
def fetch_calendar(token): logger.debug("Fetching calendar data for token '{0}'".format(str(token))) url = settings.get('mock', token) \ if settings.getboolean('app', 'debug') and settings.has_section('mock') \ else settings.get('calendar', token) logger.debug("CALENDAR - URL fetched for calendar data was '{0}'".format(str(url))) if url.startswith('http'): logger.debug("CALENDAR - URL '{0}' has http, attempting http request".format(str(url))) try: calendar = icalendar.Calendar.from_ical(requests.get(url).content) except (requests.exceptions.MissingSchema, ValueError) as e: # icalendar throws ValueError on invalid ical format logger.error("Exception {0} caught while trying to fetch calendar. Message: {1}".format(e.__class__, e.message)) flask.abort(400, e.message) else: logger.debug("CALENDAR - URL '{0}' had no http, attempting to open file".format(str(url))) try: with open(url, 'rb') as f: calendar = icalendar.Calendar.from_ical(f.read()) except IOError as e: logger.error("Exception {0} caught while trying to fetch calendar. Message: {1}".format(e.__class__, e.message)) flask.abort(500, e.message) return calendar