This commit is contained in:
@@ -105,38 +105,34 @@ defmodule CobblemonUi.TierListScraper do
|
||||
end
|
||||
|
||||
defp parse(html) do
|
||||
nodes = html |> Floki.parse_document!() |> Floki.find(".pokemon-tier")
|
||||
Logger.debug("[TierListScraper] Found #{length(nodes)} .pokemon-tier nodes")
|
||||
bands = html |> Floki.parse_document!() |> Floki.find(".tierlist-band")
|
||||
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}
|
||||
|
||||
_ ->
|
||||
pokemon = Enum.map(nodes, &extract_pokemon/1)
|
||||
valid = Enum.filter(pokemon, fn %{name: n} -> n != "" end)
|
||||
Logger.info("[TierListScraper] Parsed #{length(valid)} valid pokemon (#{length(pokemon) - length(valid)} skipped with empty names)")
|
||||
{:ok, valid}
|
||||
pokemon =
|
||||
Enum.flat_map(bands, fn band ->
|
||||
tier = band |> Floki.attribute("data-tier") |> List.first() |> to_string() |> String.upcase()
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
defp write_json(data) do
|
||||
path = output_file()
|
||||
Logger.debug("[TierListScraper] Writing cache to #{path}")
|
||||
|
||||
Reference in New Issue
Block a user