wrolpi/modules/videos/video/api.py
Roland 58de8c7c5b
Upgrading Sanic and yt-dlp. Major changes occurred because Sanic changed how multiprocessing works.
Python 3.11 is now required, old versions were causing unpredictability in tests.  (Sanic does not yet
support 3.12)

Sanic has been upgraded to 23.6.0, which is the latest version that avoids this bug:
 https://github.com/sanic-org/sanic/issues/2921

New strategy for multiprocessing is to create all multiprocessing tools in one process, then fork
to other processes.  The previous strategy was to declare multiprocessing tools at the top of every
file, or wherever they were needed at import/creation.  Now all multiprocessing tools are attached to the
app.shared_ctx.  This means `api_app` is imported in many, many places.

This forced a change in how the DownloadManager works.  Previously, it would continually run
download workers which would pull downloads from a multiprocessing.Queue.  Now, a single worker checks
for new downloads and sends a Sanic signal.

Flags have been reworked to use the `api_app`.  I removed the `which` flag functionality because the
`which` are called at import and needed their own multiprocessing.Event.
2024-04-30 10:55:25 -06:00

75 lines
2.4 KiB
Python

import json
import sys
from http import HTTPStatus
import sanic.response
from sanic import response, Blueprint
from sanic.request import Request
from sanic_ext import validate
from sanic_ext.extensions.openapi import openapi
from wrolpi.api_utils import json_response, CustomJSONEncoder
from wrolpi.common import logger, wrol_mode_check, run_after
from wrolpi.errors import InvalidOrderBy, ValidationError
from wrolpi.events import Events
from wrolpi.schema import JSONErrorResponse
from . import lib
from .. import schema
video_bp = Blueprint('Video', '/api/videos')
logger = logger.getChild(__name__)
@video_bp.get('/video/<video_id:int>')
@openapi.description('Get Video information')
@openapi.response(HTTPStatus.OK, schema.VideoResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
def video_get(_: Request, video_id: int):
video, previous_video, next_video = lib.get_video_for_app(video_id)
return json_response({'file_group': video, 'prev': previous_video, 'next': next_video})
@video_bp.post('/search')
@openapi.definition(
summary='Search Video titles and captions',
body=schema.VideoSearchRequest,
)
@openapi.response(HTTPStatus.OK, schema.VideoSearchResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(schema.VideoSearchRequest)
async def search_videos(_: Request, body: schema.VideoSearchRequest):
if body.order_by not in lib.VIDEO_ORDERS:
raise InvalidOrderBy('Invalid order by')
file_groups, videos_total = lib.search_videos(
body.search_str,
body.offset,
body.limit,
body.channel_id,
body.order_by,
body.tag_names,
body.headline,
)
ret = {'file_groups': list(file_groups), 'totals': {'file_groups': videos_total}}
return json_response(ret)
@video_bp.delete('/video/<video_ids:[0-9,]+>', name='Video Delete Many')
@video_bp.delete('/video/<video_ids:int>', name='Video Delete One')
@openapi.description('Delete videos.')
@openapi.response(HTTPStatus.NO_CONTENT)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@wrol_mode_check
@run_after(lib.save_channels_config)
async def video_delete(_: Request, video_ids: str):
try:
video_ids = [int(i) for i in str(video_ids).split(',')]
except Exception:
raise ValidationError('Unable to parse video ids')
lib.delete_videos(*video_ids)
Events.send_deleted(f'Deleted {len(video_ids)} videos')
return response.empty()