UNPKG

m3u-parser-generator

Version:

Library to parse and generate m3u or m3u8 IPTV playlist files

8 lines (7 loc) 4.39 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class E{static generate(t){const e=t.title?`${n.PLAYLIST}:${t.title}`:void 0,s=this.getCustomDataDirective(t.customData),r=t.medias.map(i=>this.getMedia(i)).join(` `),u=this.getAttributes(t.attributes);return[n.EXTM3U+u,e,s,r].filter(i=>i).join(` `)}static getMedia(t){const e=this.getAttributes(t.attributes),s=this.shouldAddInfoDirective(t,e)?`${n.EXTINF}:${t.duration}${e},${t.name}`:null,r=t.group?`${n.EXTGRP}:${t.group}`:null,u=t.bytes?`${n.EXTBYT}:${t.bytes}`:null,i=t.image?`${n.EXTIMG}:${t.image}`:null,o=t.album?`${n.EXTALB}:${t.album}`:null,c=t.artist?`${n.EXTART}:${t.artist}`:null,T=t.genre?`${n.EXTGENRE}:${t.genre}`:null,f=t.extraAttributesFromUrl?`${n.EXTATTRFROMURL}:${t.extraAttributesFromUrl}`:null,X=t.extraHttpHeaders?`${n.EXTHTTP}:${JSON.stringify(t.extraHttpHeaders)}`:null,A=t.kodiProps?[...t.kodiProps].map(([b,M])=>`${n.KODIPROP}:${b}=${M}`).join(` `):null,P=this.getCustomDataDirective(t.customData);return[s,r,u,i,o,c,T,f,X,A,P,t.location].filter(b=>b).join(` `)}static getCustomDataDirective(t){return t.map(e=>`${e.directive}:${e.value}`).join(` `)}static getAttributes(t){const e=Object.keys(t);return e.length?" "+e.map(s=>`${s}="${t[s]}"`).join(" "):""}static shouldAddInfoDirective(t,e){return t.duration!==h||e!==""||t.name!==void 0}}const d="#",h=-1;var n=(a=>(a.EXTM3U="#EXTM3U",a.EXTINF="#EXTINF",a.PLAYLIST="#PLAYLIST",a.EXTGRP="#EXTGRP",a.EXTBYT="#EXTBYT",a.EXTIMG="#EXTIMG",a.EXTALB="#EXTALB",a.EXTART="#EXTART",a.EXTGENRE="#EXTGENRE",a.EXTATTRFROMURL="#EXTATTRFROMURL",a.EXTHTTP="#EXTHTTP",a.KODIPROP="#KODIPROP",a))(n||{});class p{constructor(){this.title="",this.attributes=new l,this.medias=[],this.customData=[]}get urlTvg(){return this.attributes["url-tvg"]}set urlTvg(t){this.attributes={...this.attributes,"url-tvg":t}}getM3uString(){return E.generate(this)}}class g{constructor(t){this.location=t,this.duration=h,this.attributes=new l,this.extraAttributesFromUrl=void 0,this.extraHttpHeaders=void 0,this.bytes=void 0,this.image=void 0,this.album=void 0,this.artist=void 0,this.genre=void 0,this.customData=[]}}class l{}class ${constructor(t){this.config=t}getAttributes(t){const e=new l;return t&&(t.match(/[^ ]*?=".*?"/g)??[]).forEach(r=>{const[u,i]=r.split('="');e[u]=i.replace('"',"")}),e}processMedia(t,e){const s=t.indexOf(","),r=t.substring(0,s);e.name=t.substring(s+1);const u=r.indexOf(" "),i=u>0?u:r.length;e.duration=Number(r.substring(0,i));const o=r.substring(i+1);e.attributes=this.getAttributes(o)}processDirective(t,e,s){const r=t.indexOf(":"),u=t.substring(0,r),i=t.substring(r+1);switch(u){case n.EXTINF:{this.processMedia(i,s);break}case n.EXTGRP:{s.group=i;break}case n.EXTBYT:{s.bytes=Number(i);break}case n.EXTIMG:{s.image=i;break}case n.EXTALB:{s.album=i;break}case n.EXTART:{s.artist=i;break}case n.EXTGENRE:{s.genre=i;break}case n.PLAYLIST:{e.title=i;break}case n.EXTATTRFROMURL:{s.extraAttributesFromUrl=i;break}case n.EXTHTTP:{s.extraHttpHeaders=JSON.parse(i);break}case n.KODIPROP:{const[o,...c]=i.split("="),T=c.join("=");s.kodiProps||(s.kodiProps=new Map),s.kodiProps.set(o,T);break}default:this.processCustomData(e,s,i,u)}}processCustomData(t,e,s,r){var u,i,o,c;(i=(u=this.config)==null?void 0:u.customDataMapping)!=null&&i.media&&this.config.customDataMapping.media.includes(r)?e.customData.push({directive:r,value:s}):(c=(o=this.config)==null?void 0:o.customDataMapping)!=null&&c.playlist&&this.config.customDataMapping.playlist.includes(r)&&t.customData.push({directive:r,value:s})}processExtM3uAttributes(t,e){if(t.startsWith(n.EXTM3U)){const s=t.indexOf(" ");if(s>0){const r=t.substring(s+1);e.attributes=this.getAttributes(r)}}}getPlaylist(t){const e=new p;let s=new g("");return this.processExtM3uAttributes(t[0],e),t.forEach(r=>{this.isDirective(r)?this.processDirective(r,e,s):(s.location=r,e.medias.push(s),s=new g(""))}),e}isDirective(t){return t[0]===d}isValidM3u(t){return t[0].startsWith(n.EXTM3U)}parse(t){var s,r;if(!((s=this.config)!=null&&s.ignoreErrors)&&!t)throw new Error("m3uString can't be null!");const e=t.split(` `).map(u=>u.trim()).filter(u=>u!="");if(!((r=this.config)!=null&&r.ignoreErrors)&&!this.isValidM3u(e))throw new Error(`Missing ${n.EXTM3U} directive!`);return this.getPlaylist(e)}}exports.M3uAttributes=l;exports.M3uGenerator=E;exports.M3uMedia=g;exports.M3uParser=$;exports.M3uPlaylist=p;