fix: handling cases where the are no images related to an audio file

This commit is contained in:
danilo silva 2020-05-25 17:55:36 +00:00
parent 10d8f82134
commit 6ac4c9837f

View file

@ -32,7 +32,6 @@ defmodule PodcastFeed.Provider.Archive.Parser do
defp fetch_metadata(identifier) do defp fetch_metadata(identifier) do
metadata_url = Format.compile(@metadata_url, identifier: identifier) metadata_url = Format.compile(@metadata_url, identifier: identifier)
metadata_url |> IO.inspect
{:ok, 200, _headers, client_ref} = :hackney.get(metadata_url, [], "", [follow_redirect: true, connect_timeout: 30000, recv_timeout: 30000]) {:ok, 200, _headers, client_ref} = :hackney.get(metadata_url, [], "", [follow_redirect: true, connect_timeout: 30000, recv_timeout: 30000])
{:ok, metadata_json} = :hackney.body(client_ref) {:ok, metadata_json} = :hackney.body(client_ref)
metadata_json |> Poison.decode!() metadata_json |> Poison.decode!()
@ -95,11 +94,11 @@ defmodule PodcastFeed.Provider.Archive.Parser do
title: file["title"], title: file["title"],
description: "", description: "",
pubDate: file |> Map.get("mtime") |> Integer.parse() |> elem(0) |> DateTime.from_unix!(:second), pubDate: file |> Map.get("mtime") |> Integer.parse() |> elem(0) |> DateTime.from_unix!(:second),
link: Format.compile(@download_url, identifier: identifier, filename: filename) |> URI.encode(), link: download_url(identifier, filename),
length: (file |> Map.get("length") |> Float.parse() |> elem(0)) |> trunc(), length: (file |> Map.get("length") |> Float.parse() |> elem(0)) |> trunc(),
size: file |> Map.get("size"), size: file |> Map.get("size"),
summary: "", summary: "",
image: Format.compile(@download_url, identifier: identifier, filename: fetch_image_of_audio(Map.get(file, "name"), files)) |> URI.encode(), image: download_url(identifier, fetch_image_of_audio(filename, files)),
keywords: file |> Map.take(["album", "artist", "genre"]) |> Map.values(), keywords: file |> Map.take(["album", "artist", "genre"]) |> Map.values(),
explicit: "no", explicit: "no",
} }
@ -112,7 +111,15 @@ defmodule PodcastFeed.Provider.Archive.Parser do
format =~ ~r/JPG|JPEG|PNG|GIF/i format =~ ~r/JPG|JPEG|PNG|GIF/i
_ -> nil _ -> nil
end) end)
|> List.first() |> fetch_image_of_audio()
|> Map.get("name", nil) end
defp fetch_image_of_audio(image_files) when is_list(image_files), do: fetch_image_of_audio(List.first(image_files))
defp fetch_image_of_audio(nil), do: nil
defp fetch_image_of_audio(image_file), do: image_file |> Map.get("name", nil)
defp download_url(_identifier, nil), do: nil
defp download_url(identifier, filename) do
Format.compile(@download_url, identifier: identifier, filename: filename) |> URI.encode()
end end
end end