from flask import Flask from flask_restful import Resource, Api, reqparse from datetime import date, timedelta import configparser import csv import ast import glob import json app = Flask(__name__) api = Api(app) def read_config(): config = configparser.ConfigParser() config.read('config.ini') return config def read_logs(log_folder): # Read some log files # UTC time, so let's look at tomorrow, today, and yesterday. today = date.today() yesterday = today - timedelta(days = 1) tomorrow = today + timedelta(days = 1) file_list = glob.glob(log_folder+str(yesterday)+"*") + \ glob.glob(log_folder+str(today)+"*") + \ glob.glob(log_folder+str(tomorrow)+"*") # https://stackoverflow.com/a/66071962 json_array = [] for file in file_list: with open(file, encoding='utf-8') as csvf: csvReader = csv.DictReader(csvf) for row in csvReader: #add this python dict to json array json_array.append(row) # Add the call and location of this station to the packet info config = read_config() for item in json_array: item['station_name'] = config['Settings']['station_call'] item['station_lat'] = config['Settings']['station_lat'] item['station_lon'] = config['Settings']['station_lon'] return(json_array) class Packets(Resource): def get(self): return {'data': data}, 200 # return data and 200 OK code # Read config config = read_config() log_folder = config['Settings']['log_folder'] # Load logs first (just to check for errors before page loads) data = read_logs(log_folder) api.add_resource(Packets, '/packets') # and '/locations' is our entry point for Locations if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') # run our Flask app