File size: 6,138 Bytes
7223f8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""

IBGE API Service

Access to Brazilian geographic and demographic data

"""
import httpx
from typing import Optional, Dict, Any, List
from dataclasses import dataclass


IBGE_BASE_URL = "https://servicodados.ibge.gov.br/api/v1"


@dataclass
class Estado:
    """Brazilian state data"""
    id: int
    sigla: str
    nome: str
    regiao: str


@dataclass
class Municipio:
    """Brazilian municipality data"""
    id: int
    nome: str
    estado_sigla: str
    estado_nome: str
    regiao: str
    # Optional enriched data
    populacao: Optional[int] = None
    area_km2: Optional[float] = None


async def listar_estados() -> List[Estado]:
    """List all Brazilian states"""
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            response = await client.get(f"{IBGE_BASE_URL}/localidades/estados")
            
            if response.status_code != 200:
                return []
            
            data = response.json()
            estados = []
            
            for item in data:
                estados.append(Estado(
                    id=item["id"],
                    sigla=item["sigla"],
                    nome=item["nome"],
                    regiao=item.get("regiao", {}).get("nome", "")
                ))
            
            return sorted(estados, key=lambda x: x.nome)
            
    except Exception as e:
        print(f"IBGE estados error: {e}")
        return []


async def listar_municipios(uf: str) -> List[Municipio]:
    """List all municipalities in a state"""
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            response = await client.get(
                f"{IBGE_BASE_URL}/localidades/estados/{uf}/municipios"
            )
            
            if response.status_code != 200:
                return []
            
            data = response.json()
            municipios = []
            
            for item in data:
                municipios.append(Municipio(
                    id=item["id"],
                    nome=item["nome"],
                    estado_sigla=uf.upper(),
                    estado_nome=item.get("microrregiao", {}).get("mesorregiao", {}).get("UF", {}).get("nome", ""),
                    regiao=item.get("microrregiao", {}).get("mesorregiao", {}).get("UF", {}).get("regiao", {}).get("nome", "")
                ))
            
            return sorted(municipios, key=lambda x: x.nome)
            
    except Exception as e:
        print(f"IBGE municipios error: {e}")
        return []


async def buscar_municipio(nome: str, uf: Optional[str] = None) -> List[Municipio]:
    """Search for municipalities by name"""
    try:
        # If UF provided, search only that state
        if uf:
            municipios = await listar_municipios(uf)
            return [m for m in municipios if nome.lower() in m.nome.lower()]
        
        # Otherwise search all states (slower)
        async with httpx.AsyncClient(timeout=30.0) as client:
            response = await client.get(f"{IBGE_BASE_URL}/localidades/municipios")
            
            if response.status_code != 200:
                return []
            
            data = response.json()
            results = []
            
            for item in data:
                if nome.lower() in item["nome"].lower():
                    uf_info = item.get("microrregiao", {}).get("mesorregiao", {}).get("UF", {})
                    results.append(Municipio(
                        id=item["id"],
                        nome=item["nome"],
                        estado_sigla=uf_info.get("sigla", ""),
                        estado_nome=uf_info.get("nome", ""),
                        regiao=uf_info.get("regiao", {}).get("nome", "")
                    ))
            
            return results[:20]  # Limit results
            
    except Exception as e:
        print(f"IBGE search error: {e}")
        return []


async def obter_municipio_por_id(id_municipio: int) -> Optional[Municipio]:
    """Get municipality by IBGE code"""
    try:
        async with httpx.AsyncClient(timeout=15.0) as client:
            response = await client.get(
                f"{IBGE_BASE_URL}/localidades/municipios/{id_municipio}"
            )
            
            if response.status_code != 200:
                return None
            
            item = response.json()
            uf_info = item.get("microrregiao", {}).get("mesorregiao", {}).get("UF", {})
            
            return Municipio(
                id=item["id"],
                nome=item["nome"],
                estado_sigla=uf_info.get("sigla", ""),
                estado_nome=uf_info.get("nome", ""),
                regiao=uf_info.get("regiao", {}).get("nome", "")
            )
            
    except Exception as e:
        print(f"IBGE municipio error: {e}")
        return None


async def enriquecer_localizacao(cidade: str, uf: Optional[str] = None) -> Dict[str, Any]:
    """

    Enrich a location name with IBGE data.

    Useful for adding context to extracted locations.

    """
    resultado = {
        "cidade_original": cidade,
        "encontrado": False,
        "ibge_codigo": None,
        "cidade": None,
        "estado": None,
        "estado_sigla": None,
        "regiao": None
    }
    
    municipios = await buscar_municipio(cidade, uf)
    
    if municipios:
        # Take best match (exact or first)
        melhor = None
        for m in municipios:
            if m.nome.lower() == cidade.lower():
                melhor = m
                break
        
        if not melhor:
            melhor = municipios[0]
        
        resultado.update({
            "encontrado": True,
            "ibge_codigo": melhor.id,
            "cidade": melhor.nome,
            "estado": melhor.estado_nome,
            "estado_sigla": melhor.estado_sigla,
            "regiao": melhor.regiao
        })
    
    return resultado