/
puller.ex
79 lines (61 loc) · 2.01 KB
/
puller.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule Changelog.Github.Puller do
require Logger
alias Changelog.{Cache, Episode, Github, HTTP, Podcast, Repo}
alias ChangelogWeb.PodcastView
defmodule GetException do
defexception message: "Failed to get unknown"
end
def update(type, items) when is_list(items) do
for item <- items do
item
|> get_episode()
|> update_episode(type)
|> Cache.delete()
end
end
def update(type, item), do: update(type, [item])
defp get_episode(episode = %Episode{}), do: episode
defp get_episode(filename) do
case String.split(filename, "/") do
[podcast_slug, episode_section] ->
podcast_slug
|> get_podcast_from_repo()
|> get_episode_from_repo(episode_section)
_else ->
nil
end
end
defp get_podcast_from_repo(slug), do: Repo.get_by(Podcast, slug: slug)
defp get_episode_from_repo(nil, _filename), do: nil
defp get_episode_from_repo(podcast, filename) do
podcast_name = PodcastView.dasherized_name(podcast)
case Regex.named_captures(~r/#{podcast_name}-(?<episode>.*?)\.md/, filename) do
%{"episode" => episode_slug} ->
Episode.published()
|> Episode.with_podcast_slug(podcast.slug)
|> Episode.with_slug(episode_slug)
|> Repo.one()
nil ->
nil
end
end
defp update_episode(episode, _type) when is_nil(episode), do: nil
defp update_episode(episode, type) do
episode = Episode.preload_podcast(episode)
source = Github.Source.new(type, episode)
case HTTP.get!(source.raw_url) do
%{status_code: 200, body: text} -> update_function(type, episode, text)
_else ->
message = "Failed to get #{source.raw_url}"
Sentry.capture_exception(%__MODULE__.GetException{message: message})
Logger.info(message)
end
episode
end
defp update_function("transcripts", episode, text) do
Episode.update_transcript(episode, text)
end
defp update_function("show-notes", episode, text) do
Episode.update_notes(episode, text)
end
end