Exemplo n.º 1
0
def _hook(hook_name):
    """ Endpoint for custom webhooks """

    event_args = {
        'hook': hook_name,
        'method': request.method,
        'args': dict(request.args or {}),
        'data': request.data.decode(),
    }

    if event_args['data']:
        # noinspection PyBroadException
        try:
            event_args['data'] = json.loads(event_args['data'])
        except:
            pass

    event = WebhookEvent(**event_args)

    try:
        send_message(event)
        return Response(json.dumps({
            'status': 'ok',
            **event_args
        }),
                        mimetype='application/json')
    except Exception as e:
        logger().exception(e)
        logger().error('Error while dispatching webhook event {}: {}'.format(
            event, str(e)))
        abort(500, str(e))
Exemplo n.º 2
0
def add_subtitles(media_id, req):
    """
    This route can be used to download and/or expose subtitles files
    associated to a media file
    """

    media_hndl = media_map.get(media_id)
    if not media_hndl:
        raise FileNotFoundError(
            '{} is not a registered media_id'.format(media_id))

    subfile = None
    if req.data:
        subfile = json.loads(req.data.decode('utf-8')).get('filename')
        if not subfile:
            raise AttributeError('No filename specified in the request')

    if not subfile:
        if not media_hndl.path:
            raise NotImplementedError(
                'Subtitles are currently only supported for local media files')

        req = {
            'type': 'request',
            'action': 'media.subtitles.get_subtitles',
            'args': {
                'resource': media_hndl.path,
            }
        }

        try:
            subtitles = send_message(req).output or []
        except Exception as e:
            raise RuntimeError('Could not get subtitles: {}'.format(str(e)))

        if not subtitles:
            raise FileNotFoundError(
                'No subtitles found for resource {}'.format(media_hndl.path))

        req = {
            'type': 'request',
            'action': 'media.subtitles.download',
            'args': {
                'link': subtitles[0].get('SubDownloadLink'),
                'media_resource': media_hndl.path,
                'convert_to_vtt': True,
            }
        }

        subfile = (send_message(req).output or {}).get('filename')

    media_hndl.set_subtitles(subfile)
    return {
        'filename': subfile,
        'url': get_remote_base_url() + '/media/subtitles/' + media_id + '.vtt',
    }
Exemplo n.º 3
0
def register_media(source, subtitles=None):
    global media_map, media_map_lock

    media_id = get_media_id(source)
    media_url = get_media_url(media_id)

    with media_map_lock:
        if media_id in media_map:
            return media_map[media_id]

    subfile = None
    if subtitles:
        req = {
            'type': 'request',
            'action': 'media.subtitles.download',
            'args': {
                'link': subtitles,
                'convert_to_vtt': True,
            }
        }

        try:
            subfile = (send_message(req).output or {}).get('filename')
        except Exception as e:
            logger().warning('Unable to load subtitle {}: {}'
                             .format(subtitles, str(e)))

    with media_map_lock:
        media_hndl = MediaHandler.build(source, url=media_url, subtitles=subfile)
        media_map[media_id] = media_hndl
        media_hndl.media_id = media_id

    logger().info('Streaming "{}" on {}'.format(source, media_url))
    return media_hndl
Exemplo n.º 4
0
def execute():
    """ Endpoint to execute commands """
    try:
        msg = json.loads(request.data.decode('utf-8'))
    except Exception as e:
        logger().error('Unable to parse JSON from request {}: {}'.format(request.data, str(e)))
        return abort(400, str(e))

    logger().info('Received message on the HTTP backend: {}'.format(msg))

    try:
        response = send_message(msg)
        return Response(str(response or {}), mimetype='application/json')
    except Exception as e:
        logger().error('Error while running HTTP action: {}. Request: {}'.format(str(e), msg))
        return abort(500, str(e))