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:
parent
899cd5afff
commit
a09901e44e
|
@ -10,16 +10,25 @@ defmodule Frenzy.Task.FetchFavicon do
|
||||||
def run(feed) do
|
def run(feed) do
|
||||||
Logger.metadata(favicon_task_id: generate_task_id())
|
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} ->
|
{:ok, favicon_data} ->
|
||||||
changeset = Feed.changeset(feed, %{favicon: favicon_data})
|
changeset = Feed.changeset(feed, %{favicon: favicon_data})
|
||||||
{:ok, _feed} = Repo.update(changeset)
|
{:ok, _feed} = Repo.update(changeset)
|
||||||
|
|
||||||
{:error, reason} ->
|
{: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 =
|
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()
|
|> URI.to_string()
|
||||||
|
|
||||||
Logger.info("Trying default path: #{favicon_uri}")
|
Logger.info("Trying default path: #{favicon_uri}")
|
||||||
|
@ -30,12 +39,12 @@ defmodule Frenzy.Task.FetchFavicon do
|
||||||
{:ok, _feed} = Repo.update(changeset)
|
{:ok, _feed} = Repo.update(changeset)
|
||||||
|
|
||||||
{:error, reason} ->
|
{: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
|
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
|
case HTTP.get(url) do
|
||||||
{:ok, %HTTPoison.Response{body: body}} ->
|
{:ok, %HTTPoison.Response{body: body}} ->
|
||||||
extract_favicon(body)
|
extract_favicon(body)
|
||||||
|
@ -45,6 +54,8 @@ defmodule Frenzy.Task.FetchFavicon do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp fetch_favicon_from_webpage(_), do: {:error, "URL must be a string"}
|
||||||
|
|
||||||
defp extract_favicon(body) do
|
defp extract_favicon(body) do
|
||||||
html_tree = Floki.parse(body)
|
html_tree = Floki.parse(body)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue