refactoring: improved readability
This commit is contained in:
parent
6ac4c9837f
commit
1bce4490a0
1 changed files with 50 additions and 27 deletions
|
@ -2,56 +2,66 @@ defmodule PodcastFeed.Provider.Archive.Parser do
|
|||
alias PodcastFeed.Utility.Format
|
||||
|
||||
@extra_metadata_url "https://archive.org/download/{identifier}/metadata.json"
|
||||
@metadata_url "http://archive.org/metadata/{identifier}"
|
||||
@archive_metadata_url "http://archive.org/metadata/{identifier}"
|
||||
@download_url "https://archive.org/download/{identifier}/{filename}"
|
||||
|
||||
@extra_metadata_defaults %{
|
||||
"link" => "",
|
||||
"image" => %{
|
||||
"url" => "",
|
||||
"title" => "",
|
||||
"link" => "",
|
||||
},
|
||||
"category" => "",
|
||||
"explicit" => "",
|
||||
}
|
||||
|
||||
def by_identifier(identifier) do
|
||||
extra_metadata_json = fetch_extra_metadata(identifier)
|
||||
metadata_json = fetch_metadata(identifier)
|
||||
metadata_json = fetch_archive_metadata(identifier)
|
||||
parse(identifier, metadata_json, extra_metadata_json)
|
||||
end
|
||||
|
||||
defp fetch_extra_metadata(identifier) do
|
||||
extra_metadata_url = Format.compile(@extra_metadata_url, identifier: identifier)
|
||||
case :hackney.get(extra_metadata_url, [], "", [follow_redirect: true]) do
|
||||
{:ok, 200, _headers, client_ref} ->
|
||||
{:ok, extra_metadata_json} = :hackney.body(client_ref)
|
||||
extra_metadata_json |> String.split("\n") |> Enum.join() |> Poison.decode!()
|
||||
_ -> %{
|
||||
"link" => "",
|
||||
"image" => %{
|
||||
"url" => "",
|
||||
"title" => "",
|
||||
"link" => "",
|
||||
},
|
||||
"category" => "",
|
||||
"explicit" => "",
|
||||
}
|
||||
end
|
||||
parse_extra_metadata_response(:hackney.get(extra_metadata_url, [], "", [follow_redirect: true]))
|
||||
end
|
||||
|
||||
defp fetch_metadata(identifier) do
|
||||
metadata_url = Format.compile(@metadata_url, identifier: identifier)
|
||||
defp parse_extra_metadata_response({:ok, 200, _headers, client_ref}) do
|
||||
{:ok, extra_metadata_json} = :hackney.body(client_ref)
|
||||
extra_metadata_json
|
||||
|> String.split("\n")
|
||||
|> Enum.join()
|
||||
|> Poison.decode!()
|
||||
end
|
||||
defp parse_extra_metadata_response(_), do: @extra_metadata_defaults
|
||||
|
||||
defp fetch_archive_metadata(identifier) do
|
||||
metadata_url = Format.compile(@archive_metadata_url, identifier: identifier)
|
||||
{: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!()
|
||||
end
|
||||
|
||||
def parse(identifier, %{"metadata" => metadata, "files" => files}, extra) do
|
||||
# cover = files |> fetch_cover(identifier)
|
||||
extra = files
|
||||
|> fetch_cover(identifier)
|
||||
|> enrich_extra_metadata_with_cover(extra)
|
||||
|
||||
%{podcast: podcast_data(metadata, extra), items: items_data(files, identifier)}
|
||||
end
|
||||
|
||||
defp fetch_cover(files, identifier) do
|
||||
filename = files
|
||||
|> Enum.filter(fn f -> f["source"] == "original" end)
|
||||
|> Enum.filter(fn f -> f["format"] == "JPEG" end) #FIXME:! jpg, png, gif
|
||||
|> List.first()
|
||||
|> Map.get("name")
|
||||
# cover is nil
|
||||
defp enrich_extra_metadata_with_cover(nil, extra), do: extra
|
||||
|
||||
Format.compile(@download_url, identifier: identifier, filename: filename) |> URI.encode()
|
||||
# cover is found and image is missing in the extra_metadata
|
||||
defp enrich_extra_metadata_with_cover(cover, extra = %{"image" => %{"url" => ""}}) do
|
||||
put_in(extra, ["image", "url"], cover)
|
||||
end
|
||||
|
||||
# image is already set in the extra_metadata
|
||||
defp enrich_extra_metadata_with_cover(_cover, extra), do: extra
|
||||
|
||||
defp podcast_data(metadata, extra) do
|
||||
%{
|
||||
title: metadata["title"],
|
||||
|
@ -104,6 +114,19 @@ defmodule PodcastFeed.Provider.Archive.Parser do
|
|||
}
|
||||
end
|
||||
|
||||
defp fetch_cover(files, identifier) do
|
||||
filename = files
|
||||
|> Enum.filter(fn f -> f["source"] == "original" end)
|
||||
|> Enum.filter(fn f -> f["format"] == "JPEG" end) #FIXME:! jpg, png, gif
|
||||
|> List.first()
|
||||
|> case do
|
||||
nil -> nil
|
||||
file -> Map.get(file, "name")
|
||||
end
|
||||
|
||||
download_url(identifier, filename)
|
||||
end
|
||||
|
||||
defp fetch_image_of_audio(audio_file, files) do
|
||||
files
|
||||
|> Enum.filter(fn
|
||||
|
|
Loading…
Reference in a new issue