diff --git a/modules/api_client.py b/modules/api_client.py index 2680635..7404320 100644 --- a/modules/api_client.py +++ b/modules/api_client.py @@ -59,8 +59,10 @@ from photosapi_client.models.body_video_upload_albums_album_videos_post import ( ) from photosapi_client.models.http_validation_error import HTTPValidationError from photosapi_client.models.photo import Photo +from photosapi_client.models.photo_search import PhotoSearch from photosapi_client.models.token import Token from photosapi_client.models.video import Video +from photosapi_client.models.video_search import VideoSearch from photosapi_client.types import File from modules.http_client import http_session diff --git a/modules/sender.py b/modules/sender.py index 02163f9..9455ca9 100644 --- a/modules/sender.py +++ b/modules/sender.py @@ -4,6 +4,7 @@ from os import makedirs, path from random import choice, sample from shutil import rmtree from traceback import format_exc, print_exc +from typing import Union from uuid import uuid4 import aiofiles @@ -13,6 +14,9 @@ from photosapi_client.errors import UnexpectedStatus from PIL import Image from modules.api_client import ( + File, + PhotoSearch, + VideoSearch, authorize, client, photo_get, @@ -41,16 +45,12 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: try: funcs = [] - print(funcs) - if app.config["posting"]["types"]["photo"]: funcs.append((photo_random, photo_get, app.send_photo, photo_patch)) if app.config["posting"]["types"]["video"]: funcs.append((video_random, video_get, app.send_video, video_patch)) - print(funcs) - if not funcs: raise KeyError( "No media source provided: all seem to be disabled in config" @@ -60,16 +60,23 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: found = False for func_iter in sample(funcs, len(funcs)): func = func_iter - media = ( + + random_results = ( await func_iter[0]( album=app.config["posting"]["api"]["album"], caption="queue", client=client, limit=1, ) - ).results[0] + ).results + + if not random_results: + continue + + media: Union[PhotoSearch, VideoSearch] = random_results[0] + try: - response = await func_iter[1](id=media.id, client=client) + response: File = await func_iter[1](id=media.id, client=client) except Exception as exp: print_exc() logger.error("Media is invalid: %s", exp) @@ -78,13 +85,15 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: app.owner, f"Media is invalid: {exp}" ) return + found = True break + if not found: raise KeyError("No media found") else: func = funcs[0] - media = ( + media: Union[PhotoSearch, VideoSearch] = ( await func[0]( album=app.config["posting"]["api"]["album"], caption="queue", @@ -93,7 +102,7 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: ) ).results[0] try: - response = await func[1](id=media.id, client=client) + response: File = await func[1](id=media.id, client=client) except Exception as exp: print_exc() logger.error("Media is invalid: %s", exp) @@ -130,7 +139,10 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: await out_file.write(response.payload.read()) logger.info( - f"Candidate {media.filename} ({media.id}) is {path.getsize(path.join(app.config['locations']['tmp'], tmp_path))} bytes big", + "Candidate %s (%s) is %s bytes big", + media.filename, + media.id, + path.getsize(path.join(app.config["locations"]["tmp"], tmp_path)), ) if ( @@ -205,7 +217,7 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None: ) except Exception as exp: logger.error( - f"Could not send media {media.filename} ({media.id}) due to {exp}" + "Could not send media %s (%s) due to %s", media.filename, media.id, exp ) if app.config["reports"]["error"]: await app.send_message(