diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/plugins/boursorama.ex | 37 | ||||
-rw-r--r-- | lib/plugins/link/html.ex | 43 |
2 files changed, 51 insertions, 29 deletions
diff --git a/lib/plugins/boursorama.ex b/lib/plugins/boursorama.ex index 025a250..5b1ea9a 100644 --- a/lib/plugins/boursorama.ex +++ b/lib/plugins/boursorama.ex @@ -1,5 +1,4 @@ defmodule Nola.Plugins.Boursorama do - def irc_doc() do """ # bourses @@ -25,25 +24,34 @@ defmodule Nola.Plugins.Boursorama do {:ok, nil} end - def handle_info({:irc, :trigger, cac, m = %Nola.Message{trigger: %Nola.Trigger{type: :bang}}}, state) when cac in ["cac40", "caca40"] do + def handle_info( + {:irc, :trigger, cac, m = %Nola.Message{trigger: %Nola.Trigger{type: :bang}}}, + state + ) + when cac in ["cac40", "caca40"] do case HTTPoison.get(@cac40_url, [], []) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - html = Floki.parse(body) + {:ok, html} = Floki.parse_document(body) board = Floki.find(body, "div.c-tradingboard") cac40 = Floki.find(board, ".c-tradingboard__main > .c-tradingboard__infos") instrument = Floki.find(cac40, ".c-instrument") - last = Floki.find(instrument, "span[data-ist-last]") - |> Floki.text() - |> String.replace(" ", "") - variation = Floki.find(instrument, "span[data-ist-variation]") - |> Floki.text() - - sign = case variation do - "-"<>_ -> "▼" - "+" -> "▲" - _ -> "" - end + + last = + Floki.find(instrument, "span[data-ist-last]") + |> Floki.text() + |> String.replace(" ", "") + + variation = + Floki.find(instrument, "span[data-ist-variation]") + |> Floki.text() + + sign = + case variation do + "-" <> _ -> "▼" + "+" -> "▲" + _ -> "" + end m.replyfun.("caca40: #{sign} #{variation} #{last}") @@ -54,5 +62,4 @@ defmodule Nola.Plugins.Boursorama do m.replyfun.("caca40: erreur http") end end - end diff --git a/lib/plugins/link/html.ex b/lib/plugins/link/html.ex index 5899ed5..bef9640 100644 --- a/lib/plugins/link/html.ex +++ b/lib/plugins/link/html.ex @@ -10,14 +10,15 @@ defmodule Nola.Plugins.Link.HTML do @impl true def post_expand(url, body, _params, _opts) do - html = Floki.parse(body) + {:ok, html} = Floki.parse_document(body) opengraph = collect_open_graph(html) - text = if has_sufficient_opengraph_data?(opengraph) do - generate_text_from_opengraph(url, html, opengraph) - else - clean_text(collect_title(html)) - end + text = + if has_sufficient_opengraph_data?(opengraph) do + generate_text_from_opengraph(url, html, opengraph) + else + clean_text(collect_title(html)) + end {:ok, text} end @@ -55,16 +56,17 @@ defmodule Nola.Plugins.Link.HTML do _ -> acc end end - defp extract_meta_tag(_, acc), do: acc - defp is_valid_meta_tag?(name) do - String.starts_with?(name, "og:") || String.starts_with?(name, "article:") - end + defp extract_meta_tag(_, acc), do: acc defp is_valid_meta_tag?(nil) do false end + defp is_valid_meta_tag?(name) do + String.starts_with?(name, "og:") || String.starts_with?(name, "article:") + end + defp strip_prefix("og:" <> key), do: key defp strip_prefix(other), do: other @@ -82,6 +84,7 @@ defmodule Nola.Plugins.Link.HTML do _ -> acc end end + defp extract_itemprop(_, acc), do: acc defp collect_prefix_and_site_name(url, opengraph, itemprops) do @@ -95,7 +98,13 @@ defmodule Nola.Plugins.Link.HTML do end defp get_paywall_status(opengraph, itemprops) do - content_tier = Map.get(opengraph, "article:content_tier", Map.get(itemprops, "article:content_tier", "free")) + content_tier = + Map.get( + opengraph, + "article:content_tier", + Map.get(itemprops, "article:content_tier", "free") + ) + if content_tier == "free", do: "", else: "[paywall] " end @@ -112,7 +121,13 @@ defmodule Nola.Plugins.Link.HTML do end defp get_formatted_date(opengraph, itemprops) do - published_time = Map.get(opengraph, "article:published_time", Map.get(itemprops, "article:published_time", "")) + published_time = + Map.get( + opengraph, + "article:published_time", + Map.get(itemprops, "article:published_time", "") + ) + case DateTime.from_iso8601(published_time) do {:ok, date, _} -> "#{Timex.format!(date, "%d/%m/%y", :strftime)}. " _ -> "" @@ -120,15 +135,15 @@ defmodule Nola.Plugins.Link.HTML do end # TODO: Swap with AI description instead of truncating. + defp transform_description(nil, _), do: nil + defp transform_description(string, length) when is_binary(string) do if String.length(string) >= length, do: String.truncate(string, length), else: string end - defp transform_description(nil, _), do: nil defp clean_text(text) do text |> String.replace("\n", " ") |> HtmlEntities.decode() end - end |