diff --git a/poster.py b/poster.py index e71d991..0ab5c20 100644 --- a/poster.py +++ b/poster.py @@ -3,6 +3,7 @@ from os import getpid, path from sys import exit from time import time from traceback import format_exc +from modules.api_client import authorize from modules.cli import * from modules.http_client import http_session @@ -207,10 +208,38 @@ async def main(): if configGet("post", "mode"): scheduler.start() - # if configGet("api_based", "mode"): - # token = await authorize() - # if len(get(f'{configGet("address", "posting", "api")}/albums?q={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}).json()["results"]) == 0: - # post(f'{configGet("address", "posting", "api")}/albums?name={configGet("queue", "posting", "api", "albums")}&title={configGet("queue", "posting", "api", "albums")}', headers={"Authorization": f"Bearer {token}"}) + try: + + token = await authorize() + + if ( + len( + ( + await ( + await http_session.get( + f'{configGet("address", "posting", "api")}/albums?q={configGet("queue", "posting", "api", "albums")}', + headers={"Authorization": f"Bearer {token}"}, + ) + ).json() + )["results"] + ) + == 0 + ): + logWrite("Media album does not exist on API server. Trying to create it...") + try: + await http_session.post( + f'{configGet("address", "posting", "api")}/albums?name={configGet("queue", "posting", "api", "albums")}&title={configGet("queue", "posting", "api", "albums")}', + headers={"Authorization": f"Bearer {token}"}, + ) + logWrite( + "Created media album on API server."}" + ) + except Exception as exp: + logWrite( + f"Could not create media album on API server due to {exp}: {format_exc()}" + ) + except Exception as exp: + logWrite(f"Could not check if API album exists due to {exp}: {format_exc()}") await idle()