All files / dist m3u-parser.js

9.3% Statements 4/43
0% Branches 0/7
0% Functions 0/10
9.52% Lines 4/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91  1x 1x 1x                                                                                                                                                                           1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.M3uParser = void 0;
const m3u_playlist_1 = require("./m3u-playlist");
class M3uParser {
    /**
     *
     * @param attributesString tvg-id="" group-title=""
     * @private
     */
    static getAttributes(attributesString) {
        const attributeValuePair = attributesString.split('" ');
        const attributes = new m3u_playlist_1.M3uAttributes();
        attributeValuePair.forEach((item) => {
            const [key, value] = item.split('="');
            attributes[key] = value.replace('"', '');
        });
        return attributes;
    }
    /**
     *
     * @param trackInformation -1 tvg-id="" group-title="",Tv Name
     * @param media
     * @private
     */
    static processMedia(trackInformation, media) {
        const lastCommaIndex = trackInformation.lastIndexOf(',');
        const durationAttributes = trackInformation.substring(0, lastCommaIndex);
        media.name = trackInformation.substring(lastCommaIndex + 1);
        const firstSpaceIndex = durationAttributes.indexOf(' ');
        media.duration = Number(durationAttributes.substring(0, firstSpaceIndex));
        const attributes = durationAttributes.substring(firstSpaceIndex + 1);
        media.attributes = this.getAttributes(attributes);
    }
    /**
     *
     * @param item #EXTINF:-1 tvg-id="" group-title="",Tv Name
     * @param playlist
     * @param media
     * @private
     */
    static processDirective(item, playlist, media) {
        const firstSemicolonIndex = item.indexOf(':');
        const directive = item.substring(0, firstSemicolonIndex);
        const trackInformation = item.substring(firstSemicolonIndex + 1);
        switch (directive) {
            case m3u_playlist_1.M3uDirectives.EXTINF: {
                this.processMedia(trackInformation, media);
                break;
            }
            case m3u_playlist_1.M3uDirectives.EXTGRP: {
                media.group = trackInformation;
                break;
            }
            case m3u_playlist_1.M3uDirectives.PLAYLIST: {
                playlist.title = trackInformation;
                break;
            }
        }
    }
    static getPlaylist(lines) {
        const playlist = new m3u_playlist_1.M3uPlaylist();
        let media = new m3u_playlist_1.M3uMedia('');
        lines.forEach(item => {
            if (this.isDirective(item)) {
                this.processDirective(item, playlist, media);
            }
            else {
                media.location = item;
                playlist.medias.push(media);
                media = new m3u_playlist_1.M3uMedia('');
            }
        });
        return playlist;
    }
    static isDirective(item) {
        return item[0] === m3u_playlist_1.M3U_COMMENT;
    }
    static isValidM3u(firstLine) {
        return firstLine[0].replace('\n', '') === m3u_playlist_1.M3uDirectives.EXTM3U;
    }
    static parse(m3uString) {
        const lines = m3uString.split('\n').filter(item => item);
        if (!this.isValidM3u(lines)) {
            throw new Error(`Missing ${m3u_playlist_1.M3uDirectives.EXTM3U} directive!`);
        }
        return this.getPlaylist(lines);
    }
}
exports.M3uParser = M3uParser;