code:
import collections
import contextlib
import itertools
import json
import math
import operator
import re

from .utils import (
    NO_DEFAULT,
    ExtractorError,
    js_to_json,
    remove_quotes,
    truncate_string,
    unified_timestamp,
    write_string,
)

def _js_bit_op(op):
    def zeroise(x):
        return 0 if x in (None, JS_Undefined) else x

    def wrapped(a, b):
        return op(zeroise(a), zeroise(b)) & 0xffffffff

    return wrapped

def _js_arith_op(op):

    def wrapped(a, b):
        if JS_Undefined in (a, b):
            return float('nan')
        return op(a or 0, b or 0)

    return wrapped

def _js_div(a, b):
    if JS_Undefined in (a, b) or not (a and b):
        return float('nan')
    return (a or 0) / b if b else float('inf')

def _js_mod(a, b):
    if JS_Undefined in (a, b) or not b:
        return float('nan')
    return (a or 0) % b

def _js_exp(a, b):
    if not b:
        return 1  # even 0 ** 0 !!
    elif JS_Undefined in (a, b):
        return float('nan')
    return (a or 0) ** b

def _js_eq_op(op):

    def wrapped(a, b):
        if {a, b} <= {None, JS_Undefined}:
            return op(a, a)
        return op(a, b)

    return wrapped

def _js_comp_op(op):

    def wrapped(a, b):
        if JS_Undefined in (a, b):
            return False
        if isinstance(a, str) or isinstance(b, str):
            return op(str(a or 0), str(b or 0))
        return op(a or 0, b or 0)

    return wrapped

def _js_ternary(cndn, if_true=True, if_false=False):
    """Simulate JS's ternary operator (cndn?if_true:if_false)"""
    if cndn in (False, None, 0, '', JS_Undefined):
        return if_false
    with contextlib.suppress(TypeError):
        if math.isnan(cndn):  # NB: NaN cannot be checked by membership
            return if_false
    return if_true

# Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
_OPERATORS = {  # None => Defined in JSInterpreter._operator
    '?': None,
    '??': None,
    '||': None,
    '&&': None,

    '|': _js_bit_op(operator.or_),
    '^': _js_bit_op(operator.xor),
    '&': _js_bit_op(operator.and_),

    '===': operator.is_,
    '!==': operator.is_not,
    '==': _js_eq_op(operator.eq),
    '!=': _js_eq_op(operator.ne),

    '<=': _js_comp_op(operator.le),
    '>=': _js_comp_op(operator.ge),
    '<': _js_comp_op(operator.lt),
    '>': _js_comp_op(operator.gt),

    '>>': _js_bit_op(operator.rshift),
    '<<': _js_bit_op(operator.lshift),

    '+': _js_arith_op(operator.add),
    '-': _js_arith_op(operator.sub),

    '*': _js_arith_op(operator.mul),
    '%': _js_mod,
    '/': _js_div,
    '**': _js_exp,
}

_COMP_OPERATORS = {'===', '!==', '==', '!=', '<=', '>=', '<', '>'}

_NAME_RE = r'[a-zA-Z_$][\w$]*'
_MATCHING_PARENS = dict(zip(*zip('()', '{}', '[]')))
_QUOTES = '\'"/'

class JS_Undefined:
    pass

class JS_Break(ExtractorError):
    def __init__(self):
        ExtractorError.__init__(self, 'Invalid break')

class JS_Continue(ExtractorError):
    def __init__(self):
        ExtractorError.__init__(self, 'Invalid continue')

class JS_Throw(ExtractorError):
    def __init__(self, e):
        self.error = e
        ExtractorError.__init__(self, f'Uncaught exception {e}')

class LocalNameSpace(collections.ChainMap):
    def __setitem__(self, key, value):
        for scope in self.maps:
            if key in scope:
                scope[key] = value
                return
        self.maps[0][key] = value

    def __delitem__(self, key):
        raise NotImplementedError('Deleting is not supported')

class Debugger:
    import sys
    ENABLED = False and 'pytest' in sys.modules

    @staticmethod
    def write(*args, level=100):
        write_string(f'[debug] JS: {"  " * (100 - level)}'
                     f'{" ".join(truncate_string(str(x), 50, 50) for x in args)}\n')

    @classmethod
    def wrap_interpreter(cls, f):
        def interpret_statement(self, stmt, local_vars, allow_recursion, *args, **kwargs):
            if cls.ENABLED and stmt.strip():
                cls.write(stmt, level=allow_recursion)
            try:
                ret, should_ret = f(self, stmt, local_vars, allow_recursion, *args, **kwargs)
            except Exception as e:
                if cls.ENABLED:
                    if isinstance(e, ExtractorError):
                        e = e.orig_msg
                    cls.write('=> Raises:', e, '<-|', stmt, level=allow_recursion)
                raise
            if cls.ENABLED and stmt.strip():
                cls.write(['->', '=>'][should_ret], repr(ret), '<-|', stmt, level=allow_recursion)
            return ret, should_ret
        return interpret_statement

