Browse Source

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

danilo silva 4 years ago
parent
commit
6ac4c9837f
1 changed files with 12 additions and 5 deletions
  1. 12 5
      lib/podcast_feed/provider/archive/parser.ex

+ 12 - 5
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