API usage overhaul #27

Merged
profitroll merged 17 commits from overhaul into dev 2023-06-28 00:57:31 +03:00
2 changed files with 25 additions and 11 deletions
Showing only changes of commit e2a73d5c4d - Show all commits

View File

@ -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.http_validation_error import HTTPValidationError
from photosapi_client.models.photo import Photo 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.token import Token
from photosapi_client.models.video import Video from photosapi_client.models.video import Video
from photosapi_client.models.video_search import VideoSearch
from photosapi_client.types import File from photosapi_client.types import File
from modules.http_client import http_session from modules.http_client import http_session

View File

@ -4,6 +4,7 @@ from os import makedirs, path
from random import choice, sample from random import choice, sample
from shutil import rmtree from shutil import rmtree
from traceback import format_exc, print_exc from traceback import format_exc, print_exc
from typing import Union
from uuid import uuid4 from uuid import uuid4
import aiofiles import aiofiles
@ -13,6 +14,9 @@ from photosapi_client.errors import UnexpectedStatus
from PIL import Image from PIL import Image
from modules.api_client import ( from modules.api_client import (
File,
PhotoSearch,
VideoSearch,
authorize, authorize,
client, client,
photo_get, photo_get,
@ -41,16 +45,12 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None:
try: try:
funcs = [] funcs = []
print(funcs)
if app.config["posting"]["types"]["photo"]: if app.config["posting"]["types"]["photo"]:
funcs.append((photo_random, photo_get, app.send_photo, photo_patch)) funcs.append((photo_random, photo_get, app.send_photo, photo_patch))
if app.config["posting"]["types"]["video"]: if app.config["posting"]["types"]["video"]:
funcs.append((video_random, video_get, app.send_video, video_patch)) funcs.append((video_random, video_get, app.send_video, video_patch))
print(funcs)
if not funcs: if not funcs:
raise KeyError( raise KeyError(
"No media source provided: all seem to be disabled in config" "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 found = False
for func_iter in sample(funcs, len(funcs)): for func_iter in sample(funcs, len(funcs)):
func = func_iter func = func_iter
media = (
random_results = (
await func_iter[0]( await func_iter[0](
album=app.config["posting"]["api"]["album"], album=app.config["posting"]["api"]["album"],
caption="queue", caption="queue",
client=client, client=client,
limit=1, limit=1,
) )
).results[0] ).results
if not random_results:
continue
media: Union[PhotoSearch, VideoSearch] = random_results[0]
try: 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: except Exception as exp:
print_exc() print_exc()
logger.error("Media is invalid: %s", exp) 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}" app.owner, f"Media is invalid: {exp}"
) )
return return
found = True found = True
break break
if not found: if not found:
raise KeyError("No media found") raise KeyError("No media found")
else: else:
func = funcs[0] func = funcs[0]
media = ( media: Union[PhotoSearch, VideoSearch] = (
await func[0]( await func[0](
album=app.config["posting"]["api"]["album"], album=app.config["posting"]["api"]["album"],
caption="queue", caption="queue",
@ -93,7 +102,7 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None:
) )
).results[0] ).results[0]
try: try:
response = await func[1](id=media.id, client=client) response: File = await func[1](id=media.id, client=client)
except Exception as exp: except Exception as exp:
print_exc() print_exc()
logger.error("Media is invalid: %s", exp) 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()) await out_file.write(response.payload.read())
logger.info( 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 ( if (
@ -205,7 +217,7 @@ async def send_content(app: PyroClient, http_session: ClientSession) -> None:
) )
except Exception as exp: except Exception as exp:
logger.error( 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"]: if app.config["reports"]["error"]:
await app.send_message( await app.send_message(