Format and add some comments to the linkbot module
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -19,24 +19,27 @@ defmodule Omnibot.Contrib.Linkbot do
|
|||||||
plug Tesla.Middleware.FollowRedirects, max_redirects: 10
|
plug Tesla.Middleware.FollowRedirects, max_redirects: 10
|
||||||
plug Tesla.Middleware.Compression, format: "gzip"
|
plug Tesla.Middleware.Compression, format: "gzip"
|
||||||
|
|
||||||
# TODO instead of checking for <title> exclusively, do this:
|
|
||||||
# 1. check for "meta" tag (in the header) with a "property" attribute of "og:title", and fetch the "content" attribute of that tag
|
|
||||||
# 2. check for meta tag with attribute "name" == "title", and fetch "content" attribute
|
|
||||||
# 3. Fall back to the <title>
|
|
||||||
|
|
||||||
@title_regex ~r"<title>(?<title>.+)</title>"i
|
@title_regex ~r"<title>(?<title>.+)</title>"i
|
||||||
|
|
||||||
def get_title(url) do
|
def get_title(url) do
|
||||||
|
# 1. check for "meta" tag (in the header) with a "property" attribute of "og:title", and fetch the "content" attribute of that tag
|
||||||
|
# 2. check for meta tag with attribute "name" == "title", and fetch "content" attribute
|
||||||
|
# 3. Fall back to the <title>
|
||||||
|
|
||||||
html = get_url(url)
|
html = get_url(url)
|
||||||
document = Meeseeks.parse(html)
|
document = Meeseeks.parse(html)
|
||||||
[title | _] = (Meeseeks.all(document, css("meta")) ++ [Meeseeks.one(document, css("title"))])
|
[title | _] =
|
||||||
|> Enum.map(&(
|
# Get all meta tags and the title tag in a single list
|
||||||
Meeseeks.attr(&1, "property") == "og:title" && Meeseeks.attr(&1, "content")
|
(Meeseeks.all(document, css("meta")) ++ [Meeseeks.one(document, css("title"))])
|
||||||
|| Meeseeks.attr(&1, "name") == "title" && Meeseeks.attr(&1, "content")
|
# Filter/map appropriately
|
||||||
|| Meeseeks.tag(&1) == "title" && Meeseeks.text(&1)
|
|> Enum.map(
|
||||||
))
|
&((Meeseeks.attr(&1, "property") == "og:title" && Meeseeks.attr(&1, "content")) ||
|
||||||
|> Enum.filter(& &1)
|
(Meeseeks.attr(&1, "name") == "title" && Meeseeks.attr(&1, "content")) ||
|
||||||
|
(Meeseeks.tag(&1) == "title" && Meeseeks.text(&1)))
|
||||||
|
)
|
||||||
|
|> Enum.filter(& &1)
|
||||||
|
# Return the title
|
||||||
title
|
title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user