This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/classes/poster_client.py

97 lines
3.3 KiB
Python
Raw Normal View History

2023-02-17 17:44:30 +02:00
from os import path, remove, sep
from shutil import rmtree
from typing import Union
from pyrogram.client import Client
from pyrogram.types import Message
2023-02-17 17:44:30 +02:00
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
2023-03-09 12:33:02 +02:00
class PosterClient(Client):
def __init__(self, name: str, **kwargs): # type: ignore
2023-02-17 17:44:30 +02:00
super().__init__(name, **kwargs)
2023-02-17 22:53:43 +02:00
self.owner = configGet("owner")
2023-03-09 12:33:02 +02:00
self.admins = configGet("admins") + [configGet("owner")]
2023-02-17 17:44:30 +02:00
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:
2023-03-09 12:33:02 +02:00
if not path.exists(
path.join(
configGet("data", "locations"),
"submissions",
db_entry["temp"]["uuid"],
db_entry["temp"]["file"],
)
):
2023-02-17 17:44:30 +02:00
raise SubmissionUnavailableError()
else:
2023-03-09 12:33:02 +02:00
filepath = path.join(
configGet("data", "locations"),
"submissions",
db_entry["temp"]["uuid"],
db_entry["temp"]["file"],
)
2023-02-18 00:18:19 +02:00
try:
2023-03-09 12:33:02 +02:00
submission = await self.get_messages(
db_entry["user"], db_entry["telegram"]["msg_id"]
)
2023-02-18 00:18:19 +02:00
except:
pass
2023-02-17 17:44:30 +02:00
else:
try:
2023-03-09 12:33:02 +02:00
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
)
2023-02-17 17:44:30 +02:00
except:
raise SubmissionUnavailableError()
2023-03-09 12:33:02 +02:00
response = await upload_pic(
str(filepath), allow_duplicates=configGet("allow_duplicates", "submission")
)
2023-02-17 17:44:30 +02:00
2023-02-17 22:53:43 +02:00
if len(response[1]) > 0:
2023-02-17 17:44:30 +02:00
raise SubmissionDuplicatesError(str(filepath), response[1])
2023-03-09 12:33:02 +02:00
col_submitted.find_one_and_update(
{"_id": ObjectId(id)}, {"$set": {"done": True}}
)
2023-02-17 17:44:30 +02:00
try:
if db_entry["temp"]["uuid"] is not None:
2023-03-09 12:33:02 +02:00
rmtree(
path.join(
configGet("data", "locations"),
"submissions",
db_entry["temp"]["uuid"],
),
ignore_errors=True,
)
2023-02-17 17:44:30 +02:00
else:
remove(str(filepath))
except (FileNotFoundError, NotADirectoryError):
2023-03-09 12:33:02 +02:00
logWrite(
f"Could not delete '{filepath}' on submission accepted", debug=True
)
2023-02-17 17:44:30 +02:00
return submission
async def ban_user(self, id: int) -> None:
pass
async def unban_user(self, id: int) -> None:
2023-03-09 12:33:02 +02:00
pass