This commit is contained in:
@@ -105,36 +105,32 @@ defmodule CobblemonUi.TierListScraper do
|
|||||||
end
|
end
|
||||||
|
|
||||||
defp parse(html) do
|
defp parse(html) do
|
||||||
nodes = html |> Floki.parse_document!() |> Floki.find(".pokemon-tier")
|
bands = html |> Floki.parse_document!() |> Floki.find(".tierlist-band")
|
||||||
Logger.debug("[TierListScraper] Found #{length(nodes)} .pokemon-tier nodes")
|
Logger.debug("[TierListScraper] Found #{length(bands)} .tierlist-band nodes")
|
||||||
|
|
||||||
case nodes do
|
case bands do
|
||||||
[] ->
|
[] ->
|
||||||
Logger.warning("[TierListScraper] No .pokemon-tier elements found — page structure may have changed or content is JS-rendered")
|
Logger.warning("[TierListScraper] No .tierlist-band elements found — page structure may have changed")
|
||||||
{:error, :no_pokemon_found}
|
{:error, :no_pokemon_found}
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
pokemon = Enum.map(nodes, &extract_pokemon/1)
|
pokemon =
|
||||||
valid = Enum.filter(pokemon, fn %{name: n} -> n != "" end)
|
Enum.flat_map(bands, fn band ->
|
||||||
Logger.info("[TierListScraper] Parsed #{length(valid)} valid pokemon (#{length(pokemon) - length(valid)} skipped with empty names)")
|
tier = band |> Floki.attribute("data-tier") |> List.first() |> to_string() |> String.upcase()
|
||||||
{:ok, valid}
|
cards = Floki.find(band, ".tierlist-card")
|
||||||
|
|
||||||
|
Enum.flat_map(cards, fn card ->
|
||||||
|
case card |> Floki.attribute("data-name") |> List.first() do
|
||||||
|
nil -> []
|
||||||
|
"" -> []
|
||||||
|
name -> [%{name: String.trim(name), tier: tier}]
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
Logger.info("[TierListScraper] Parsed #{length(pokemon)} pokemon across #{length(bands)} tiers")
|
||||||
|
{:ok, pokemon}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp extract_pokemon(node) do
|
|
||||||
name =
|
|
||||||
node
|
|
||||||
|> Floki.find(".name")
|
|
||||||
|> Floki.text()
|
|
||||||
|> String.trim()
|
|
||||||
|
|
||||||
tier =
|
|
||||||
node
|
|
||||||
|> Floki.find(".tier")
|
|
||||||
|> Floki.text()
|
|
||||||
|> String.trim()
|
|
||||||
|
|
||||||
%{name: name, tier: tier}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp write_json(data) do
|
defp write_json(data) do
|
||||||
|
|||||||
Reference in New Issue
Block a user