122 lines
4.2 KiB
Python
122 lines
4.2 KiB
Python
from os import path, remove, sep
|
|
from shutil import rmtree
|
|
from typing import Tuple, Union
|
|
from photosapi_client.api.default.photo_upload_albums_album_photos_post import (
|
|
asyncio as upload_pic,
|
|
)
|
|
from photosapi_client.models.body_photo_upload_albums_album_photos_post import (
|
|
BodyPhotoUploadAlbumsAlbumPhotosPost,
|
|
)
|
|
from photosapi_client.types import File
|
|
from photosapi_client.models.http_validation_error import HTTPValidationError
|
|
import aiofiles
|
|
from pyrogram.client import Client
|
|
from pyrogram.types import Message
|
|
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
|
|
from modules.api_client import client
|
|
from modules.database import col_submitted
|
|
from bson import ObjectId
|
|
from modules.logger import logWrite
|
|
|
|
from modules.utils import configGet
|
|
|
|
|
|
class PosterClient(Client):
|
|
def __init__(self, name: str, **kwargs): # type: ignore
|
|
super().__init__(name, **kwargs)
|
|
self.owner = configGet("owner")
|
|
self.admins = configGet("admins") + [configGet("owner")]
|
|
|
|
async def submit_photo(
|
|
self, id: str
|
|
) -> Tuple[Union[Message, None], Union[str, None]]:
|
|
db_entry = col_submitted.find_one({"_id": ObjectId(id)})
|
|
submission = None
|
|
|
|
if db_entry is None:
|
|
raise SubmissionUnavailableError()
|
|
else:
|
|
if db_entry["temp"]["uuid"] is not None:
|
|
if not path.exists(
|
|
path.join(
|
|
configGet("data", "locations"),
|
|
"submissions",
|
|
db_entry["temp"]["uuid"],
|
|
db_entry["temp"]["file"],
|
|
)
|
|
):
|
|
raise SubmissionUnavailableError()
|
|
else:
|
|
filepath = path.join(
|
|
configGet("data", "locations"),
|
|
"submissions",
|
|
db_entry["temp"]["uuid"],
|
|
db_entry["temp"]["file"],
|
|
)
|
|
try:
|
|
submission = await self.get_messages(
|
|
db_entry["user"], db_entry["telegram"]["msg_id"]
|
|
)
|
|
except:
|
|
pass
|
|
else:
|
|
try:
|
|
submission = await self.get_messages(
|
|
db_entry["user"], db_entry["telegram"]["msg_id"]
|
|
)
|
|
filepath = await self.download_media(
|
|
submission, file_name=configGet("tmp", "locations") + sep
|
|
)
|
|
except:
|
|
raise SubmissionUnavailableError()
|
|
|
|
logWrite(f"Uploading {path.basename(filepath)} to the API...", debug=True)
|
|
|
|
async with aiofiles.open(filepath, "rb") as f:
|
|
file_bytes = await f.read()
|
|
|
|
response = await upload_pic(
|
|
album=configGet("album", "posting", "api"),
|
|
client=client,
|
|
multipart_data=BodyPhotoUploadAlbumsAlbumPhotosPost(
|
|
File(file_bytes, path.basename(filepath), "image/jpeg")
|
|
),
|
|
ignore_duplicates=configGet("allow_duplicates", "submission"),
|
|
caption="queue",
|
|
compress=False,
|
|
)
|
|
|
|
if isinstance(response, HTTPValidationError) > 0:
|
|
raise SubmissionDuplicatesError(
|
|
str(filepath), response.to_dict()["duplicates"]
|
|
)
|
|
|
|
col_submitted.find_one_and_update(
|
|
{"_id": ObjectId(id)}, {"$set": {"done": True}}
|
|
)
|
|
|
|
try:
|
|
if db_entry["temp"]["uuid"] is not None:
|
|
rmtree(
|
|
path.join(
|
|
configGet("data", "locations"),
|
|
"submissions",
|
|
db_entry["temp"]["uuid"],
|
|
),
|
|
ignore_errors=True,
|
|
)
|
|
else:
|
|
remove(str(filepath))
|
|
except (FileNotFoundError, NotADirectoryError):
|
|
logWrite(
|
|
f"Could not delete '{filepath}' on submission accepted", debug=True
|
|
)
|
|
|
|
return submission, response[2]
|
|
|
|
async def ban_user(self, id: int) -> None:
|
|
pass
|
|
|
|
async def unban_user(self, id: int) -> None:
|
|
pass
|