When fetching favicons, if a feed doens't have a site URL, fallback on

the root page of the domain.
This commit is contained in:
Shadowfacts 2019-11-10 15:01:14 -05:00
parent 899cd5afff
commit a09901e44e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 16 additions and 5 deletions

View File

@ -10,16 +10,25 @@ defmodule Frenzy.Task.FetchFavicon do
def run(feed) do
Logger.metadata(favicon_task_id: generate_task_id())
case fetch_favicon_from_webpage(feed.site_url) do
site_url =
case feed.site_url do
url when is_binary(url) ->
url
_ ->
%URI{URI.parse(feed.feed_url) | path: nil, query: nil, fragment: nil} |> URI.to_string()
end
case fetch_favicon_from_webpage(site_url) do
{:ok, favicon_data} ->
changeset = Feed.changeset(feed, %{favicon: favicon_data})
{:ok, _feed} = Repo.update(changeset)
{:error, reason} ->
Logger.info("Couldn't fetch favicon for #{feed.site_url}: #{reason}")
Logger.info("Couldn't fetch favicon for #{site_url}: #{reason}")
favicon_uri =
%{URI.parse(feed.site_url) | path: "/favicon.ico", query: nil, fragment: nil}
%{URI.parse(site_url) | path: "/favicon.ico", query: nil, fragment: nil}
|> URI.to_string()
Logger.info("Trying default path: #{favicon_uri}")
@ -30,12 +39,12 @@ defmodule Frenzy.Task.FetchFavicon do
{:ok, _feed} = Repo.update(changeset)
{:error, reason} ->
Logger.info("Couldn't fetch default /favicon.ico for #{feed.site_url}: #{reason}")
Logger.info("Couldn't fetch default /favicon.ico for #{site_url}: #{reason}")
end
end
end
defp fetch_favicon_from_webpage(url) do
defp fetch_favicon_from_webpage(url) when is_binary(url) do
case HTTP.get(url) do
{:ok, %HTTPoison.Response{body: body}} ->
extract_favicon(body)
@ -45,6 +54,8 @@ defmodule Frenzy.Task.FetchFavicon do
end
end
defp fetch_favicon_from_webpage(_), do: {:error, "URL must be a string"}
defp extract_favicon(body) do
html_tree = Floki.parse(body)