Files
something-erlang/lib/something_erlang/awful_api/bookmarks.ex
2024-08-12 15:46:13 +02:00

81 lines
1.8 KiB
Elixir

defmodule SomethingErlang.AwfulApi.Bookmarks do
import Meeseeks.CSS
require Logger
def compile(html) do
for thread <- Meeseeks.all(html, css("tr.thread")) do
thread_id =
Meeseeks.attr(thread, "id")
|> String.split("thread")
|> List.last()
|> String.to_integer()
parse(thread)
|> Map.put(:id, thread_id)
end
end
def parse(thread) do
for col <- Meeseeks.all(thread, css("td:not(.star)")),
class = Meeseeks.attr(col, "class") |> String.split() |> List.first(),
into: %{} do
{String.to_atom(class), thread_data(class, col)}
end
end
defp thread_data("icon", td) do
img = Meeseeks.one(td, css("img"))
%{
icon: Meeseeks.attr(img, "src"),
title: Meeseeks.attr(img, "alt")
}
end
defp thread_data("title", td) do
last_seen = Meeseeks.one(td, css(".lastseen .count"))
info = Meeseeks.one(td, css(".info"))
%{
new_posts: Meeseeks.text(last_seen) |> String.to_integer(),
thread_title: Meeseeks.text(Meeseeks.one(info, css(".thread_title")))
}
end
defp thread_data("author", td) do
Meeseeks.text(td)
end
defp thread_data("replies", td) do
Meeseeks.text(td)
|> String.to_integer()
end
defp thread_data("views", td) do
Meeseeks.text(td)
|> String.to_integer()
end
defp thread_data("rating", td) do
img = Meeseeks.one(td, css("img"))
%{
icon: Meeseeks.attr(img, "src"),
title: Meeseeks.attr(img, "title")
}
end
defp thread_data("lastpost", td) do
date = Meeseeks.one(td, css(".date"))
author = Meeseeks.one(td, css(".author"))
%{
date: Meeseeks.text(date),
author: Meeseeks.text(author)
}
end
defp thread_data(class, _td), do: Logger.error("#{inspect(class)} not found")
end