frenzy/lib/frenzy/pipeline/extractor/whatever_scalzi.ex

57 lines
1.4 KiB
Elixir
Raw Normal View History

defmodule Frenzy.Pipeline.Extractor.WhateverScalzi do
2019-10-31 20:52:01 +00:00
@moduledoc """
Extractor for https://whatever.scalzi.com
"""
alias Frenzy.Pipeline.Extractor
@behaviour Extractor
@impl Extractor
def extract(html_tree) do
2019-10-31 20:52:01 +00:00
case get_article_content(html_tree) do
nil ->
{:error, "no matching elements"}
elem ->
{:ok, elem}
2019-10-31 20:52:01 +00:00
end
end
defp get_article_content(html_tree) do
2020-08-15 01:55:38 +00:00
# there's no element that contains only the post content
# .postarea contains the headline, post content, social media buttons, and comments
2021-03-31 19:30:05 +00:00
case Floki.find(html_tree, ".postarea") do
[{_tag, _attrs, postarea_children}] ->
Enum.split_while(postarea_children, fn
{"h1", _, _} -> true
_ -> false
end)
|> case do
{_before_headline, [_headline | rest]} ->
{article_content, _rest} =
Enum.split_while(rest, fn
{"div", attrs, _} = el ->
class = Floki.attribute(el, "class") |> List.first()
if {"id", "comments"} in attrs do
false
else
is_nil(class) || !String.contains?(class, "sharedaddy")
end
_ ->
true
end)
2021-04-01 00:19:01 +00:00
Extractor.Util.strip_wp_lazy_loading(article_content)
2021-03-31 19:30:05 +00:00
_ ->
nil
end
2019-10-31 20:52:01 +00:00
_ ->
nil
end
end
end