Raiper34 / m3u-parser-generator

Library to parse and generate m3u or m3u8 IPTV playlist files
https://m3u-parser-generator.netlify.app
MIT License
24 stars 11 forks source link

Skip error "Attribute value can't be null!" #7

Closed PhamMinhKha closed 11 months ago

PhamMinhKha commented 12 months ago

please add option skip error "throw new Error(Attribute value can't be null!);"

 static getAttributes(attributesString) {
        const attributes = new m3u_playlist_1.M3uAttributes();
        // console.log(attributesString);
        if (!attributesString) {
            return attributes;
        }
        const attributeValuePair = attributesString.split('" ');
        attributeValuePair.forEach((item) => {
            const [key, value] = item.split('="');
            // console.log(value)/;
            if (value == null) {
              // value ="dsds"
                // throw new Error(`Attribute value can't be null!`); <== skip this error
            }else
            attributes[key] = value.replace('"', '');
        });
        return attributes;
    }
Raiper34 commented 12 months ago

Hi friend, can you provide please some m3u example for this?

PhamMinhKha commented 12 months ago

http://gg.gg/phimiptv but you need set user agent is "OTT Navigator/1.6.8.3 (Linux;Android ANDVER; vn; HASH)'" or you can see raw file is here https://raw.githubusercontent.com/PhamMinhKha/m3u-parser-generator/main/test.m3u8 Thank you so much

Raiper34 commented 11 months ago

Can you copy the desired line from that file and paste it here, please?

PhamMinhKha commented 11 months ago

i know this m3u8 file has an error somewhere but i don't know and i hope i can ignore that error line

Raiper34 commented 11 months ago

I added ignoreErrors argument for parsing. It is released now under v1.4.0. See documentation: https://m3u-parser-generator.netlify.app/classes/m3uparser#parse

PhamMinhKha commented 11 months ago

Thanks for adding the option to ignore errors. But still error

TypeError: Cannot read properties of undefined (reading 'replace')
    at /Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:27:42
    at Array.forEach (<anonymous>)
    at M3uParser.getAttributes (/Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:22:28)
    at M3uParser.processMedia (/Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:46:33)
    at M3uParser.processDirective (/Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:62:22)
    at /Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:87:22
    at Array.forEach (<anonymous>)
    at M3uParser.getPlaylist (/Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:85:15)
    at M3uParser.parse (/Users/phamkha/Desktop/apptv/node_modules/m3u-parser-generator/dist/m3u-parser.js:136:21)
    at eval (webpack-internal:///./src/pages/xemphim/index.tsx:59:82)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

i change your code like that it work

static getAttributes(attributesString, ignoreErrors) {
    const attributes = new m3u_playlist_1.M3uAttributes();
    if (!attributesString) {
      return attributes;
    }
    const attributeValuePair = attributesString.split('" ');
    attributeValuePair.forEach((item) => {
      const [key, value] = item.split('="');
      try {
        if (!ignoreErrors && value == null) {
          throw new Error(`Attribute value can't be null!`);
        }
        attributes[key] = value.replace('"', '');
      } catch (error) {
        if (ignoreErrors) {
          console.warn(error);
        } else {
          throw new Error(`Attribute value can't be null!`);
        }
      }

    });
    return attributes;
  }
Raiper34 commented 11 months ago

@PhamMinhKha that is the reason why I needed an example. You can reopen issue, if you need and I can try fix it again, or you can fix it and open PR.

Raiper34 commented 8 months ago

@PhamMinhKha I released v1.4.1 which should be really able to parse also invalid playlists.

PhamMinhKha commented 8 months ago

Thank you. Your project is awesome