-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.exs
65 lines (50 loc) · 1.17 KB
/
main.exs
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
defmodule Storage do
use Agent
def start_link() do
Agent.start_link(fn -> [] end, name: :storage)
end
def add(data) do
Agent.update(:storage, fn l -> [data] ++ l end)
end
def pop() do
{ret, l} = Agent.get(:storage, fn l -> List.pop_at(l, 0) end)
Agent.update(:storage, fn _ -> l end)
ret
end
end
# module
defmodule Reader do
def read(path) do
path
|> File.stream!()
|> Enum.each(fn x -> Storage.add(String.trim(x)) end)
end
end
# module
defmodule Downloader do
def download(url, dir) do
path = Path.join(dir, "%(uploader)s-%(title)s.%(ext)s")
cmd =
'youtube-dl #{url} -o "#{path}" --extract-audio --audio-format mp3'
:os.cmd(cmd)
IO.puts("Downloaded #{url}")
end
end
# module
## Main ##
{[dir: dir, input: input], _, _} =
OptionParser.parse(System.argv(), strict: [dir: :string, input: :string])
Storage.start_link()
Reader.read(input)
create_downloaders = fn f, t ->
case Storage.pop() do
nil ->
t
x ->
task = Task.async(fn -> Downloader.download(x, dir) end)
f.(f, [task] ++ t)
end
end
create_downloaders
|> create_downloaders.([])
|> Enum.map(&Task.await(&1, :infinity))