class JSInterpreter:
    __named_object_counter = 0

    _RE_FLAGS = {
        # special knowledge: Python's re flags are bitmask values, current max 128
        # invent new bitmask values well above that for literal parsing
        # TODO: new pattern class to execute matches with these flags
        'd': 1024,  # Generate indices for substring matches
        'g': 2048,  # Global search
        'i': re.I,  # Case-insensitive search
        'm': re.M,  # Multi-line search
        's': re.S,  # Allows . to match newline characters
        'u': re.U,  # Treat a pattern as a sequence of unicode code points
        'y': 4096,  # Perform a "sticky" search that matches starting at the current position in the target string
    }

    _EXC_NAME = '__yt_dlp_exception__'

    def __init__(self, code, objects=None):
        self.code, self._functions = code, {}
        self._objects = {} if objects is None else objects

    class Exception(ExtractorError):
        def __init__(self, msg, expr=None, *args, **kwargs):
            if expr is not None:
                msg = f'{msg.rstrip()} in: {truncate_string(expr, 50, 50)}'
            super().__init__(msg, *args, **kwargs)

    def _named_object(self, namespace, obj):
        self.__named_object_counter += 1
        name = f'__yt_dlp_jsinterp_obj{self.__named_object_counter}'
        namespace[name] = obj
        return name

    @classmethod
    def _regex_flags(cls, expr):
        flags = 0
        if not expr:
            return flags, expr
        for idx, ch in enumerate(expr):
            if ch not in cls._RE_FLAGS:
                break
            flags |= cls._RE_FLAGS[ch]
        return flags, expr[idx + 1:]

    @staticmethod
    def _separate(expr, delim=',', max_split=None):
        OP_CHARS = '+-*/%&|^=<>!,;{}:'
        if not expr:
            return
        counters = {k: 0 for k in _MATCHING_PARENS.values()}
        start, splits, pos, delim_len = 0, 0, 0, len(delim) - 1
        in_quote, escaping, after_op, in_regex_char_group = None, False, True, False
        for idx, char in enumerate(expr):
            if not in_quote and char in _MATCHING_PARENS:
                counters[_MATCHING_PARENS[char]] += 1
            elif not in_quote and char in counters:
                counters[char] -= 1
            elif not escaping:
                if char in _QUOTES and in_quote in (char, None):
                    if in_quote or after_op or char != '/':
                        in_quote = None if in_quote and not in_regex_char_group else char
                elif in_quote == '/' and char in '[]':
                    in_regex_char_group = char == '['
            escaping = not escaping and in_quote and char == '\\'
            after_op = not in_quote and char in OP_CHARS or (char.isspace() and after_op)

            if char != delim[pos] or any(counters.values()) or in_quote:
                pos = 0
                continue
            elif pos != delim_len:
                pos += 1
                continue
            yield expr[start: idx - delim_len]
            start, pos = idx + 1, 0
            splits += 1
            if max_split and splits >= max_split:
                break
        yield expr[start:]

    @classmethod
    def _separate_at_paren(cls, expr, delim=None):
        if delim is None:
            delim = expr and _MATCHING_PARENS[expr[0]]
        separated = list(cls._separate(expr, delim, 1))
        if len(separated) < 2:
            raise cls.Exception(f'No terminating paren {delim}', expr)
        return separated[0][1:].strip(), separated[1].strip()

    def _operator(self, op, left_val, right_expr, expr, local_vars, allow_recursion):
        if op in ('||', '&&'):
            if (op == '&&') ^ _js_ternary(left_val):
                return left_val  # short circuiting
        elif op == '??':
            if left_val not in (None, JS_Undefined):
                return left_val
        elif op == '?':
            right_expr = _js_ternary(left_val, *self._separate(right_expr, ':', 1))

        right_val = self.interpret_expression(right_expr, local_vars, allow_recursion)
        if not _OPERATORS.get(op):
            return right_val

        try:
            return _OPERATORS[op](left_val, right_val)
        except Exception as e:
            raise self.Exception(f'Failed to evaluate {left_val!r} {op} {right_val!r}', expr, cause=e)

    def _index(self, obj, idx, allow_undefined=False):
        if idx == 'length':
            return len(obj)
        try:
            return obj[int(idx)] if isinstance(obj, list) else obj[idx]
        except Exception as e:
            if allow_undefined:
                return JS_Undefined
            raise self.Exception(f'Cannot get index {idx}', repr(obj), cause=e)

    def _dump(self, obj, namespace):
        try:
            return json.dumps(obj)
        except TypeError:
            return self._named_object(namespace, obj)

    @Debugger.wrap_interpreter
    def interpret_statement(self, stmt, local_vars, allow_recursion=100):
        if allow_recursion < 0:
            raise self.Exception('Recursion limit reached')
        allow_recursion -= 1

        should_return = False
        sub_statements = list(self._separate(stmt, ';')) or ['']
        expr = stmt = sub_statements.pop().strip()

        for sub_stmt in sub_statements:
            ret, should_return = self.interpret_statement(sub_stmt, local_vars, allow_recursion)
            if should_return:
                return ret, should_return

        m = re.match(r'(?P<var>(?:var|const|let)\s)|return(?:\s+|(?=["\'])|$)|(?P<throw>throw\s+)', stmt)
        if m:
            expr = stmt[len(m.group(0)):].strip()
            if m.group('throw'):
                raise JS_Throw(self.interpret_expression(expr, local_vars, allow_recursion))
            should_return = not m.group('var')
        if not expr:
            return None, should_return

        if expr[0] in _QUOTES:
            inner, outer = self._separate(expr, expr[0], 1)
            if expr[0] == '/':
                flags, outer = self._regex_flags(outer)
                inner = re.compile(inner[1:], flags=flags)
            else:
                inner = json.loads(js_to_json(f'{inner}{expr[0]}', strict=True))
            if not outer:
                return inner, should_return
            expr = self._named_object(local_vars, inner) + outer

        if expr.startswith('new '):
            obj = expr[4:]
            if obj.startswith('Date('):
                left, right = self._separate_at_paren(obj[4:])
                expr = unified_timestamp(
                    self.interpret_expression(left, local_vars, allow_recursion), False)
                if not expr:
                    raise self.Exception(f'Failed to parse date {left!r}', expr)
                expr = self._dump(int(expr * 1000), local_vars) + right
            else:
                raise self.Exception(f'Unsupported object {obj}', expr)

        if expr.startswith('void '):
            left = self.interpret_expression(expr[5:], local_vars, allow_recursion)
            return None, should_return

        if expr.startswith('{'):
            inner, outer = self._separate_at_paren(expr)
            # try for object expression (Map)
            sub_expressions = [list(self._separate(sub_expr.strip(), ':', 1)) for sub_expr in self._separate(inner)]
            if all(len(sub_expr) == 2 for sub_expr in sub_expressions):
                def dict_item(key, val):
                    val = self.interpret_expression(val, local_vars, allow_recursion)
                    if re.match(_NAME_RE, key):
                        return key, val
                    return self.interpret_expression(key, local_vars, allow_recursion), val

                return dict(dict_item(k, v) for k, v in sub_expressions), should_return

            inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
            if not outer or should_abort:
                return inner, should_abort or should_return
            else:
                expr = self._dump(inner, local_vars) + outer

        if expr.startswith('('):
            inner, outer = self._separate_at_paren(expr)
            inner, should_abort = self.interpret_statement(inner, local_vars, allow_recursion)
            if not outer or should_abort:
                return inner, should_abort or should_return
            else:
                expr = self._dump(inner, local_vars) + outer

        if expr.startswith('['):
            inner, outer = self._separate_at_paren(expr)
            name = self._named_object(local_vars, [
                self.interpret_expression(item, local_vars, allow_recursion)
                for item in self._separate(inner)])
            expr = name + outer

        m = re.match(r'''(?x)
                (?P<try>try)\s*\{|
                (?P<switch>switch)\s*\(|
                (?P<for>for)\s*\(
                ''', expr)
        md = m.groupdict() if m else {}
        if md.get('try'):
            try_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
            err = None
            try:
                ret, should_abort = self.interpret_statement(try_expr, local_vars, allow_recursion)
                if should_abort:
                    return ret, True
            except Exception as e:
                # XXX: This works for now, but makes debugging future issues very hard
                err = e

            pending = (None, False)
            m = re.match(r'catch\s*(?P<err>\(\s*{_NAME_RE}\s*\))?\{{'.format(**globals()), expr)
            if m:
                sub_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
                if err:
                    catch_vars = {}
                    if m.group('err'):
                        catch_vars[m.group('err')] = err.error if isinstance(err, JS_Throw) else err
                    catch_vars = local_vars.new_child(catch_vars)
                    err, pending = None, self.interpret_statement(sub_expr, catch_vars, allow_recursion)

            m = re.match(r'finally\s*\{', expr)
            if m:
                sub_expr, expr = self._separate_at_paren(expr[m.end() - 1:])
                ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
                if should_abort:
                    return ret, True

            ret, should_abort = pending
            if should_abort:
                return ret, True

            if err:
                raise err

        elif md.get('for'):
            constructor, remaining = self._separate_at_paren(expr[m.end() - 1:])
            if remaining.startswith('{'):
                body, expr = self._separate_at_paren(remaining)
            else:
                switch_m = re.match(r'switch\s*\(', remaining)  # FIXME
                if switch_m:
                    switch_val, remaining = self._separate_at_paren(remaining[switch_m.end() - 1:])
                    body, expr = self._separate_at_paren(remaining, '}')
                    body = 'switch(%s){%s}' % (switch_val, body)
                else:
                    body, expr = remaining, ''
            start, cndn, increment = self._separate(constructor, ';')
            self.interpret_expression(start, local_vars, allow_recursion)
            while True:
                if not _js_ternary(self.interpret_expression(cndn, local_vars, allow_recursion)):
                    break
                try:
                    ret, should_abort = self.interpret_statement(body, local_vars, allow_recursion)
                    if should_abort:
                        return ret, True
                except JS_Break:
                    break
                except JS_Continue:
                    pass
                self.interpret_expression(increment, local_vars, allow_recursion)

        elif md.get('switch'):
            switch_val, remaining = self._separate_at_paren(expr[m.end() - 1:])
            switch_val = self.interpret_expression(switch_val, local_vars, allow_recursion)
            body, expr = self._separate_at_paren(remaining, '}')
            items = body.replace('default:', 'case default:').split('case ')[1:]
            for default in (False, True):
                matched = False
                for item in items:
                    case, stmt = (i.strip() for i in self._separate(item, ':', 1))
                    if default:
                        matched = matched or case == 'default'
                    elif not matched:
                        matched = (case != 'default'
                                   and switch_val == self.interpret_expression(case, local_vars, allow_recursion))
                    if not matched:
                        continue
                    try:
                        ret, should_abort = self.interpret_statement(stmt, local_vars, allow_recursion)
                        if should_abort:
                            return ret
                    except JS_Break:
                        break
                if matched:
                    break

        if md:
            ret, should_abort = self.interpret_statement(expr, local_vars, allow_recursion)
            return ret, should_abort or should_return

        # Comma separated statements
        sub_expressions = list(self._separate(expr))
        if len(sub_expressions) > 1:
            for sub_expr in sub_expressions:
                ret, should_abort = self.interpret_statement(sub_expr, local_vars, allow_recursion)
                if should_abort:
                    return ret, True
            return ret, False

        for m in re.finditer(rf'''(?x)
                (?P<pre_sign>\+\+|--)(?P<var1>{_NAME_RE})|
                (?P<var2>{_NAME_RE})(?P<post_sign>\+\+|--)''', expr):
            var = m.group('var1') or m.group('var2')
            start, end = m.span()
            sign = m.group('pre_sign') or m.group('post_sign')
            ret = local_vars[var]
            local_vars[var] += 1 if sign[0] == '+' else -1
            if m.group('pre_sign'):
                ret = local_vars[var]
            expr = expr[:start] + self._dump(ret, local_vars) + expr[end:]

        if not expr:
            return None, should_return

        m = re.match(fr'''(?x)
            (?P<assign>
                (?P<out>{_NAME_RE})(?:\[(?P<index>[^\]]+?)\])?\s*
                (?P<op>{"|".join(map(re.escape, set(_OPERATORS) - _COMP_OPERATORS))})?
                =(?!=)(?P<expr>.*)$
            )|(?P<return>
                (?!if|return|true|false|null|undefined|NaN)(?P<name>{_NAME_RE})$
            )|(?P<indexing>
                (?P<in>{_NAME_RE})\[(?P<idx>.+)\]$
            )|(?P<attribute>
                (?P<var>{_NAME_RE})(?:(?P<nullish>\?)?\.(?P<member>[^(]+)|\[(?P<member2>[^\]]+)\])\s*
            )|(?P<function>
                (?P<fname>{_NAME_RE})\((?P<args>.*)\)$
            )''', expr)
        if m and m.group('assign'):
            left_val = local_vars.get(m.group('out'))

            if not m.group('index'):
                local_vars[m.group('out')] = self._operator(
                    m.group('op'), left_val, m.group('expr'), expr, local_vars, allow_recursion)
                return local_vars[m.group('out')], should_return
            elif left_val in (None, JS_Undefined):
                raise self.Exception(f'Cannot index undefined variable {m.group("out")}', expr)

            idx = self.interpret_expression(m.group('index'), local_vars, allow_recursion)
            if not isinstance(idx, (int, float)):
                raise self.Exception(f'List index {idx} must be integer', expr)
            idx = int(idx)
            left_val[idx] = self._operator(
                m.group('op'), self._index(left_val, idx), m.group('expr'), expr, local_vars, allow_recursion)
            return left_val[idx], should_return

        elif expr.isdigit():
            return int(expr), should_return

        elif expr == 'break':
            raise JS_Break()
        elif expr == 'continue':
            raise JS_Continue()
        elif expr == 'undefined':
            return JS_Undefined, should_return
        elif expr == 'NaN':
            return float('NaN'), should_return

        elif m and m.group('return'):
            return local_vars.get(m.group('name'), JS_Undefined), should_return

        with contextlib.suppress(ValueError):
            return json.loads(js_to_json(expr, strict=True)), should_return

        if m and m.group('indexing'):
            val = local_vars[m.group('in')]
            idx = self.interpret_expression(m.group('idx'), local_vars, allow_recursion)
            return self._index(val, idx), should_return

        for op in _OPERATORS:
            separated = list(self._separate(expr, op))
            right_expr = separated.pop()
            while True:
                if op in '?<>*-' and len(separated) > 1 and not separated[-1].strip():
                    separated.pop()
                elif not (separated and op == '?' and right_expr.startswith('.')):
                    break
                right_expr = f'{op}{right_expr}'
                if op != '-':
                    right_expr = f'{separated.pop()}{op}{right_expr}'
            if not separated:
                continue
            left_val = self.interpret_expression(op.join(separated), local_vars, allow_recursion)
            return self._operator(op, left_val, right_expr, expr, local_vars, allow_recursion), should_return

        if m and m.group('attribute'):
            variable, member, nullish = m.group('var', 'member', 'nullish')
            if not member:
                member = self.interpret_expression(m.group('member2'), local_vars, allow_recursion)
            arg_str = expr[m.end():]
            if arg_str.startswith('('):
                arg_str, remaining = self._separate_at_paren(arg_str)
            else:
                arg_str, remaining = None, arg_str

            def assertion(cndn, msg):
                """ assert, but without risk of getting optimized out """
                if not cndn:
                    raise self.Exception(f'{member} {msg}', expr)

            def eval_method():
                if (variable, member) == ('console', 'debug'):
                    if Debugger.ENABLED:
                        Debugger.write(self.interpret_expression(f'[{arg_str}]', local_vars, allow_recursion))
                    return

                types = {
                    'String': str,
                    'Math': float,
                }
                obj = local_vars.get(variable, types.get(variable, NO_DEFAULT))
                if obj is NO_DEFAULT:
                    if variable not in self._objects:
                        try:
                            self._objects[variable] = self.extract_object(variable)
                        except self.Exception:
                            if not nullish:
                                raise
                    obj = self._objects.get(variable, JS_Undefined)

                if nullish and obj is JS_Undefined:
                    return JS_Undefined

                # Member access
                if arg_str is None:
                    return self._index(obj, member, nullish)

                # Function call
                argvals = [
                    self.interpret_expression(v, local_vars, allow_recursion)
                    for v in self._separate(arg_str)]

                if obj == str:
                    if member == 'fromCharCode':
                        assertion(argvals, 'takes one or more arguments')
                        return ''.join(map(chr, argvals))
                    raise self.Exception(f'Unsupported String method {member}', expr)
                elif obj == float:
                    if member == 'pow':
                        assertion(len(argvals) == 2, 'takes two arguments')
                        return argvals[0] ** argvals[1]
                    raise self.Exception(f'Unsupported Math method {member}', expr)

                if member == 'split':
                    assertion(argvals, 'takes one or more arguments')
                    assertion(len(argvals) == 1, 'with limit argument is not implemented')
                    return obj.split(argvals[0]) if argvals[0] else list(obj)
                elif member == 'join':
                    assertion(isinstance(obj, list), 'must be applied on a list')
                    assertion(len(argvals) == 1, 'takes exactly one argument')
                    return argvals[0].join(obj)
                elif member == 'reverse':
                    assertion(not argvals, 'does not take any arguments')
                    obj.reverse()
                    return obj
                elif member == 'slice':
                    assertion(isinstance(obj, list), 'must be applied on a list')
                    assertion(len(argvals) == 1, 'takes exactly one argument')
                    return obj[argvals[0]:]
                elif member == 'splice':
                    assertion(isinstance(obj, list), 'must be applied on a list')
                    assertion(argvals, 'takes one or more arguments')
                    index, howMany = map(int, (argvals + [len(obj)])[:2])
                    if index < 0:
                        index += len(obj)
                    add_items = argvals[2:]
                    res = []
                    for i in range(index, min(index + howMany, len(obj))):
                        res.append(obj.pop(index))
                    for i, item in enumerate(add_items):
                        obj.insert(index + i, item)
                    return res
                elif member == 'unshift':
                    assertion(isinstance(obj, list), 'must be applied on a list')
                    assertion(argvals, 'takes one or more arguments')
                    for item in reversed(argvals):
                        obj.insert(0, item)
                    return obj
                elif member == 'pop':
                    assertion(isinstance(obj, list), 'must be applied on a list')
                    assertion(not argvals, 'does not take any arguments')
                    if not obj:
                        return
                    return obj.pop()
                elif member == 'push':
                    assertion(argvals, 'takes one or more arguments')
                    obj.extend(argvals)
                    return obj
                elif member == 'forEach':
                    assertion(argvals, 'takes one or more arguments')
                    assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
                    f, this = (argvals + [''])[:2]
                    return [f((item, idx, obj), {'this': this}, allow_recursion) for idx, item in enumerate(obj)]
                elif member == 'indexOf':
                    assertion(argvals, 'takes one or more arguments')
                    assertion(len(argvals) <= 2, 'takes at-most 2 arguments')
                    idx, start = (argvals + [0])[:2]
                    try:
                        return obj.index(idx, start)
                    except ValueError:
                        return -1
                elif member == 'charCodeAt':
                    assertion(isinstance(obj, str), 'must be applied on a string')
                    assertion(len(argvals) == 1, 'takes exactly one argument')
                    idx = argvals[0] if isinstance(argvals[0], int) else 0
                    if idx >= len(obj):
                        return None
                    return ord(obj[idx])

                idx = int(member) if isinstance(obj, list) else member
                return obj[idx](argvals, allow_recursion=allow_recursion)

            if remaining:
                ret, should_abort = self.interpret_statement(
                    self._named_object(local_vars, eval_method()) + remaining,
                    local_vars, allow_recursion)
                return ret, should_return or should_abort
            else:
                return eval_method(), should_return

        elif m and m.group('function'):
            fname = m.group('fname')
            argvals = [self.interpret_expression(v, local_vars, allow_recursion)
                       for v in self._separate(m.group('args'))]
            if fname in local_vars:
                return local_vars[fname](argvals, allow_recursion=allow_recursion), should_return
            elif fname not in self._functions:
                self._functions[fname] = self.extract_function(fname)
            return self._functions[fname](argvals, allow_recursion=allow_recursion), should_return

        raise self.Exception(
            f'Unsupported JS expression {truncate_string(expr, 20, 20) if expr != stmt else ""}', stmt)

    def interpret_expression(self, expr, local_vars, allow_recursion):
        ret, should_return = self.interpret_statement(expr, local_vars, allow_recursion)
        if should_return:
            raise self.Exception('Cannot return from an expression', expr)
        return ret

    def extract_object(self, objname):
        _FUNC_NAME_RE = r'''(?:[a-zA-Z$0-9]+|"[a-zA-Z$0-9]+"|'[a-zA-Z$0-9]+')'''
        obj = {}
        obj_m = re.search(
            r'''(?x)
                (?<!this\.)%s\s*=\s*{\s*
                    (?P<fields>(%s\s*:\s*function\s*\(.*?\)\s*{.*?}(?:,\s*)?)*)
                }\s*;
            ''' % (re.escape(objname), _FUNC_NAME_RE),
            self.code)
        if not obj_m:
            raise self.Exception(f'Could not find object {objname}')
        fields = obj_m.group('fields')
        # Currently, it only supports function definitions
        fields_m = re.finditer(
            r'''(?x)
                (?P<key>%s)\s*:\s*function\s*\((?P<args>(?:%s|,)*)\){(?P<code>[^}]+)}
            ''' % (_FUNC_NAME_RE, _NAME_RE),
            fields)
        for f in fields_m:
            argnames = f.group('args').split(',')
            obj[remove_quotes(f.group('key'))] = self.build_function(argnames, f.group('code'))

        return obj

    def extract_function_code(self, funcname):
        """ @returns argnames, code """
        func_m = re.search(
            r'''(?xs)
                (?:
                    function\s+%(name)s|
                    [{;,]\s*%(name)s\s*=\s*function|
                    (?:var|const|let)\s+%(name)s\s*=\s*function
                )\s*
                \((?P<args>[^)]*)\)\s*
                (?P<code>{.+})''' % {'name': re.escape(funcname)},
            self.code)
        code, _ = self._separate_at_paren(func_m.group('code'))
        if func_m is None:
            raise self.Exception(f'Could not find JS function "{funcname}"')
        return [x.strip() for x in func_m.group('args').split(',')], code

    def extract_function(self, funcname):
        return self.extract_function_from_code(*self.extract_function_code(funcname))

    def extract_function_from_code(self, argnames, code, *global_stack):
        local_vars = {}
        while True:
            mobj = re.search(r'function\((?P<args>[^)]*)\)\s*{', code)
            if mobj is None:
                break
            start, body_start = mobj.span()
            body, remaining = self._separate_at_paren(code[body_start - 1:])
            name = self._named_object(local_vars, self.extract_function_from_code(
                [x.strip() for x in mobj.group('args').split(',')],
                body, local_vars, *global_stack))
            code = code[:start] + name + remaining
        return self.build_function(argnames, code, local_vars, *global_stack)

    def call_function(self, funcname, *args):
        return self.extract_function(funcname)(args)

    def build_function(self, argnames, code, *global_stack):
        global_stack = list(global_stack) or [{}]
        argnames = tuple(argnames)

        def resf(args, kwargs={}, allow_recursion=100):
            global_stack[0].update(itertools.zip_longest(argnames, args, fillvalue=None))
            global_stack[0].update(kwargs)
            var_stack = LocalNameSpace(*global_stack)
            ret, should_abort = self.interpret_statement(code.replace('\n', ' '), var_stack, allow_recursion - 1)
            if should_abort:
                return ret
        return resf


