From 6ac4c9837f1aef009784f6ae44043f7221ad02ea Mon Sep 17 00:00:00 2001 From: danilo silva Date: Mon, 25 May 2020 17:55:36 +0000 Subject: [PATCH] fix: handling cases where the are no images related to an audio file --- lib/podcast_feed/provider/archive/parser.ex | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/podcast_feed/provider/archive/parser.ex b/lib/podcast_feed/provider/archive/parser.ex index 86978d2..91b3fc5 100644 --- a/lib/podcast_feed/provider/archive/parser.ex +++ b/lib/podcast_feed/provider/archive/parser.ex @@ -32,7 +32,6 @@ defmodule PodcastFeed.Provider.Archive.Parser do defp fetch_metadata(identifier) do 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, metadata_json} = :hackney.body(client_ref) metadata_json |> Poison.decode!() @@ -95,11 +94,11 @@ defmodule PodcastFeed.Provider.Archive.Parser do title: file["title"], description: "", 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(), size: file |> Map.get("size"), 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(), explicit: "no", } @@ -112,7 +111,15 @@ defmodule PodcastFeed.Provider.Archive.Parser do format =~ ~r/JPG|JPEG|PNG|GIF/i _ -> nil end) - |> List.first() - |> Map.get("name", nil) + |> fetch_image_of_audio() + 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