rewrite old parsing logic; parse postdate

This commit is contained in:
2022-08-02 10:28:16 +02:00
parent 1cb97fa7f7
commit b369c0862b
2 changed files with 25 additions and 189 deletions

View File

@ -33,15 +33,13 @@ defmodule SomethingErlang.AwfulApi.Thread do
posts: posts}
end
def userinfo(post) do
defp userinfo(post) do
user = Floki.find(post, "dl.userinfo")
user |> IO.inspect()
name = user |> Floki.find("dt") |> Floki.text() |> IO.inspect()
regdate = user |> Floki.find("dd.registered") |> Floki.text() |> IO.inspect()
title = user |> Floki.find_and_update("dd.title", fn {"dd", attrs} -> {"div", attrs} end) |> Floki.raw_html()
name = user |> Floki.find("dt") |> Floki.text()
regdate = user |> Floki.find("dd.registered") |> Floki.text()
title =
user |> Floki.find("dd.title") |> List.first()
|> Floki.children() |> Floki.raw_html()
%{
name: name,
@ -50,28 +48,34 @@ defmodule SomethingErlang.AwfulApi.Thread do
}
end
def postdate(post) do
_date =
defp postdate(post) do
date =
Floki.find(post, "td.postdate")
|> Floki.find("td.postdate") |> Floki.children() |> Floki.text()
|> Floki.find("td.postdate") |> Floki.text()
[month_text, day, year, hours, minutes] = date
|> String.split(~r{[\s,:]}, trim: true)
|> Enum.drop(1)
month = 1 + Enum.find_index(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
fn m -> m == month_text end)
NaiveDateTime.new!(year |> String.to_integer(), month, day |> String.to_integer(),
hours |> String.to_integer(), minutes |> String.to_integer(), 0)
end
def postbody(post) do
body = Floki.find(post, "td.postbody")
defp postbody(post) do
body =
Floki.find(post, "td.postbody")
|> List.first()
|> Floki.filter_out(:comment)
Floki.traverse_and_update(body, fn
{"img", attrs, []} -> transform(:img, attrs)
{"a", attrs, children} -> transform(:a, attrs, children)
{:comment, _} -> nil
other -> other
end)
|> Floki.traverse_and_update([], fn
{"td", [{"class", "postbody"}], children}, acc ->
{nil, [Floki.raw_html(children) | acc]}
other, acc ->
{other, acc}
end)
|> Floki.children()
|> Floki.raw_html()
end
defp transform(elem, attr, children \\ [])