-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathm3uutil.js
83 lines (65 loc) · 2.12 KB
/
m3uutil.js
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
function supportsHLS() {
var video = document.createElement("video");
return Boolean(
video.canPlayType("application/vnd.apple.mpegURL") ||
video.canPlayType("audio/mpegurl")
);
}
// isMp4: function (playUrl) {
// if (playUrl.indexOf(".mp4") != -1) {
// return true;
// }
// return false;
// },
function isMp4(playUrl) {
if (playUrl.endsWith(".mp4")) {
return true;
} else {
return false;
}
}
function filterByGroupName(itemArr, groupName) {
return itemArr.filter((item) => item["group-title"] === groupName);
}
function extractGroupTitles(m3uData) {
const groupTitlePattern = new RegExp('group-title="([^"]+)"', "gm");
const matches = [...m3uData.matchAll(groupTitlePattern)];
const groupTitles = new Set();
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const groupTitle = match[1];
groupTitles.add(groupTitle);
}
return Array.from(groupTitles);
}
function parseToMap(m3uData) {
const urlPattern = new RegExp("^http[s]?://.+", "gm");
const urlMatches = m3uData.match(urlPattern);
const channelPattern = new RegExp("#EXTINF:-1 ([^,]+),(.+)", "gm");
const matches = [...m3uData.matchAll(channelPattern)];
if (urlMatches.length !== matches.length) {
throw new Error("数据格式有问题");
}
const kvRegex = new RegExp('([\\w-]+)="([^"]+)"', "gm");
const result = [];
for (let idx = 0; idx < matches.length; idx++) {
const match = matches[idx];
const attributes = match[1];
const channelName = match[2];
const kvMatches = [...attributes.matchAll(kvRegex)];
const itemMap = {};
for (let j = 0; j < kvMatches.length; j++) {
const kvMatch = kvMatches[j];
const key = kvMatch[1];
const value = kvMatch[2];
itemMap[key] = value;
}
itemMap["channel-name"] = channelName;
itemMap["channel-url"] = urlMatches[idx];
result.push(itemMap);
}
return result;
}
function getTvName(tvItem) {
return tvItem["channel-name"] ?? tvItem["tvg-name"];
}