Files
something-erlang/lib/something_erlang/awful_api/bookmarks.ex

85 lines
1.8 KiB
Elixir
Raw Normal View History

defmodule SomethingErlang.AwfulApi.Bookmarks do
2024-08-12 15:46:13 +02:00
import Meeseeks.CSS
2024-08-12 15:46:13 +02:00
require Logger
2024-08-12 15:46:13 +02:00
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)
2024-08-12 15:46:13 +02:00
|> Map.put(:id, thread_id)
end
end
def parse(thread) do
2024-08-12 15:46:13 +02:00
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"))
%{
2024-08-12 15:46:13 +02:00
icon: Meeseeks.attr(img, "src"),
title: Meeseeks.attr(img, "alt")
}
2024-08-12 15:46:13 +02:00
end
2024-08-12 15:46:13 +02:00
defp thread_data("title", td) do
last_seen = Meeseeks.one(td, css(".lastseen .count"))
info = Meeseeks.one(td, css(".info"))
2024-08-12 15:46:13 +02:00
%{
2024-08-12 16:37:50 +02:00
new_posts:
case Meeseeks.text(last_seen) do
nil -> 0
n -> String.to_integer(n)
end,
2024-08-12 15:46:13 +02:00
thread_title: Meeseeks.text(Meeseeks.one(info, css(".thread_title")))
}
end
2024-08-12 15:46:13 +02:00
defp thread_data("author", td) do
Meeseeks.text(td)
end
2024-08-12 15:46:13 +02:00
defp thread_data("replies", td) do
Meeseeks.text(td)
|> String.to_integer()
end
2024-08-12 15:46:13 +02:00
defp thread_data("views", td) do
Meeseeks.text(td)
|> String.to_integer()
end
2024-08-12 15:46:13 +02:00
defp thread_data("rating", td) do
img = Meeseeks.one(td, css("img"))
2024-08-12 15:46:13 +02:00
%{
icon: Meeseeks.attr(img, "src"),
title: Meeseeks.attr(img, "title")
}
end
2024-08-12 15:46:13 +02:00
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
2024-08-12 15:46:13 +02:00
defp thread_data(class, _td), do: Logger.error("#{inspect(class)} not found")
end