from flask import Flask, request, render_template, redirect from typing import TypedDict import configparser import sqlite3 import os from ipe_fetcher import Liazo,Axione,Arcep,AreaCoordinates from eligibility_api.elig_api_exceptions import FlaskExceptions from eligibility_api.elig_api_routes import EligibilityApiRoutes class Config(TypedDict): axione_ipe_path: str axione_ipe_db_name: str arcep_ipe_path: str arcep_ipe_db_name: str def parseConfig() -> Config: cfg_path = os.environ.get("CONFIG", "/etc/ftth-ipe-map/conf.ini") cfg = configparser.ConfigParser() with open(cfg_path, "r") as f: cfg.read_file(f) return { "axione_ipe_path": cfg.get("DB", "axione_ipe_path"), "axione_ipe_db_name": cfg.get("DB", "axione_ipe_db_name"), "arcep_ipe_path": cfg.get("DB", "arcep_ipe_path"), "arcep_ipe_db_name": cfg.get("DB", "arcep_ipe_db_name"), } app = Flask(__name__) cfg: Config = parseConfig() axione = Axione(cfg.get("axione_ipe_path"), cfg.get("axione_ipe_db_name")) arcep = Arcep(cfg.get("arcep_ipe_path"), cfg.get("arcep_ipe_db_name")) liazo = Liazo() elig_api_routes = EligibilityApiRoutes(app, axione) elig_api_routes.add_routes() elig_api_exceptions = FlaskExceptions(app) elig_api_exceptions.add_exceptions() @app.route("/", methods=["GET"]) def getMap(): return render_template("map.html") @app.route("/eligdata", methods=["GET"]) def getEligData(): args = request.args valid_args = True processed_args = {} for k in ["swx", "swy", "nex", "ney"]: valid_args = valid_args and k in args if valid_args: try: processed_args[k] = float(args[k]) except ValueError: valid_args = False if valid_args: # Need to narrow coordinates for Liazo API call # computes center centerx = (processed_args['swx'] + processed_args['nex']) / 2 centery = (processed_args['swy'] + processed_args['ney']) / 2 narrow_x = 0.0022 narrow_y = 0.0011 narrow_coordinates = AreaCoordinates( swx=centerx - narrow_x, swy=centery - narrow_y, nex=centerx + narrow_x, ney=centery + narrow_y, ) buildings = dict() try: buildings = arcep.getAreaBuildings(narrow_coordinates, buildings) buildings = axione.getAreaBuildings(narrow_coordinates, buildings) except ValueError as err: print("Could not get Axione data for this area:", err) buildings = liazo.getAreaBuildings(narrow_coordinates, buildings) return {"buildings": buildings} else: return "Invalid bounding box coordinates", 400 @app.route("/eligtest/ftth", methods=["GET"]) def testFtth(): args = request.args idImm=args['idImm'] codePostal=args['codePostal'] axioneOk=args['axione'] liazoOk=args['liazo'] pto_url = f"https://tools.aquilenet.fr/cgi-bin/recherchepto.cgi?refimmeuble={idImm}&cp={codePostal}&axione={axioneOk}&liazo={liazoOk}" return redirect(pto_url)