2021-11-01 16:42:52 -06:00
|
|
|
import asyncio
|
2022-08-06 05:22:07 +00:00
|
|
|
import pathlib
|
2021-11-01 16:42:52 -06:00
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
|
|
from sanic import response, Blueprint
|
|
|
|
|
from sanic.request import Request
|
2022-01-13 09:29:58 -07:00
|
|
|
from sanic_ext import validate
|
|
|
|
|
from sanic_ext.extensions.openapi import openapi
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2022-01-15 13:23:57 -07:00
|
|
|
from wrolpi.common import logger, wrol_mode_check, make_media_directory, \
|
2022-04-08 09:57:04 -06:00
|
|
|
get_media_directory, run_after
|
2022-12-14 19:11:11 -07:00
|
|
|
from wrolpi.errors import UnknownChannel
|
2021-12-20 16:08:13 -07:00
|
|
|
from wrolpi.root_api import json_response
|
2022-01-13 09:29:58 -07:00
|
|
|
from wrolpi.schema import JSONErrorResponse
|
2022-04-08 09:57:04 -06:00
|
|
|
from wrolpi.vars import PYTEST
|
2022-02-03 12:39:14 -07:00
|
|
|
from . import lib
|
2022-08-06 05:22:07 +00:00
|
|
|
from .. import schema, Channel
|
2022-04-08 09:57:04 -06:00
|
|
|
from ..lib import save_channels_config
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
channel_bp = Blueprint('Channel', url_prefix='/api/videos/channels')
|
|
|
|
|
|
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@channel_bp.get('/')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Get a list of all Channels')
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelsResponse)
|
2021-11-01 16:42:52 -06:00
|
|
|
async def get_channels(_: Request):
|
2022-02-03 12:39:14 -07:00
|
|
|
channels = await lib.get_minimal_channels()
|
2021-11-01 16:42:52 -06:00
|
|
|
return json_response({'channels': channels})
|
|
|
|
|
|
|
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
@channel_bp.get('/<channel_id:int>')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Get a Channel')
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelResponse)
|
2022-04-08 09:57:04 -06:00
|
|
|
def channel_get(_: Request, channel_id: int = None):
|
2022-08-06 05:22:07 +00:00
|
|
|
channel = lib.get_channel(channel_id=channel_id, return_dict=True)
|
2021-11-01 16:42:52 -06:00
|
|
|
channel.pop('info_json')
|
|
|
|
|
return json_response({'channel': channel})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@channel_bp.post('/')
|
2022-02-03 11:36:26 -07:00
|
|
|
@openapi.definition(
|
|
|
|
|
description='Insert a Channel',
|
2022-02-03 12:39:14 -07:00
|
|
|
body=schema.ChannelPostRequest,
|
2022-02-03 11:36:26 -07:00
|
|
|
)
|
2022-02-03 12:39:14 -07:00
|
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelPostResponse)
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
2022-02-03 12:39:14 -07:00
|
|
|
@validate(schema.ChannelPostRequest)
|
2021-11-01 16:42:52 -06:00
|
|
|
@wrol_mode_check
|
2022-02-03 12:39:14 -07:00
|
|
|
def channel_post(_: Request, body: schema.ChannelPostRequest):
|
2022-01-31 21:45:46 -07:00
|
|
|
body.directory = get_media_directory() / body.directory
|
|
|
|
|
if not body.directory.is_dir() and body.mkdir:
|
|
|
|
|
make_media_directory(body.directory)
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
channel = lib.create_channel(data=body, return_dict=False)
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
# Refresh the videos asynchronously
|
2022-04-08 09:57:04 -06:00
|
|
|
if not PYTEST:
|
2022-08-06 05:22:07 +00:00
|
|
|
asyncio.ensure_future(channel.refresh_files())
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
return response.json({'success': 'Channel created successfully'}, HTTPStatus.CREATED,
|
2022-04-08 09:57:04 -06:00
|
|
|
{'Location': f'/api/videos/channels/{channel.id}'})
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
@channel_bp.put('/<channel_id:int>')
|
2022-02-03 11:36:26 -07:00
|
|
|
@openapi.definition(
|
|
|
|
|
description='Update a Channel',
|
2022-02-03 12:39:14 -07:00
|
|
|
body=schema.ChannelPutRequest,
|
2022-02-03 11:36:26 -07:00
|
|
|
)
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.response(HTTPStatus.NO_CONTENT)
|
|
|
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
2022-02-03 12:39:14 -07:00
|
|
|
@validate(schema.ChannelPutRequest)
|
2022-04-08 09:57:04 -06:00
|
|
|
@run_after(save_channels_config)
|
2021-11-01 16:42:52 -06:00
|
|
|
@wrol_mode_check
|
2022-04-08 09:57:04 -06:00
|
|
|
def channel_update(_: Request, channel_id: int, body: schema.ChannelPutRequest):
|
2022-08-06 05:22:07 +00:00
|
|
|
body.directory = pathlib.Path(body.directory) if body.directory else None
|
2022-04-08 09:57:04 -06:00
|
|
|
channel = lib.update_channel(data=body, channel_id=channel_id)
|
2021-11-01 16:42:52 -06:00
|
|
|
return response.raw('', HTTPStatus.NO_CONTENT,
|
2022-04-08 09:57:04 -06:00
|
|
|
headers={'Location': f'/api/videos/channels/{channel.id}'})
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
|
2022-04-08 09:57:04 -06:00
|
|
|
@channel_bp.delete('/<channel_id:int>')
|
2022-01-13 09:29:58 -07:00
|
|
|
@openapi.description('Delete a Channel')
|
|
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
2021-11-01 16:42:52 -06:00
|
|
|
@wrol_mode_check
|
2022-04-08 09:57:04 -06:00
|
|
|
def channel_delete(_: Request, channel_id: int):
|
|
|
|
|
lib.delete_channel(channel_id=channel_id)
|
2021-11-01 16:42:52 -06:00
|
|
|
return response.raw('', HTTPStatus.NO_CONTENT)
|
2022-08-06 05:22:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@channel_bp.post('/refresh/<channel_id:int>')
|
|
|
|
|
@openapi.description('Refresh all files in the Channel directory')
|
|
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
|
|
|
@wrol_mode_check
|
|
|
|
|
def channel_refresh(_: Request, channel_id: int):
|
|
|
|
|
channel: Channel = lib.get_channel(channel_id=channel_id, return_dict=False)
|
|
|
|
|
if not channel:
|
|
|
|
|
raise UnknownChannel()
|
|
|
|
|
|
|
|
|
|
asyncio.ensure_future(channel.refresh_files())
|
|
|
|
|
return response.empty()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@channel_bp.post('/download/<channel_id:int>')
|
|
|
|
|
@openapi.description('Download the latest catalog for a Channel, download any missing videos.')
|
|
|
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
|
|
|
@wrol_mode_check
|
|
|
|
|
def channel_download(_: Request, channel_id: int):
|
|
|
|
|
lib.download_channel(channel_id)
|
|
|
|
|
return response.empty()
|