Skip to content

Commit

Permalink
refactor: reuse parse_date function
Browse files Browse the repository at this point in the history
  • Loading branch information
eteubert committed Dec 30, 2024
1 parent 2880fd0 commit 539bf62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
58 changes: 37 additions & 21 deletions server/lib/publisher/wordpress/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 8 additions & 22 deletions server/lib/publisher_web/controllers/validator/save_episode.ex
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

0 comments on commit 539bf62

Please sign in to comment.