Files

92 lines
3.4 KiB
Python

"""
Proceso independiente que ejecuta el scan y escribe resultados a JSON.
Llamado por Flask via subprocess.
"""
# -*- coding: utf-8 -*-
import sys, io, json, os, argparse
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATUS_FILE = os.path.join(BASE_DIR, "scan_status.json")
RESULTS_FILE = os.path.join(BASE_DIR, "scan_results.json")
def write_status(msg, running=True):
with open(STATUS_FILE, "w", encoding="utf-8") as f:
json.dump({"running": running, "progress": msg}, f)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--cities", default="Vero Beach,Jacksonville,Melbourne,Stuart,Daytona Beach,St. Augustine,Palm Coast,New Smyrna Beach")
parser.add_argument("--max_price", type=int, default=230000)
args = parser.parse_args()
cities = [c.strip() for c in args.cities.split(",") if c.strip()]
max_price = args.max_price
write_status(f"Iniciando scan en {len(cities)} ciudades...")
sys.path.insert(0, BASE_DIR)
from scrapers import scrape_zillow, scrape_realtor, scrape_hud, scrape_homepath, score_property
all_results = []
log = {}
# Zillow
write_status(f"Zillow: buscando en {len(cities)} ciudades... (Chrome se abrira)")
try:
z_results = scrape_zillow(cities, max_price)
log["Zillow"] = {"found": len(z_results), "status": "ok"}
all_results.extend(z_results)
write_status(f"Zillow: {len(z_results)} encontradas. Buscando en Realtor...")
except Exception as e:
log["Zillow"] = {"found": 0, "status": f"error: {e}"}
write_status(f"Zillow error: {e}. Continuando con Realtor...")
# Realtor.com
try:
r_results = scrape_realtor(cities, max_price)
log["Realtor.com"] = {"found": len(r_results), "status": "ok"}
all_results.extend(r_results)
write_status(f"Realtor: {len(r_results)} encontradas. Procesando...")
except Exception as e:
log["Realtor.com"] = {"found": 0, "status": f"error: {e}"}
# HUD + HomePath (rapidos, sin browser)
try:
h_results = scrape_hud(cities, max_price)
log["HUD"] = {"found": len(h_results), "status": "ok"}
all_results.extend(h_results)
except Exception as e:
log["HUD"] = {"found": 0, "status": f"error: {e}"}
try:
hp_results = scrape_homepath(cities, max_price)
log["HomePath"] = {"found": len(hp_results), "status": "ok"}
all_results.extend(hp_results)
except Exception as e:
log["HomePath"] = {"found": 0, "status": f"error: {e}"}
# Deduplicar
seen, unique = set(), []
for r in all_results:
key = (r.get("address","").lower().strip(), r.get("price",0))
if key[0] and key not in seen:
seen.add(key)
r["score"] = score_property(r, cities, max_price)
unique.append(r)
unique.sort(key=lambda x: x.get("score", 0), reverse=True)
with open(RESULTS_FILE, "w", encoding="utf-8") as f:
json.dump({"properties": unique, "log": log, "total": len(unique)}, f, ensure_ascii=False)
write_status(
f"Completado: {len(unique)} propiedades unicas encontradas",
running=False
)
print(f"SCAN DONE: {len(unique)} propiedades")
if __name__ == "__main__":
main()