From fcfdb17daab8dfdd0a859eabfca21e3d64d0f7da Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Thu, 2 Jul 2020 18:14:38 -0700 Subject: [PATCH] Format and add some comments to the linkbot module Signed-off-by: Alek Ratzloff --- lib/contrib/linkbot.ex | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/contrib/linkbot.ex b/lib/contrib/linkbot.ex index 38bb605..728c39f 100644 --- a/lib/contrib/linkbot.ex +++ b/lib/contrib/linkbot.ex @@ -19,24 +19,27 @@ defmodule Omnibot.Contrib.Linkbot do plug Tesla.Middleware.FollowRedirects, max_redirects: 10 plug Tesla.Middleware.Compression, format: "gzip" - # TODO instead of checking for 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>.+)"i 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 + html = get_url(url) document = Meeseeks.parse(html) - [title | _] = (Meeseeks.all(document, css("meta")) ++ [Meeseeks.one(document, css("title"))]) - |> Enum.map(&( - Meeseeks.attr(&1, "property") == "og:title" && Meeseeks.attr(&1, "content") - || Meeseeks.attr(&1, "name") == "title" && Meeseeks.attr(&1, "content") - || Meeseeks.tag(&1) == "title" && Meeseeks.text(&1) - )) - |> Enum.filter(& &1) - + [title | _] = + # Get all meta tags and the title tag in a single list + (Meeseeks.all(document, css("meta")) ++ [Meeseeks.one(document, css("title"))]) + # Filter/map appropriately + |> Enum.map( + &((Meeseeks.attr(&1, "property") == "og:title" && Meeseeks.attr(&1, "content")) || + (Meeseeks.attr(&1, "name") == "title" && Meeseeks.attr(&1, "content")) || + (Meeseeks.tag(&1) == "title" && Meeseeks.text(&1))) + ) + |> Enum.filter(& &1) + # Return the title title end