This commit is contained in:
parent
f23f6cc55f
commit
36591fda85
@ -1,9 +1,15 @@
|
|||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
|
from tkinter.tix import IMAGE
|
||||||
|
from turtle import Turtle
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
from idna import intranges_contain
|
||||||
|
|
||||||
TIPS_MESSAGE = ":bulb: TIPS: You can disable Discord's default preview for link by wrapping it around `<` `>`"
|
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):
|
async def send_message(message: discord.Message, files, tmp_dir: str, muted: bool):
|
||||||
@ -11,23 +17,29 @@ async def send_message(message: discord.Message, files, tmp_dir: str, muted: boo
|
|||||||
for file in files:
|
for file in files:
|
||||||
joined = os.path.join(tmp_dir, file)
|
joined = os.path.join(tmp_dir, file)
|
||||||
to_send_files.append(discord.File(joined, file))
|
to_send_files.append(discord.File(joined, file))
|
||||||
if len(to_send_files) > 10:
|
|
||||||
|
upload_size_limit = (
|
||||||
|
message.guild.filesize_limit
|
||||||
|
if message.guild is not None
|
||||||
|
else DEFAULT_UPLOAD_SIZE_LIMIT
|
||||||
|
)
|
||||||
first = True
|
first = True
|
||||||
while len(to_send_files) > 10:
|
while len(to_send_files) > 0:
|
||||||
splited_file = to_send_files[:10]
|
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
|
||||||
to_send_files = to_send_files[10:]
|
splited_file = to_send_files[:nbr_file_to_send]
|
||||||
if first:
|
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(
|
await message.reply(
|
||||||
files=splited_file, content=(TIPS_MESSAGE if not muted else None)
|
files=splited_file, content=(TIPS_MESSAGE if not muted else None)
|
||||||
)
|
)
|
||||||
first = False
|
first = False
|
||||||
else:
|
else:
|
||||||
await message.channel.send(files=splited_file)
|
await message.channel.send(files=splited_file) # type: ignore
|
||||||
await message.channel.send(files=to_send_files)
|
|
||||||
else:
|
|
||||||
await message.reply(
|
|
||||||
files=to_send_files, content=(TIPS_MESSAGE if not muted else None)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def send_command_reply(
|
async def send_command_reply(
|
||||||
@ -43,13 +55,26 @@ async def send_command_reply(
|
|||||||
for file in files:
|
for file in files:
|
||||||
joined = os.path.join(tmp_dir, file)
|
joined = os.path.join(tmp_dir, file)
|
||||||
to_send_files.append(discord.File(joined, file))
|
to_send_files.append(discord.File(joined, file))
|
||||||
if len(to_send_files) > 10:
|
|
||||||
if interaction.channel is not None:
|
upload_size_limit = (
|
||||||
|
interaction.guild.filesize_limit
|
||||||
|
if interaction.guild is not None
|
||||||
|
else DEFAULT_UPLOAD_SIZE_LIMIT
|
||||||
|
)
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
while len(to_send_files) > 10:
|
while len(to_send_files) > 0:
|
||||||
splited_file = to_send_files[:10]
|
nbr_file_to_send = get_max_files_number(to_send_files, upload_size_limit)
|
||||||
to_send_files = to_send_files[10:]
|
splited_file = to_send_files[:nbr_file_to_send]
|
||||||
if first:
|
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(
|
await interaction.followup.send(
|
||||||
files=splited_file,
|
files=splited_file,
|
||||||
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}",
|
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}",
|
||||||
@ -57,9 +82,21 @@ async def send_command_reply(
|
|||||||
first = False
|
first = False
|
||||||
else:
|
else:
|
||||||
await interaction.channel.send(files=splited_file) # type: ignore
|
await interaction.channel.send(files=splited_file) # type: ignore
|
||||||
await interaction.channel.send(files=to_send_files) # 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:
|
else:
|
||||||
await interaction.followup.send(
|
return nbr - 1
|
||||||
files=to_send_files,
|
# We didn't reach any limit
|
||||||
content=f"{title} - Submited by {author.mention} - <{url}> {chr(10) + TIPS_MESSAGE if not muted else ''}",
|
return nbr
|
||||||
)
|
|
||||||
|
@ -8,7 +8,7 @@ from typing import List, Tuple, Union
|
|||||||
import pixivpy3
|
import pixivpy3
|
||||||
|
|
||||||
cpu_count = os.cpu_count()
|
cpu_count = os.cpu_count()
|
||||||
max_thread_num = cpu_count * 4 if cpu_count is not None else 4
|
max_thread_num = cpu_count * 2 if cpu_count is not None else 4
|
||||||
|
|
||||||
|
|
||||||
def dowload_pixiv_images(
|
def dowload_pixiv_images(
|
||||||
@ -73,5 +73,5 @@ def download_thread(q: Queue[Union[Tuple[str, str, pixivpy3.AppPixivAPI], None]]
|
|||||||
(url, dest_folder, api) = data
|
(url, dest_folder, api) = data
|
||||||
logging.debug(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)
|
||||||
logging.debug(f"[{thread_name}] - ... Done")
|
print(f"[{thread_name}] - ... Done")
|
||||||
q.task_done()
|
q.task_done()
|
||||||
|
Loading…
Reference in New Issue
Block a user