From f23f6cc55f472e06f5a7b40b3c65e8a1c037f670 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 2 Nov 2023 12:02:03 +0100 Subject: [PATCH] Add logging --- src/main.py | 14 ++++++++++---- src/pixiv/pixiv_api.py | 17 +++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main.py b/src/main.py index d12529a..07d11f4 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import logging import os from shlex import join from tempfile import TemporaryDirectory @@ -8,13 +9,18 @@ import discord_tools.message import discord_tools.pixiv_link_extractor import pixiv.pixiv_api as pixiv_api +logging.basicConfig( + format="[%(asctime)s] [%(levelname)8s] %(name)s: %(message)s", level=logging.INFO +) + + REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN") if REFRESH_TOKEN is None: - print("Pixiv refresh token is missing, please set PIXIV_REFRESH_TOKEN") + logging.fatal("Pixiv refresh token is missing, please set PIXIV_REFRESH_TOKEN") exit(1) DISCORD_TOKEN = os.getenv("DISCORD_TOKEN") if DISCORD_TOKEN is None: - print("Discord token is missing, please set DISCORD_TOKEN") + logging.fatal("Discord token is missing, please set DISCORD_TOKEN") exit(1) @@ -36,7 +42,7 @@ client = MyClient(intents=intents) @client.event async def on_ready(): - print(f"We have logged in as {client.user}") + logging.info(f"We have logged in as {client.user}") @client.tree.command() @@ -106,7 +112,7 @@ async def pixiv_preview(interaction: discord.Interaction, message: discord.Messa @client.event async def on_guild_join(guild: discord.Guild): - print(f"Joined new guild: {guild.name}") + logging.info(f"Joined new guild: {guild.name}") @client.event diff --git a/src/pixiv/pixiv_api.py b/src/pixiv/pixiv_api.py index 46bc8f4..bed34e1 100644 --- a/src/pixiv/pixiv_api.py +++ b/src/pixiv/pixiv_api.py @@ -1,3 +1,4 @@ +import logging import os import threading from queue import Queue @@ -24,13 +25,13 @@ def dowload_pixiv_images( return None pages = illust["meta_pages"] if len(pages) > 0: - print(f"Dowloading mutiple images to {dest_folder} ({len(pages)})") + logging.info(f"Dowloading mutiple images to {dest_folder} ({len(pages)})") if max_thread_num > len(pages): num_threads = len(pages) else: num_threads = max_thread_num - - print(f"Starting {num_threads} thread(s)") + + logging.info(f"Starting {num_threads} thread(s)") workers: List[Thread] = [] q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]] = Queue() for i in range(num_threads): @@ -53,11 +54,11 @@ def dowload_pixiv_images( worker.join() else: - print(f"Dowloading single image to {dest_folder}") + logging.info(f"Dowloading single image to {dest_folder}") api.download( url=illust["meta_single_page"]["original_image_url"], path=dest_folder ) - print("Download finished !") + logging.info("Download finished !") return illust["title"] @@ -66,11 +67,11 @@ def download_thread(q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]] while True: data = q.get() if data is None: - print(f"[{thread_name}] - Bye bye") + logging.debug(f"[{thread_name}] - Bye bye") q.task_done() break (url, dest_folder, api) = data - print(f"[{thread_name}] - Downloading {url}...") + logging.debug(f"[{thread_name}] - Downloading {url}...") api.download(url=url, path=dest_folder) - print(f"[{thread_name}] - ... Done") + logging.debug(f"[{thread_name}] - ... Done") q.task_done()