-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuilder.js
More file actions
136 lines (109 loc) · 5.16 KB
/
Copy pathbuilder.js
File metadata and controls
136 lines (109 loc) · 5.16 KB
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
const fs = require('fs');
const zlib = require('zlib');
const streaming = require('./streaming.js');
const getEpgData = require('./epg.js');
const allPlaylist = require('./playlist.js');
const currentEpochDatetime = new Date().getTime();
const currentDatetimePlus7Hrs = new Date(currentEpochDatetime + 7 * 60 * 60 * 1000);
const currentBkkDatetimeStr = currentDatetimePlus7Hrs.toISOString().slice(0, 16);
const main = async () => {
// prefetch epg data
let epgDataPromise = getEpgData();
// dynamically add streaming url
// await streaming.dynamicallyAddStreamingUrlFromPPTV();
// await streaming.dynamicallyAddStreamingUrlFromByteArkNextData();
// remember all active channel key to build epg
let allActiveChannelKey = [];
// generate M3U PLAYLIST file
for (let playlist of allPlaylist) {
let textStr = `#EXTM3U url-tvg="https://iptv36.vercel.app/epg.xml.gz" refresh="3600"\n#\n`;
textStr += `# Homepage: http://iptv36.mooo.com/ (Find another version of IPTV playlists here)\n`;
textStr += `# Automatically update at: ${currentBkkDatetimeStr} ICT\n\n`;
// test all streaming simultaneously
console.log(`\nChecking streaming url for playlist '${playlist.filename}'...`);
let uniqueChannelKeyForThisPlaylist = playlist.channelList.reduce((channelList, [channelKey, skip = 0]) => {
if (!channelList.includes(channelKey)) {
channelList.push(channelKey);
}
return channelList;
}, []);
allActiveChannelKey = [...allActiveChannelKey, ...uniqueChannelKeyForThisPlaylist];
await Promise.all(
uniqueChannelKeyForThisPlaylist.map(async (channelKey) => {
await streaming.getStreamingInfo(channelKey);
}),
);
// generate playlist file
for (let i = 0; i < playlist.channelList.length; i++) {
let [channelKey, skip = 0] = playlist.channelList[i];
let streamingInfo = await streaming.getStreamingInfo(channelKey, skip);
let channelName = streamingInfo.channelName;
let tvgId = streamingInfo.tvgId ? streamingInfo.tvgId : `iptv36.${channelKey}`;
let channelStr = `#EXTINF:-1 tvg-chno="${i + 1}" tvg-id="${tvgId}" group-title="${
streamingInfo.groupName
}" tvg-logo="${streamingInfo.logo}",${channelName}`;
// add option #EXTVLCOPT
if (streamingInfo.options) {
if (streamingInfo.options.referer) {
channelStr += `\n#EXTVLCOPT:http-referrer=${streamingInfo.options.referer}`;
}
if (streamingInfo.options.userAgent) {
channelStr += `\n#EXTVLCOPT:http-user-agent=${streamingInfo.options.userAgent}`;
}
}
channelStr += `\n${streamingInfo.url}\n\n`;
textStr = textStr + `${channelStr}`;
}
// Added latest update date
let versionInfo = `#EXTINF:-1 tvg-chno="${playlist.channelList.length + 1}" group-title="Thai Free TV" `;
versionInfo += `tvg-logo="http://iptv36.mooo.com/logo/info.png",${currentBkkDatetimeStr}\nhttp://iptv36.mooo.com/logo/info.png\n\n`;
textStr = textStr + `${versionInfo}`;
// Added website url
let websiteUrl = `#EXTINF:-1 tvg-chno="${playlist.channelList.length + 2}" group-title="Thai Free TV" `;
websiteUrl += `tvg-logo="http://iptv36.mooo.com/logo/info.png",https://iptv36.mooo.com/\nhttp://iptv36.mooo.com/logo/info.png\n\n`;
textStr = textStr + `${websiteUrl}`;
fs.writeFileSync(`${playlist.filename}`, textStr, 'utf8');
console.log(`==> Created playlist '${playlist.filename}'`);
}
// generate XMLTV EPG file
allActiveChannelKey = Array.from(new Set(allActiveChannelKey));
const epgData = await epgDataPromise;
let xmlHead = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
<tv>
`;
let xmlTail = '</tv>';
let availableTvgId = [];
let xmlProgramBody = '';
for (let epg of epgData) {
if (!allActiveChannelKey.includes(epg.channelKey)) {
continue;
}
let tvgId = `iptv36.${epg.channelKey}`;
xmlProgramBody += ` <programme start="${epg.programStartStr}" `;
xmlProgramBody += epg.programEndStr ? `stop="${epg.programEndStr}" ` : '';
xmlProgramBody += `channel="${tvgId}">\n`;
xmlProgramBody += ` <title><![CDATA[${epg.programTitle}]]></title>\n`;
if (epg.programSubtitle) {
xmlProgramBody += ` <sub-title><![CDATA[${epg.programSubtitle}]]></sub-title>\n`;
}
if (epg.programDescription) {
xmlProgramBody += ` <desc><![CDATA[${epg.programDescription}]]></desc>\n`;
}
xmlProgramBody += ` </programme>\n`;
availableTvgId.push(tvgId);
}
let xmlChannelBody = '';
for (let tvgId of new Set(availableTvgId)) {
xmlChannelBody += ` <channel id="${tvgId}">\n`;
xmlChannelBody += ` <display-name>${tvgId}</display-name>\n`;
xmlChannelBody += ` </channel>\n`;
}
const xmlContent = xmlHead + xmlChannelBody + xmlProgramBody + xmlTail;
const compressedData = zlib.gzipSync(xmlContent);
fs.writeFileSync('epg.xml', xmlContent, 'utf8');
console.log(`\n==> Created EPG 'epg.xml'`);
fs.writeFileSync('epg.xml.gz', compressedData);
console.log(`\n==> Created EPG 'epg.xml.gz'`);
};
main();