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: case Meeseeks.text(last_seen) do nil -> 0 n -> String.to_integer(n) end, 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