code:
# coding: utf-8
from __future__ import unicode_literals

import re

from typing import Union
from os import PathLike, fsdecode, remove, rename, sep, stat, utime, unlink, makedirs, replace
from os.path import exists, isfile, getsize, normpath, basename, dirname, isabs

from .utils import (
    sanitize_open,
    get_filesystem_encoding,
)

# this file is to escape long file names in following manner:
# 1. split each path segment in 255-N bytes (N=byte length of DEFAULT_DELIMITER below)
# 2. append DEFAULT_DELIMITER, so path segments are split within filesystem limit,
#      with a marker on each split chunks

FS_LENGTH_LIMIT = 255  # length limit from filesystem
DEFAULT_DELIMITER = "~~"

# http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/dc4322602480/src/share/classes/java/lang/Character.java
# Constants from JDK
MIN_HIGH_SURROGATE = 0xD800
MAX_HIGH_SURROGATE = 0xDBFF
MIN_LOW_SURROGATE = 0xDC00
MAX_LOW_SURROGATE = 0xDFFF

def split_longname(input: Union[bytes, str, PathLike], encoding: str = get_filesystem_encoding()) -> bytes:
    if PathLike and isinstance(input, PathLike):
        input = fsdecode(input)

    was_bytes = isinstance(input, bytes)
    if was_bytes:
        input = input.decode(encoding)

    result = split_longname_str(input, encoding)

    if was_bytes:
        result = result.encode(encoding)
    return result

