dev #19
@ -1,6 +1,6 @@
|
||||
from os import path, remove, sep
|
||||
from shutil import rmtree
|
||||
from typing import Union
|
||||
from typing import Tuple, Union
|
||||
from pyrogram.client import Client
|
||||
from pyrogram.types import Message
|
||||
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
|
||||
@ -18,7 +18,9 @@ class PosterClient(Client):
|
||||
self.owner = configGet("owner")
|
||||
self.admins = configGet("admins") + [configGet("owner")]
|
||||
|
||||
async def submit_photo(self, id: str) -> Union[Message, None]:
|
||||
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
|
||||
|
||||
@ -87,7 +89,7 @@ class PosterClient(Client):
|
||||
f"Could not delete '{filepath}' on submission accepted", debug=True
|
||||
)
|
||||
|
||||
return submission
|
||||
return submission, response[2]
|
||||
|
||||
async def ban_user(self, id: int) -> None:
|
||||
pass
|
||||
|
@ -96,6 +96,7 @@
|
||||
"file_size": 15728640,
|
||||
"tmp_size": 15728640,
|
||||
"allow_duplicates": false,
|
||||
"send_uploaded_id": false,
|
||||
"require_confirmation": {
|
||||
"users": true,
|
||||
"admins": true
|
||||
|
@ -96,7 +96,7 @@ async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]:
|
||||
|
||||
async def upload_pic(
|
||||
filepath: str, allow_duplicates: bool = False, token: Union[str, None] = None
|
||||
) -> Tuple[bool, list, str]:
|
||||
) -> Tuple[bool, list, Union[str, None]]:
|
||||
token = await authorize() if token is None else token
|
||||
try:
|
||||
pic_name = path.basename(filepath)
|
||||
@ -117,6 +117,7 @@ async def upload_pic(
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
data=formdata,
|
||||
)
|
||||
response_json = await response.json()
|
||||
if response.status != 200 and response.status != 409:
|
||||
logWrite(
|
||||
f"Could not upload '{filepath}' to API: HTTP {response.status} with message '{response.content}'"
|
||||
@ -124,21 +125,22 @@ async def upload_pic(
|
||||
raise SubmissionUploadError(
|
||||
str(filepath), response.status, response.content
|
||||
)
|
||||
id = response_json["id"] if "id" in await response.json() else None
|
||||
duplicates = []
|
||||
if "duplicates" in (await response.json()):
|
||||
for index, duplicate in enumerate((await response.json())["duplicates"]):
|
||||
if (await response.json())["access_token"] is None:
|
||||
if "duplicates" in response_json:
|
||||
for index, duplicate in enumerate(response_json)["duplicates"]: # type: ignore
|
||||
if response_json["access_token"] is None:
|
||||
duplicates.append(
|
||||
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}'
|
||||
)
|
||||
else:
|
||||
duplicates.append(
|
||||
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{(await response.json())["access_token"]}?id={index}'
|
||||
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{response_json["access_token"]}?id={index}'
|
||||
)
|
||||
return True, duplicates, (await response.json())["id"]
|
||||
return True, duplicates, id
|
||||
except Exception as exp:
|
||||
print_exc()
|
||||
return False, [], ""
|
||||
return False, [], None
|
||||
|
||||
|
||||
async def find_pic(
|
||||
|
@ -40,9 +40,9 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
|
||||
)
|
||||
return
|
||||
|
||||
if submission is not None:
|
||||
await submission.reply_text(
|
||||
locale("sub_yes", "message", locale=submission.from_user.language_code),
|
||||
if submission[0] is not None:
|
||||
await submission[0].reply_text(
|
||||
locale("sub_yes", "message", locale=submission[0].from_user.language_code),
|
||||
quote=True,
|
||||
)
|
||||
elif db_entry is not None:
|
||||
@ -73,6 +73,12 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
|
||||
]
|
||||
]
|
||||
)
|
||||
|
||||
if configGet("send_uploaded_id", "submission"):
|
||||
await clb.message.edit_caption(
|
||||
clb.message.caption + f"\n\nID: `{submission[1]}`"
|
||||
)
|
||||
|
||||
await clb.message.edit_reply_markup(
|
||||
reply_markup=InlineKeyboardMarkup(edited_markup)
|
||||
)
|
||||
|
@ -188,12 +188,14 @@ async def get_submission(app: PosterClient, msg: Message):
|
||||
and configGet("admins", "submission", "require_confirmation") is False
|
||||
):
|
||||
try:
|
||||
await app.submit_photo(str(inserted.inserted_id))
|
||||
submitted = await app.submit_photo(str(inserted.inserted_id))
|
||||
await msg.reply_text(
|
||||
locale("sub_yes_auto", "message", locale=user_locale),
|
||||
disable_notification=True,
|
||||
quote=True,
|
||||
)
|
||||
if configGet("send_uploaded_id", "submission"):
|
||||
caption += f"\n\nID: `{submitted[1]}`"
|
||||
await msg.copy(app.owner, caption=caption, disable_notification=True)
|
||||
return
|
||||
except SubmissionDuplicatesError as exp:
|
||||
@ -212,12 +214,14 @@ async def get_submission(app: PosterClient, msg: Message):
|
||||
and configGet("users", "submission", "require_confirmation") is False
|
||||
):
|
||||
try:
|
||||
await app.submit_photo(str(inserted.inserted_id))
|
||||
submitted = await app.submit_photo(str(inserted.inserted_id))
|
||||
await msg.reply_text(
|
||||
locale("sub_yes_auto", "message", locale=user_locale),
|
||||
disable_notification=True,
|
||||
quote=True,
|
||||
)
|
||||
if configGet("send_uploaded_id", "submission"):
|
||||
caption += f"\n\nID: `{submitted[1]}`"
|
||||
await msg.copy(app.owner, caption=caption)
|
||||
return
|
||||
except SubmissionDuplicatesError as exp:
|
||||
|
Reference in New Issue
Block a user