I have a function that parses an html body to get the Open Graph attributes like below.
I am not sure how to make use of Stream so that the parse can only be done once -- and this is even possible?
def og(body) do
image = attribute_content(body, "meta[property=og:image]")
title = attribute_content(body, "meta[property=og:title]")
site_name = attribute_content(body, "meta[property=og:site_name]")
desc = attribute_content(body, "meta[property=og:description]")
type = attribute_content(body, "meta[property=og:type]")
url = attribute_content(body, "meta[property=og:url]")
author = attribute_content(body, "meta[name=author]")
%{image: image, title: title, type: type,
site_title: site_title, url: url, site_name: site_name,
description: desc, author: author}
end
@doc """
Parse html body for the target element and return the content.
"""
defp attribute_content(body, target) do
Floki.find(body, target) |> Floki.attribute("content") |> List.first
end
attribute_content
? – Dogbert