def combine_longname(input: Union[bytes, str, PathLike], encoding: str = get_filesystem_encoding()) -> bytes:
    if PathLike and isinstance(input, PathLike):
        input = fsdecode(input)

    was_bytes = isinstance(input, bytes)
    if was_bytes:
        input = input.decode(encoding)

    result = combine_longname_str(input)

    if was_bytes:
        result = result.encode(encoding)
    return result

def split_longname_str(input: str, encoding: str = get_filesystem_encoding()) -> str:
    # https://docs.python.org/3/library/codecs.html
    chunks = re.split(r'[\\/]', input)
    result = []
    if encoding in ('utf_8', 'U8', 'UTF', 'utf8', 'cp65001'):
        # fast(er) path: UTF-8
        CHUNK_LENGTH = FS_LENGTH_LIMIT - 2
        for chunk in chunks:
            if utf8_byte_length_all_chr(chunk) <= FS_LENGTH_LIMIT:
                result.append(chunk)
                continue
            current_split, current_length = '', 0

            for chr in chunk:
                chrlen = utf8_byte_length(chr)
                print(current_split)
                if current_length + chrlen > CHUNK_LENGTH:
                    if current_split:
                        result.append(current_split + DEFAULT_DELIMITER)
                    current_split, current_length = '', 0
                current_split += chr
                current_length += chrlen

            if current_split:
                result.append(current_split)
    elif encoding in ('utf_16', 'U16', 'utf16', 'utf_16_be', 'UTF-16BE', 'utf_16_le', 'UTF-16LE'):
        # fast path: UTF-16 ANY Endian
        CHUNK_LENGTH = FS_LENGTH_LIMIT - 4
        for chunk in chunks:
            if len(chunk) * 2 <= FS_LENGTH_LIMIT:
                result.append(chunk)
                continue
            current_split, current_length = '', 0

            for chr in chunk:
                chrord = ord(chr)
                chrlen = 2
                if chrord >= MIN_HIGH_SURROGATE and chrord < (MAX_HIGH_SURROGATE + 1):
                    chrlen = 4
                elif chrord >= MIN_LOW_SURROGATE and chrord < (MAX_LOW_SURROGATE + 1):
                    chrlen = 0  # same reason as UTF-8 does

                if current_length + chrlen > CHUNK_LENGTH:
                    if current_split:
                        result.append(current_split + DEFAULT_DELIMITER)
                    current_split, current_length = '', 0
                current_split += chr
                current_length += chrlen

            if current_split:
                result.append(current_split)
    elif encoding in ('utf_32', 'U32', 'utf32', 'utf_32_be', 'UTF-32BE', 'utf_32_le', 'UTF-32LE'):
        # (very) fast path: UTF-32 ANY Endian
        CHUNK_LENGTH = FS_LENGTH_LIMIT - 8
        for chunk in chunks:
            chunk_len = len(chunk)
            if chunk_len * 4 <= FS_LENGTH_LIMIT:
                result.append(chunk)
                continue

            for i in range(0, chunk_len, 4):
                if chunk_len < i + 4:
                    result.append(chunk[i:i + 4] + DEFAULT_DELIMITER)
                else:
                    result.append(chunk[i:i + 4])
    else:
        # slow path: encode each charaters
        # any encoding with header/marking will break this (e.g. UTF-16 with BOM, 'idna')
        CHUNK_LENGTH = FS_LENGTH_LIMIT - len(DEFAULT_DELIMITER.encode(encoding))
        for chunk in chunks:
            if len(chunk.encode(encoding)) <= FS_LENGTH_LIMIT:
                result.append(chunk)
                continue
            current_split, current_length = '', 0

            for chr in chunk:
                chrlen = len(chr.encode(encoding))
                if current_length + chrlen > CHUNK_LENGTH:
                    if current_split:
                        result.append(current_split + DEFAULT_DELIMITER)
                    current_split, current_length = '', 0
                current_split += chr
                current_length += chrlen

            if current_split:
                result.append(current_split)

    return sep.join(result)

