Skip to content

Commit d4c355e

Browse files
committed
add better attribute parsing helper function and special case for interstitial item with cue
1 parent 1a2df9b commit d4c355e

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

m3u.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,23 @@ M3U.prototype.toString = function toString() {
104104
});
105105

106106
if (this.items.PlaylistItem.length) {
107+
let cuedInterstitial = false;
108+
let final_interstitial_item = null;
109+
if (this.items.PlaylistItem[this.items.PlaylistItem.length - 1].get("daterange") &&
110+
this.items.PlaylistItem[this.items.PlaylistItem.length - 1].get("daterange")["CLASS"] == "com.apple.hls.interstitial" &&
111+
this.items.PlaylistItem[this.items.PlaylistItem.length - 1].get("daterange")["CUE"]
112+
) {
113+
cuedInterstitial = true;
114+
}
115+
if (cuedInterstitial) {
116+
final_interstitial_item = this.items.PlaylistItem.pop()
117+
}
107118
output.push(this.items.PlaylistItem.map(itemToString).join('\n'));
108-
109119
if (this.get('playlistType') === 'VOD') {
110120
output.push('#EXT-X-ENDLIST');
121+
if (final_interstitial_item) {
122+
output.push(final_interstitial_item.toString());
123+
}
111124
}
112125
} else {
113126
if (this.items.StreamItem.length) {

m3u/AttributeList.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ function parseQuotedString(value) {
142142
}
143143
}
144144

145+
function parseAttributeStringToDict(inputString) {
146+
const data = {};
147+
const regex = /([\w-]+)=(".*?"|[^,]+)/g;
148+
let match;
149+
while ((match = regex.exec(inputString)) !== null) {
150+
const key = match[1];
151+
const value = match[2].replace(/^"|"$/g, '');
152+
data[key] = value;
153+
}
154+
return data;
155+
}
156+
145157
var parse = {
146158
'boolean': function parseBoolean(value) {
147159
return typeof value == 'boolean'
@@ -161,20 +173,7 @@ var parse = {
161173
'quoted-string': parseQuotedString,
162174
'closed-captions': parseQuotedString,
163175
'quoted-string-array': function parseQuotedStringArray(value) {
164-
var data = {};
165-
value.split(',').map(function(kv) {
166-
var s = kv.split('=');
167-
var unquoted = "";
168-
if (s[1]) {
169-
if (s[1].indexOf('"') === 0 && s[1].lastIndexOf('"') == s[1].length - 1) {
170-
unquoted = s[1].slice(1, -1);
171-
} else {
172-
unquoted = s[1];
173-
}
174-
data[s[0]] = unquoted;
175-
}
176-
});
177-
return data;
176+
return parseAttributeStringToDict(value);
178177
},
179178
'hexadecimal-sequence': function parseHexadecimalSequence(value) {
180179
return value;

0 commit comments

Comments
 (0)