forked from evoactivity/PlutoIPTV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·208 lines (179 loc) · 5.68 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node
const request = require('request');
const j2x = require('jsontoxml');
const moment = require('moment');
const fs = require('fs-extra');
const uuid4 = require('uuid').v4;
const uuid1 = require('uuid').v1;
const url = require('url');
const plutoIPTV = {
grabJSON: function (callback) {
callback = callback || function () {};
console.log('[INFO] Grabbing EPG...');
// check for cache
if (fs.existsSync('cache.json')) {
let stat = fs.statSync('cache.json');
let now = new Date() / 1000;
let mtime = new Date(stat.mtime) / 1000;
// it's under 30 mins old
if (now - mtime <= 1800) {
console.log("[DEBUG] Using cache.json, it's under 30 minutes old.");
callback(false, fs.readJSONSync('cache.json'));
return;
}
}
// 2020-03-24%2021%3A00%3A00.000%2B0000
let startTime = encodeURIComponent(
moment().format('YYYY-MM-DD HH:00:00.000ZZ')
);
// 2020-03-25%2005%3A00%3A00.000%2B0000
let stopTime = encodeURIComponent(
moment().add(13, 'hours').format('YYYY-MM-DD HH:00:00.000ZZ')
);
let url = `http://api.pluto.tv/v2/channels?start=${startTime}&stop=${stopTime}`;
console.log(url);
request(url, function (err, code, raw) {
console.log('[DEBUG] Using api.pluto.tv, writing cache.json.');
fs.writeFileSync('cache.json', raw);
callback(err || false, JSON.parse(raw));
return;
});
},
};
module.exports = plutoIPTV;
plutoIPTV.grabJSON(function (err, channels) {
///////////////////
// M3U8 Playlist //
///////////////////
let m3u8 = `#EXTM3U` + `\n`;
channels.forEach((channel) => {
let deviceId = uuid1();
let sid = uuid4();
if (channel.isStitched) {
let m3uUrl = new URL(channel.stitched.urls[0].url);
let queryString = url.search;
let params = new URLSearchParams(queryString);
// set the url params
params.set('advertisingId', '');
params.set('appName', 'web');
params.set('appVersion', 'unknown');
params.set('appStoreUrl', '');
params.set('architecture', '');
params.set('buildVersion', '');
params.set('clientTime', '0');
params.set('deviceDNT', '0');
params.set('deviceId', deviceId);
params.set('deviceMake', 'Chrome');
params.set('deviceModel', 'web');
params.set('deviceType', 'web');
params.set('deviceVersion', 'unknown');
params.set('includeExtendedEvents', 'false');
params.set('sid', sid);
params.set('userId', '');
params.set('serverSideAds', 'true');
m3uUrl.search = params.toString();
m3uUrl = m3uUrl.toString();
let slug = channel.slug;
let logo = channel.colorLogoPNG.path;
let group = channel.category;
let name = channel.name;
m3u8 =
m3u8 +
`#EXTINF:0 channel-id="${slug}" tvg-logo="${logo}" group-title="${group}", ${name}
${m3uUrl}
`;
console.log('[INFO] Adding ' + channel.name + ' channel.');
} else {
console.log("[DEBUG] Skipping 'fake' channel " + channel.name + '.');
}
});
///////////////////////////
// XMLTV Programme Guide //
///////////////////////////
let tv = [];
//////////////
// Channels //
//////////////
channels.forEach((channel) => {
if (channel.isStitched) {
tv.push({
name: 'channel',
attrs: { id: channel.slug },
children: [
{ name: 'display-name', text: channel.name },
{ name: 'display-name', text: channel.number },
{ name: 'desc', text: channel.summary },
{ name: 'icon', attrs: { src: channel.colorLogoPNG.path } },
],
});
//////////////
// Episodes //
//////////////
if (channel.timelines) {
channel.timelines.forEach((programme) => {
console.log(
'[INFO] Adding instance of ' +
programme.title +
' to channel ' +
channel.name +
'.'
);
tv.push({
name: 'programme',
attrs: {
start: moment(programme.start).format('YYYYMMDDHHmmss ZZ'),
stop: moment(programme.stop).format('YYYYMMDDHHmmss ZZ'),
channel: channel.slug,
},
children: [
{ name: 'title', attrs: { lang: 'en' }, text: programme.title },
{
name: 'sub-title',
attrs: { lang: 'en' },
text:
programme.title == programme.episode.name
? ''
: programme.episode.name,
},
{
name: 'desc',
attrs: { lang: 'en' },
text: programme.episode.description,
},
{
name: 'date',
text: moment(programme.episode.firstAired).format('YYYYMMDD'),
},
{
name: 'category',
attrs: { lang: 'en' },
text: programme.episode.genre,
},
{
name: 'category',
attrs: { lang: 'en' },
text: programme.episode.subGenre,
},
{
name: 'episode-num',
attrs: { system: 'onscreen' },
text: programme.episode.number,
},
],
});
});
}
}
});
let epg = j2x(
{ tv },
{
prettyPrint: true,
escape: true,
}
);
fs.writeFileSync('pluto_guide.xml', epg);
console.log('[SUCCESS] Wrote the EPG to pluto_guide.xml!');
fs.writeFileSync('pluto_channels.m3u8', m3u8);
console.log('[SUCCESS] Wrote the M3U8 tuner to pluto_channels.m3u8!');
});