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

@@ -9,7 +9,14 @@ import aiofiles
from classes.poster_client import PosterClient
from modules.api_client import authorize, move_pic, random_pic, http_session
from modules.api_client import (
client,
authorize,
photo_find,
photo_get,
photo_patch,
http_session,
)
from modules.database import col_sent, col_submitted
from modules.logger import logWrite
from modules.utils import configGet, locale
@@ -27,8 +34,15 @@ async def send_content(app: PosterClient) -> None:
return
try:
pic = await random_pic()
except KeyError:
search_result = await photo_find(
client=client,
album=configGet("album", "posting", "api"),
caption="queue",
page_size=configGet("page_size", "posting"),
)
print(search_result, flush=True)
pic = choice(search_result.results)
except (IndexError, KeyError):
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
await app.send_message(
@@ -45,7 +59,7 @@ async def send_content(app: PosterClient) -> None:
return
response = await http_session.get(
f'{configGet("address", "posting", "api")}/photos/{pic[0]}',
f'{configGet("address", "posting", "api")}/photos/{pic.id}',
headers={"Authorization": f"Bearer {token}"},
)
@@ -67,7 +81,7 @@ async def send_content(app: PosterClient) -> None:
makedirs(path.join(configGet("tmp", "locations"), tmp_dir), exist_ok=True)
tmp_path = path.join(tmp_dir, pic[1])
tmp_path = path.join(tmp_dir, pic.filename)
async with aiofiles.open(
path.join(configGet("tmp", "locations"), tmp_path), "wb"
@@ -75,7 +89,7 @@ async def send_content(app: PosterClient) -> None:
await out_file.write(await response.read())
logWrite(
f'Candidate {pic[1]} ({pic[0]}) is {path.getsize(path.join(configGet("tmp", "locations"), tmp_path))} bytes big',
f'Candidate {pic.filename} ({pic.id}) is {path.getsize(path.join(configGet("tmp", "locations"), tmp_path))} bytes big',
debug=True,
)
@@ -107,7 +121,7 @@ async def send_content(app: PosterClient) -> None:
del response
submitted = col_submitted.find_one({"temp.file": pic[1]})
submitted = col_submitted.find_one({"temp.file": pic.filename})
if submitted is not None and submitted["caption"] is not None:
caption = submitted["caption"].strip()
@@ -144,7 +158,7 @@ async def send_content(app: PosterClient) -> None:
disable_notification=configGet("silent", "posting"),
)
except Exception as exp:
logWrite(f"Could not send image {pic[1]} ({pic[0]}) due to {exp}")
logWrite(f"Could not send image {pic.filename} ({pic.id}) due to {exp}")
if configGet("error", "reports"):
await app.send_message(
app.owner,
@@ -158,8 +172,8 @@ async def send_content(app: PosterClient) -> None:
col_sent.insert_one(
{
"date": datetime.now(),
"image": pic[0],
"filename": pic[1],
"image": pic.id,
"filename": pic.filename,
"channel": configGet("channel", "posting"),
"caption": None
if (submitted is None or submitted["caption"] is None)
@@ -167,13 +181,13 @@ async def send_content(app: PosterClient) -> None:
}
)
await move_pic(pic[0])
await photo_patch(client=client, id=pic.id, caption="sent")
rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
logWrite(
locale("post_sent", "console", locale=configGet("locale")).format(
pic[0],
pic.id,
str(configGet("channel", "posting")),
caption.replace("\n", "%n"),
str(configGet("silent", "posting")),