Add logging
This commit is contained in:
parent
5912a8527f
commit
f23f6cc55f
14
src/main.py
14
src/main.py
@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from shlex import join
|
from shlex import join
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
@ -8,13 +9,18 @@ import discord_tools.message
|
|||||||
import discord_tools.pixiv_link_extractor
|
import discord_tools.pixiv_link_extractor
|
||||||
import pixiv.pixiv_api as pixiv_api
|
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")
|
REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN")
|
||||||
if REFRESH_TOKEN is None:
|
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)
|
exit(1)
|
||||||
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
||||||
if DISCORD_TOKEN is None:
|
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)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
@ -36,7 +42,7 @@ client = MyClient(intents=intents)
|
|||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
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()
|
@client.tree.command()
|
||||||
@ -106,7 +112,7 @@ async def pixiv_preview(interaction: discord.Interaction, message: discord.Messa
|
|||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_guild_join(guild: discord.Guild):
|
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
|
@client.event
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
@ -24,13 +25,13 @@ def dowload_pixiv_images(
|
|||||||
return None
|
return None
|
||||||
pages = illust["meta_pages"]
|
pages = illust["meta_pages"]
|
||||||
if len(pages) > 0:
|
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):
|
if max_thread_num > len(pages):
|
||||||
num_threads = len(pages)
|
num_threads = len(pages)
|
||||||
else:
|
else:
|
||||||
num_threads = max_thread_num
|
num_threads = max_thread_num
|
||||||
|
|
||||||
print(f"Starting {num_threads} thread(s)")
|
logging.info(f"Starting {num_threads} thread(s)")
|
||||||
workers: List[Thread] = []
|
workers: List[Thread] = []
|
||||||
q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]] = Queue()
|
q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]] = Queue()
|
||||||
for i in range(num_threads):
|
for i in range(num_threads):
|
||||||
@ -53,11 +54,11 @@ def dowload_pixiv_images(
|
|||||||
worker.join()
|
worker.join()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"Dowloading single image to {dest_folder}")
|
logging.info(f"Dowloading single image to {dest_folder}")
|
||||||
api.download(
|
api.download(
|
||||||
url=illust["meta_single_page"]["original_image_url"], path=dest_folder
|
url=illust["meta_single_page"]["original_image_url"], path=dest_folder
|
||||||
)
|
)
|
||||||
print("Download finished !")
|
logging.info("Download finished !")
|
||||||
return illust["title"]
|
return illust["title"]
|
||||||
|
|
||||||
|
|
||||||
@ -66,11 +67,11 @@ def download_thread(q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]]
|
|||||||
while True:
|
while True:
|
||||||
data = q.get()
|
data = q.get()
|
||||||
if data is None:
|
if data is None:
|
||||||
print(f"[{thread_name}] - Bye bye")
|
logging.debug(f"[{thread_name}] - Bye bye")
|
||||||
q.task_done()
|
q.task_done()
|
||||||
break
|
break
|
||||||
(url, dest_folder, api) = data
|
(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)
|
api.download(url=url, path=dest_folder)
|
||||||
print(f"[{thread_name}] - ... Done")
|
logging.debug(f"[{thread_name}] - ... Done")
|
||||||
q.task_done()
|
q.task_done()
|
||||||
|
Loading…
Reference in New Issue
Block a user