import requests import re def get_geolocation(adresse, latitude, longitude): """Return latitude, longitude & code INSEE from an adress. Latitude & longitude only if they are not specified in the function. Args: adresse (str): Adress or city for example Returns: Tuple(float, float, str): latitude, longitude & code INSEE """ if latitude is not None and longitude is not None: url = f"https://data.geopf.fr/geocodage/reverse?lon={longitude}&lat={latitude}&index=parcel" else: url = f"https://data.geopf.fr/geocodage/search?q={adresse}" response = requests.get(url) if response.status_code == 200: data = response.json()["features"][0] if 'parcel' in url: properties = data["properties"] code_insee = properties["departmentcode"] + \ properties["municipalitycode"] else: coordinates = data["geometry"]["coordinates"] latitude, longitude = coordinates[1], coordinates[0] code_insee = data["properties"]["citycode"] return latitude, longitude, code_insee return None, None, None def get_risques(code_insee): """Get list of risks from a 'code INSEE' Args: code_insee (str): Code INSEE Returns: List[str]: List of risks """ requete = f"https://georisques.gouv.fr/api/v1/gaspar/risques?code_insee={code_insee}" response = requests.get(requete) if response.status_code == 200: risques = eval(response.content.decode().replace("null", "-1")) if risques["results"] > 0: risques = risques["data"] risques = [r["libelle_risque_long"] for r in risques[0]["risques_detail"]] else: risques = [] else: risques = None return risques