Compare commits
132 Commits
Author | SHA1 | Date | |
---|---|---|---|
284f5d9592 | |||
4039d7c02e | |||
ea9c07a428 | |||
a0379e2a62 | |||
4115240506 | |||
a6857407be | |||
c30eab2945 | |||
a43885f0ca | |||
2f31ca22c6 | |||
19ae9d1b81 | |||
c37d874d8c | |||
1ff508162f | |||
179d108e6a | |||
ceaa96ac21 | |||
78e904bd10 | |||
a7de73957a | |||
951793bf7f | |||
00b157c21a | |||
aca4f7306d | |||
089c32ad6f | |||
c291cd674d | |||
8cda1029d0 | |||
b77b0cc50d | |||
6b5daaf154 | |||
c74b9fae2d | |||
ccab852e25 | |||
215d3187ad | |||
aa8b6ac403 | |||
e4fbdf6986 | |||
6dc7501f6f | |||
202ce8d7e5 | |||
f2a9acb6ca | |||
f9e8ba50c5 | |||
f48524443b | |||
0d99704345 | |||
360e2c4308 | |||
39e98e45c4 | |||
aaba8b2cae | |||
2dfcaeadba | |||
c57d65b402 | |||
a08938ca38 | |||
3499ddfe68 | |||
b084c2e643 | |||
342ed840bc | |||
241d989600 | |||
f5e1ec2a51 | |||
54a9eaae6c | |||
86446f22e6 | |||
5cd7621065 | |||
f52b555f6c | |||
9156129790 | |||
27beb67f5b | |||
9015c6c4fd | |||
5f32eebd6b | |||
e1969bcae9 | |||
343ef81e5e | |||
84c29fd4b6 | |||
bbb97828e1 | |||
e4beb8ae64 | |||
a738435020 | |||
c7b312b41f | |||
82e220d920 | |||
0c08a8c0b2 | |||
c99f631220 | |||
f2a56448f7 | |||
67e232ff33 | |||
|
0b63bb9915 | ||
ca2ebe0fe3 | |||
fe6ecdfa05 | |||
|
250524a936 | ||
|
b1228b458d | ||
adcd7e92d3 | |||
|
7742051c45 | ||
a06aa2e307 | |||
481bb06b73 | |||
4c00f4f620 | |||
|
ade474e035 | ||
|
4cbb59f8aa | ||
|
0e4d60c466 | ||
776f46a30b | |||
b16383e856 | |||
|
cfb770d553 | ||
|
dabd9c37dd | ||
8dd8ab0996 | |||
d57751e1a1 | |||
62155703ae | |||
d7ab55a64a | |||
d9a5e50685 | |||
|
6a4aaf76f7 | ||
|
bd7758fce2 | ||
|
19f4e88a07 | ||
|
98ca76d285 | ||
cc6a8bd47c | |||
246e846052 | |||
afaa6e42ee | |||
36b895cf22 | |||
a5b690b4af | |||
|
2b2190a32b | ||
|
0b614ba907 | ||
|
759a63a7d6 | ||
|
501c77af7b | ||
|
8c47ae8a07 | ||
b305825bf6 | |||
|
11bdc75857 | ||
a7883d8edd | |||
311baa21e0 | |||
1900355cf2 | |||
|
b80613e3d1 | ||
1aecdc2d9c | |||
1ea6f9f871 | |||
ff4b530b26 | |||
e61325fc1e | |||
69f55616e2 | |||
|
4d7a91801b | ||
30df41f5a5 | |||
ee75a9dbc0 | |||
9759eb7bf4 | |||
|
840f8a58c5 | ||
703feb8ca3 | |||
bc5683cd72 | |||
f4e4c5dda4 | |||
2cb1539517 | |||
82b9ee4a39 | |||
1fd60f9207 | |||
36591fda85 | |||
f23f6cc55f | |||
5912a8527f | |||
|
61ef3bfd30 | ||
|
50034cf7b0 | ||
|
7047538a78 | ||
|
d08d5131f7 | ||
|
c592146efb |
48
.gitea/workflows/docker.yml
Normal file
48
.gitea/workflows/docker.yml
Normal file
@ -0,0 +1,48 @@
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- "*"
|
||||
tags:
|
||||
- "*"
|
||||
pull_request:
|
||||
branches:
|
||||
- "main"
|
||||
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: catthehacker/ubuntu:act-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Docker meta
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: |
|
||||
git.sebclem.fr/${{ gitea.repository }}
|
||||
tags: |
|
||||
type=ref,event=branch
|
||||
type=ref,event=pr
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,pattern={{major}}.{{minor}}
|
||||
|
||||
- name: Login to registry
|
||||
if: github.ref_type == 'tag'
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: git.sebclem.fr
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
push: ${{ github.ref_type == 'tag' }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
12
.vscode/launch.json
vendored
12
.vscode/launch.json
vendored
@ -4,11 +4,21 @@
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Python: Remote Attach",
|
||||
"type": "python",
|
||||
"request": "attach",
|
||||
"connect": { "host": "docker.cloud.home", "port": 5678 },
|
||||
"pathMappings": [
|
||||
{ "localRoot": "${workspaceFolder}", "remoteRoot": "." }
|
||||
],
|
||||
"justMyCode": true
|
||||
},
|
||||
{
|
||||
"name": "Python: Current File",
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"program": "main.py",
|
||||
"program": "src/main.py",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"console": "integratedTerminal",
|
||||
"justMyCode": true
|
||||
|
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -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,
|
||||
|
@ -1,5 +1,5 @@
|
||||
# For more information, please refer to https://aka.ms/vscode-docker-python
|
||||
FROM python:3.10-slim
|
||||
FROM python:3.12-alpine
|
||||
|
||||
# Keeps Python from generating .pyc files in the container
|
||||
ENV PYTHONDONTWRITEBYTECODE=1
|
||||
@ -7,6 +7,8 @@ ENV PYTHONDONTWRITEBYTECODE=1
|
||||
# Turns off buffering for easier container logging
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
|
||||
RUN apk add build-base
|
||||
|
||||
# Install pip requirements
|
||||
COPY requirements.txt .
|
||||
RUN python -m pip install -r requirements.txt
|
||||
@ -19,4 +21,5 @@ COPY . /app
|
||||
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
|
||||
USER appuser
|
||||
|
||||
CMD ["python", "main.py"]
|
||||
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
|
||||
CMD ["python", "src/main.py"]
|
||||
|
@ -6,6 +6,6 @@ services:
|
||||
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 "]
|
||||
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 src/main.py "]
|
||||
ports:
|
||||
- 5678:5678
|
||||
|
55
main.py
55
main.py
@ -1,55 +0,0 @@
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
from shlex import join
|
||||
from tempfile import TemporaryDirectory
|
||||
import discord
|
||||
import pixiv
|
||||
import send_message
|
||||
|
||||
REFRESH_TOKEN = os.getenv("PIXIV_REFRESH_TOKEN")
|
||||
if REFRESH_TOKEN is None:
|
||||
print("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")
|
||||
exit(1)
|
||||
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
|
||||
client = discord.Client(intents=intents)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print(f"We have logged in as {client.user}")
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message(message: discord.Message):
|
||||
if message.author == client.user:
|
||||
return
|
||||
|
||||
if (
|
||||
match := re.search(
|
||||
r"(?P<url>https?://(?:www\.)?pixiv.net/\w+/artworks/(?P<id>\d+))",
|
||||
message.content,
|
||||
)
|
||||
) is not None:
|
||||
with TemporaryDirectory() as tmp_dir:
|
||||
async with message.channel.typing():
|
||||
title = pixiv.dowload_pixiv_images(
|
||||
int(match.group("id")), tmp_dir, REFRESH_TOKEN
|
||||
)
|
||||
files = os.listdir(tmp_dir)
|
||||
files.reverse()
|
||||
await send_message.send_message_with_embed(
|
||||
message, files, title, tmp_dir, match.group("url")
|
||||
)
|
||||
await send_message.send_message(message, files, tmp_dir)
|
||||
|
||||
|
||||
client.run(DISCORD_TOKEN)
|
27
pixiv.py
27
pixiv.py
@ -1,27 +0,0 @@
|
||||
import pixivpy3
|
||||
|
||||
|
||||
def dowload_pixiv_images(
|
||||
illust_id: int,
|
||||
dest_folder: str,
|
||||
refresh_token: str,
|
||||
):
|
||||
api = pixivpy3.AppPixivAPI()
|
||||
api.auth(refresh_token=refresh_token)
|
||||
# get origin url
|
||||
json_result = api.illust_detail(illust_id)
|
||||
illust = json_result.illust
|
||||
pages = illust["meta_pages"]
|
||||
if len(pages) > 0:
|
||||
print(f"Dowloading mutiple images to {dest_folder}")
|
||||
for page in pages:
|
||||
url = page["image_urls"]["original"]
|
||||
print(url)
|
||||
api.download(url=url, path=dest_folder)
|
||||
else:
|
||||
print(f"Dowloading single image to {dest_folder}")
|
||||
api.download(
|
||||
url=illust["meta_single_page"]["original_image_url"], path=dest_folder
|
||||
)
|
||||
print("Down4load finished !")
|
||||
return illust["title"]
|
8
renovate.json
Normal file
8
renovate.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"config:recommended"
|
||||
],
|
||||
"commitMessagePrefix": ":arrow_up:"
|
||||
|
||||
}
|
@ -1,2 +1,21 @@
|
||||
pixivpy3
|
||||
discord.py
|
||||
pixivpy3==3.7.5
|
||||
discord.py==2.4.0
|
||||
|
||||
# Freeze
|
||||
aiohttp==3.10.6
|
||||
aiosignal==1.3.1
|
||||
async-timeout==4.0.3
|
||||
attrs==24.2.0
|
||||
certifi==2024.8.30
|
||||
charset-normalizer==3.3.2
|
||||
cloudscraper==1.2.71
|
||||
frozenlist==1.4.1
|
||||
idna==3.10
|
||||
isort==5.13.2
|
||||
multidict==6.1.0
|
||||
pyparsing==3.1.4
|
||||
requests==2.32.3
|
||||
requests-toolbelt==1.0.0
|
||||
typing_extensions==4.12.2
|
||||
urllib3==2.2.3
|
||||
yarl==1.12.1
|
@ -1,59 +0,0 @@
|
||||
import math
|
||||
import os
|
||||
import discord
|
||||
|
||||
|
||||
async def send_message_with_embed(
|
||||
message: discord.Message, files, title: str, tmp_dir: str, url: str
|
||||
):
|
||||
to_send_files = []
|
||||
to_send_embeds = []
|
||||
|
||||
for file in files:
|
||||
joined = os.path.join(tmp_dir, file)
|
||||
embed = discord.Embed(url=url)
|
||||
embed.set_image(url=f"attachment://{file}")
|
||||
to_send_files.append(discord.File(joined, file))
|
||||
to_send_embeds.append(embed)
|
||||
if len(to_send_embeds) > 4:
|
||||
parts = math.ceil(len(to_send_embeds) / 4)
|
||||
i = 1
|
||||
while len(to_send_embeds) > 4:
|
||||
splited_file = to_send_files[:4]
|
||||
to_send_files = to_send_files[4:]
|
||||
splited_embed = to_send_embeds[:4]
|
||||
to_send_embeds = to_send_embeds[4:]
|
||||
for embed in splited_embed:
|
||||
embed.title = f"{title} - Part {i}/{parts}"
|
||||
if i == 1:
|
||||
await message.reply(files=splited_file, embeds=splited_embed)
|
||||
else:
|
||||
await message.channel.send(files=splited_file, embeds=splited_embed)
|
||||
i = i + 1
|
||||
for embed in to_send_embeds:
|
||||
embed.title = f"{title} - Part {i}/{parts}"
|
||||
await message.channel.send(files=to_send_files, embeds=to_send_embeds)
|
||||
else:
|
||||
for embed in to_send_embeds:
|
||||
embed.title = title
|
||||
await message.reply(files=to_send_files, embeds=to_send_embeds)
|
||||
|
||||
|
||||
async def send_message(message: discord.Message, files, tmp_dir: str):
|
||||
to_send_files = []
|
||||
for file in files:
|
||||
joined = os.path.join(tmp_dir, file)
|
||||
to_send_files.append(discord.File(joined, file))
|
||||
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:]
|
||||
if first:
|
||||
await message.reply(files=splited_file)
|
||||
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)
|
0
src/discord_tools/__init__.py
Normal file
0
src/discord_tools/__init__.py
Normal file
108
src/discord_tools/message.py
Normal file
108
src/discord_tools/message.py
Normal file
@ -0,0 +1,108 @@
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
import discord
|
||||
from idna import intranges_contain
|
||||
|
||||
TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` `>`"
|
||||
IMAGE_NBR_LIMIT = 10
|
||||
DEFAULT_UPLOAD_SIZE_LIMIT = 209715200
|
||||
|
||||
|
||||
async def send_message(
|
||||
message: discord.Message,
|
||||
files,
|
||||
tmp_dir: str,
|
||||
muted: bool,
|
||||
title: str,
|
||||
art_author: str,
|
||||
):
|
||||
to_send_files = []
|
||||
for file in files:
|
||||
joined = os.path.join(tmp_dir, file)
|
||||
to_send_files.append(discord.File(joined, file))
|
||||
|
||||
upload_size_limit = (
|
||||
message.guild.filesize_limit
|
||||
if message.guild is not None
|
||||
else DEFAULT_UPLOAD_SIZE_LIMIT
|
||||
)
|
||||
first = True
|
||||
while len(to_send_files) > 0:
|
||||
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
|
||||
splited_file = to_send_files[:nbr_file_to_send]
|
||||
to_send_files = to_send_files[nbr_file_to_send:]
|
||||
if first and nbr_file_to_send <= 0:
|
||||
return
|
||||
elif nbr_file_to_send <= 0:
|
||||
# Next file is to big to be sent, skip it
|
||||
to_send_files = to_send_files[1:]
|
||||
elif first:
|
||||
await message.reply(
|
||||
files=splited_file,
|
||||
content=f"**{title}** by `{art_author}` {chr(10) + TIPS_MESSAGE if not muted else ''}",
|
||||
)
|
||||
first = False
|
||||
else:
|
||||
await message.channel.send(files=splited_file) # type: ignore
|
||||
|
||||
|
||||
async def send_command_reply(
|
||||
interaction: discord.Interaction,
|
||||
files,
|
||||
tmp_dir: str,
|
||||
muted: bool,
|
||||
title: str,
|
||||
art_author: str,
|
||||
url: str,
|
||||
author: discord.User | discord.Member,
|
||||
):
|
||||
to_send_files = []
|
||||
for file in files:
|
||||
joined = os.path.join(tmp_dir, file)
|
||||
to_send_files.append(discord.File(joined, file))
|
||||
|
||||
upload_size_limit = (
|
||||
interaction.guild.filesize_limit
|
||||
if interaction.guild is not None
|
||||
else DEFAULT_UPLOAD_SIZE_LIMIT
|
||||
)
|
||||
|
||||
first = True
|
||||
while len(to_send_files) > 0:
|
||||
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
|
||||
splited_file = to_send_files[:nbr_file_to_send]
|
||||
to_send_files = to_send_files[nbr_file_to_send:]
|
||||
if first and nbr_file_to_send <= 0:
|
||||
await interaction.followup.send(
|
||||
content=f"This image exceeds upload file size limit", ephemeral=True
|
||||
)
|
||||
elif nbr_file_to_send <= 0:
|
||||
# Next file is to big to be sent, skip it
|
||||
to_send_files = to_send_files[1:]
|
||||
elif first:
|
||||
await interaction.followup.send(
|
||||
files=splited_file,
|
||||
content=f"**{title}** by `{art_author}` - <{url}> - Submited by {author.mention} {chr(10) + TIPS_MESSAGE if not muted else ''}",
|
||||
)
|
||||
first = False
|
||||
else:
|
||||
await interaction.channel.send(files=splited_file) # type: ignore
|
||||
|
||||
|
||||
def get_max_files_number(files: List[discord.File], size_limit: int):
|
||||
size = 0
|
||||
nbr = 0
|
||||
for file in files:
|
||||
size = size + os.path.getsize(file.fp.name) # type: ignore
|
||||
nbr = nbr + 1
|
||||
if nbr >= IMAGE_NBR_LIMIT and size < size_limit:
|
||||
return IMAGE_NBR_LIMIT
|
||||
elif size > size_limit:
|
||||
# We exceeds size limit, remove one image
|
||||
if nbr >= IMAGE_NBR_LIMIT:
|
||||
return IMAGE_NBR_LIMIT - 1
|
||||
else:
|
||||
return nbr - 1
|
||||
# We didn't reach any limit
|
||||
return nbr
|
25
src/discord_tools/pixiv_link_extractor.py
Normal file
25
src/discord_tools/pixiv_link_extractor.py
Normal file
@ -0,0 +1,25 @@
|
||||
import re
|
||||
from typing import Tuple, Union
|
||||
|
||||
import discord
|
||||
|
||||
link_regexp = r"(?P<muted1><)?(?P<url>https?://(?:www\.)?pixiv.net/(?:\w+/)?artworks/(?P<id>\d+))(?P<muted2>>)?"
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def check_pixiv_url(url: str):
|
||||
if match := re.search(link_regexp, url):
|
||||
return (True, match)
|
||||
else:
|
||||
return (False, None)
|
161
src/main.py
Normal file
161
src/main.py
Normal file
@ -0,0 +1,161 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from shlex import join
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import discord
|
||||
|
||||
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:
|
||||
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:
|
||||
logging.fatal("Discord token is missing, please set DISCORD_TOKEN")
|
||||
exit(1)
|
||||
|
||||
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
|
||||
|
||||
class MyClient(discord.Client):
|
||||
def __init__(self, *, intents: discord.Intents):
|
||||
super().__init__(intents=intents)
|
||||
self.tree = discord.app_commands.CommandTree(self)
|
||||
|
||||
async def setup_hook(self):
|
||||
await self.tree.sync()
|
||||
|
||||
|
||||
client = MyClient(intents=intents)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
logging.info(f"We have logged in as {client.user}")
|
||||
|
||||
|
||||
@client.tree.command()
|
||||
@discord.app_commands.describe(
|
||||
url="Pixiv art url",
|
||||
)
|
||||
async def pixivprev(interaction: discord.Interaction, url: str):
|
||||
"""Create pixiv preview"""
|
||||
(result, match) = discord_tools.pixiv_link_extractor.check_pixiv_url(url)
|
||||
if result and match is not None:
|
||||
await interaction.response.defer()
|
||||
with TemporaryDirectory() as tmp_dir:
|
||||
loop = asyncio.get_running_loop()
|
||||
data = await loop.run_in_executor(
|
||||
None,
|
||||
pixiv_api.download_pixiv_images,
|
||||
int(match.group("id")),
|
||||
tmp_dir,
|
||||
REFRESH_TOKEN,
|
||||
)
|
||||
if data is None:
|
||||
await interaction.response.send_message(
|
||||
"Fail to extract pixiv image", ephemeral=True
|
||||
)
|
||||
return
|
||||
(title, author) = data
|
||||
files = os.listdir(tmp_dir)
|
||||
files.sort()
|
||||
await discord_tools.message.send_command_reply(
|
||||
interaction,
|
||||
files,
|
||||
tmp_dir,
|
||||
True,
|
||||
title=title,
|
||||
art_author=author,
|
||||
url=url,
|
||||
author=interaction.user,
|
||||
)
|
||||
else:
|
||||
await interaction.response.send_message("Invalid pixiv url", ephemeral=True)
|
||||
|
||||
|
||||
@client.tree.context_menu(name="Create pixiv preview")
|
||||
async def pixiv_preview(interaction: discord.Interaction, message: discord.Message):
|
||||
(result, muted, match) = discord_tools.pixiv_link_extractor.should_process(message)
|
||||
if result and match is not None:
|
||||
await interaction.response.defer()
|
||||
with TemporaryDirectory() as tmp_dir:
|
||||
loop = asyncio.get_running_loop()
|
||||
data = await loop.run_in_executor(
|
||||
None,
|
||||
pixiv_api.download_pixiv_images,
|
||||
int(match.group("id")),
|
||||
tmp_dir,
|
||||
REFRESH_TOKEN,
|
||||
)
|
||||
if data is None:
|
||||
await interaction.response.send_message(
|
||||
"Fail to extract pixiv image", ephemeral=True
|
||||
)
|
||||
return
|
||||
files = os.listdir(tmp_dir)
|
||||
files.sort()
|
||||
(title, author) = data
|
||||
await discord_tools.message.send_command_reply(
|
||||
interaction=interaction,
|
||||
files=files,
|
||||
tmp_dir=tmp_dir,
|
||||
muted=muted,
|
||||
title=title,
|
||||
art_author=author,
|
||||
url=match.group("url"),
|
||||
author=message.author,
|
||||
)
|
||||
else:
|
||||
await interaction.response.send_message(
|
||||
"Can't find any valid pixiv link.", ephemeral=True
|
||||
)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_guild_join(guild: discord.Guild):
|
||||
logging.info(f"Joined new guild: {guild.name}")
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message(message: discord.Message):
|
||||
if message.author == client.user:
|
||||
return
|
||||
(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():
|
||||
loop = asyncio.get_running_loop()
|
||||
data = await loop.run_in_executor(
|
||||
None,
|
||||
pixiv_api.download_pixiv_images,
|
||||
int(match.group("id")),
|
||||
tmp_dir,
|
||||
REFRESH_TOKEN,
|
||||
)
|
||||
if data is None:
|
||||
return
|
||||
(title, author) = data
|
||||
files = os.listdir(tmp_dir)
|
||||
files.sort()
|
||||
# await send_message.send_message_with_embed(
|
||||
# message, files, title, tmp_dir, match.group("url")
|
||||
# )
|
||||
await discord_tools.message.send_message(
|
||||
message, files, tmp_dir, muted, title, author
|
||||
)
|
||||
|
||||
|
||||
client.run(DISCORD_TOKEN)
|
0
src/pixiv/__init__.py
Normal file
0
src/pixiv/__init__.py
Normal file
80
src/pixiv/pixiv_api.py
Normal file
80
src/pixiv/pixiv_api.py
Normal file
@ -0,0 +1,80 @@
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
from typing import List, Tuple, Union
|
||||
|
||||
import pixivpy3
|
||||
|
||||
cpu_count = os.cpu_count()
|
||||
max_thread_num = cpu_count * 2 if cpu_count is not None else 4
|
||||
if max_thread_num > 10:
|
||||
max_thread_num = 10
|
||||
|
||||
|
||||
def download_pixiv_images(
|
||||
illust_id: int,
|
||||
dest_folder: str,
|
||||
refresh_token: str,
|
||||
):
|
||||
api = pixivpy3.AppPixivAPI()
|
||||
api.auth(refresh_token=refresh_token)
|
||||
# get origin url
|
||||
json_result = api.illust_detail(illust_id)
|
||||
illust = json_result.illust
|
||||
if not illust["visible"]:
|
||||
logging.info("Can't be viewed, return None")
|
||||
return None
|
||||
pages = illust["meta_pages"]
|
||||
if len(pages) > 0:
|
||||
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
|
||||
|
||||
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):
|
||||
worker = Thread(target=download_thread, args=(q,))
|
||||
worker.setDaemon(True)
|
||||
worker.start()
|
||||
workers.append(worker)
|
||||
|
||||
for page in pages:
|
||||
url = page["image_urls"]["original"]
|
||||
q.put((url, dest_folder, api))
|
||||
|
||||
q.join()
|
||||
|
||||
# Send end signal to all threads
|
||||
for i in range(num_threads):
|
||||
q.put(None)
|
||||
# Wait for them to finish
|
||||
for worker in workers:
|
||||
worker.join()
|
||||
|
||||
else:
|
||||
logging.info(f"Dowloading single image to {dest_folder}")
|
||||
api.download(
|
||||
url=illust["meta_single_page"]["original_image_url"], path=dest_folder
|
||||
)
|
||||
logging.info("Download finished !")
|
||||
return (illust["title"], f"{illust['user']['name']} ({illust['user']['account']})")
|
||||
|
||||
|
||||
def download_thread(q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]]):
|
||||
thread_name = threading.currentThread().getName()
|
||||
while True:
|
||||
data = q.get()
|
||||
if data is None:
|
||||
logging.debug(f"[{thread_name}] - Bye bye")
|
||||
q.task_done()
|
||||
break
|
||||
(url, dest_folder, api) = data
|
||||
logging.debug(f"[{thread_name}] - Downloading {url}...")
|
||||
api.download(url=url, path=dest_folder)
|
||||
print(f"[{thread_name}] - ... Done")
|
||||
q.task_done()
|
Loading…
Reference in New Issue
Block a user