Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Idealistats

idealistats.livemd

Idealistats

Mix.install([
  {:fast64, "~> 0.1.3"},
  {:tesla, "~> 1.8.0"},
  {:explorer, "~> 0.7.2"},
  {:kino_vega_lite, "~> 0.1.11"},
  {:jason, "~> 1.4.1"},
  {:kino_maplibre, "~> 0.1.10"},
  {:nimble_options, "~> 1.0"},
  {:idealistats, path: __DIR__}
])

Search options

Below, select the options for the search filters.

args =
  [
    country:
      Kino.Input.select(
        "Country",
        [
          {"es", "Spain"},
          {"it", "Italy"},
          {"pt", "Portugal"}
        ]
      ),
    operation:
      Kino.Input.select(
        "Operation",
        [
          {"sale", "Sale"},
          {"rent", "Rent"}
        ]
      ),
    propertyType:
      Kino.Input.select(
        "Property Type",
        [
          {"homes", "Homes"},
          {"offices", "Offices"},
          {"premises", "Premise"},
          {"garages", "Garages"},
          {"bedrooms", "Bedrooms"}
        ]
      ),
    locationId:
      Kino.Input.select(
        "Location Id",
        Enum.map(1..56, &{"0-EU-ES-#{&1}", "0-EU-ES-#{&1}"})
      )
  ]
  |> Enum.map(fn {key, value} ->
    {key,
     value
     |> Kino.render()
     |> Kino.Input.read()}
  end)

Getting data from Idealista

Using the arguments, we get a page from Idealista using those filters.

(res = %{"elementList" => elements}) =
  Auth.get_header()
  |> Auth.get_token()
  |> Idealistats.get_page(1, args)

:ok

Put the results in a map

First, we extract the latitude and longitudes of the results.

has_coordinates = fn elements -> elements["latitude"] != nil and elements["longitude"] != nil end

{la, lo} =
  elements
  |> Enum.filter(has_coordinates)
  |> Enum.map(fn point ->
    point
    |> Map.take(["latitude", "longitude"])
    |> Map.values()
    |> List.to_tuple()
  end)
  |> Enum.unzip()

:ok
homes = %{"longitude" => lo, "latitude" => la}

avg = &(Enum.sum(&1) / Enum.count(&1))

MapLibre.new(center: {avg.(lo), avg.(la)}, zoom: 8, style: :street)
|> MapLibre.add_table_source("homes", homes, {:lng_lat, ["longitude", "latitude"]})
|> MapLibre.add_layer(id: "homes", type: :circle, source: "homes")