mix format

This commit is contained in:
2022-11-07 13:49:31 +01:00
parent 51b9cc7f76
commit 0e74b0e1e0
16 changed files with 137 additions and 110 deletions

View File

@ -8,7 +8,6 @@ defmodule SomethingErlang.AwfulApi.Thread do
html = Floki.parse_document!(doc)
thread = Floki.find(html, "#thread") |> Floki.filter_out("table.post.ignored")
title = Floki.find(html, "title") |> Floki.text()
title = title |> String.replace(" - The Something Awful Forums", "")
@ -18,28 +17,23 @@ defmodule SomethingErlang.AwfulApi.Thread do
s -> String.to_integer(s)
end
posts = for post <- Floki.find(thread, "table.post") do
%{
userinfo: post |> userinfo(),
postdate: post |> postdate(),
postbody: post |> postbody()
}
end
posts =
for post <- Floki.find(thread, "table.post") do
%{
userinfo: post |> userinfo(),
postdate: post |> postdate(),
postbody: post |> postbody()
}
end
%{id: id,
title: title,
page: page,
page_count: page_count,
posts: posts}
%{id: id, title: title, page: page, page_count: page_count, posts: posts}
end
defp userinfo(post) do
user = Floki.find(post, "dl.userinfo")
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()
title = user |> Floki.find("dd.title") |> List.first() |> Floki.children() |> Floki.raw_html()
%{
name: name,
@ -49,18 +43,28 @@ defmodule SomethingErlang.AwfulApi.Thread do
end
defp postdate(post) do
date =
Floki.find(post, "td.postdate")
|> Floki.find("td.postdate") |> Floki.text()
date = Floki.find(post, "td.postdate") |> Floki.find("td.postdate") |> Floki.text()
[month_text, day, year, hours, minutes] = date
|> String.split(~r{[\s,:]}, trim: true)
|> Enum.drop(1)
[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)
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
defp postbody(post) do
@ -82,6 +86,7 @@ defmodule SomethingErlang.AwfulApi.Thread do
defp transform(:img, attrs, _children) do
{"class", class} = List.keyfind(attrs, "class", 0, {"class", ""})
if class == "sa-smilie" do
{"img", attrs, []}
else
@ -92,6 +97,7 @@ defmodule SomethingErlang.AwfulApi.Thread do
defp transform(:a, attrs, children) do
{"href", href} = List.keyfind(attrs, "href", 0, {"href", ""})
cond do
# skip internal links
String.starts_with?(href, "/") ->
@ -113,27 +119,31 @@ defmodule SomethingErlang.AwfulApi.Thread do
transform_link(:ytshort, href)
true ->
Logger.debug "no transform for #{href}"
Logger.debug("no transform for #{href}")
{"a", [{"href", href}], children}
end
end
defp transform_link(:mp4, href),
do: {"div", [{"class", "responsive-embed"}],
[{"video", [{"class", "img-responsive"}, {"controls", ""}],
[{"source", [{"src", href}, {"type", "video/mp4"}], []}]
}]
}
do:
{"div", [{"class", "responsive-embed"}],
[
{"video", [{"class", "img-responsive"}, {"controls", ""}],
[{"source", [{"src", href}, {"type", "video/mp4"}], []}]}
]}
defp transform_link(:gifv, href),
do: {"div", [{"class", "responsive-embed"}],
[{"video", [{"class", "img-responsive"}, {"controls", ""}],
[{"source", [{"src", String.replace(href, ".gifv", ".webm")},
{"type", "video/webm"}], []},
{"source", [{"src", String.replace(href, ".gifv", ".mp4")},
{"type", "video/mp4"}], []}]
}]
}
do:
{"div", [{"class", "responsive-embed"}],
[
{"video", [{"class", "img-responsive"}, {"controls", ""}],
[
{"source", [{"src", String.replace(href, ".gifv", ".webm")}, {"type", "video/webm"}],
[]},
{"source", [{"src", String.replace(href, ".gifv", ".mp4")}, {"type", "video/mp4"}],
[]}
]}
]}
defp transform_link(:ytlong, href) do
String.replace(href, "/watch?v=", "/embed/")
@ -146,14 +156,15 @@ defmodule SomethingErlang.AwfulApi.Thread do
end
defp youtube_iframe(src),
do: {"div", [{"class", "responsive-embed"}],
[{"iframe",
[
{"class", "youtube-player"},
{"loading", "lazy"},
{"allow", "fullscreen"},
{"src", src}
], []}
]}
do:
{"div", [{"class", "responsive-embed"}],
[
{"iframe",
[
{"class", "youtube-player"},
{"loading", "lazy"},
{"allow", "fullscreen"},
{"src", src}
], []}
]}
end