From b43d7496ae50c78c6b1d91c241a07b6473072a8f Mon Sep 17 00:00:00 2001 From: TJ Date: Mon, 22 Jul 2024 21:34:40 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/todos.md | 8 +++++++- lib/aier_bot/bot.ex | 24 +++++++----------------- lib/aier_bot/cobalt_client.ex | 10 +++++----- lib/aier_bot/file_helper.ex | 27 ++++++++++++--------------- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/docs/todos.md b/docs/todos.md index 522f8d8..812f062 100644 --- a/docs/todos.md +++ b/docs/todos.md @@ -1,5 +1,11 @@ # Todos +DevOps + +- [ ] ssh & scp skip password, setup token to ssh and scp + +features + - [ ] list all ins response - [ ] image - [ ] images @@ -10,7 +16,7 @@ big task -- [x] download ins videos and images +- [x] download ins videos and _images_ - [ ] download multiple files in one task - [ ] make it fast and with UI progress bar diff --git a/lib/aier_bot/bot.ex b/lib/aier_bot/bot.ex index beaa8f3..75d9aa5 100644 --- a/lib/aier_bot/bot.ex +++ b/lib/aier_bot/bot.ex @@ -37,10 +37,14 @@ defmodule AierBot.Bot do case CobaltClient.get_download_url(url) do {:ok, download_url} -> - {:ok, file_name, file_content} = FileHelper.download(download_url) + case FileHelper.download(download_url) do + {:ok, file_name, file_content} -> + {:ok, _} = bot_send_file(chat.id, file_name, file_content, url) + FileHelper.write_into_file(chat.id, file_name, file_content) - {:ok, _} = bot_send_file(chat.id, file_name, file_content, url) - FileHelper.write_into_file(chat.id, file_name, file_content) + {:error, error} -> + ExGram.send_message(chat.id, "Failed to download file. #{inspect(error)}") + end {:error, error} -> ExGram.send_message(chat.id, "Failed to download file. Reason: #{inspect(error)}") @@ -58,8 +62,6 @@ defmodule AierBot.Bot do # TODO: 额外参数可以使用 options 来传递 defp bot_send_file(chat_id, file_name, file_content, _original_url) do - IO.inspect(file_name) - cond do String.ends_with?(file_name, ".png") -> ExGram.send_photo( @@ -103,16 +105,4 @@ defmodule AierBot.Bot do ) end end - - def create_memo_success(_, context) do - answer(context, "Memo saved!") - end - - def image_generation_success(%{data: images}, chat_id) do - [first | _] = images - - res = ExGram.send_photo(chat_id, {:file_content, first, "image.png"}, bot: @bot) - - IO.inspect(res) - end end diff --git a/lib/aier_bot/cobalt_client.ex b/lib/aier_bot/cobalt_client.ex index 890f735..ab4ad70 100644 --- a/lib/aier_bot/cobalt_client.ex +++ b/lib/aier_bot/cobalt_client.ex @@ -1,4 +1,6 @@ defmodule AierBot.CobaltClient do + require Logger + use Tesla plug(Tesla.Middleware.BaseUrl, "https://api.cobalt.tools") @@ -20,7 +22,7 @@ defmodule AierBot.CobaltClient do def get_download_url(text) do # https://www.instagram.com/p/C9pr7NDPAyd/?igsh=azBiNHJ0ZXd3bTFh => https://www.instagram.com/p/C9pr7NDPAyd/ url = String.split(text, "?") |> hd() - # IO.inspect(pure_url) + case post("api/json", %{url: url}) do {:ok, response} -> # @@ -35,14 +37,12 @@ defmodule AierBot.CobaltClient do # TODO: handle multiple urls {:ok, Enum.at(picker_items, 0)["url"]} - # url %{"status" => "error", "text" => error_msg} -> - IO.puts("Error: #{error_msg}") - + Logger.warning("http error: #{error_msg}") {:error, error_msg} _ -> - IO.inspect(response.body) + Logger.warning("Unknown error") {:error, "Unknown error"} end diff --git a/lib/aier_bot/file_helper.ex b/lib/aier_bot/file_helper.ex index bd2d795..668e699 100644 --- a/lib/aier_bot/file_helper.ex +++ b/lib/aier_bot/file_helper.ex @@ -1,15 +1,16 @@ defmodule AierBot.FileHelper do + require Logger use Tesla def download(url) do cond do - String.contains?(url, "/api/stream") -> download_streaming(url) + String.contains?(url, "/api/stream") -> download_stream(url) true -> download_file(url) end end - defp download_streaming(url) do - IO.inspect(url, label: "Download URL") + defp download_stream(url) do + Logger.info("Start downloading stream", url: url) case get(url) do {:ok, %Tesla.Env{status: 200, body: body}} -> @@ -17,17 +18,15 @@ defmodule AierBot.FileHelper do {:ok, file_name, body} {:ok, %Tesla.Env{status: status}} -> - IO.puts("Failed to download file. Status: #{status}") - {:error, "Failed to download file"} + {:error, "Status: #{status}"} {:error, reason} -> - IO.puts("Failed to download file. Reason: #{inspect(reason)}") - {:error, "Failed to download file"} + {:error, "Reason: #{inspect(reason)}"} end end defp download_file(url) do - IO.inspect(url, label: "Download URL") + Logger.info("Start downloading file", url: url) case get(url) do {:ok, %Tesla.Env{status: 200, body: body, headers: headers}} -> @@ -42,12 +41,10 @@ defmodule AierBot.FileHelper do {:ok, file_name, body} {:ok, %Tesla.Env{status: status}} -> - IO.puts("Failed to download file. Status: #{status}") - {:error, "Failed to download file"} + {:error, "Status: #{status}"} {:error, reason} -> - IO.puts("Failed to download file. Reason: #{inspect(reason)}") - {:error, "Failed to download file"} + {:error, "Reason #{inspect(reason)}"} end end @@ -58,14 +55,14 @@ defmodule AierBot.FileHelper do :ok -> case File.write(Path.join([dir, file_name]), file_content) do :ok -> - IO.puts("File written successfully.") + Logger.info("File written successfully", file_name: file_name) {:error, reason} -> - IO.puts("Failed to write file: #{reason}") + Logger.error("Error: failed to write file, reason: #{reason}") end {:error, reason} -> - IO.puts("Failed to create directory: #{reason}") + Logger.error("Error: failed to create directory, reason: #{reason}") end end