From 0d2e9fa6ec73f1196d9d80f60ab256bb90c21e13 Mon Sep 17 00:00:00 2001 From: Profitroll <47523801+profitrollgame@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:44:30 +0100 Subject: [PATCH] Using PosterClient instead of Client --- classes/poster_client.py | 62 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 classes/poster_client.py diff --git a/classes/poster_client.py b/classes/poster_client.py new file mode 100644 index 0000000..a664d84 --- /dev/null +++ b/classes/poster_client.py @@ -0,0 +1,62 @@ +from os import path, remove, sep +from shutil import rmtree +from typing import Union +from pyrogram.client import Client +from pyrogram.types import Message, CallbackQuery +from pyrogram.enums.parse_mode import ParseMode +from pyrogram.session.session import Session +from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError +from modules.api_client import upload_pic +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) + + async def submit_photo(self, id: str) -> Union[Message, 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"]) + 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() + + response = await upload_pic(str(filepath)) + + if response[0] is False: + raise SubmissionDuplicatesError(str(filepath), response[1]) + + 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 + + async def ban_user(self, id: int) -> None: + pass + + async def unban_user(self, id: int) -> None: + pass \ No newline at end of file