def combine_longname_str(input: str) -> str:
    result = []
    for part in re.split(r'[\\/]', input):
        if result and result[-1].endswith(DEFAULT_DELIMITER):
            result[-1] = result[-1][:-2] + part
        else:
            result.append(part)
    return sep.join(result)

def utf8_byte_length(chr: Union[str, int]) -> int:
    "Calculates byte length in UTF-8 without encode/decode"
    if isinstance(chr, str):
        chr = ord(chr[0])

    if chr <= 0x7F:
        return 1
    if chr <= 0x7FF:
        return 2
    # refer to Character.isHighSurrogate from Java
    if chr >= MIN_HIGH_SURROGATE and chr < (MAX_HIGH_SURROGATE + 1):
        return 4  # HIGH+LOW, low should be added later without cost
    if chr >= MIN_LOW_SURROGATE and chr < (MAX_LOW_SURROGATE + 1):
        return 0  # length for this is already accounted at high surrogate

    return 3

def utf8_byte_length_all_chr(string: str) -> int:
    "Calculates byte length in UTF-8 without encode/decode"
    result = 0
    for chr in string:
        chr = ord(chr[0])
        if chr <= 0x7F:
            result += 1
        elif chr <= 0x7FF:
            result += 2
        # refer to Character.isHighSurrogate from Java
        elif chr >= MIN_HIGH_SURROGATE and chr < (MAX_HIGH_SURROGATE + 1):
            result += 4  # HIGH+LOW, low should be added later without cost
        elif chr >= MIN_LOW_SURROGATE and chr < (MAX_LOW_SURROGATE + 1):
            result += 0  # length for this is already accounted at high surrogate
        else:
            result += 3
    return result

