jcalbornoz commited on
Commit
e7b2987
·
verified ·
1 Parent(s): 291a6a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -28
app.py CHANGED
@@ -20,39 +20,37 @@ except ImportError:
20
  subprocess.run(["pip", "install", "fake-useragent"], check=True)
21
  from fake_useragent import UserAgent
22
 
23
- # --- 1. GENERADOR DE URLS (NUEVA JERARQUÍA GEOGRÁFICA) ---
24
- def construir_urls_final(barrio, sector, ciudad, tipo, hab, ban, park, antiguedad):
25
- # Slugs de filtros
26
  mapa_ant = {
27
  "Menos de 1 año": "de-0-a-1-anos",
28
- "1 a 8 años": "de-1-a-8-anios",
29
  "9 a 15 años": "de-9-a-15-anos",
30
  "16 a 30 años": "de-16-a-30-anos",
31
  "Más de 30 años": "mas-de-30-anos"
32
  }
33
- slug_ant = mapa_ant.get(antiguedad, "de-1-a-8-anios")
34
  slug_park = f"{int(park)}-parqueadero" if int(park) == 1 else f"{int(park)}-parqueaderos"
35
 
36
- # Limpieza estricta de jerarquía geográfica
37
  b_slug = barrio.lower().strip().replace(" ", "-")
38
- s_slug = sector.lower().strip().replace(" ", "-")
39
  c_slug = ciudad.lower().strip().replace(" ", "-")
40
 
41
- # Manejo de tipos de inmueble
42
  tipo_slug = tipo.lower().strip()
43
- if tipo_slug == "apartamento": tipo_fr = "apartamentos"
44
- elif tipo_slug == "casa": tipo_fr = "casas"
45
- else: tipo_fr = tipo_slug + "s" # bodegas, lotes, oficinas
 
46
 
47
  # --- URL FINCA RAÍZ EXACTA ---
48
- # Ejemplo del usuario: /arriendo/casas-y-apartamentos/la-concepcion/norte-centro-historico/barranquilla/3...
49
  if tipo_slug in ["bodega", "lote", "oficina"]:
50
- url_fr = f"https://www.fincaraiz.com.co/arriendo/{tipo_fr}/{b_slug}/{s_slug}/{c_slug}/{slug_park}/{slug_ant}"
51
  else:
52
- url_fr = f"https://www.fincaraiz.com.co/arriendo/{tipo_fr}/{b_slug}/{s_slug}/{c_slug}/{int(hab)}-o-mas-habitaciones/{int(ban)}-o-mas-banos/{slug_park}/{slug_ant}"
53
 
54
  # --- URL METROCUADRADO ---
55
- # MC suele funcionar con ciudad/barrio o ciudad/sector
56
  url_mc = f"https://www.metrocuadrado.com/{tipo_slug}-casa-oficina/arriendo/{c_slug}/{b_slug}/{int(ban)}-banos-{int(hab)}-habitaciones/?search=form"
57
 
58
  return url_fr, url_mc
@@ -63,19 +61,16 @@ def extraer_precio_regex(texto):
63
  coincidencias = re.findall(patron, texto)
64
  if coincidencias:
65
  precios = [int(p.replace('.', '').replace(',', '')) for p in coincidencias]
 
66
  precios_validos = [p for p in precios if 600000 <= p <= 40000000]
67
  if precios_validos:
68
  return precios_validos[0]
69
  return 0
70
 
71
  # --- 3. MOTOR DE EXTRACCIÓN ROBUSTO ---
72
- def motor_tramitia_final(barrio, sector, ciudad, area, tipo, hab, ban, park, antiguedad):
73
  resultados = []
74
-
75
- # Si el usuario deja sector vacío, igualamos a ciudad para no romper la URL
76
- if not sector: sector = ciudad
77
-
78
- url_fr, url_mc = construir_urls_final(barrio, sector, ciudad, tipo, hab, ban, park, antiguedad)
79
 
80
  log_visible = f"✅ URLs GENERADAS (AUDITORÍA):\nFR: {url_fr}\nMC: {url_mc}\n\n"
81
  ua = UserAgent()
