2023-06-21 17:39:33 +03:00
|
|
|
import logging
|
2023-06-21 23:03:52 +03:00
|
|
|
from datetime import datetime
|
2023-02-14 17:25:56 +02:00
|
|
|
from os import makedirs, path
|
2023-06-28 00:05:37 +03:00
|
|
|
from random import choice, sample
|
2023-03-02 23:38:48 +02:00
|
|
|
from shutil import rmtree
|
2023-06-22 16:17:44 +03:00
|
|
|
from traceback import format_exc, print_exc
|
2023-06-28 00:40:36 +03:00
|
|
|
from typing import Union
|
2023-02-14 17:25:56 +02:00
|
|
|
from uuid import uuid4
|
2023-06-21 23:03:52 +03:00
|
|
|
|
2023-03-02 23:38:48 +02:00
|
|
|
import aiofiles
|
2023-06-21 20:28:17 +03:00
|
|
|
from aiohttp import ClientSession
|
2023-06-28 00:05:37 +03:00
|
|
|
from libbot.pyrogram.classes import PyroClient
|
2023-06-21 23:03:52 +03:00
|
|
|
from photosapi_client.errors import UnexpectedStatus
|
|
|
|
from PIL import Image
|
2023-02-14 17:25:56 +02:00
|
|
|
|
2023-06-22 16:17:44 +03:00
|
|
|
from modules.api_client import (
|
2023-06-28 00:40:36 +03:00
|
|
|
File,
|
|
|
|
PhotoSearch,
|
|
|
|
VideoSearch,
|
2023-06-22 16:17:44 +03:00
|
|
|
authorize,
|
|
|
|
client,
|
|
|
|
photo_get,
|
|
|
|
photo_patch,
|
2023-06-28 00:05:37 +03:00
|
|
|
photo_random,
|
2023-06-22 16:17:44 +03:00
|
|
|
video_get,
|
|
|
|
video_patch,
|
2023-06-28 00:05:37 +03:00
|
|
|
video_random,
|
2023-06-22 16:17:44 +03:00
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
from modules.database import col_sent, col_submitted
|
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
|
2023-06-28 00:05:37 +03:00
|
|
|
async def send_content(app: PyroClient, http_session: ClientSession) -> None:
|
2023-01-10 13:52:44 +02:00
|
|
|
try:
|
2023-02-14 17:25:56 +02:00
|
|
|
try:
|
2023-06-21 20:28:17 +03:00
|
|
|
token = await authorize(http_session)
|
2023-02-14 17:25:56 +02:00
|
|
|
except ValueError:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
2023-06-21 17:39:33 +03:00
|
|
|
app._("api_creds_invalid", "message"),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
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:
|
2023-06-22 16:17:44 +03:00
|
|
|
funcs = []
|
2023-06-28 00:05:37 +03:00
|
|
|
|
2023-06-22 16:17:44 +03:00
|
|
|
if app.config["posting"]["types"]["photo"]:
|
2023-06-28 00:05:37 +03:00
|
|
|
funcs.append((photo_random, photo_get, app.send_photo, photo_patch))
|
|
|
|
|
|
|
|
if app.config["posting"]["types"]["video"]:
|
|
|
|
funcs.append((video_random, video_get, app.send_video, video_patch))
|
|
|
|
|
|
|
|
if not funcs:
|
|
|
|
raise KeyError(
|
|
|
|
"No media source provided: all seem to be disabled in config"
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(funcs) > 1:
|
|
|
|
found = False
|
|
|
|
for func_iter in sample(funcs, len(funcs)):
|
|
|
|
func = func_iter
|
2023-06-28 00:40:36 +03:00
|
|
|
|
|
|
|
random_results = (
|
2023-06-28 00:05:37 +03:00
|
|
|
await func_iter[0](
|
|
|
|
album=app.config["posting"]["api"]["album"],
|
|
|
|
caption="queue",
|
|
|
|
client=client,
|
|
|
|
limit=1,
|
|
|
|
)
|
2023-06-28 00:40:36 +03:00
|
|
|
).results
|
|
|
|
|
|
|
|
if not random_results:
|
|
|
|
continue
|
|
|
|
|
|
|
|
media: Union[PhotoSearch, VideoSearch] = random_results[0]
|
|
|
|
|
2023-06-28 00:05:37 +03:00
|
|
|
try:
|
2023-06-28 00:40:36 +03:00
|
|
|
response: File = await func_iter[1](id=media.id, client=client)
|
2023-06-28 00:05:37 +03:00
|
|
|
except Exception as exp:
|
|
|
|
print_exc()
|
|
|
|
logger.error("Media is invalid: %s", exp)
|
|
|
|
if app.config["reports"]["error"]:
|
|
|
|
await app.send_message(
|
|
|
|
app.owner, f"Media is invalid: {exp}"
|
|
|
|
)
|
|
|
|
return
|
2023-06-28 00:40:36 +03:00
|
|
|
|
2023-06-28 00:05:37 +03:00
|
|
|
found = True
|
|
|
|
break
|
2023-06-28 00:40:36 +03:00
|
|
|
|
2023-06-28 00:05:37 +03:00
|
|
|
if not found:
|
|
|
|
raise KeyError("No media found")
|
|
|
|
else:
|
|
|
|
func = funcs[0]
|
2023-06-28 00:40:36 +03:00
|
|
|
media: Union[PhotoSearch, VideoSearch] = (
|
2023-06-28 00:05:37 +03:00
|
|
|
await func[0](
|
2023-06-21 20:28:17 +03:00
|
|
|
album=app.config["posting"]["api"]["album"],
|
|
|
|
caption="queue",
|
|
|
|
client=client,
|
2023-06-28 00:05:37 +03:00
|
|
|
limit=1,
|
2023-06-21 20:28:17 +03:00
|
|
|
)
|
2023-06-28 00:05:37 +03:00
|
|
|
).results[0]
|
|
|
|
try:
|
2023-06-28 00:40:36 +03:00
|
|
|
response: File = await func[1](id=media.id, client=client)
|
2023-06-28 00:05:37 +03:00
|
|
|
except Exception as exp:
|
|
|
|
print_exc()
|
|
|
|
logger.error("Media is invalid: %s", exp)
|
|
|
|
if app.config["reports"]["error"]:
|
|
|
|
await app.send_message(app.owner, f"Media is invalid: {exp}")
|
|
|
|
return
|
|
|
|
|
2023-06-21 22:43:23 +03:00
|
|
|
except (KeyError, AttributeError, TypeError, IndexError):
|
2023-06-21 17:39:33 +03:00
|
|
|
logger.info(app._("post_empty", "console"))
|
|
|
|
if app.config["reports"]["error"]:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
2023-06-21 17:39:33 +03:00
|
|
|
app._("api_queue_empty", "message"),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
return
|
2023-06-28 00:05:37 +03:00
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
except (ValueError, UnexpectedStatus):
|
|
|
|
if app.config["reports"]["error"]:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
2023-06-21 17:39:33 +03:00
|
|
|
app._("api_queue_error", "message"),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
return
|
2023-03-09 12:33:02 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
tmp_dir = str(uuid4())
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-06-21 20:28:17 +03:00
|
|
|
makedirs(path.join(app.config["locations"]["tmp"], tmp_dir), exist_ok=True)
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-06-22 16:17:44 +03:00
|
|
|
tmp_path = path.join(tmp_dir, media.filename)
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-03-09 12:33:02 +02:00
|
|
|
async with aiofiles.open(
|
2023-06-21 20:28:17 +03:00
|
|
|
path.join(app.config["locations"]["tmp"], tmp_path), "wb"
|
2023-03-09 12:33:02 +02:00
|
|
|
) as out_file:
|
2023-06-22 16:17:44 +03:00
|
|
|
await out_file.write(response.payload.read())
|
2023-01-17 15:11:23 +02:00
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
logger.info(
|
2023-06-28 00:40:36 +03:00
|
|
|
"Candidate %s (%s) is %s bytes big",
|
|
|
|
media.filename,
|
|
|
|
media.id,
|
|
|
|
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
|
2023-06-22 16:17:44 +03:00
|
|
|
if (
|
|
|
|
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)) > 5242880
|
2023-06-28 00:05:37 +03:00
|
|
|
) and func[0] is photo_random:
|
2023-06-21 20:28:17 +03:00
|
|
|
image = Image.open(path.join(app.config["locations"]["tmp"], tmp_path))
|
2023-02-17 17:46:44 +02:00
|
|
|
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(
|
2023-06-21 20:28:17 +03:00
|
|
|
path.join(app.config["locations"]["tmp"], tmp_path),
|
2023-03-09 12:33:02 +02:00
|
|
|
"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(
|
2023-06-21 20:28:17 +03:00
|
|
|
path.join(app.config["locations"]["tmp"], tmp_path),
|
2023-03-09 12:33:02 +02:00
|
|
|
"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-06-22 16:17:44 +03:00
|
|
|
if (
|
|
|
|
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)) > 5242880
|
2023-06-28 00:05:37 +03:00
|
|
|
) and func[0] is photo_random:
|
2023-03-09 12:33:02 +02:00
|
|
|
rmtree(
|
2023-06-21 20:28:17 +03:00
|
|
|
path.join(app.config["locations"]["tmp"], tmp_dir), ignore_errors=True
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
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-06-22 16:17:44 +03:00
|
|
|
submitted = col_submitted.find_one({"temp.file": media.filename})
|
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
|
2023-06-21 17:39:33 +03:00
|
|
|
and app.config["posting"]["submitted_caption"]["enabled"]
|
2023-03-09 12:33:02 +02:00
|
|
|
and (
|
|
|
|
(submitted["user"] not in app.admins)
|
2023-06-21 20:28:17 +03:00
|
|
|
or (
|
|
|
|
app.config["posting"]["submitted_caption"]["ignore_admins"] is False
|
|
|
|
)
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-24 20:48:06 +02:00
|
|
|
):
|
2023-03-09 12:33:02 +02:00
|
|
|
caption = (
|
2023-06-21 17:39:33 +03:00
|
|
|
f"{caption}\n\n{app.config['posting']['submitted_caption']['text']}\n"
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-24 19:39:33 +02:00
|
|
|
else:
|
|
|
|
caption = f"{caption}\n\n"
|
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
if app.config["caption"]["enabled"]:
|
|
|
|
if app.config["caption"]["link"] is not None:
|
|
|
|
caption = f"{caption}[{choice(app.config['caption']['text'])}]({app.config['caption']['link']})"
|
2023-01-10 13:52:44 +02:00
|
|
|
else:
|
2023-06-21 17:39:33 +03:00
|
|
|
caption = f"{caption}{choice(app.config['caption']['text'])}"
|
2023-01-10 13:52:44 +02:00
|
|
|
else:
|
|
|
|
caption = caption
|
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
try:
|
2023-06-28 00:05:37 +03:00
|
|
|
sent = await func[2](
|
|
|
|
app.config["posting"]["channel"],
|
|
|
|
path.join(app.config["locations"]["tmp"], tmp_path),
|
|
|
|
caption=caption,
|
|
|
|
disable_notification=app.config["posting"]["silent"],
|
|
|
|
)
|
2023-02-14 17:25:56 +02:00
|
|
|
except Exception as exp:
|
2023-06-22 16:17:44 +03:00
|
|
|
logger.error(
|
2023-06-28 00:40:36 +03:00
|
|
|
"Could not send media %s (%s) due to %s", media.filename, media.id, exp
|
2023-06-22 16:17:44 +03:00
|
|
|
)
|
2023-06-21 17:39:33 +03:00
|
|
|
if app.config["reports"]["error"]:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
2023-06-21 17:39:33 +03:00
|
|
|
app._("post_exception", "message").format(exp, format_exc()),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-06-21 17:39:33 +03:00
|
|
|
# rmtree(path.join(app.config['locations']['tmp'], tmp_dir), ignore_errors=True)
|
2023-02-14 17:25:56 +02:00
|
|
|
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-06-22 16:17:44 +03:00
|
|
|
"image": media.id,
|
|
|
|
"filename": media.filename,
|
2023-06-21 17:39:33 +03:00
|
|
|
"channel": app.config["posting"]["channel"],
|
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-06-28 00:05:37 +03:00
|
|
|
await func[3](id=media.id, client=client, caption="sent")
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-06-21 20:28:17 +03:00
|
|
|
rmtree(path.join(app.config["locations"]["tmp"], tmp_dir), ignore_errors=True)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-06-21 17:39:33 +03:00
|
|
|
logger.info(
|
|
|
|
app._("post_sent", "console").format(
|
2023-06-22 16:17:44 +03:00
|
|
|
media.id,
|
2023-06-21 17:39:33 +03:00
|
|
|
str(app.config["posting"]["channel"]),
|
2023-03-09 12:33:02 +02:00
|
|
|
caption.replace("\n", "%n"),
|
2023-06-21 17:39:33 +03:00
|
|
|
str(app.config["posting"]["silent"]),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
|
|
|
)
|
2023-01-10 13:52:44 +02:00
|
|
|
|
2023-02-14 17:25:56 +02:00
|
|
|
except Exception as exp:
|
2023-06-21 17:39:33 +03:00
|
|
|
logger.error(app._("post_exception", "console").format(str(exp), format_exc()))
|
|
|
|
if app.config["reports"]["error"]:
|
2023-03-09 12:33:02 +02:00
|
|
|
await app.send_message(
|
|
|
|
app.owner,
|
2023-06-21 17:39:33 +03:00
|
|
|
app._("post_exception", "message").format(exp, format_exc()),
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
try:
|
2023-03-09 12:33:02 +02:00
|
|
|
rmtree(
|
2023-06-21 20:28:17 +03:00
|
|
|
path.join(app.config["locations"]["tmp"], tmp_dir), ignore_errors=True
|
2023-03-09 12:33:02 +02:00
|
|
|
)
|
2023-02-17 17:46:44 +02:00
|
|
|
except:
|
2023-03-09 12:33:02 +02:00
|
|
|
pass
|