def ensure_directory(filename):
    split = split_longname(filename, get_filesystem_encoding())
    if split != filename:
        try:
            makedirs(normpath(dirname(split)))
        except FileExistsError:
            pass
    return split

def escaped_open(filename, open_mode, **kwargs):
    "open() that escapes long names"
    split = ensure_directory(filename)
    return open(split, open_mode, **kwargs)

def escaped_sanitize_open(filename, open_mode):
    "sanitized_open() that escapes long names"
    split = ensure_directory(filename)
    a, b = sanitize_open(split, open_mode)
    b = combine_longname(b)
    return a, b

def escaped_stat(path, *args, **kwargs):
    "os.stat() that escapes long names"
    return stat(split_longname(path, get_filesystem_encoding()), *args, **kwargs)

def escaped_unlink(path, *args, **kwargs):
    "os.unlink() that escapes long names"
    unlink(split_longname(path, get_filesystem_encoding()), *args, **kwargs)

def escaped_path_isfile(path):
    "os.path.isfile() that escapes long names"
    return isfile(split_longname(path, get_filesystem_encoding()))

def escaped_path_exists(path):
    "os.path.exists() that escapes long names"
    return exists(split_longname(path, get_filesystem_encoding()))

def escaped_path_getsize(filename):
    "os.path.getsize() that escapes long names"
    return getsize(split_longname(filename, get_filesystem_encoding()))

def escaped_utime(path, *args, **kwargs):
    "os.utime() that escapes long names"
    utime(split_longname(path, get_filesystem_encoding()), *args, **kwargs)

def escaped_rename(src, dst, *args, **kwargs):
    "os.rename() that escapes long names"
    dst = ensure_directory(dst)
    rename(
        split_longname(src, get_filesystem_encoding()),
        dst, *args, **kwargs)

def escaped_replace(src, dst, *args, **kwargs):
    "os.replace() that escapes long names"
    dst = ensure_directory(dst)
    replace(
        split_longname(src, get_filesystem_encoding()),
        dst, *args, **kwargs)

def escaped_remove(path, *args, **kwargs):
    "os.remove() that escapes long names"
    remove(split_longname(path, get_filesystem_encoding()), *args, **kwargs)

def escaped_basename(path):
    "os.path.basename() that escapes long names"
    return basename(combine_longname(path, get_filesystem_encoding()))

def escaped_dirname(path):
    "os.path.dirname() that escapes long names"
    return dirname(combine_longname(path, get_filesystem_encoding()))

def escaped_isabs(path):
    "os.path.isabs() that escapes long names"
    return isabs(combine_longname(path, get_filesystem_encoding()))

repo_name: Lesmiscore/ytdl


code:
import functools
from threading import Lock

from .utils import supports_terminal_sequences, write_string

CONTROL_SEQUENCES = {
    'DOWN': '\n',
    'UP': '\033[A',
    'ERASE_LINE': '\033[K',
    'RESET': '\033[0m',
}

_COLORS = {
    'BLACK': '0',
    'RED': '1',
    'GREEN': '2',
    'YELLOW': '3',
    'BLUE': '4',
    'PURPLE': '5',
    'CYAN': '6',
    'WHITE': '7',
}

_TEXT_STYLES = {
    'NORMAL': '0',
    'BOLD': '1',
    'UNDERLINED': '4',
}

def format_text(text, f):
    '''
    @param f    String representation of formatting to apply in the form:
                [style] [light] font_color [on [light] bg_color]
                E.g. "red", "bold green on light blue"
    '''
    f = f.upper()
    tokens = f.strip().split()

    bg_color = ''
    if 'ON' in tokens:
        if tokens[-1] == 'ON':
            raise SyntaxError(f'Empty background format specified in {f!r}')
        if tokens[-1] not in _COLORS:
            raise SyntaxError(f'{tokens[-1]} in {f!r} must be a color')
        bg_color = f'4{_COLORS[tokens.pop()]}'
        if tokens[-1] == 'LIGHT':
            bg_color = f'0;10{bg_color[1:]}'
            tokens.pop()
        if tokens[-1] != 'ON':
            raise SyntaxError(f'Invalid format {f.split(" ON ", 1)[1]!r} in {f!r}')
        bg_color = f'\033[{bg_color}m'
        tokens.pop()

    if not tokens:
        fg_color = ''
    elif tokens[-1] not in _COLORS:
        raise SyntaxError(f'{tokens[-1]} in {f!r} must be a color')
    else:
        fg_color = f'3{_COLORS[tokens.pop()]}'
        if tokens and tokens[-1] == 'LIGHT':
            fg_color = f'9{fg_color[1:]}'
            tokens.pop()
        fg_style = tokens.pop() if tokens and tokens[-1] in _TEXT_STYLES else 'NORMAL'
        fg_color = f'\033[{_TEXT_STYLES[fg_style]};{fg_color}m'
        if tokens:
            raise SyntaxError(f'Invalid format {" ".join(tokens)!r} in {f!r}')

    if fg_color or bg_color:
        text = text.replace(CONTROL_SEQUENCES['RESET'], f'{fg_color}{bg_color}')
        return f'{fg_color}{bg_color}{text}{CONTROL_SEQUENCES["RESET"]}'
    else:
        return text

class MultilinePrinterBase:
    def __init__(self, stream=None, lines=1):
        self.stream = stream
        self.maximum = lines - 1
        self._HAVE_FULLCAP = supports_terminal_sequences(stream)

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.end()

    def print_at_line(self, text, pos):
        pass

    def end(self):
        pass

    def _add_line_number(self, text, line):
        if self.maximum:
            return f'{line + 1}: {text}'
        return text

    def write(self, *text):
        write_string(''.join(text), self.stream)

class QuietMultilinePrinter(MultilinePrinterBase):
    pass

class MultilineLogger(MultilinePrinterBase):
    def write(self, *text):
        self.stream.debug(''.join(text))

    def print_at_line(self, text, pos):
        # stream is the logger object, not an actual stream
        self.write(self._add_line_number(text, pos))

class BreaklineStatusPrinter(MultilinePrinterBase):
    def print_at_line(self, text, pos):
        self.write(self._add_line_number(text, pos), '\n')

