refactoring: improved readability
This commit is contained in:
parent
6ac4c9837f
commit
1bce4490a0
1 changed files with 50 additions and 27 deletions
|
@ -2,22 +2,10 @@ defmodule PodcastFeed.Provider.Archive.Parser do
|
||||||
alias PodcastFeed.Utility.Format
|
alias PodcastFeed.Utility.Format
|
||||||
|
|
||||||
@extra_metadata_url "https://archive.org/download/{identifier}/metadata.json"
|
@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}"
|
@download_url "https://archive.org/download/{identifier}/{filename}"
|
||||||
|
|
||||||
def by_identifier(identifier) do
|
@extra_metadata_defaults %{
|
||||||
extra_metadata_json = fetch_extra_metadata(identifier)
|
|
||||||
metadata_json = fetch_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" => "",
|
"link" => "",
|
||||||
"image" => %{
|
"image" => %{
|
||||||
"url" => "",
|
"url" => "",
|
||||||
|
@ -27,31 +15,53 @@ defmodule PodcastFeed.Provider.Archive.Parser do
|
||||||
"category" => "",
|
"category" => "",
|
||||||
"explicit" => "",
|
"explicit" => "",
|
||||||
}
|
}
|
||||||
end
|
|
||||||
|
def by_identifier(identifier) do
|
||||||
|
extra_metadata_json = fetch_extra_metadata(identifier)
|
||||||
|
metadata_json = fetch_archive_metadata(identifier)
|
||||||
|
parse(identifier, metadata_json, extra_metadata_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fetch_metadata(identifier) do
|
defp fetch_extra_metadata(identifier) do
|
||||||
metadata_url = Format.compile(@metadata_url, identifier: identifier)
|
extra_metadata_url = Format.compile(@extra_metadata_url, identifier: identifier)
|
||||||
|
parse_extra_metadata_response(:hackney.get(extra_metadata_url, [], "", [follow_redirect: true]))
|
||||||
|
end
|
||||||
|
|
||||||
|
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, 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!()
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(identifier, %{"metadata" => metadata, "files" => files}, extra) do
|
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)}
|
%{podcast: podcast_data(metadata, extra), items: items_data(files, identifier)}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fetch_cover(files, identifier) do
|
# cover is nil
|
||||||
filename = files
|
defp enrich_extra_metadata_with_cover(nil, extra), do: extra
|
||||||
|> 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")
|
|
||||||
|
|
||||||
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
|
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
|
defp podcast_data(metadata, extra) do
|
||||||
%{
|
%{
|
||||||
title: metadata["title"],
|
title: metadata["title"],
|
||||||
|
@ -104,6 +114,19 @@ defmodule PodcastFeed.Provider.Archive.Parser do
|
||||||
}
|
}
|
||||||
end
|
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
|
defp fetch_image_of_audio(audio_file, files) do
|
||||||
files
|
files
|
||||||
|> Enum.filter(fn
|
|> Enum.filter(fn
|
||||||
|
|
Loading…
Reference in a new issue