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 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
def get_holidays(): logger.debug("Received call to Holidays controller from {0}".format(str(flask.request.remote_addr))) if settings.getboolean('calendar', 'hide_holidays'): return flask.jsonify(results=[]) calendar = fetch_calendar('holidays') events = [] for event in calendar.walk('VEVENT'): if datetime.date.today() <= event['dtstart'].dt: events.append({ 'summary' : event['summary'], 'start' : str(event['dtstart'].dt), 'end' : str(event['dtend'].dt)}) logger.info("Holiday request succeeded and yielded {0} holidays".format(len(events))) return flask.jsonify(results=events)