diff --git a/.vscode/launch.json b/.vscode/launch.json index e08a664..0572f28 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "Python: Current File", "type": "python", "request": "launch", - "program": "main.py", + "program": "src/main.py", "envFile": "${workspaceFolder}/.env", "console": "integratedTerminal", "justMyCode": true diff --git a/.vscode/settings.json b/.vscode/settings.json index c1670f7..56bda65 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,9 @@ { "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.codeActionsOnSave": { + "source.organizeImports": true + } }, "python.formatting.provider": "none", "python.analysis.autoImportCompletions": true, diff --git a/Dockerfile b/Dockerfile index 5bd3305..26528a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,4 +19,4 @@ COPY . /app RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app USER appuser -CMD ["python", "main.py"] +CMD ["python", "src/main.py"] diff --git a/docker-compose.debug.yml b/docker-compose.debug.yml deleted file mode 100644 index 0ae9fbc..0000000 --- a/docker-compose.debug.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: '3.4' - -services: - pixivcord: - image: pixivcord - build: - context: . - dockerfile: ./Dockerfile - command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 main.py "] - ports: - - 5678:5678 diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 1bdb3de..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: '3.4' - -services: - pixivcord: - image: pixivcord - build: - context: . - dockerfile: ./Dockerfile diff --git a/src/discord_tools/__init__.py b/src/discord_tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/send_message.py b/src/discord_tools/message.py similarity index 79% rename from send_message.py rename to src/discord_tools/message.py index 753208e..cfc2ab0 100644 --- a/send_message.py +++ b/src/discord_tools/message.py @@ -1,7 +1,10 @@ import math import os + import discord +TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` and `>`." + async def send_message_with_embed( message: discord.Message, files, title: str, tmp_dir: str, url: str @@ -39,7 +42,7 @@ async def send_message_with_embed( await message.reply(files=to_send_files, embeds=to_send_embeds) -async def send_message(message: discord.Message, files, tmp_dir: str): +async def send_message(message: discord.Message, files, tmp_dir: str, muted: bool): to_send_files = [] for file in files: joined = os.path.join(tmp_dir, file) @@ -47,13 +50,17 @@ async def send_message(message: discord.Message, files, tmp_dir: str): if len(to_send_files) > 10: first = True while len(to_send_files) > 10: - splited_file = to_send_files[:4] - to_send_files = to_send_files[4:] + splited_file = to_send_files[:10] + to_send_files = to_send_files[10:] if first: - await message.reply(files=splited_file) + await message.reply( + files=splited_file, content=(TIPS_MESSAGE if not muted else None) + ) first = False else: await message.channel.send(files=splited_file) await message.channel.send(files=to_send_files) else: - await message.reply(files=to_send_files) + await message.reply( + files=to_send_files, content=(TIPS_MESSAGE if not muted else None) + ) diff --git a/src/discord_tools/pixiv_link_extractor.py b/src/discord_tools/pixiv_link_extractor.py new file mode 100644 index 0000000..1071603 --- /dev/null +++ b/src/discord_tools/pixiv_link_extractor.py @@ -0,0 +1,18 @@ +import re +from typing import Tuple, Union + +import discord + +link_regexp = r"(?P<)?(?Phttps?://(?:www\.)?pixiv.net/(?:\w+/)?artworks/(?P\d+))(?P>)?" + + +def should_process( + message: discord.Message, +) -> Tuple[bool, bool, Union[re.Match, None]]: + if match := re.search(link_regexp, message.content): + muted = bool(match.group("muted1")) and bool(match.group("muted2")) + if len(message.attachments) == 0: + return (True, muted, match) + else: + return (False, False, match) + return (False, False, None) diff --git a/main.py b/src/main.py similarity index 75% rename from main.py rename to src/main.py index 70564fc..763931d 100644 --- a/main.py +++ b/src/main.py @@ -1,11 +1,12 @@ -import math import os -import re from shlex import join from tempfile import TemporaryDirectory + import discord -import pixiv -import send_message + +import discord_tools.message +import discord_tools.pixiv_link_extractor +import pixiv.pixiv_api as pixiv_api REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN") if REFRESH_TOKEN is None: @@ -32,16 +33,11 @@ async def on_ready(): async def on_message(message: discord.Message): if message.author == client.user: return - - if ( - match := re.search( - r"(?Phttps?://(?:www\.)?pixiv.net/(?:\w+/)?artworks/(?P\d+))", - message.content, - ) - ) is not None: + (result, muted, match) = discord_tools.pixiv_link_extractor.should_process(message) + if result and match is not None: with TemporaryDirectory() as tmp_dir: async with message.channel.typing(): - title = pixiv.dowload_pixiv_images( + title = pixiv_api.dowload_pixiv_images( int(match.group("id")), tmp_dir, REFRESH_TOKEN ) files = os.listdir(tmp_dir) @@ -49,7 +45,7 @@ async def on_message(message: discord.Message): # await send_message.send_message_with_embed( # message, files, title, tmp_dir, match.group("url") # ) - await send_message.send_message(message, files, tmp_dir) + await discord_tools.message.send_message(message, files, tmp_dir, muted) client.run(DISCORD_TOKEN) diff --git a/src/pixiv/__init__.py b/src/pixiv/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pixiv.py b/src/pixiv/pixiv_api.py similarity index 100% rename from pixiv.py rename to src/pixiv/pixiv_api.py