2021-11-01 16:42:52 -06:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
|
|
from sanic import response, Blueprint
|
|
|
|
|
from sanic.request import Request
|
2022-01-13 09:29:58 -07:00
|
|
|
from sanic_ext import validate
|
|
|
|
|
from sanic_ext.extensions.openapi import openapi
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2024-06-12 16:47:31 -06:00
|
|
|
from wrolpi.api_utils import json_response
|
2022-11-09 08:33:55 -07:00
|
|
|
from wrolpi.common import logger, wrol_mode_check, run_after
|
2022-09-23 16:43:45 -06:00
|
|
|
from wrolpi.errors import InvalidOrderBy, ValidationError
|
2023-07-17 17:32:29 -06:00
|
|
|
from wrolpi.events import Events
|
2022-01-13 09:29:58 -07:00
|
|
|
from wrolpi.schema import JSONErrorResponse
|
2022-02-03 12:39:14 -07:00
|
|
|
from . import lib
|
2022-11-09 08:33:55 -07:00
|
|
|
from .. import schema
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
video_bp = Blueprint('Video', '/api/videos')
|
|
|
|
|
|
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@video_bp.get('/video/<video_id:int>')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Get Video information')
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.VideoResponse)
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
2021-11-01 16:42:52 -06:00
|
|
|
def video_get(_: Request, video_id: int):
|
2022-02-03 12:39:14 -07:00
|
|
|
video, previous_video, next_video = lib.get_video_for_app(video_id)
|
2023-03-24 08:58:18 -06:00
|
|
|
return json_response({'file_group': video, 'prev': previous_video, 'next': next_video})
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@video_bp.post('/search')
|
2022-02-03 11:36:26 -07:00
|
|
|
@openapi.definition(
|
|
|
|
|
summary='Search Video titles and captions',
|
2022-02-03 12:39:14 -07:00
|
|
|
body=schema.VideoSearchRequest,
|
2022-02-03 11:36:26 -07:00
|
|
|
)
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.VideoSearchResponse)
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
2022-02-03 12:39:14 -07:00
|
|
|
@validate(schema.VideoSearchRequest)
|
2024-04-17 11:25:26 -06:00
|
|
|
async def search_videos(_: Request, body: schema.VideoSearchRequest):
|
2022-02-03 12:39:14 -07:00
|
|
|
if body.order_by not in lib.VIDEO_ORDERS:
|
2021-11-01 16:42:52 -06:00
|
|
|
raise InvalidOrderBy('Invalid order by')
|
|
|
|
|
|
2023-03-24 08:58:18 -06:00
|
|
|
file_groups, videos_total = lib.search_videos(
|
2022-01-31 21:18:49 -07:00
|
|
|
body.search_str,
|
|
|
|
|
body.offset,
|
|
|
|
|
body.limit,
|
2022-04-08 09:57:04 -06:00
|
|
|
body.channel_id,
|
2022-01-31 21:18:49 -07:00
|
|
|
body.order_by,
|
2023-03-24 08:58:18 -06:00
|
|
|
body.tag_names,
|
2023-04-12 18:51:26 -06:00
|
|
|
body.headline,
|
2022-01-31 21:18:49 -07:00
|
|
|
)
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2023-03-24 08:58:18 -06:00
|
|
|
ret = {'file_groups': list(file_groups), 'totals': {'file_groups': videos_total}}
|
2021-11-01 16:42:52 -06:00
|
|
|
return json_response(ret)
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 11:25:26 -06:00
|
|
|
@video_bp.delete('/video/<video_ids:[0-9,]+>', name='Video Delete Many')
|
|
|
|
|
@video_bp.delete('/video/<video_ids:int>', name='Video Delete One')
|
2022-09-23 16:43:45 -06:00
|
|
|
@openapi.description('Delete videos.')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.response(HTTPStatus.NO_CONTENT)
|
|
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
2021-11-01 16:42:52 -06:00
|
|
|
@wrol_mode_check
|
2022-02-03 12:39:14 -07:00
|
|
|
@run_after(lib.save_channels_config)
|
2023-07-21 15:14:09 -06:00
|
|
|
async def video_delete(_: Request, video_ids: str):
|
2022-09-23 16:43:45 -06:00
|
|
|
try:
|
|
|
|
|
video_ids = [int(i) for i in str(video_ids).split(',')]
|
|
|
|
|
except Exception:
|
2022-10-15 17:41:10 -06:00
|
|
|
raise ValidationError('Unable to parse video ids')
|
2022-09-23 16:43:45 -06:00
|
|
|
|
|
|
|
|
lib.delete_videos(*video_ids)
|
2023-07-17 17:32:29 -06:00
|
|
|
|
|
|
|
|
Events.send_deleted(f'Deleted {len(video_ids)} videos')
|
2023-07-21 15:14:09 -06:00
|
|
|
return response.empty()
|