class MultilinePrinter(MultilinePrinterBase):
    def __init__(self, stream=None, lines=1, preserve_output=True):
        super().__init__(stream, lines)
        self.preserve_output = preserve_output
        self._lastline = self._lastlength = 0
        self._movelock = Lock()
        self._dirty = False

    def lock(func):
        @functools.wraps(func)
        def wrapper(self, *args, **kwargs):
            with self._movelock:
                return func(self, *args, **kwargs)
        return wrapper

    def _move_cursor(self, dest):
        assert dest <= self.maximum
        assert dest >= 0

        current = min(self._lastline, self.maximum)
        yield '\r'
        distance = dest - current
        if distance < 0:
            yield CONTROL_SEQUENCES['UP'] * -distance
        elif distance > 0:
            yield CONTROL_SEQUENCES['DOWN'] * distance
        self._lastline = dest

    @lock
    def print_at_line(self, text, pos):
        self._dirty = True
        if self._HAVE_FULLCAP:
            self.write(*self._move_cursor(pos), CONTROL_SEQUENCES['ERASE_LINE'], text)
            return

        text = self._add_line_number(text, pos)
        textlen = len(text)
        if self._lastline == pos:
            # move cursor at the start of progress when writing to same line
            prefix = '\r'
            if self._lastlength > textlen:
                text += ' ' * (self._lastlength - textlen)
            self._lastlength = textlen
        else:
            # otherwise, break the line
            prefix = '\n'
            self._lastlength = textlen
        self.write(prefix, text)
        self._lastline = pos

    @lock
    def end(self):
        # don't do anything if the terminal is not under MultilinePrinter
        if not self._dirty:
            return
        # move cursor to the end of the last line, and write line break
        # so that other to_screen calls can precede
        text = self._move_cursor(self.maximum) if self._HAVE_FULLCAP else []
        if self.preserve_output:
            self.write(*text, '\n')
            self._lastline = self._lastlength = 0
            return

        if self._HAVE_FULLCAP:
            self.write(
                *text, CONTROL_SEQUENCES['ERASE_LINE'],
                f'{CONTROL_SEQUENCES["UP"]}{CONTROL_SEQUENCES["ERASE_LINE"]}' * self.maximum)
        else:
            self.write('\r', ' ' * self._lastlength, '\r')
        self._lastline = self._lastlength = 0
        self._dirty = False


code:
# coding: utf-8
from __future__ import unicode_literals

# NeoNippori - Danmaku to ASS converter for "Unlicense"d applications

import collections
import io
import json
import math
import random
import re
import xml.dom.minidom
import xml.etree.ElementTree as ET
from typing import Iterable, List, Optional

from .version import __version__

def noop(*a, **b):
    return

NICONICO_COLOR_MAPPINGS = {
    'red': 0xff0000,
    'pink': 0xff8080,
    'orange': 0xffcc00,
    'yellow': 0xffff00,
    'green': 0x00ff00,
    'cyan': 0x00ffff,
    'blue': 0x0000ff,
    'purple': 0xc000ff,
    'black': 0x000000,
    'niconicowhite': 0xcccc99,
    'white2': 0xcccc99,
    'truered': 0xcc0033,
    'red2': 0xcc0033,
    'passionorange': 0xff6600,
    'orange2': 0xff6600,
    'madyellow': 0x999900,
    'yellow2': 0x999900,
    'elementalgreen': 0x00cc66,
    'green2': 0x00cc66,
    'marineblue': 0x33ffcc,
    'blue2': 0x33ffcc,
    'nobleviolet': 0x6633cc,
    'purple2': 0x6633cc}

Comment = collections.namedtuple(
    'Comment', [
        'timeline', 'timestamp', 'no', 'comment', 'pos', 'color', 'size', 'height', 'width'])

def process_mailstyle(mail: Optional[str], fontsize):
    pos, color, size = 0, 0xffffff, fontsize
    if not mail:
        return pos, color, size
    for mailstyle in mail.split():
        if mailstyle == 'ue':
            pos = 1
        elif mailstyle == 'shita':
            pos = 2
        elif mailstyle == 'big':
            size = fontsize * 1.44
        elif mailstyle == 'small':
            size = fontsize * 0.64
        elif mailstyle in NICONICO_COLOR_MAPPINGS:
            color = NICONICO_COLOR_MAPPINGS[mailstyle]
    return pos, color, size

def parse_comments_nnxml(f: str, fontsize: float, report_warning):
    """ (timeline, timestamp, no, comment, pos, color, size, height, width) """
    dom = xml.dom.minidom.parse(f)
    comment_element = dom.getElementsByTagName('chat')
    for comment in comment_element:
        try:
            c = str(comment.childNodes[0].wholeText)
            if c.startswith('/'):
                continue  # ignore advanced comments
            pos, color, size = process_mailstyle(comment.getAttribute('mail'), fontsize)
            yield Comment(max(int(comment.getAttribute('vpos')), 0) * 0.01, int(comment.getAttribute('date')), int(comment.getAttribute('no')), c, pos, color, size, (c.count('\n') + 1) * size, maximum_line_length(c) * size)
        except (AssertionError, AttributeError, IndexError, TypeError, ValueError) as e:
            report_warning('Invalid comment: %s %s' % (e, comment.toxml()))
            continue

def parse_comments_nnjson(f: str, fontsize: float, report_warning):
    for comment_dom in json.loads(f):
        comment = None

        if 'chat' in comment_dom:
            comment = comment_dom['chat']
        elif 'content' in comment_dom:
            comment = comment_dom
        else:
            continue

        try:
            if 'deleted' in comment:
                continue

            c = comment['content']
            if c.startswith('/'):
                continue  # ignore advanced comments

            pos, color, size = process_mailstyle(comment.get('mail'), fontsize)
            yield Comment(max(comment['vpos'], 0) * 0.01, comment['date'], comment.get('no', 0), c, pos, color, size, (c.count('\n') + 1) * size, maximum_line_length(c) * size)
        except (AssertionError, AttributeError, IndexError, TypeError, ValueError, KeyError) as e:
            report_warning('Invalid comment: %s %s' % (e, comment and json.dumps(comment)))
            continue

def _subelem(parent, tag, text: str = None, **extra: dict):
    extra = {k: str(v) for k, v in extra.items()}
    e = ET.SubElement(parent, tag, **extra)
    if text:
        e.text = text

def convert_niconico_json_to_xml(data: str) -> str:
    # https://github.com/Hayao-H/Niconicome/blob/master/Niconicome/Models/Domain/Niconico/Download/Comment/CommentConverter.cs
    # https://github.com/Hayao-H/Niconicome/blob/master/Niconicome/Models/Domain/Niconico/Net/Xml/Comment/Comment.cs
    packet = ET.Element("packet")
    for item in json.loads(data):
        if 'chat' in item or 'content' in item:
            comment = item.get('chat') or item
            if 'deleted' in comment:
                continue
            _subelem(
                packet, "chat",
                text=comment.get('content'),
                thread=comment.get('thread') or '',
                no=comment.get('no'),
                vpos=comment.get('vpos'),
                date=comment.get('date'),
                anonymity=comment.get('anonymity') or 0,
                user_id=comment.get('user_id'),
                mail=comment.get('mail'),
                premium=comment.get('premium') or 1,
            )
        elif 'thread' in item:
            thread = item.get('thread')
            _subelem(
                packet, "thread",
                resultcode=thread.get('resultcode') or 0,
                thread=thread.get('thread') or '',
                server_time=thread.get('server_time') or 0,
                last_res=thread.get('last_res') or 0,
                ticket=thread.get('ticket') or '',
                revision=thread.get('revision') or 0,
            )

    return '<?xml version="1.0" encoding="UTF-8"?>\n' + ET.tostring(packet, encoding='utf-8').decode('utf-8')

def process_comments(comments: Iterable[Comment], f, width, height, bottomReserved, fontface, fontsize, alpha, duration_marquee, duration_still, report_warning):
    styleid = 'NeoNippori_%04x' % random.randint(0, 0xffff)
    write_ass_header(f, width, height, fontface, fontsize, alpha, styleid)
    rows: List[List[Comment]] = [[None] * (height - bottomReserved + 1) for i in range(4)]
    for i in comments:
        row = 0
        rowmax = height - bottomReserved - i.height
        while row <= rowmax:
            avail = find_free_row(rows, i, row, width, height, bottomReserved, duration_marquee, duration_still)
            if avail >= i.height:
                break
            else:
                row += avail or 1
        else:
            row = find_alternative_row(rows, i, height, bottomReserved)
        mark_comment_raw(rows, i, row)
        write_comment(f, i, row, width, height, bottomReserved, fontsize, duration_marquee, duration_still, styleid)