@@ -182,7 +177,7 @@ def motor_tramitia_final(barrio, sector, ciudad, area, tipo, hab, ban, park, ant
182
 
183
  df_final = pd.concat([df_fr, df_mc]).reset_index(drop=True)
184
 
185
- log_visible += f"\n✨ PROCESADO FINAL: {len(df_final)} inmuebles únicos seleccionados para el reporte."
186
 
187
  # --- PDF ---
188
  pdf_path = f"Reporte_{int(time.time())}.pdf"
@@ -222,16 +217,12 @@ def motor_tramitia_final(barrio, sector, ciudad, area, tipo, hab, ban, park, ant
222
 
223
  # --- INTERFAZ GRÁFICA ---
224
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
225
- gr.Markdown("## 🤖 TramitIA Pro: Analizador Inmobiliario (Jerarquía Geográfica)")
226
 
227
  with gr.Row():
228
  with gr.Column(scale=1):
229
  c = gr.Textbox(label="Ciudad", value="Barranquilla")
230
-
231
- # --- NUEVO CAMPO DE SECTOR ---
232
- s = gr.Textbox(label="Localidad / Sector (Ej: Norte Centro Historico)", value="Norte Centro Historico")
233
  b = gr.Textbox(label="Barrio (Ej: La Concepcion)", value="La Concepcion")
234
-
235
  a = gr.Number(label="Área M2", value=70)
236
 
237
  t = gr.Dropdown(
@@ -258,6 +249,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
258
  with gr.TabItem("Tabla de Resultados"): out_df = gr.Dataframe()
259
  with gr.TabItem("Descargar PDF"): out_pdf = gr.File()
260
 
261
- btn.click(motor_tramitia_final, [b, s, c, a, t, h, ban, p, e], [msg, out_df, out_pdf, res_fin])
262
 
263
  demo.launch()
 
20
  subprocess.run(["pip", "install", "fake-useragent"], check=True)
21
  from fake_useragent import UserAgent
22
 
23
+ # --- 1. GENERADOR DE URLS (LLAVE MAESTRA) ---
24
+ def construir_urls_final(barrio, ciudad, tipo, hab, ban, park, antiguedad):
25
+ # Mapeo exacto de antigüedad (Con "anos")
26
  mapa_ant = {
27
  "Menos de 1 año": "de-0-a-1-anos",
28
+ "1 a 8 años": "de-1-a-8-anos",
29
  "9 a 15 años": "de-9-a-15-anos",
30
  "16 a 30 años": "de-16-a-30-anos",
31
  "Más de 30 años": "mas-de-30-anos"
32
  }
33
+ slug_ant = mapa_ant.get(antiguedad, "de-1-a-8-anos")
34
  slug_park = f"{int(park)}-parqueadero" if int(park) == 1 else f"{int(park)}-parqueaderos"
35
 
36
+ # Limpieza de zona
37
  b_slug = barrio.lower().strip().replace(" ", "-")
 
38
  c_slug = ciudad.lower().strip().replace(" ", "-")
39
 
40
+ # Manejo de tipos de inmueble (Agrupación descubierta por el usuario)
41
  tipo_slug = tipo.lower().strip()
42
+ if tipo_slug in ["apartamento", "casa"]:
43
+ tipo_fr = "casas-y-apartamentos-y-apartaestudios"
44
+ else:
45
+ tipo_fr = tipo_slug + "s" # bodegas, lotes, oficinas
46
 
47
  # --- URL FINCA RAÍZ EXACTA ---
 
48
  if tipo_slug in ["bodega", "lote", "oficina"]:
49
+ url_fr = f"https://www.fincaraiz.com.co/arriendo/{tipo_fr}/{b_slug}/{c_slug}/{slug_park}/{slug_ant}"
50
  else:
51
+ url_fr = f"https://www.fincaraiz.com.co/arriendo/{tipo_fr}/{b_slug}/{c_slug}/{int(hab)}-o-mas-habitaciones/{int(ban)}-o-mas-banos/{slug_park}/{slug_ant}"
52
 
53
  # --- URL METROCUADRADO ---
 
54
  url_mc = f"https://www.metrocuadrado.com/{tipo_slug}-casa-oficina/arriendo/{c_slug}/{b_slug}/{int(ban)}-banos-{int(hab)}-habitaciones/?search=form"
55
 
56
  return url_fr, url_mc
 
61
  coincidencias = re.findall(patron, texto)
62
  if coincidencias:
63
  precios = [int(p.replace('.', '').replace(',', '')) for p in coincidencias]
64
+ # Filtro: Ignorar precios de venta (> 40 millones)
65
  precios_validos = [p for p in precios if 600000 <= p <= 40000000]
66
  if precios_validos:
67
  return precios_validos[0]
68
  return 0
69
 
70
  # --- 3. MOTOR DE EXTRACCIÓN ROBUSTO ---
71
+ def motor_tramitia_final(barrio, ciudad, area, tipo, hab, ban, park, antiguedad):
72
  resultados = []
73
+ url_fr, url_mc = construir_urls_final(barrio, ciudad, tipo, hab, ban, park, antiguedad)
 
 
 
 
74
 
75
  log_visible = f"✅ URLs GENERADAS (AUDITORÍA):\nFR: {url_fr}\nMC: {url_mc}\n\n"
76
  ua = UserAgent()
 
177
 
178
  df_final = pd.concat([df_fr, df_mc]).reset_index(drop=True)
179
 
180
+ log_visible += f"\n✨ PROCESADO FINAL: {len(df_final)} inmuebles únicos seleccionados."
181
 
182
  # --- PDF ---
183
  pdf_path = f"Reporte_{int(time.time())}.pdf"
 
217
 
218
  # --- INTERFAZ GRÁFICA ---
219
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
220
+ gr.Markdown("## 🤖 TramitIA Pro: Analizador Inmobiliario (Rutas Exactas)")
221
 
222
  with gr.Row():
223
  with gr.Column(scale=1):
224
  c = gr.Textbox(label="Ciudad", value="Barranquilla")
 
 
 
225
  b = gr.Textbox(label="Barrio (Ej: La Concepcion)", value="La Concepcion")
 
226
  a = gr.Number(label="Área M2", value=70)
227
 
228
  t = gr.Dropdown(
 
249
  with gr.TabItem("Tabla de Resultados"): out_df = gr.Dataframe()
250
  with gr.TabItem("Descargar PDF"): out_pdf = gr.File()
251
 
252
+ btn.click(motor_tramitia_final, [b, c, a, t, h, ban, p, e], [msg, out_df, out_pdf, res_fin])
253
 
254
  demo.launch()