""" Copyright (c) 2020 Sdj Geek Voir le fichier LICENSE Classe d'accès aux données du site https://public.opendatasoft.com """ import requests def get_ville_from_cog(cog): r = requests.get("https://public.opendatasoft.com/api/records/1.0/search/", params={'dataset': "correspondance-code-insee-code-postal", 'q': f"insee_com={cog}", 'lang': "fr"}, headers={'Accept': "application/json"}) try: r.raise_for_status() except requests.HTTPError: print(f"Warning get_ville_from_cog: requests return status {r.headers}") return None result = r.json() if result['nhits'] > 0: return f"{result['records'][0]['fields']['nom_comm']} ({result['records'][0]['fields']['postal_code']})" print(f"Warning get_ville_from_cog: no result returned for cog {cog}") return None