def find_free_row(rows: List[List[Comment]], c: Comment, row, width, height, bottomReserved, duration_marquee, duration_still):
    res = 0
    rowmax = height - bottomReserved
    target = None
    if c.pos in (1, 2):
        while row < rowmax and res < c.height:
            candidate = rows[c.pos][row]
            if target != candidate:
                target = candidate
                if target and target.timeline + duration_still > c.timeline:
                    break
            row += 1
            res += 1
    else:
        try:
            thresholdTime = c.timeline - duration_marquee * (1 - width / (c.width + width))
        except ZeroDivisionError:
            thresholdTime = c.timeline - duration_marquee
        while row < rowmax and res < c.height:
            candidate = rows[c.pos][row]
            if target != candidate:
                target = candidate
                try:
                    if target and (target.timeline > thresholdTime or target.timeline + target.width * duration_marquee / (target.width + width) > c.timeline):
                        break
                except ZeroDivisionError:
                    pass
            row += 1
            res += 1
    return res

def find_alternative_row(rows, c: Comment, height, bottomReserved):
    res = 0
    for row in range(height - bottomReserved - math.ceil(c.height)):
        if not rows[c.pos][row]:
            return row
        elif rows[c.pos][row].timeline < rows[c.pos][res].timeline:
            res = row
    return res

def mark_comment_raw(rows, c: Comment, row):
    try:
        for i in range(row, row + math.ceil(c.height)):
            rows[c.pos][i] = c
    except IndexError:
        pass

def write_ass_header(f, width, height, fontface, fontsize, alpha, styleid):
    f.write(
        '''[Script Info]
; NeoNippori (ネオ日暮里) %(version)s
; Converted from Danmaku comments
; https://github.com/ytdl-patched/ytdl-patched/blob/ytdlp/yt_dlp/neonippori.py
Script Updated By: NeoNippori %(version)s (https://github.com/ytdl-patched/ytdl-patched/blob/ytdlp/yt_dlp/neonippori.py)
ScriptType: v4.00+
PlayResX: %(width)d
PlayResY: %(height)d
Aspect Ratio: %(width)d:%(height)d
Collisions: Normal
WrapStyle: 2
ScaledBorderAndShadow: yes
YCbCr Matrix: TV.601

; JSON DATA HERE

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: %(styleid)s, %(fontface)s, %(fontsize).0f, &H%(alpha)02XFFFFFF, &H%(alpha)02XFFFFFF, &H%(alpha)02X000000, &H%(alpha)02X000000, 0, 0, 0, 0, 100, 100, 0.00, 0.00, 1, %(outline).0f, 0, 7, 0, 0, 0, 0

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
''' % {'width': width, 'height': height, 'fontface': fontface, 'fontsize': fontsize, 'alpha': 255 - round(alpha * 255), 'outline': max(fontsize / 25.0, 1), 'styleid': styleid, 'version': __version__}
    )

def write_comment(f, c: Comment, row, width, height, bottomReserved, fontsize, duration_marquee, duration_still, styleid):
    text = escape_ass_text(c.comment)
    styles = []
    if c.pos == 1:  # ue
        styles.append(r'\an8\pos(%(halfwidth)d, %(row)d)' % {'halfwidth': width / 2, 'row': row})
        duration = duration_still
    elif c.pos == 2:  # shita
        styles.append(r'\an2\pos(%(halfwidth)d, %(row)d)' % {'halfwidth': width / 2, 'row': position_shita(row, height, bottomReserved)})
        duration = duration_still
    else:
        styles.append(r'\move(%(width)d, %(row)d, %(neglen)d, %(row)d)' % {'width': width, 'row': row, 'neglen': -math.ceil(c.width)})
        duration = duration_marquee
    if (fontsize - 1 >= c.size) or (c.size >= 1 + fontsize):
        styles.append(r'\fs%.0f' % c.size)
    if c.color != 0xffffff:
        styles.append(r'\c&H%s&' % format_color(c.color))
        if c.color == 0x000000:
            styles.append(r'\3c&HFFFFFF&')
    f.write('Dialogue: 2,%(start)s,%(end)s,%(styleid)s,,0000,0000,0000,,{%(styles)s}%(text)s\n' % {'start': format_timestamp(c.timeline), 'end': format_timestamp(c.timeline + duration), 'styles': ''.join(styles), 'text': text, 'styleid': styleid})

def escape_ass_text(s):
    def process_blanks(s):
        a, b, c = re.search(r'(^\s*)(.+?)(\s*$)', s).groups()
        return ''.join(('\u2007' * len(a), b, '\u2007' * len(c))) or ' '
    return r'\N'.join(map(process_blanks, re.sub(r'([\\}{])', r'\\\1', s).splitlines()))

def maximum_line_length(s: str):
    return max(len(s) for s in s.split('\n'))  # May not be accurate

def format_timestamp(timestamp: float):
    timestamp *= 100.0
    hour, minute = divmod(timestamp, 360000)
    minute, second = divmod(minute, 6000)
    second, centsecond = divmod(second, 100)
    return '%d:%02d:%02d.%02d' % (math.floor(hour), math.floor(minute), math.floor(second), math.floor(centsecond))

def format_color(color):
    if color == 0x000000:
        return '000000'
    elif color == 0xffffff:
        return 'FFFFFF'
    R = (color >> 16) & 0xff
    G = (color >> 8) & 0xff
    B = color & 0xff

    return '%02X%02X%02X' % (B, G, R)

def position_shita(row, height, bottomReserved):
    return height - bottomReserved - row

def filter_badchars(f):
    return re.sub(r'[\x00-\x08\x0b\x0c\x0e-\x1f]', '\ufffd', f)

PROCESSORS = {
    'Niconico': parse_comments_nnxml,
    'NiconicoJson': parse_comments_nnjson,
}

def parse_comments(input_text, input_format, font_size=25.0, report_warning=noop):
    processor = PROCESSORS.get(input_format)
    if not processor:
        raise ValueError('Unknown comment file format: %s' % input_format)
    comments = list(processor(filter_badchars(input_text), font_size, report_warning))
    comments.sort()
    return comments

def load_comments(input_text, input_format, stage_width, stage_height, reserve_blank=0, font_face='sans-serif', font_size=25.0, text_opacity=1.0, duration_marquee=5.0, duration_still=5.0, report_warning=noop):
    comments = parse_comments(input_text, input_format, font_size, report_warning)
    with io.StringIO() as fo:
        process_comments(comments, fo, stage_width, stage_height, reserve_blank, font_face, font_size, text_opacity, duration_marquee, duration_still, report_warning)
        return re.sub(r'^' + re.escape('; JSON DATA HERE'), lambda x: f'; {input_text}', fo.getvalue())

__all__ = [
    'load_comments',
    'convert_niconico_json_to_xml',
]