Compare commits

...

2 commits

Author SHA1 Message Date
ef6828531b Test download util 2024-10-13 17:42:02 +02:00
4c8d48c575 Add doc 2024-10-13 17:41:55 +02:00
3 changed files with 38 additions and 3 deletions

View file

@ -21,6 +21,15 @@ defmodule HahaYes.Commands.Download do
User: h3h3 download https://www.youtube.com/watch?v=ryS7TS_J7KA
Bot: <video file>
User: h3h3 download https://cdn.discordapp.com/attachments/790701936394633216/1295042668929355816/1295042650113704087.mp4?ex=670d35f9&is=670be479&hm=bf61358b9b6f8c94a14be4e7e3650799ef733331e84d87254b71a098646c4bc2&
Bot: <video file>
User: h3h3 download invalid
Bot: `[generic] Extracting URL: invalid
ERROR: [generic] 'invalid' is not a valid URL. Set --default-search "ytsearch" (or run yt-dlp "ytsearch:invalid" ) to search YouTube`
"""
def execute(msg, _ws_state, args) do
url = Enum.at(args, 0)

View file

@ -1,4 +1,26 @@
defmodule HahaYes.Utility do
@moduledoc """
Various utilities to be reused in commands
"""
@doc """
Download utility with the format at 480p by default.
## Example
```
iex> HahaYes.Utility.download("https://x.com/i/status/1844841048603783249", "#{System.tmp_dir}/test")
{:ok, /tmp}/test.mp4
```
```
iex> HahaYes.Utility.download("invalid", "#{System.tmp_dir}/test")
{:error, "[generic] Extracting URL: invalid\\nERROR: [generic] 'invalid' is not a valid URL. Set --default-search "ytsearch" (or run yt-dlp "ytsearch:invalid" ) to search YouTube"}
```
"""
def download(url, output, format \\ "bestvideo[height<=?480]+bestaudio/best") do
opt = ["-f", format, url, "-o", "#{output}.%(ext)s", "--force-overwrites", "--playlist-reverse", "--no-playlist", "--remux-video=mp4/webm/mov", "--no-warnings"];

View file

@ -1,8 +1,12 @@
defmodule HahaYesTest do
use ExUnit.Case
doctest HahaYes
doctest HahaYes.Utility
test "greets the world" do
assert HahaYes.hello() == :world
@tag :tmp_dir
test "download utils", %{tmp_dir: tmp_dir} do
assert HahaYes.Utility.download("https://x.com/i/status/1844841048603783249", "#{tmp_dir}/test") == {:ok, "#{tmp_dir}/test.mp4"}
assert HahaYes.Utility.download("invalid", "#{tmp_dir}/test") == {:error, "[generic] Extracting URL: invalid\nERROR: [generic] 'invalid' is not a valid URL. Set --default-search \"ytsearch\" (or run yt-dlp \"ytsearch:invalid\" ) to search YouTube\n"}
end
end