wrolpi/modules/videos/video/api.py
Roland aa203cc102
Moving non-video functions to wrolpi.common. Handling Channel/Downlaod relationship in Channel.update.
Removing default download freqeuncy so channels will not be downloaded without a url and frequency.

Adding channel unique url constraint.

Handling when a Channel's directory may still be relative in DB.

Adding TODOs to remove old favorites.

Removing outdated favorites from config after favorites have been created.

Testing that old-style favorited videos in the config are imported.

Removing channels_by_link.

Using list to store channels in config.

Moving channel import function into lib.

Supporting old channel import with links.

Bumping version for channel.link removal.

Removing all reference to Channel.link in app and api.

Adding relative property to MediaPathType.

Testing that a channel can be downloaded after it has a frequency added.

All tests pass.

Video and Channel tests pass without channel.link.

Removing channel.link usage in channel api.

Changing video Statistics page to About.
2022-04-29 19:09:25 -06:00

81 lines
2.8 KiB
Python

from http import HTTPStatus
from sanic import response, Blueprint
from sanic.request import Request
from sanic_ext import validate
from sanic_ext.extensions.openapi import openapi
from wrolpi.common import logger, wrol_mode_check, run_after, get_media_directory, \
get_relative_to_media_directory
from wrolpi.db import get_db_session
from wrolpi.errors import InvalidOrderBy
from wrolpi.root_api import json_response
from wrolpi.schema import JSONErrorResponse
from . import lib
from .. import common, 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({'video': 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(_: Request, body: schema.VideoSearchRequest):
if body.order_by not in lib.VIDEO_ORDERS:
raise InvalidOrderBy('Invalid order by')
videos, videos_total = lib.video_search(
body.search_str,
body.offset,
body.limit,
body.channel_id,
body.order_by,
body.filters,
)
ret = {'videos': list(videos), 'totals': {'videos': videos_total}}
return json_response(ret)
@video_bp.post('/directories')
@openapi.definition(
summary='Get all directories that match the search_str, prefixed by the media directory.',
body=schema.DirectoriesRequest,
)
@openapi.response(HTTPStatus.OK, schema.DirectoriesResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(schema.DirectoriesRequest)
def directories(_, body: schema.DirectoriesRequest):
search_str = str(get_media_directory() / (body.search_str or ''))
dirs = common.get_matching_directories(search_str)
dirs = [str(get_relative_to_media_directory(i)) for i in dirs]
return response.json({'directories': dirs})
@video_bp.delete('/video/<video_id:int>')
@openapi.description('Delete a video.')
@openapi.response(HTTPStatus.NO_CONTENT)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@wrol_mode_check
@run_after(lib.save_channels_config)
def video_delete(_: Request, video_id: int):
with get_db_session(commit=True) as session:
video = lib.get_video(session, video_id)
video.delete()
return response.raw('', HTTPStatus.NO_CONTENT)