From 23a884339d2d504044927507ed41f388409e34cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Bersier?= Date: Sun, 13 Oct 2024 17:17:40 +0200 Subject: [PATCH] Moved download to its own function --- lib/HahaCommands/download.ex | 42 +++++++++++++++++------------------- lib/haha_yes/utils.ex | 12 +++++++++++ 2 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 lib/haha_yes/utils.ex diff --git a/lib/HahaCommands/download.ex b/lib/HahaCommands/download.ex index 731c301..cd051ba 100644 --- a/lib/HahaCommands/download.ex +++ b/lib/HahaCommands/download.ex @@ -26,31 +26,29 @@ defmodule HahaYes.Commands.Download do url = Enum.at(args, 0) {:ok, loading} = Api.create_message(msg.channel_id, "Downloading...") + with {:ok, output} <- HahaYes.Utility.download(url, "#{System.tmp_dir}/#{msg.id}") do + {:ok, file} = File.stat(output) + file_size = + file.size / 1000000.0 + |> Decimal.from_float() + |> Decimal.round(2) + |> Decimal.to_float() - opt = ["-f", "bestvideo[height<=?480]+bestaudio/best", url, "-o", "#{System.tmp_dir}/#{msg.id}.%(ext)si", "--force-overwrites", "--playlist-reverse", "--no-playlist", "--remux-video=mp4/webm/mov", "--no-warnings"]; + Api.delete_message(loading.channel_id, loading.id) + Api.delete_message(msg) - System.cmd("yt-dlp", opt) - - output = Enum.at(Path.wildcard("#{System.tmp_dir}/#{msg.id}.*"), 0) - {:ok, file} = File.stat(output) - file_size = - file.size / 1000000.0 - |> Decimal.from_float() - |> Decimal.round(2) - |> Decimal.to_float() - - Api.delete_message(loading.channel_id, loading.id) - Api.delete_message(msg) - - if file_size >= 25 do - Api.create_message(msg.channel_id, "File size is too big! (#{file_size})") + if file_size >= 25 do + Api.create_message(msg.channel_id, "File size is too big! (#{file_size})") + else + embed = + %Nostrum.Struct.Embed{} + |> put_color(431_948) + |> put_author("Downloaded by #{msg.author.username} (#{file_size} MB)", url, "https://cdn.discordapp.com/avatars/#{msg.author.id}/#{msg.author.avatar}.webp") + |> put_footer("You can get the original video by clicking on the \"Downloaded by #{msg.author.username}\" message!") + Api.create_message(msg.channel_id, files: [output], embeds: [embed]) + end else - embed = - %Nostrum.Struct.Embed{} - |> put_color(431_948) - |> put_author("Downloaded by #{msg.author.username} (#{file_size} MB)", url, "https://cdn.discordapp.com/avatars/#{msg.author.id}/#{msg.author.avatar}.webp") - |> put_footer("You can get the original video by clicking on the \"Downloaded by #{msg.author.username}\" message!") - Api.create_message(msg.channel_id, files: [output], embeds: [embed]) + {:error, error} -> Api.create_message(msg.channel_id, "`#{error}`") end end end diff --git a/lib/haha_yes/utils.ex b/lib/haha_yes/utils.ex new file mode 100644 index 0000000..d545ac3 --- /dev/null +++ b/lib/haha_yes/utils.ex @@ -0,0 +1,12 @@ +defmodule HahaYes.Utility do + 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"]; + + {error_output, status} = System.cmd("yt-dlp", opt, [stderr_to_stdout: true]) + if status !== 0 do + {:error, error_output} + else + {:ok, Enum.at(Path.wildcard("#{output}.*"), 0)} + end + end +end