globocom / globocom/m3u8

IPTV example for custom tags has problems woth properties that might contain comma

Open
#353 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.3k
Forks
496
PR merge metrics
No merged PRs in 30d

Description

The issue https://github.com/globocom/m3u8/issues/206 talked about parsing iptv properties, a solution was presented with a custom parser.

Though, because how the properties are parsed, if the property contains a comma (most commonly it happens with tvg-name) it will result in incorrectly parsed information.

I believe the code below parses information correctly:

import re
from typing import Final, Any

import m3u8
from m3u8 import protocol
from m3u8.parser import save_segment_custom_value

EXTINF_PROP: Final = re.compile(r"([\w\-]+)=\"([^\"]*)\"")
EXTINF_MATCH: Final = re.compile(protocol.extinf + r':(?P<duration>-?\d+(\.\d+)?)(?P<props>( ' + EXTINF_PROP.pattern + ')*)(,(?P<title>.+))?')


def parse_iptv_attributes(line: str, lineno: int, data: dict[str, Any], state: dict[str, Any]) -> bool:
    if not (match := EXTINF_MATCH.match(line)):
        return False

    if 'segment' not in state:
        state['segment']: dict[str, dict[str, Any]] = {}

    state['segment']['duration'] = float(match.group('duration'))
    state['segment']['title'] = match.group('title')

    additional_props: dict[str, str] = dict(EXTINF_PROP.findall(match.group('props')))
    save_segment_custom_value(state, 'extinf_props', additional_props)
    state['expect_segment'] = True

    return True

[ the rest is same as in the original example ]

For the purpose of copyright I declare the above code public domain. You're free to use it however you like.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the custom-tag parsing example discussed in issue #206 and reproduce it with an IPTV property such as tvg-name containing a comma. Compare the parsed properties and title with the intended values shown here; done means comma-containing properties are not split incorrectly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.