From 539bf62563d9b799e764d71ba41e242426946248 Mon Sep 17 00:00:00 2001 From: Eric Teubert Date: Mon, 30 Dec 2024 09:58:55 +0800 Subject: [PATCH] refactor: reuse parse_date function --- server/lib/publisher/wordpress/episode.ex | 58 ++++++++++++------- .../controllers/validator/save_episode.ex | 30 +++------- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/server/lib/publisher/wordpress/episode.ex b/server/lib/publisher/wordpress/episode.ex index 9b00fd1..ea2a13b 100644 --- a/server/lib/publisher/wordpress/episode.ex +++ b/server/lib/publisher/wordpress/episode.ex @@ -38,6 +38,38 @@ defmodule Publisher.WordPress.Episode do |> String.trim_leading(".") end + @doc """ + Parses the given date string and returns a DateTime struct or nil. + + ## Examples + + iex> parse_date("2023-10-05T14:48:00Z") + ~U[2023-10-05 14:48:00Z] + + iex> parse_date("not a date") + nil + + """ + def parse_date(date) do + formats = [ + "{RFC822}", + "{RFC822z}", + "{RFC1123}", + "{RFC1123z}", + "{RFC3339}", + "{RFC3339z}", + "{ISO:Extended}", + "{ISO:Extended:Z}" + ] + + Enum.find_value(formats, fn format -> + case Timex.parse(date, format) do + {:ok, date} -> date + {:error, _} -> false + end + end) + end + # Finds episode by guid. Creates episode with that episode if none exists. # Returns episode id. defp find_or_create_episode(req, guid) do @@ -106,26 +138,6 @@ defmodule Publisher.WordPress.Episode do |> DateTime.to_iso8601() end - defp parse_date(date) do - formats = [ - "{RFC822}", - "{RFC822z}", - "{RFC1123}", - "{RFC1123z}", - "{RFC3339}", - "{RFC3339z}", - "{ISO:Extended}", - "{ISO:Extended:Z}" - ] - - Enum.find_value(formats, fn format -> - case Timex.parse(date, format) do - {:ok, date} -> date - {:error, _} -> false - end - end) - end - defp upload_content(req, post_id, %{"content" => content, "pub_date" => pub_date} = _params) when not is_nil(content) and not is_nil(pub_date) do Logger.info("Episode post #{post_id} content is #{String.length(content)}") @@ -283,6 +295,7 @@ defmodule Publisher.WordPress.Episode do case create_contributor(req, name) do {:ok, id} -> [id | acc] + {:error, reason} -> Logger.info("Couldn't create a contributor: #{inspect(reason)}") acc @@ -368,16 +381,19 @@ defmodule Publisher.WordPress.Episode do defp save_episode_image_url(req, episode_id, source_url) do Logger.info("Episode use #{source_url} as cover: #{episode_id}") + body = %{ episode_poster: source_url } + Req.post(req, url: "podlove/v2/episodes/#{episode_id}", json: body) :ok end defp upload_cover(req, episode_id, post_id, %{"cover" => cover} = params) - when not is_nil(cover) do + when not is_nil(cover) do image_name = "cover-" <> params["slug"] + with {:ok, source_url} <- Media.upload_media_from_url(req, post_id, cover, image_name), :ok <- save_episode_image_url(req, episode_id, source_url) do :ok diff --git a/server/lib/publisher_web/controllers/validator/save_episode.ex b/server/lib/publisher_web/controllers/validator/save_episode.ex index 76b9fd8..90b4ee9 100644 --- a/server/lib/publisher_web/controllers/validator/save_episode.ex +++ b/server/lib/publisher_web/controllers/validator/save_episode.ex @@ -1,6 +1,8 @@ defmodule PublisherWeb.Controllers.Validator.SaveEpisode do use PublisherWeb.Controllers.Validator.Validator + import Publisher.WordPress.Episode, only: [parse_date: 1] + embedded_schema do field(:guid, :string) field(:title, :string) @@ -22,33 +24,17 @@ defmodule PublisherWeb.Controllers.Validator.SaveEpisode do |> validate_date(:pub_date) end - defp validate_date(changeset, field) do - validate_change(changeset, field, fn _, value -> - if publication_date_valid?(value) do + defp validate_date(changeset, :pub_date) do + validate_change(changeset, :pub_date, fn _, pub_date -> + if publication_date_valid?(pub_date) do [] else - [{field, " is not a conformed date"}] + [pub_date: "is not a valid date"] end end) end - defp publication_date_valid?(field) do - formats = [ - "{RFC822}", - "{RFC822z}", - "{RFC1123}", - "{RFC1123z}", - "{RFC3339}", - "{RFC3339z}", - "{ISO:Extended}", - "{ISO:Extended:Z}" - ] - - Enum.find_value(formats, fn format -> - case Timex.parse(field, format) do - {:ok, field} -> field - {:error, _} -> false - end - end) + defp publication_date_valid?(value) do + parse_date(value) !== nil end end