UNPKG

m3u-parser-generator

Version:

Library to parse and generate m3u or m3u8 IPTV playlist files

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