Skip to content

Commit

Permalink
refactor: 💡 logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusJiang committed Jul 22, 2024
1 parent d981c7f commit b43d749
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 38 deletions.
8 changes: 7 additions & 1 deletion docs/todos.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Todos

DevOps

- [ ] ssh & scp skip password, setup token to ssh and scp

features

- [ ] list all ins response
- [ ] image
- [ ] images
Expand All @@ -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

Expand Down
24 changes: 7 additions & 17 deletions lib/aier_bot/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand All @@ -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(
Expand Down Expand Up @@ -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
10 changes: 5 additions & 5 deletions lib/aier_bot/cobalt_client.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule AierBot.CobaltClient do
require Logger

use Tesla

plug(Tesla.Middleware.BaseUrl, "https://api.cobalt.tools")
Expand All @@ -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} ->
#
Expand All @@ -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

Expand Down
27 changes: 12 additions & 15 deletions lib/aier_bot/file_helper.ex
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
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}} ->
file_name = gen_desc_filename() <> ".mp4"
{: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}} ->
Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit b43d749

Please sign in to comment.