-1

I am trying to make a wrapper function for the existing itertags one here: https://github.com/streamlink/streamlink/blob/master/src/streamlink/plugin/api/utils.py#L16

Currently i have this:

def itertags_wrapper(html, tag, attrs=None, ret=False):

    try:

        result = list(itertags(html, tag))

        if isinstance(attrs, dict):

            attrs = list(iteritems(attrs))

            result = [i for i in result if any([a for a in attrs if a in list(iteritems(i.attributes))])]

        if ret:

            # noinspection PyTypeChecker
            result = [i.attributes[ret] for i in result if ret in i.attributes]

    except Exception:

        result = []

    return result

Now it returns the tags containing the exact same key-value pair as in attrs, but HOW do I have the value of the pair in a regex pattern and broaden possible results?

P.S. iteritems is passed through a "compat" module first to work on both python 2 & 3.

Twilight0
  • 41
  • 7

1 Answers1

0

After lots of tests I came up with this solution:

def itertags_wrapper(html, tag, attrs=None, ret=False):

    try:

        result = list(itertags(html, tag))

        if isinstance(attrs, dict):

            attrs = list(iteritems(attrs))

            result = [
                i for i in result if any(
                    [a for a in attrs if any([a[0] == k and re.match(a[1], v) for k, v in iteritems(i.attributes)])]
                )
            ]

        if ret:

            # noinspection PyTypeChecker
            result = [i.attributes[ret] for i in result if ret in i.attributes]

    except Exception:

        result = []

    return result

All I did is add a second iteration of the attrs key value pairs.

Twilight0
  • 41
  • 7