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>
146 lines
5.1 KiB
Python
146 lines
5.1 KiB
Python
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
|
|
from . import lib, schema
|
|
from . import cookies
|
|
from .channel.api import channel_bp
|
|
from .downloader import preview_filename
|
|
from .lib import format_videos_destination
|
|
from .models import Channel
|
|
from .video.api import video_bp
|
|
|
|
content_bp = Blueprint('VideoContent', '/api/videos')
|
|
videos_bp = Blueprint('Videos', '/api/videos').group(
|
|
content_bp, # view and manage video content and settings
|
|
channel_bp, # view and manage channels
|
|
video_bp, # view videos
|
|
)
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
@content_bp.get('/statistics')
|
|
@openapi.response(HTTPStatus.OK, schema.VideosStatisticsResponse)
|
|
async def statistics(request: Request):
|
|
ret = await lib.get_statistics(request.ctx.session)
|
|
return json_response(ret, HTTPStatus.OK)
|
|
|
|
|
|
@content_bp.post('/tag_info')
|
|
@openapi.definition(
|
|
description='Get data about tagging a Channel',
|
|
body=schema.ChannelTagInfoRequest,
|
|
)
|
|
@validate(schema.ChannelTagInfoRequest)
|
|
async def channel_tag_info(request: Request, body: schema.ChannelTagInfoRequest):
|
|
from wrolpi.tags import Tag
|
|
session = request.ctx.session
|
|
channel = Channel.find_by_id(session, body.channel_id) if body.channel_id else None
|
|
channel_name = channel.name if channel else None
|
|
channel_url = channel.url if channel else None
|
|
tag_name = Tag.find_by_name(session, body.tag_name).name if body.tag_name else None
|
|
videos_destination = format_videos_destination(channel_name, tag_name, channel_url)
|
|
ret = dict(videos_destination=videos_destination)
|
|
return json_response(ret)
|
|
|
|
|
|
@content_bp.get('/download-defaults')
|
|
@openapi.description('Get flattened global inheritable video download settings for form pre-fill.')
|
|
async def get_download_defaults(_: Request):
|
|
config = lib.get_videos_downloader_config()
|
|
defaults = lib._extract_global_inheritable(config)
|
|
return json_response(defaults)
|
|
|
|
|
|
@content_bp.post('/file_format')
|
|
@openapi.definition(
|
|
description='Preview the video file format',
|
|
body=schema.VideoFileFormatRequest,
|
|
)
|
|
@validate(schema.VideoFileFormatRequest)
|
|
async def post_file_format(_: Request, body: schema.VideoFileFormatRequest):
|
|
try:
|
|
preview = preview_filename(body.video_file_format)
|
|
return json_response(dict(
|
|
video_file_format=body.video_file_format,
|
|
preview=preview,
|
|
))
|
|
except RuntimeError as e:
|
|
return json_response(dict(
|
|
error=str(e),
|
|
video_file_format=body.video_file_format,
|
|
), status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@content_bp.post('/cookies')
|
|
@openapi.definition(
|
|
description='Upload and encrypt cookies for yt-dlp',
|
|
body=schema.CookiesUploadRequest,
|
|
)
|
|
@validate(schema.CookiesUploadRequest)
|
|
async def upload_cookies(_: Request, body: schema.CookiesUploadRequest):
|
|
"""Upload cookies content, encrypt with password, and save."""
|
|
try:
|
|
cookies.save_encrypted_cookies(body.cookies_content, body.password)
|
|
return json_response({'success': True}, HTTPStatus.CREATED)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@content_bp.get('/cookies/status')
|
|
@openapi.response(HTTPStatus.OK, schema.CookiesStatusResponse)
|
|
async def get_cookies_status(_: Request):
|
|
"""Get cookies status (exist? unlocked?)."""
|
|
status = cookies.get_cookies_status()
|
|
return json_response(status)
|
|
|
|
|
|
@content_bp.delete('/cookies')
|
|
async def delete_cookies(_: Request):
|
|
"""Delete stored encrypted cookies."""
|
|
deleted = cookies.delete_cookies()
|
|
if deleted:
|
|
return json_response({'success': True}, HTTPStatus.NO_CONTENT)
|
|
else:
|
|
return json_response({'error': 'No cookies file found'}, HTTPStatus.NOT_FOUND)
|
|
|
|
|
|
@content_bp.post('/cookies/unlock')
|
|
@openapi.definition(
|
|
description='Decrypt cookies to memory for session use',
|
|
body=schema.CookiesUnlockRequest,
|
|
)
|
|
@validate(schema.CookiesUnlockRequest)
|
|
async def unlock_cookies(_: Request, body: schema.CookiesUnlockRequest):
|
|
"""Unlock cookies by decrypting to memory."""
|
|
try:
|
|
cookies.unlock_cookies(body.password)
|
|
return json_response({'success': True})
|
|
except FileNotFoundError:
|
|
return json_response({'error': 'No cookies file found'}, HTTPStatus.NOT_FOUND)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@content_bp.post('/cookies/lock')
|
|
async def lock_cookies(_: Request):
|
|
"""Lock cookies by clearing from memory."""
|
|
cookies.lock_cookies()
|
|
return json_response({'success': True})
|
|
|
|
|
|
@content_bp.get('/suggested-user-agent')
|
|
async def get_suggested_user_agent(request: Request):
|
|
"""Return the user-agent of the browser making this request.
|
|
|
|
This is useful for pre-filling the user_agent field in Video Settings
|
|
with the browser's user-agent, which should match the browser used to
|
|
export cookies."""
|
|
user_agent = request.headers.get('User-Agent', '')
|
|
return json_response({'user_agent': user_agent})
|