-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssiptv.js
162 lines (130 loc) · 4 KB
/
ssiptv.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var fs = require('fs');
function compareNames(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
function compareFiles(a, b) {
if ((a.isDirectory() && b.isDirectory()) || (a.isFile() && b.isFile())) {
// TODO a.name.localeCompare(b.name);
return compareNames(a.name, b.name);
}
if (a.isDirectory()) {
return -1;
}
return 1;
}
function isVideo(name) {
return name.endsWith('.avi')
|| name.endsWith('.mkv')
|| name.endsWith('.mp4')
|| name.endsWith('.ts');
}
function isPlaylist(name) {
return name.endsWith('.liveproxy')
|| name.endsWith('.m3u');
}
function getBasename(name) {
return name.split('.').slice(0, -1).join('.');
}
function getLogoExtension(path) {
var logoExtensions = ['jpeg', 'jpg', 'png'];
return logoExtensions.find(ext => {
try {
fs.accessSync(`${path}.${ext}`, fs.constants.R_OK);
return true;
} catch (e) {
return false;
}
});
}
function getLogo(name, dir, localDir, defaultLogo) {
var ext = getLogoExtension(`${localDir}/${name}`);
return ext ? `${dir}${name}.${ext}` : defaultLogo;
}
function getPlaylistLogo(name, dir, localDir) {
return getLogo(name, dir, localDir, 'video-dir.png');
}
function getVideoLogo(name, dir, localDir) {
return getLogo(name, dir, localDir, 'video-file.png');
}
function filesToM3U(dirContent, host, dir, localDir) {
var extension = /\.[^/.]+$/;
var s = '#EXTM3U\n';
if (dir) {
dir += '/';
}
dirContent.forEach(item => {
var logo;
var type;
var url = encodeURI(`${dir}${item.name}`);
if (item.isDirectory()) {
logo = getPlaylistLogo(item.name, dir, localDir);
type = 'playlist';
url += '.m3u';
} else if (item.isFile()) {
if (isPlaylist(item.name)) {
logo = getPlaylistLogo(getBasename(item.name), dir, localDir);
type = 'playlist';
} else if (isVideo(item.name)) {
logo = getVideoLogo(getBasename(item.name), dir, localDir);
type = 'video';
}
}
if (!type) {
return;
}
logo = encodeURI(logo);
s += `#EXTINF:0 tvg-logo="http://${host}/${logo}" type="${type}", ${item.name.replace(extension, '')}\n`;
if (type === 'playlist') {
s += '#EXTSIZE: Medium\n';
}
s += `http://${host}/${url}\n`;
}, s);
return s;
}
function m3u(r) {
var dir = decodeURI(r.uri.slice(1, -4)); // cut / and .m3u
var localDir = `${r.variables.video_dir}/${dir}`;
try {
// check if m3u file exists and return it's contents
var m3uFile = `${localDir}.m3u`;
fs.accessSync(m3uFile, fs.constants.R_OK);
r.return(200, fs.readFileSync(m3uFile));
return;
} catch (e) {}
var files = fs.readdirSync(localDir, {withFileTypes: true});
files.sort(compareFiles);
var s = filesToM3U(files, r.headersIn.Host, dir, localDir);
r.return(200, s);
}
function liveproxyToM3U(content, host) {
var s = '';
var lines = content.split(/\r?\n/);
lines.forEach(line => {
if (line === '' || line.startsWith('#')) {
s += `${line}\n`;
} else {
var base64 = line.toString('base64');
s += `http://${host}:53422/base64/${base64}/\n`;
}
}, s);
return s;
}
function liveproxy(r) {
var path = decodeURI(r.uri.slice(1)); // cut /
var localPath = `${r.variables.video_dir}/${path}`;
try {
// check if file exists and convert content to liveproxy links
fs.accessSync(localPath, fs.constants.R_OK);
var content = fs.readFileSync(localPath, 'utf8');
var host = r.headersIn.Host.split(':')[0]; // get host part of host:port string
var s = liveproxyToM3U(content, host);
r.return(200, s);
} catch (e) {}
}
export default {m3u, liveproxy};