@@ -38,6 +38,38 @@ defmodule Publisher.WordPress.Episode do
38
38
|> String . trim_leading ( "." )
39
39
end
40
40
41
+ @ doc """
42
+ Parses the given date string and returns a DateTime struct or nil.
43
+
44
+ ## Examples
45
+
46
+ iex> parse_date("2023-10-05T14:48:00Z")
47
+ ~U[2023-10-05 14:48:00Z]
48
+
49
+ iex> parse_date("not a date")
50
+ nil
51
+
52
+ """
53
+ def parse_date ( date ) do
54
+ formats = [
55
+ "{RFC822}" ,
56
+ "{RFC822z}" ,
57
+ "{RFC1123}" ,
58
+ "{RFC1123z}" ,
59
+ "{RFC3339}" ,
60
+ "{RFC3339z}" ,
61
+ "{ISO:Extended}" ,
62
+ "{ISO:Extended:Z}"
63
+ ]
64
+
65
+ Enum . find_value ( formats , fn format ->
66
+ case Timex . parse ( date , format ) do
67
+ { :ok , date } -> date
68
+ { :error , _ } -> false
69
+ end
70
+ end )
71
+ end
72
+
41
73
# Finds episode by guid. Creates episode with that episode if none exists.
42
74
# Returns episode id.
43
75
defp find_or_create_episode ( req , guid ) do
@@ -106,26 +138,6 @@ defmodule Publisher.WordPress.Episode do
106
138
|> DateTime . to_iso8601 ( )
107
139
end
108
140
109
- defp parse_date ( date ) do
110
- formats = [
111
- "{RFC822}" ,
112
- "{RFC822z}" ,
113
- "{RFC1123}" ,
114
- "{RFC1123z}" ,
115
- "{RFC3339}" ,
116
- "{RFC3339z}" ,
117
- "{ISO:Extended}" ,
118
- "{ISO:Extended:Z}"
119
- ]
120
-
121
- Enum . find_value ( formats , fn format ->
122
- case Timex . parse ( date , format ) do
123
- { :ok , date } -> date
124
- { :error , _ } -> false
125
- end
126
- end )
127
- end
128
-
129
141
defp upload_content ( req , post_id , % { "content" => content , "pub_date" => pub_date } = _params )
130
142
when not is_nil ( content ) and not is_nil ( pub_date ) do
131
143
Logger . info ( "Episode post #{ post_id } content is #{ String . length ( content ) } " )
@@ -283,6 +295,7 @@ defmodule Publisher.WordPress.Episode do
283
295
case create_contributor ( req , name ) do
284
296
{ :ok , id } ->
285
297
[ id | acc ]
298
+
286
299
{ :error , reason } ->
287
300
Logger . info ( "Couldn't create a contributor: #{ inspect ( reason ) } " )
288
301
acc
@@ -368,16 +381,19 @@ defmodule Publisher.WordPress.Episode do
368
381
369
382
defp save_episode_image_url ( req , episode_id , source_url ) do
370
383
Logger . info ( "Episode use #{ source_url } as cover: #{ episode_id } " )
384
+
371
385
body = % {
372
386
episode_poster: source_url
373
387
}
388
+
374
389
Req . post ( req , url: "podlove/v2/episodes/#{ episode_id } " , json: body )
375
390
:ok
376
391
end
377
392
378
393
defp upload_cover ( req , episode_id , post_id , % { "cover" => cover } = params )
379
- when not is_nil ( cover ) do
394
+ when not is_nil ( cover ) do
380
395
image_name = "cover-" <> params [ "slug" ]
396
+
381
397
with { :ok , source_url } <- Media . upload_media_from_url ( req , post_id , cover , image_name ) ,
382
398
:ok <- save_episode_image_url ( req , episode_id , source_url ) do
383
399
:ok
0 commit comments