pawanpaudel93 / m3u-parser

A parser for m3u files. It parses the contents of the m3u file to a list of streams information which can be saved as a JSON/CSV/M3U file.
MIT License
30 stars 16 forks source link

Avoid generating empty JSON keys #10

Closed dineiar closed 2 years ago

dineiar commented 2 years ago

When using to_file or get_json functions, m3u_parser generates all the keys, even the ones that were not present in the parsed M3U. For example, consider the following sample M3U input file (sample.m3u):

#EXTM3U
#EXTINF:-1 tvg-id="google" tvg-name="Google" group-title="Google Group", Google
http://www.google.com/

Using the following code:

parser = M3uParser()
parser.parse_m3u("sample.m3u")
parser.to_file("out.json")

The output file (out.json) looks like:

[
    {
        "name": " Google",
        "logo": "",
        "url": "http://www.google.com/",
        "category": "Google Group",
        "language": {
            "code": "",
            "name": ""
        },
        "country": {
            "code": "",
            "name": ""
        },
        "tvg": {
            "id": "google",
            "name": "Google",
            "url": ""
        },
        "status": "GOOD"
    }
]

It doesn't make sense to output fields like "language" and "country", since they were not provided in the source M3U file.

pawanpaudel93 commented 2 years ago

@dineiar My motive was to create a uniform structure for all the m3u files. What if some contain one information and some other don't inside a m3u file? Do you have any suggestions ??

dineiar commented 2 years ago

I think it is better to only generate the keys that were present at the original M3U file. JSON allows for this flexibility, it does not enforces a schema for all items on a list. When processing a JSON structure, it is expected that some keys may be missing if they do not have a value.

pawanpaudel93 commented 2 years ago

@dineiar do you think its better now ??

dineiar commented 2 years ago

@pawanpaudel93, yes, it is!

I continued your work and added this same treatment for the other keys, such as tvg- ones, category, and so on. Check my PR #11.