2021-11-01 16:42:52 -06:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
2023-07-17 17:32:29 -06:00
|
|
|
from sanic import Blueprint
|
2021-11-01 16:42:52 -06:00
|
|
|
from sanic.request import Request
|
2024-08-15 19:32:01 -06:00
|
|
|
from sanic_ext import validate
|
2022-01-13 09:29:58 -07:00
|
|
|
from sanic_ext.extensions.openapi import openapi
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2024-04-17 11:25:26 -06:00
|
|
|
from wrolpi.api_utils import json_response
|
2021-11-01 16:42:52 -06:00
|
|
|
from wrolpi.common import logger
|
2022-02-16 21:57:27 -07:00
|
|
|
from . import lib, schema
|
2026-02-20 14:37:58 -07:00
|
|
|
from . import cookies
|
2021-11-01 16:42:52 -06:00
|
|
|
from .channel.api import channel_bp
|
2024-11-29 17:01:16 -07:00
|
|
|
from .downloader import preview_filename
|
2024-08-15 19:32:01 -06:00
|
|
|
from .lib import format_videos_destination
|
|
|
|
|
from .models import Channel
|
2021-11-01 16:42:52 -06:00
|
|
|
from .video.api import video_bp
|
|
|
|
|
|
2021-12-20 16:08:13 -07:00
|
|
|
content_bp = Blueprint('VideoContent', '/api/videos')
|
2024-04-17 11:25:26 -06:00
|
|
|
videos_bp = Blueprint('Videos', '/api/videos').group(
|
2021-11-01 16:42:52 -06:00
|
|
|
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')
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.VideosStatisticsResponse)
|
db: request-path library functions use the request's connection (#774)
* 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>
2026-10-04 11:05:20 -06:00
|
|
|
async def statistics(request: Request):
|
|
|
|
|
ret = await lib.get_statistics(request.ctx.session)
|
2021-11-01 16:42:52 -06:00
|
|
|
return json_response(ret, HTTPStatus.OK)
|
2024-08-15 19:32:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@content_bp.post('/tag_info')
|
|
|
|
|
@openapi.definition(
|
|
|
|
|
description='Get data about tagging a Channel',
|
|
|
|
|
body=schema.ChannelTagInfoRequest,
|
|
|
|
|
)
|
|
|
|
|
@validate(schema.ChannelTagInfoRequest)
|
2025-12-10 21:16:10 -07:00
|
|
|
async def channel_tag_info(request: Request, body: schema.ChannelTagInfoRequest):
|
2024-08-15 19:32:01 -06:00
|
|
|
from wrolpi.tags import Tag
|
2025-12-10 21:16:10 -07:00
|
|
|
session = request.ctx.session
|
|
|
|
|
channel = Channel.find_by_id(session, body.channel_id) if body.channel_id else None
|
2024-08-15 19:32:01 -06:00
|
|
|
channel_name = channel.name if channel else None
|
|
|
|
|
channel_url = channel.url if channel else None
|
2025-12-10 21:16:10 -07:00
|
|
|
tag_name = Tag.find_by_name(session, body.tag_name).name if body.tag_name else None
|
2024-08-15 19:32:01 -06:00
|
|
|
videos_destination = format_videos_destination(channel_name, tag_name, channel_url)
|
|
|
|
|
ret = dict(videos_destination=videos_destination)
|
|
|
|
|
return json_response(ret)
|
2024-11-29 17:01:16 -07:00
|
|
|
|
|
|
|
|
|
2026-03-05 16:18:03 -07:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
2024-11-29 17:01:16 -07:00
|
|
|
@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)
|
2025-04-17 17:02:39 -06:00
|
|
|
|
|
|
|
|
|
2026-02-20 14:37:58 -07:00
|
|
|
@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})
|