WIP: API change

This commit is contained in:
2023-03-23 15:03:30 +01:00
parent c948f2e34d
commit 2d0d72b026
6 changed files with 168 additions and 59 deletions

View File

@@ -5,16 +5,24 @@ from shutil import disk_usage, rmtree
from traceback import format_exc
from uuid import uuid4
from zipfile import ZipFile
import aiofiles
from convopyro import listen_message
from pyrogram import filters
from pyrogram.types import Message
from classes.poster_client import PosterClient
from modules.api_client import remove_pic, upload_pic
from modules.app import app, users_with_context
from modules.logger import logWrite
from modules.utils import configGet, extract_and_save, locale
from modules.api_client import (
client,
photo_upload,
photo_delete,
HTTPValidationError,
BodyPhotoUploadAlbumsAlbumPhotosPost,
File,
)
@app.on_message(~filters.scheduled & filters.command(["import"], prefixes=["", "/"]))
@@ -114,13 +122,34 @@ async def cmd_import(app: PosterClient, msg: Message):
if not path.isfile(filename):
continue
# upload filename
uploaded = await upload_pic(filename)
if uploaded[0] is False:
logWrite(
f"Could not upload '{filename}' from '{path.join(configGet('tmp', 'locations'), tmp_dir)}'. Duplicates: {str(uploaded[1])}",
debug=True,
async with aiofiles.open(filename, "rb") as f:
file_bytes = await f.read()
uploaded = await photo_upload(
album=configGet("album", "posting", "api"),
client=client,
multipart_data=BodyPhotoUploadAlbumsAlbumPhotosPost(
File(file_bytes, path.basename(filename), "image/jpeg")
),
ignore_duplicates=configGet("allow_duplicates", "submission"),
caption="queue",
compress=False,
)
if uploaded is None or isinstance(uploaded, HTTPValidationError):
await msg.reply_text(
locale(
"import_upload_error_other",
"message",
locale=msg.from_user.language_code,
).format(path.basename(filename)),
disable_notification=True,
)
if len(uploaded[1]) > 0:
return
if not hasattr(uploaded, "id"):
if hasattr(uploaded, "duplicates"):
logWrite(
f"Could not upload '{filename}' from '{path.join(configGet('tmp', 'locations'), tmp_dir)}'. Duplicates: {str(uploaded.duplicates)}",
debug=True,
)
await msg.reply_text(
locale(
"import_upload_error_duplicate",
@@ -140,7 +169,7 @@ async def cmd_import(app: PosterClient, msg: Message):
)
else:
logWrite(
f"Uploaded '{filename}' from '{path.join(configGet('tmp', 'locations'), tmp_dir)}' and got ID {uploaded[2]}",
f"Uploaded '{filename}' from '{path.join(configGet('tmp', 'locations'), tmp_dir)}' and got ID {uploaded.id}",
debug=True,
)
@@ -187,8 +216,8 @@ async def cmd_remove(app: PosterClient, msg: Message):
locale("remove_abort", "message", locale=msg.from_user.language_code)
)
return
response = await remove_pic(answer.text)
if response:
try:
response = await photo_delete(id=answer.text, client=client)
logWrite(
f"Removed '{answer.text}' by request of user {answer.from_user.id}"
)
@@ -197,7 +226,7 @@ async def cmd_remove(app: PosterClient, msg: Message):
"remove_success", "message", locale=msg.from_user.language_code
).format(answer.text)
)
else:
except:
logWrite(
f"Could not remove '{answer.text}' by request of user {answer.from_user.id}"
)