2023-02-17 22:55:38 +02:00
|
|
|
from datetime import datetime
|
2023-02-14 17:25:56 +02:00
|
|
|
from os import makedirs, path
|
2023-03-15 22:58:14 +02:00
|
|
|
from random import choice
|
2023-03-02 23:38:48 +02:00
|
|
|
from shutil import rmtree
|
2023-01-10 13:52:44 +02:00
|
|
|
from traceback import format_exc
|
2023-02-14 17:25:56 +02:00
|
|
|
from uuid import uuid4
|
2023-02-17 17:46:44 +02:00
|
|
|
from PIL import Image
|
2023-03-02 23:38:48 +02:00
|
|
|
import aiofiles
|
2023-02-14 17:25:56 +02:00
|
|
|
|
2023-02-17 17:46:44 +02:00
|
|
|
from classes.poster_client import PosterClient
|
2023-02-14 17:25:56 +02:00
|
|
|
|
2023-03-02 17:39:59 +02:00
|
|
|
from modules.api_client import authorize, move_pic, random_pic, http_session
|
2023-02-14 17:25:56 +02:00
|
|
|
from modules.database import col_sent, col_submitted
|
|
|
|
from modules.logger import logWrite
|
|
|
|
from modules.utils import configGet, locale
|
|
|
|
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-17 23:59:03 +02:00
|
|
|
async def send_content(app: PosterClient) -> None:
|
2023-01-10 13:52:44 +02:00
|
|
|
try:
|
2023-02-14 17:25:56 +02:00
|
|
|
try:
|
|
|
|
token = await authorize()
|
|
|
|
except ValueError:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale("api_creds_invalid", "message", locale=configGet("locale")),
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
return
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
try:
|
|
|
|
pic = await random_pic()
|
|
|
|
except KeyError:
|
|
|
|
logWrite(locale("post_empty", "console", locale=configGet("locale")))
|
|
|
|
if configGet("error", "reports"):
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale("api_queue_empty", "message", locale=configGet("locale")),
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
return
|
|
|
|
except ValueError:
|
|
|
|
if configGet("error", "reports"):
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale("api_queue_error", "message", locale=configGet("locale")),
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
return
|
2023-03-09 12:33:02 +02:00
|
|
|
|
|
|
|
response = await http_session.get(
|
|
|
|
f'{configGet("address", "posting", "api")}/photos/{pic[0]}',
|
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
|
|
)
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-03-02 17:39:59 +02:00
|
|
|
if response.status != 200:
|
2023-03-09 12:33:02 +02:00
|
|
|
logWrite(
|
|
|
|
locale(
|
|
|
|
"post_invalid_pic", "console", locale=configGet("locale")
|
|
|
|
).format(response.status, str(response.json()))
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
if configGet("error", "reports"):
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale(
|
|
|
|
"post_invalid_pic", "message", locale=configGet("locale")
|
|
|
|
).format(response.status, response.json()),
|
|
|
|
)
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
tmp_dir = str(uuid4())
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
makedirs(path.join(configGet("tmp", "locations"), tmp_dir), exist_ok=True)
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
tmp_path = path.join(tmp_dir, pic[1])
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-03-09 12:33:02 +02:00
|
|
|
async with aiofiles.open(
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_path), "wb"
|
|
|
|
) as out_file:
|
2023-03-02 23:38:48 +02:00
|
|
|
await out_file.write(await response.read())
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-03-09 12:33:02 +02:00
|
|
|
logWrite(
|
|
|
|
f'Candidate {pic[1]} ({pic[0]}) is {path.getsize(path.join(configGet("tmp", "locations"), tmp_path))} bytes big',
|
|
|
|
debug=True,
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
|
|
|
|
if path.getsize(path.join(configGet("tmp", "locations"), tmp_path)) > 5242880:
|
|
|
|
image = Image.open(path.join(configGet("tmp", "locations"), tmp_path))
|
|
|
|
width, height = image.size
|
2023-03-09 12:33:02 +02:00
|
|
|
image = image.resize((int(width / 2), int(height / 2)), Image.ANTIALIAS)
|
2023-02-17 17:46:44 +02:00
|
|
|
if tmp_path.lower().endswith(".jpeg") or tmp_path.lower().endswith(".jpg"):
|
2023-03-09 12:33:02 +02:00
|
|
|
image.save(
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_path),
|
|
|
|
"JPEG",
|
|
|
|
optimize=True,
|
|
|
|
quality=50,
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
elif tmp_path.lower().endswith(".png"):
|
2023-03-09 12:33:02 +02:00
|
|
|
image.save(
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_path),
|
|
|
|
"PNG",
|
|
|
|
optimize=True,
|
|
|
|
compress_level=8,
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
image.close()
|
2023-03-09 12:33:02 +02:00
|
|
|
|
2023-02-17 17:46:44 +02:00
|
|
|
if path.getsize(path.join(configGet("tmp", "locations"), tmp_path)) > 5242880:
|
2023-03-09 12:33:02 +02:00
|
|
|
rmtree(
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
raise BytesWarning
|
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
del response
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-18 00:18:34 +02:00
|
|
|
submitted = col_submitted.find_one({"temp.file": pic[1]})
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-18 00:18:34 +02:00
|
|
|
if submitted is not None and submitted["caption"] is not None:
|
|
|
|
caption = submitted["caption"].strip()
|
2023-01-10 13:52:44 +02:00
|
|
|
else:
|
|
|
|
caption = ""
|
|
|
|
|
2023-03-09 12:33:02 +02:00
|
|
|
if (
|
|
|
|
submitted is not None
|
|
|
|
and configGet("enabled", "posting", "submitted_caption")
|
|
|
|
and (
|
|
|
|
(submitted["user"] not in app.admins)
|
|
|
|
or (configGet("ignore_admins", "posting", "submitted_caption") is False)
|
|
|
|
)
|
2023-02-24 20:48:06 +02:00
|
|
|
):
|
2023-03-09 12:33:02 +02:00
|
|
|
caption = (
|
|
|
|
f"{caption}\n\n{configGet('text', 'posting', 'submitted_caption')}\n"
|
|
|
|
)
|
2023-02-24 19:39:33 +02:00
|
|
|
else:
|
|
|
|
caption = f"{caption}\n\n"
|
|
|
|
|
2023-01-10 13:52:44 +02:00
|
|
|
if configGet("enabled", "caption"):
|
|
|
|
if configGet("link", "caption") != None:
|
2023-03-15 22:58:14 +02:00
|
|
|
caption = f"{caption}[{choice(configGet('text', 'caption'))}]({configGet('link', 'caption')})"
|
2023-01-10 13:52:44 +02:00
|
|
|
else:
|
2023-03-15 22:58:14 +02:00
|
|
|
caption = f"{caption}{choice(configGet('text', 'caption'))}"
|
2023-01-10 13:52:44 +02:00
|
|
|
else:
|
|
|
|
caption = caption
|
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
try:
|
2023-03-09 12:33:02 +02:00
|
|
|
sent = await app.send_photo(
|
|
|
|
configGet("channel", "posting"),
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_path),
|
|
|
|
caption=caption,
|
|
|
|
disable_notification=configGet("silent", "posting"),
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
except Exception as exp:
|
|
|
|
logWrite(f"Could not send image {pic[1]} ({pic[0]}) due to {exp}")
|
|
|
|
if configGet("error", "reports"):
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale(
|
|
|
|
"post_exception", "message", locale=configGet("locale")
|
|
|
|
).format(exp, format_exc()),
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
# rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
|
|
|
|
return
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
col_sent.insert_one(
|
|
|
|
{
|
2023-02-17 22:55:38 +02:00
|
|
|
"date": datetime.now(),
|
2023-02-14 17:25:56 +02:00
|
|
|
"image": pic[0],
|
|
|
|
"filename": pic[1],
|
|
|
|
"channel": configGet("channel", "posting"),
|
2023-03-09 12:33:02 +02:00
|
|
|
"caption": None
|
|
|
|
if (submitted is None or submitted["caption"] is None)
|
|
|
|
else submitted["caption"].strip(),
|
2023-02-14 17:25:56 +02:00
|
|
|
}
|
|
|
|
)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
await move_pic(pic[0])
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-03-09 12:33:02 +02:00
|
|
|
logWrite(
|
|
|
|
locale("post_sent", "console", locale=configGet("locale")).format(
|
|
|
|
pic[0],
|
|
|
|
str(configGet("channel", "posting")),
|
|
|
|
caption.replace("\n", "%n"),
|
|
|
|
str(configGet("silent", "posting")),
|
|
|
|
)
|
|
|
|
)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
except Exception as exp:
|
2023-03-09 12:33:02 +02:00
|
|
|
logWrite(
|
|
|
|
locale("post_exception", "console", locale=configGet("locale")).format(
|
|
|
|
str(exp), format_exc()
|
|
|
|
)
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
if configGet("error", "reports"):
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
|
|
|
locale("post_exception", "message", locale=configGet("locale")).format(
|
|
|
|
exp, format_exc()
|
|
|
|
),
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
try:
|
2023-03-09 12:33:02 +02:00
|
|
|
rmtree(
|
|
|
|
path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True
|
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
except:
|
2023-03-09 12:33:02 +02:00
|
|
|
pass
|