mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
* db: request-path library functions use the request's connection Library functions an API request reaches now take the request's Session (first argument) instead of opening their own session or cursor, so a request needs one SQLite connection. Raw SQL runs through the new `wrolpi.db.session_curs(session)`, a cursor on the Session's connection; `RequestDB.curs()` uses it. - Search: files, videos, archives, flasher, and docs search share the request connection for the total, the page of ids, and the page's models (3 connections -> 1). - Zim search, including tag-filtered search and estimates (2 -> 1). - Channel detail (`Channel.get_statistics` reads on the Channel's own Session) and the channel list. - Video detail, edit, description, captions, comments and transcode. `get_video_for_app` and `update_video` no longer commit; their handlers use `request.ctx.db.write()`, so editing a Video and reading it back is one transaction. - Tags (list, overlaps, recent), file statistics and estimates, other estimates, bulk tag preview, the download list, retry, kill and the status summary, and docs/videos/archive statistics. Not converted: file operations that refresh files inside the request (delete, rename, upload, channel and collection tag/move) commit page by page in code shared with the file worker; the cached zim entry lookup; config import. The connection-count tests now assert one connection for every endpoint they cover (no xfails left), and cover the videos and archive statistics endpoints, which had no API test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * downloads: startup retries in a write session of its own start_single_tasks still called retry_downloads() without a Session after it began requiring one, so any start after a completed refresh raised TypeError there: incomplete downloads were not reset and the download_on_startup enable after it never ran. Startup is not a request, so DownloadManager.retry_downloads_on_startup() opens and commits its own short write transaction. Tested through the method, not start_single_tasks: importing main registers its startup listeners on the shared app, and every later test in that worker then runs them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6 KiB
Python
145 lines
6 KiB
Python
import asyncio
|
|
from http import HTTPStatus
|
|
|
|
from sanic import 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
|
|
from wrolpi.common import logger, wrol_mode_check
|
|
from wrolpi.errors import InvalidOrderBy, InvalidJob
|
|
from wrolpi.schema import JSONErrorResponse
|
|
from . import lib
|
|
from .. import schema
|
|
from ..transcode import transcode_video_job, validate_transcode_request, REMOVE_VIDEO, REMOVE_AUDIO
|
|
|
|
video_bp = Blueprint('Video', '/api/videos')
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
@video_bp.get('/<file_group_id:int>')
|
|
@openapi.description('Get Video information')
|
|
@openapi.response(HTTPStatus.OK, schema.VideoResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
def video_get(request: Request, file_group_id: int):
|
|
skip_viewed = request.args.get('skip_viewed', '').lower() == 'true'
|
|
# Marking the Video viewed is a write.
|
|
with request.ctx.db.write() as session:
|
|
video, previous_video, next_video = lib.get_video_for_app(session, file_group_id, skip_viewed=skip_viewed)
|
|
return json_response({'file_group': video, 'prev': previous_video, 'next': next_video})
|
|
|
|
|
|
@video_bp.put('/<file_group_id:int>')
|
|
@openapi.description('Edit a Video\'s details. Written to its info json; the Video is validated again.')
|
|
@openapi.body({'application/json': schema.VideoUpdateRequest})
|
|
@openapi.response(HTTPStatus.OK, schema.VideoResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@validate(schema.VideoUpdateRequest)
|
|
@wrol_mode_check
|
|
async def video_update(request: Request, file_group_id: int, body: schema.VideoUpdateRequest):
|
|
with request.ctx.db.write() as session:
|
|
lib.update_video(session, file_group_id, title=body.title, description=body.description)
|
|
video, previous_video, next_video = lib.get_video_for_app(session, file_group_id)
|
|
return json_response({'file_group': video, 'prev': previous_video, 'next': next_video})
|
|
|
|
|
|
@video_bp.get('/<file_group_id:int>/comments')
|
|
@openapi.description('Get Video comments')
|
|
@openapi.response(HTTPStatus.OK, schema.VideoCommentsResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
def video_get_comments(request: Request, file_group_id: int):
|
|
video = lib.get_video(request.ctx.session, file_group_id)
|
|
return json_response({'comments': video.get_comments()})
|
|
|
|
|
|
@video_bp.get('/<file_group_id:int>/description')
|
|
@openapi.description('Get Video description')
|
|
@openapi.response(HTTPStatus.OK, schema.VideoDescriptionResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
def video_get_description(request: Request, file_group_id: int):
|
|
video = lib.get_video(request.ctx.session, file_group_id)
|
|
return json_response({'description': video.get_description()})
|
|
|
|
|
|
@video_bp.get('/<file_group_id:int>/captions')
|
|
@openapi.description('Get Video captions')
|
|
@openapi.response(HTTPStatus.OK, schema.VideoCaptionsResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
def video_get_captions(request: Request, file_group_id: int):
|
|
video = lib.get_video(request.ctx.session, file_group_id)
|
|
return json_response({'captions': video.get_caption_chunks()})
|
|
|
|
|
|
@video_bp.post('/<file_group_id:int>/transcode')
|
|
@openapi.description('Queue a Job which transcodes the Video to the requested codecs/container.')
|
|
@openapi.body({'application/json': schema.VideoTranscodeRequest})
|
|
@openapi.response(HTTPStatus.OK, schema.VideoTranscodeResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@validate(schema.VideoTranscodeRequest)
|
|
@wrol_mode_check
|
|
async def video_transcode(request: Request, file_group_id: int, body: schema.VideoTranscodeRequest):
|
|
try:
|
|
validate_transcode_request(body.video_codec, body.audio_codec, body.container, body.fragmented)
|
|
except ValueError as e:
|
|
raise InvalidJob(str(e))
|
|
# Raises UnknownVideo (404) before anything is queued.
|
|
video = lib.get_video(request.ctx.session, file_group_id)
|
|
is_audio = (video.file_group.mimetype or '').startswith('audio/')
|
|
if is_audio and body.video_codec and body.video_codec != REMOVE_VIDEO:
|
|
raise InvalidJob(f'This is an audio file; it has no video stream to transcode to {body.video_codec}')
|
|
if is_audio and body.audio_codec == REMOVE_AUDIO:
|
|
raise InvalidJob('This is an audio file; removing its audio would leave nothing')
|
|
if body.video_codec == REMOVE_VIDEO and not is_audio:
|
|
action = 'Extract audio from'
|
|
elif body.audio_codec == REMOVE_AUDIO:
|
|
action = 'Remove audio from'
|
|
elif not body.video_codec and not body.audio_codec:
|
|
action = 'Remux'
|
|
else:
|
|
action = 'Transcode'
|
|
job_id = transcode_video_job.enqueue(
|
|
description=f'{action} {video.video_path.name}',
|
|
file_group_id=file_group_id,
|
|
video_codec=body.video_codec,
|
|
audio_codec=body.audio_codec,
|
|
container=body.container,
|
|
fragmented=body.fragmented,
|
|
)
|
|
return json_response({'job_id': job_id})
|
|
|
|
|
|
@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: Request, body: schema.VideoSearchRequest):
|
|
if body.order_by not in lib.VIDEO_ORDERS:
|
|
raise InvalidOrderBy('Invalid order by')
|
|
|
|
# Synchronous SQLite query; keep it off the event loop.
|
|
file_groups, videos_total = await asyncio.to_thread(
|
|
lib.search_videos,
|
|
request.ctx.session,
|
|
body.search_str,
|
|
body.offset,
|
|
body.limit,
|
|
body.channel_id,
|
|
body.order_by,
|
|
body.tag_names,
|
|
body.headline,
|
|
body.censored,
|
|
body.deep,
|
|
)
|
|
|
|
ret = {'file_groups': list(file_groups), 'totals': {'file_groups': videos_total}}
|
|
return json_response(ret)
|
|
|
|
|