Now using black for formatting

This commit is contained in:
2023-03-09 11:33:02 +01:00
parent 4331af415e
commit 88692ebc85
19 changed files with 701 additions and 250 deletions

View File

@@ -15,13 +15,14 @@ from modules.utils import configGet, locale
async def send_content(app: PosterClient) -> None:
try:
try:
token = await authorize()
except ValueError:
await app.send_message(app.owner, locale("api_creds_invalid", "message", locale=configGet("locale")))
await app.send_message(
app.owner,
locale("api_creds_invalid", "message", locale=configGet("locale")),
)
return
try:
@@ -29,19 +30,37 @@ async def send_content(app: PosterClient) -> None:
except KeyError:
logWrite(locale("post_empty", "console", locale=configGet("locale")))
if configGet("error", "reports"):
await app.send_message(app.owner, locale("api_queue_empty", "message", locale=configGet("locale")))
await app.send_message(
app.owner,
locale("api_queue_empty", "message", locale=configGet("locale")),
)
return
except ValueError:
if configGet("error", "reports"):
await app.send_message(app.owner, locale("api_queue_error", "message", locale=configGet("locale")))
await app.send_message(
app.owner,
locale("api_queue_error", "message", locale=configGet("locale")),
)
return
response = await http_session.get(f'{configGet("address", "posting", "api")}/photos/{pic[0]}', headers={"Authorization": f"Bearer {token}"})
response = await http_session.get(
f'{configGet("address", "posting", "api")}/photos/{pic[0]}',
headers={"Authorization": f"Bearer {token}"},
)
if response.status != 200:
logWrite(locale("post_invalid_pic", "console", locale=configGet("locale")).format(response.status, str(response.json())))
logWrite(
locale(
"post_invalid_pic", "console", locale=configGet("locale")
).format(response.status, str(response.json()))
)
if configGet("error", "reports"):
await app.send_message(app.owner, locale("post_invalid_pic", "message", locale=configGet("locale")).format(response.status, response.json()))
await app.send_message(
app.owner,
locale(
"post_invalid_pic", "message", locale=configGet("locale")
).format(response.status, response.json()),
)
tmp_dir = str(uuid4())
@@ -49,23 +68,40 @@ async def send_content(app: PosterClient) -> None:
tmp_path = path.join(tmp_dir, pic[1])
async with aiofiles.open(path.join(configGet("tmp", "locations"), tmp_path), 'wb') as out_file:
async with aiofiles.open(
path.join(configGet("tmp", "locations"), tmp_path), "wb"
) as out_file:
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', debug=True)
logWrite(
f'Candidate {pic[1]} ({pic[0]}) is {path.getsize(path.join(configGet("tmp", "locations"), tmp_path))} bytes big',
debug=True,
)
if path.getsize(path.join(configGet("tmp", "locations"), tmp_path)) > 5242880:
image = Image.open(path.join(configGet("tmp", "locations"), tmp_path))
width, height = image.size
image = image.resize((int(width/2), int(height/2)), Image.ANTIALIAS)
image = image.resize((int(width / 2), int(height / 2)), Image.ANTIALIAS)
if tmp_path.lower().endswith(".jpeg") or tmp_path.lower().endswith(".jpg"):
image.save(path.join(configGet("tmp", "locations"), tmp_path), "JPEG", optimize=True, quality=50)
image.save(
path.join(configGet("tmp", "locations"), tmp_path),
"JPEG",
optimize=True,
quality=50,
)
elif tmp_path.lower().endswith(".png"):
image.save(path.join(configGet("tmp", "locations"), tmp_path), "PNG", optimize=True, compress_level=8)
image.save(
path.join(configGet("tmp", "locations"), tmp_path),
"PNG",
optimize=True,
compress_level=8,
)
image.close()
if path.getsize(path.join(configGet("tmp", "locations"), tmp_path)) > 5242880:
rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
rmtree(
path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True
)
raise BytesWarning
del response
@@ -77,10 +113,17 @@ async def send_content(app: PosterClient) -> None:
else:
caption = ""
if submitted is not None and configGet("enabled", "posting", "submitted_caption") and (
(submitted["user"] not in app.admins) or (configGet("ignore_admins", "posting", "submitted_caption") is False)
if (
submitted is not None
and configGet("enabled", "posting", "submitted_caption")
and (
(submitted["user"] not in app.admins)
or (configGet("ignore_admins", "posting", "submitted_caption") is False)
)
):
caption = f"{caption}\n\n{configGet('text', 'posting', 'submitted_caption')}\n"
caption = (
f"{caption}\n\n{configGet('text', 'posting', 'submitted_caption')}\n"
)
else:
caption = f"{caption}\n\n"
@@ -93,11 +136,21 @@ async def send_content(app: PosterClient) -> None:
caption = caption
try:
sent = await app.send_photo(configGet("channel", "posting"), path.join(configGet("tmp", "locations"), tmp_path), caption=caption, disable_notification=configGet("silent", "posting"))
sent = await app.send_photo(
configGet("channel", "posting"),
path.join(configGet("tmp", "locations"), tmp_path),
caption=caption,
disable_notification=configGet("silent", "posting"),
)
except Exception as exp:
logWrite(f"Could not send image {pic[1]} ({pic[0]}) due to {exp}")
if configGet("error", "reports"):
await app.send_message(app.owner, locale("post_exception", "message", locale=configGet("locale")).format(exp, format_exc()))
await app.send_message(
app.owner,
locale(
"post_exception", "message", locale=configGet("locale")
).format(exp, format_exc()),
)
# rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
return
@@ -107,7 +160,9 @@ async def send_content(app: PosterClient) -> None:
"image": pic[0],
"filename": pic[1],
"channel": configGet("channel", "posting"),
"caption": None if (submitted is None or submitted["caption"] is None) else submitted["caption"].strip()
"caption": None
if (submitted is None or submitted["caption"] is None)
else submitted["caption"].strip(),
}
)
@@ -115,13 +170,31 @@ async def send_content(app: PosterClient) -> None:
rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
logWrite(locale("post_sent", "console", locale=configGet("locale")).format(pic[0], str(configGet("channel", "posting")), caption.replace("\n", "%n"), str(configGet("silent", "posting"))))
logWrite(
locale("post_sent", "console", locale=configGet("locale")).format(
pic[0],
str(configGet("channel", "posting")),
caption.replace("\n", "%n"),
str(configGet("silent", "posting")),
)
)
except Exception as exp:
logWrite(locale("post_exception", "console", locale=configGet("locale")).format(str(exp), format_exc()))
logWrite(
locale("post_exception", "console", locale=configGet("locale")).format(
str(exp), format_exc()
)
)
if configGet("error", "reports"):
await app.send_message(app.owner, locale("post_exception", "message", locale=configGet("locale")).format(exp, format_exc()))
await app.send_message(
app.owner,
locale("post_exception", "message", locale=configGet("locale")).format(
exp, format_exc()
),
)
try:
rmtree(path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True)
rmtree(
path.join(configGet("tmp", "locations"), tmp_dir), ignore_errors=True
)
except:
pass
pass