scripts/generate_turnstile_errors.livemd
2024-08-07 12:56:54 -04:00

66 lines
1.3 KiB
Markdown

# Generate Turnstile Errors as JSON
```elixir
Mix.install([
{:req, "~> 0.5.6"},
{:floki, "~> 0.36.2"},
{:html5ever, "~> 0.16.0"}
])
```
## Section
```elixir
page = Req.get!("https://developers.cloudflare.com/turnstile/troubleshooting/client-side-errors/error-codes/")
```
```elixir
extract_code = fn row ->
row
|> Floki.find("code")
|> Enum.map(&Floki.text/1)
end
```
```elixir
extract_retry = fn row ->
row |> Floki.find("td") |> Enum.drop(2) |> Enum.take(1) |> Floki.text() == "Yes"
end
```
```elixir
extract_name = fn row ->
row
|> Floki.find("td")
|> Enum.drop(1)
|> Enum.at(0)
|> Floki.text()
|> String.split(":")
|> Enum.at(0)
|> then(fn w -> Regex.replace(~r/[-\.]/, w, " ") end)
|> String.split(" ")
|> Enum.map(&String.capitalize/1)
|> Enum.join("")
end
```
```elixir
page.body |> Floki.parse_document!(html_parser: Floki.HTMLParser.Html5ever)
|> Floki.find("table")
|> Floki.find("tr")
|> Enum.drop(1)
|> Enum.map(fn row ->
extract_code.(row) |> Enum.map(fn code ->
do_retry = extract_retry.(row)
name = extract_name.(row)
%{code: code, do_retry: do_retry, name: name}
end)
end
)
|> Enum.flat_map(fn i -> i end)
|> Enum.dedup()
|> Enum.map(&:json.encode/1)
|> Enum.map(&to_string/1)
|> Enum.join(",\n")
|> IO.puts
```