Compare commits
4 Commits
ce6363cb98
...
e2a73d5c4d
Author | SHA1 | Date | |
---|---|---|---|
e2a73d5c4d
|
|||
66d9956026
|
|||
f0cad86dd6
|
|||
39b9c365fb
|
@@ -4,5 +4,5 @@ from enum import Enum
|
||||
class SubmissionType(Enum):
|
||||
DOCUMENT = "document"
|
||||
VIDEO = "video"
|
||||
ANIMATION = "animation"
|
||||
# ANIMATION = "animation"
|
||||
PHOTO = "photo"
|
||||
|
@@ -199,15 +199,15 @@ class PyroClient(PyroClient):
|
||||
),
|
||||
caption="queue",
|
||||
)
|
||||
elif db_entry["type"] == SubmissionType.ANIMATION.value:
|
||||
response = await video_upload(
|
||||
self.config["posting"]["api"]["album"],
|
||||
client=client,
|
||||
multipart_data=BodyVideoUpload(
|
||||
File(media_bytes, filepath.name, "video/*")
|
||||
),
|
||||
caption="queue",
|
||||
)
|
||||
# elif db_entry["type"] == SubmissionType.ANIMATION.value:
|
||||
# response = await video_upload(
|
||||
# self.config["posting"]["api"]["album"],
|
||||
# client=client,
|
||||
# multipart_data=BodyVideoUpload(
|
||||
# File(media_bytes, filepath.name, "video/*")
|
||||
# ),
|
||||
# caption="queue",
|
||||
# )
|
||||
except UnexpectedStatus as exp:
|
||||
raise SubmissionUnsupportedError(str(filepath)) from exp
|
||||
|
||||
|
@@ -51,7 +51,6 @@
|
||||
"move_sent": false,
|
||||
"use_interval": false,
|
||||
"interval": "1h30m",
|
||||
"page_size": 300,
|
||||
"submitted_caption": {
|
||||
"enabled": true,
|
||||
"ignore_admins": true,
|
||||
@@ -59,14 +58,12 @@
|
||||
},
|
||||
"types": {
|
||||
"photo": true,
|
||||
"video": false,
|
||||
"animation": false
|
||||
"video": false
|
||||
},
|
||||
"extensions": {
|
||||
"photo": [
|
||||
"jpg",
|
||||
"png",
|
||||
"gif",
|
||||
"jpeg"
|
||||
],
|
||||
"video": [
|
||||
@@ -114,7 +111,6 @@
|
||||
},
|
||||
"mime_types": [
|
||||
"image/png",
|
||||
"image/gif",
|
||||
"image/jpeg",
|
||||
"video/mp4",
|
||||
"video/quicktime"
|
||||
|
@@ -27,6 +27,9 @@ from photosapi_client.api.default.photo_get_photos_id_get import asyncio as phot
|
||||
from photosapi_client.api.default.photo_patch_photos_id_patch import (
|
||||
asyncio as photo_patch,
|
||||
)
|
||||
from photosapi_client.api.default.photo_random_albums_album_photos_random_get import (
|
||||
asyncio as photo_random,
|
||||
)
|
||||
from photosapi_client.api.default.photo_upload_albums_album_photos_post import (
|
||||
asyncio_detailed as photo_upload,
|
||||
)
|
||||
@@ -39,6 +42,9 @@ from photosapi_client.api.default.video_get_videos_id_get import asyncio as vide
|
||||
from photosapi_client.api.default.video_patch_videos_id_patch import (
|
||||
asyncio as video_patch,
|
||||
)
|
||||
from photosapi_client.api.default.video_random_albums_album_videos_random_get import (
|
||||
asyncio as video_random,
|
||||
)
|
||||
from photosapi_client.api.default.video_upload_albums_album_videos_post import (
|
||||
asyncio as video_upload,
|
||||
)
|
||||
@@ -53,8 +59,10 @@ from photosapi_client.models.body_video_upload_albums_album_videos_post import (
|
||||
)
|
||||
from photosapi_client.models.http_validation_error import HTTPValidationError
|
||||
from photosapi_client.models.photo import Photo
|
||||
from photosapi_client.models.photo_search import PhotoSearch
|
||||
from photosapi_client.models.token import Token
|
||||
from photosapi_client.models.video import Video
|
||||
from photosapi_client.models.video_search import VideoSearch
|
||||
from photosapi_client.types import File
|
||||
|
||||
from modules.http_client import http_session
|
||||
|
@@ -1,33 +1,37 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from os import makedirs, path
|
||||
from random import choice
|
||||
from random import choice, sample
|
||||
from shutil import rmtree
|
||||
from traceback import format_exc, print_exc
|
||||
from typing import Union
|
||||
from uuid import uuid4
|
||||
|
||||
import aiofiles
|
||||
from aiohttp import ClientSession
|
||||
from libbot.pyrogram.classes import PyroClient
|
||||
from photosapi_client.errors import UnexpectedStatus
|
||||
from PIL import Image
|
||||
from pyrogram.client import Client
|
||||
|
||||
from modules.api_client import (
|
||||
File,
|
||||
PhotoSearch,
|
||||
VideoSearch,
|
||||
authorize,
|
||||
client,
|
||||
photo_find,
|
||||
photo_get,
|
||||
photo_patch,
|
||||
video_find,
|
||||
photo_random,
|
||||
video_get,
|
||||
video_patch,
|
||||
video_random,
|
||||
)
|
||||
from modules.database import col_sent, col_submitted
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
async def send_content(app: PyroClient, http_session: ClientSession) -> None:
|
||||
try:
|
||||
try:
|
||||
token = await authorize(http_session)
|
||||
@@ -40,25 +44,72 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
|
||||
try:
|
||||
funcs = []
|
||||
if app.config["posting"]["types"]["photo"]:
|
||||
funcs.append(photo_find)
|
||||
if (
|
||||
app.config["posting"]["types"]["video"]
|
||||
or app.config["posting"]["types"]["animation"]
|
||||
):
|
||||
funcs.append(video_find)
|
||||
func = choice(funcs)
|
||||
|
||||
media = choice(
|
||||
(
|
||||
await func(
|
||||
if app.config["posting"]["types"]["photo"]:
|
||||
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
|
||||
|
||||
random_results = (
|
||||
await func_iter[0](
|
||||
album=app.config["posting"]["api"]["album"],
|
||||
caption="queue",
|
||||
client=client,
|
||||
limit=1,
|
||||
)
|
||||
).results
|
||||
|
||||
if not random_results:
|
||||
continue
|
||||
|
||||
media: Union[PhotoSearch, VideoSearch] = random_results[0]
|
||||
|
||||
try:
|
||||
response: File = await func_iter[1](id=media.id, client=client)
|
||||
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
|
||||
|
||||
found = True
|
||||
break
|
||||
|
||||
if not found:
|
||||
raise KeyError("No media found")
|
||||
else:
|
||||
func = funcs[0]
|
||||
media: Union[PhotoSearch, VideoSearch] = (
|
||||
await func[0](
|
||||
album=app.config["posting"]["api"]["album"],
|
||||
caption="queue",
|
||||
page_size=app.config["posting"]["page_size"],
|
||||
client=client,
|
||||
limit=1,
|
||||
)
|
||||
).results
|
||||
)
|
||||
).results[0]
|
||||
try:
|
||||
response: File = await func[1](id=media.id, client=client)
|
||||
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
|
||||
|
||||
except (KeyError, AttributeError, TypeError, IndexError):
|
||||
logger.info(app._("post_empty", "console"))
|
||||
if app.config["reports"]["error"]:
|
||||
@@ -67,6 +118,7 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
app._("api_queue_empty", "message"),
|
||||
)
|
||||
return
|
||||
|
||||
except (ValueError, UnexpectedStatus):
|
||||
if app.config["reports"]["error"]:
|
||||
await app.send_message(
|
||||
@@ -75,30 +127,6 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
)
|
||||
return
|
||||
|
||||
try:
|
||||
if func is photo_find:
|
||||
response = await photo_get(id=media.id, client=client)
|
||||
else:
|
||||
response = await video_get(id=media.id, client=client)
|
||||
except Exception as exp:
|
||||
print_exc()
|
||||
logger.warning(
|
||||
"Media is invalid: %s",
|
||||
exp
|
||||
# app._("post_invalid_pic", "console").format(
|
||||
# response.status, str(await response.json())
|
||||
# )
|
||||
)
|
||||
if app.config["reports"]["error"]:
|
||||
await app.send_message(app.owner, f"Media is invalid: {exp}")
|
||||
return
|
||||
# await app.send_message(
|
||||
# app.owner,
|
||||
# app._("post_invalid_pic", "message").format(
|
||||
# response.status, await response.json()
|
||||
# ),
|
||||
# )
|
||||
|
||||
tmp_dir = str(uuid4())
|
||||
|
||||
makedirs(path.join(app.config["locations"]["tmp"], tmp_dir), exist_ok=True)
|
||||
@@ -111,12 +139,15 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
await out_file.write(response.payload.read())
|
||||
|
||||
logger.info(
|
||||
f"Candidate {media.filename} ({media.id}) is {path.getsize(path.join(app.config['locations']['tmp'], tmp_path))} bytes big",
|
||||
"Candidate %s (%s) is %s bytes big",
|
||||
media.filename,
|
||||
media.id,
|
||||
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)),
|
||||
)
|
||||
|
||||
if (
|
||||
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)) > 5242880
|
||||
) and func is photo_find:
|
||||
) and func[0] is photo_random:
|
||||
image = Image.open(path.join(app.config["locations"]["tmp"], tmp_path))
|
||||
width, height = image.size
|
||||
image = image.resize((int(width / 2), int(height / 2)), Image.ANTIALIAS)
|
||||
@@ -138,7 +169,7 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
|
||||
if (
|
||||
path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)) > 5242880
|
||||
) and func is photo_find:
|
||||
) and func[0] is photo_random:
|
||||
rmtree(
|
||||
path.join(app.config["locations"]["tmp"], tmp_dir), ignore_errors=True
|
||||
)
|
||||
@@ -178,23 +209,15 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
caption = caption
|
||||
|
||||
try:
|
||||
if func is photo_find:
|
||||
sent = await app.send_photo(
|
||||
app.config["posting"]["channel"],
|
||||
path.join(app.config["locations"]["tmp"], tmp_path),
|
||||
caption=caption,
|
||||
disable_notification=app.config["posting"]["silent"],
|
||||
)
|
||||
else:
|
||||
sent = await app.send_video(
|
||||
app.config["posting"]["channel"],
|
||||
path.join(app.config["locations"]["tmp"], tmp_path),
|
||||
caption=caption,
|
||||
disable_notification=app.config["posting"]["silent"],
|
||||
)
|
||||
sent = await func[2](
|
||||
app.config["posting"]["channel"],
|
||||
path.join(app.config["locations"]["tmp"], tmp_path),
|
||||
caption=caption,
|
||||
disable_notification=app.config["posting"]["silent"],
|
||||
)
|
||||
except Exception as exp:
|
||||
logger.error(
|
||||
f"Could not send media {media.filename} ({media.id}) due to {exp}"
|
||||
"Could not send media %s (%s) due to %s", media.filename, media.id, exp
|
||||
)
|
||||
if app.config["reports"]["error"]:
|
||||
await app.send_message(
|
||||
@@ -216,8 +239,7 @@ async def send_content(app: Client, http_session: ClientSession) -> None:
|
||||
}
|
||||
)
|
||||
|
||||
func_patch = photo_patch if func is photo_find else video_patch
|
||||
await func_patch(id=media.id, client=client, caption="sent")
|
||||
await func[3](id=media.id, client=client, caption="sent")
|
||||
|
||||
rmtree(path.join(app.config["locations"]["tmp"], tmp_dir), ignore_errors=True)
|
||||
|
||||
|
@@ -23,7 +23,7 @@ logger = logging.getLogger(__name__)
|
||||
@Client.on_message(
|
||||
~filters.scheduled & filters.private & filters.photo
|
||||
| filters.video
|
||||
| filters.animation
|
||||
# | filters.animation
|
||||
| filters.document
|
||||
)
|
||||
async def get_submission(app: PyroClient, msg: Message):
|
||||
@@ -103,27 +103,27 @@ async def get_submission(app: PyroClient, msg: Message):
|
||||
save_tmp = False
|
||||
contents = msg.video.file_id, SubmissionType.VIDEO # , msg.video.file_name
|
||||
|
||||
if msg.animation is not None:
|
||||
logger.info(
|
||||
"User %s is trying to submit an animation with name '%s' and size of %s MB",
|
||||
msg.from_user.id,
|
||||
msg.animation.file_name,
|
||||
msg.animation.file_size / 1024 / 1024,
|
||||
)
|
||||
if msg.animation.file_size > app.config["submission"]["file_size"]:
|
||||
await msg.reply_text(
|
||||
app._("document_too_large", "message", locale=user_locale).format(
|
||||
str(app.config["submission"]["file_size"] / 1024 / 1024)
|
||||
),
|
||||
quote=True,
|
||||
)
|
||||
return
|
||||
if msg.animation.file_size > app.config["submission"]["tmp_size"]:
|
||||
save_tmp = False
|
||||
contents = (
|
||||
msg.animation.file_id,
|
||||
SubmissionType.ANIMATION,
|
||||
) # , msg.animation.file_name
|
||||
# if msg.animation is not None:
|
||||
# logger.info(
|
||||
# "User %s is trying to submit an animation with name '%s' and size of %s MB",
|
||||
# msg.from_user.id,
|
||||
# msg.animation.file_name,
|
||||
# msg.animation.file_size / 1024 / 1024,
|
||||
# )
|
||||
# if msg.animation.file_size > app.config["submission"]["file_size"]:
|
||||
# await msg.reply_text(
|
||||
# app._("document_too_large", "message", locale=user_locale).format(
|
||||
# str(app.config["submission"]["file_size"] / 1024 / 1024)
|
||||
# ),
|
||||
# quote=True,
|
||||
# )
|
||||
# return
|
||||
# if msg.animation.file_size > app.config["submission"]["tmp_size"]:
|
||||
# save_tmp = False
|
||||
# contents = (
|
||||
# msg.animation.file_id,
|
||||
# SubmissionType.ANIMATION,
|
||||
# ) # , msg.animation.file_name
|
||||
|
||||
if msg.photo is not None:
|
||||
logger.info(
|
||||
|
Reference in New Issue
Block a user