Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
def init():
	# Check if logging folder exists
	log_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs')
	if not os.path.exists(log_path):
		os.makedirs(log_path)

	# Checking for logger config files
	config_path = settings.get('logger', 'config')
	if os.path.exists(config_path):
		with open(config_path, 'rt') as f:
			config = json.load(f)
		logging.config.dictConfig(config)
	else:
		logging.basicConfig(level=logging.INFO)

	return logging.getLogger('mirror')
Beispiel #4
0
def init():
    # Check if logging folder exists
    log_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs')
    if not os.path.exists(log_path):
        os.makedirs(log_path)

    # Checking for logger config files
    config_path = settings.get('logger', 'config')
    if os.path.exists(config_path):
        with open(config_path, 'rt') as f:
            config = json.load(f)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=logging.INFO)

    return logging.getLogger('mirror')