Added random photo/video request
This commit is contained in:
@@ -2,6 +2,7 @@ import re
|
||||
from datetime import datetime, timezone
|
||||
from os import makedirs, remove
|
||||
from pathlib import Path
|
||||
from random import randint
|
||||
from secrets import token_urlsafe
|
||||
from shutil import move
|
||||
from typing import Union
|
||||
@@ -17,12 +18,18 @@ from starlette.status import HTTP_204_NO_CONTENT
|
||||
|
||||
from classes.exceptions import (
|
||||
AlbumNameNotFoundError,
|
||||
SearchLimitInvalidError,
|
||||
SearchPageInvalidError,
|
||||
SearchTokenInvalidError,
|
||||
VideoNotFoundError,
|
||||
VideoSearchQueryEmptyError,
|
||||
)
|
||||
from classes.models import SearchResultsVideo, Video, VideoPublic
|
||||
from classes.models import (
|
||||
RandomSearchResultsVideo,
|
||||
SearchResultsVideo,
|
||||
Video,
|
||||
VideoPublic,
|
||||
)
|
||||
from modules.app import app
|
||||
from modules.database import col_albums, col_tokens, col_videos
|
||||
from modules.security import User, get_current_active_user
|
||||
@@ -262,6 +269,71 @@ async def video_delete(
|
||||
return Response(status_code=HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
video_random_responses = {
|
||||
400: SearchLimitInvalidError().openapi,
|
||||
404: AlbumNameNotFoundError("name").openapi,
|
||||
}
|
||||
|
||||
|
||||
@app.get(
|
||||
"/albums/{album}/videos/random",
|
||||
description="Get one random video, optionally by caption",
|
||||
response_class=UJSONResponse,
|
||||
response_model=RandomSearchResultsVideo,
|
||||
responses=video_random_responses,
|
||||
)
|
||||
async def video_random(
|
||||
album: str,
|
||||
caption: Union[str, None] = None,
|
||||
limit: int = 100,
|
||||
current_user: User = Security(get_current_active_user, scopes=["videos.list"]),
|
||||
):
|
||||
if col_albums.find_one({"user": current_user.user, "name": album}) is None:
|
||||
raise AlbumNameNotFoundError(album)
|
||||
|
||||
if limit <= 0:
|
||||
raise SearchLimitInvalidError()
|
||||
|
||||
output = {"results": []}
|
||||
|
||||
db_query = (
|
||||
{
|
||||
"user": current_user.user,
|
||||
"album": album,
|
||||
"caption": re.compile(caption),
|
||||
}
|
||||
if caption is not None
|
||||
else {
|
||||
"user": current_user.user,
|
||||
"album": album,
|
||||
}
|
||||
)
|
||||
|
||||
documents_count = col_videos.count_documents(db_query)
|
||||
skip = randint(0, documents_count - 1) if documents_count > 1 else 0
|
||||
|
||||
videos = list(
|
||||
col_videos.aggregate(
|
||||
[
|
||||
{"$match": db_query},
|
||||
{"$skip": skip},
|
||||
{"$limit": limit},
|
||||
]
|
||||
)
|
||||
)
|
||||
|
||||
for video in videos:
|
||||
output["results"].append(
|
||||
{
|
||||
"id": video["_id"].__str__(),
|
||||
"filename": video["filename"],
|
||||
"caption": video["caption"],
|
||||
}
|
||||
)
|
||||
|
||||
return UJSONResponse(output)
|
||||
|
||||
|
||||
video_find_responses = {
|
||||
400: SearchPageInvalidError().openapi,
|
||||
401: SearchTokenInvalidError().openapi,
|
||||
|
Reference in New Issue
Block a user