-
Notifications
You must be signed in to change notification settings - Fork 17
/
code.gs
504 lines (404 loc) · 17.1 KB
/
code.gs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
function doGet(e) {
const params = e.parameter;
const region = (params.region || 'us').toLowerCase().trim();
const service = params.service;
const sort = params.sort || 'name';
if (!service) {
return handleError('Error: No service type provided');
}
// Handle Pluto TV service
if (service.toLowerCase() === 'plutotv') {
return handlePluto(region, sort);
}
// Handle Plex service
if (service.toLowerCase() === 'plex') {
return handlePlex(region, sort);
}
// Handle SamsungTVPlus service
if (service.toLowerCase() === 'samsungtvplus') {
return handleSamsungTVPlus(region, sort);
}
// Handle Roku service
if (service.toLowerCase() === 'roku') {
return handleRoku(sort);
}
// Handle Stirr service
if (service.toLowerCase() === 'stirr') {
return handleStirr(sort);
}
// Handle Tubi service
if (service.toLowerCase() === 'tubi') {
return handleTubi(service);
}
// Handle PBSKids service
if (service.toLowerCase() === 'pbskids') {
return handlePBSKids(service);
}
// Handle PBS service
if (service.toLowerCase() === 'pbs') {
return handlePBS();
}
// If no matching service was found, return an error
return handleError('Error: Unsupported service type provided');
}
//------ Service Functions ------//
function handlePluto(region, sort) {
const PLUTO_URL = 'https://i.mjh.nz/PlutoTV/.channels.json';
const STREAM_URL_TEMPLATE = 'https://jmp2.uk/plu-{id}.m3u8';
sort = sort || 'name';
let data;
try {
Logger.log('Fetching new Pluto data from URL: ' + PLUTO_URL);
// Fetch JSON data directly
const response = UrlFetchApp.fetch(PLUTO_URL);
const extractedData = response.getContentText();
// Parse JSON data
data = JSON.parse(extractedData);
} catch (error) {
Logger.log('Error fetching or processing Pluto data: ' + error.message);
return handleError('Error fetching Pluto data: ' + error.message);
}
let output = `#EXTM3U url-tvg="https://github.com/matthuisman/i.mjh.nz/raw/master/PlutoTV/${region}.xml.gz"\n`;
const regionNameMap = {
ar: "Argentina",
br: "Brazil",
ca: "Canada",
cl: "Chile",
de: "Germany",
dk: "Denmark",
es: "Spain",
fr: "France",
gb: "United Kingdom",
it: "Italy",
mx: "Mexico",
no: "Norway",
se: "Sweden",
us: "United States"
};
let channels = {};
// If "all" is specified, gather channels from each region
if (region === 'all') {
for (const regionKey in data.regions) {
const regionData = data.regions[regionKey];
const regionFullName = regionNameMap[regionKey] || regionKey.toUpperCase();
for (const channelKey in regionData.channels) {
const channel = { ...regionData.channels[channelKey], region: regionFullName };
// Generate a unique channel ID for each region to avoid overwriting
const uniqueChannelId = `${channelKey}-${regionKey}`;
channels[uniqueChannelId] = channel;
}
}
} else {
// Handle a single specified region
if (!data.regions[region]) {
return handleError(`Error: Region '${region}' not found in Pluto data.`);
}
channels = data.regions[region].channels || {};
}
// Sort channels based on the specified sorting criteria
const sortedChannelIds = Object.keys(channels).sort((a, b) => {
const channelA = channels[a];
const channelB = channels[b];
if (sort === 'chno') {
return channelA.chno - channelB.chno;
} else {
return channelA.name.localeCompare(channelB.name);
}
});
sortedChannelIds.forEach(channelId => {
const channel = channels[channelId];
const { chno, name, description, group, logo, region: channelRegion } = channel;
// Include region name in group title when "all" is specified
const groupTitle = region === 'all' ? `${channelRegion}` : group;
output += `#EXTINF:-1 channel-id="${channelId}" tvg-id="${channelId}" tvg-chno="${chno}" tvg-name="${name}" tvg-logo="${logo}" group-title="${groupTitle}", ${name}\n`;
output += STREAM_URL_TEMPLATE.replace('{id}', channelId.split('-')[0]) + '\n';
});
output = output.replace(/tvg-id="(.*?)-\w{2}"/g, 'tvg-id="$1"');
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
function handlePlex(region, sort) {
const PLEX_URL = 'https://i.mjh.nz/Plex/.channels.json';
const CHANNELS_JSON_URL = 'https://raw.githubusercontent.com/dtankdempse/free-iptv-channels/main/plex/channels.json';
const STREAM_URL_TEMPLATE = 'https://jmp2.uk/plex-{id}.m3u8';
sort = sort || 'name';
let data;
let plexChannels = [];
try {
Logger.log('Fetching new Plex data from URL: ' + PLEX_URL);
const response = UrlFetchApp.fetch(PLEX_URL);
data = JSON.parse(response.getContentText());
Logger.log('Fetching new channels.json data from URL: ' + CHANNELS_JSON_URL);
const channelsResponse = UrlFetchApp.fetch(CHANNELS_JSON_URL);
plexChannels = JSON.parse(channelsResponse.getContentText());
} catch (error) {
return handleError('Error fetching Plex or channels data: ' + error.message);
}
let output = `#EXTM3U url-tvg="https://github.com/matthuisman/i.mjh.nz/raw/master/Plex/${region}.xml.gz"\n`;
const regionNameMap = {
us: "United States",
mx: "Mexico",
es: "Spain",
ca: "Canada",
au: "Australia",
nz: "New Zealand"
};
let channels = {};
// Process channels based on region
if (region === 'all') {
for (const regionKey in data.regions) {
const regionData = data.regions[regionKey];
const regionFullName = regionNameMap[regionKey] || regionKey.toUpperCase();
for (const channelKey in data.channels) {
const channel = data.channels[channelKey];
if (channel.regions.includes(regionKey)) {
const uniqueChannelId = `${channelKey}-${regionKey}`;
channels[uniqueChannelId] = { ...channel, region: regionFullName, group: regionFullName, originalId: channelKey };
}
}
}
} else {
if (!data.regions[region]) {
return handleError(`Error: Region '${region}' not found in Plex data.`);
}
for (const channelKey in data.channels) {
const channel = data.channels[channelKey];
if (channel.regions.includes(region)) {
const matchingChannel = plexChannels.find(ch => ch.Title === channel.name);
const genre = matchingChannel && matchingChannel.Genre ? matchingChannel.Genre : 'Uncategorized';
channels[channelKey] = { ...channel, group: genre, originalId: channelKey };
}
}
}
// Sort channels based on the specified sorting criteria
const sortedChannelIds = Object.keys(channels).sort((a, b) => {
const channelA = channels[a];
const channelB = channels[b];
return sort === 'chno' ? (channelA.chno - channelB.chno) : channelA.name.localeCompare(channelB.name);
});
sortedChannelIds.forEach(channelId => {
const channel = channels[channelId];
const { chno, name, logo, group, originalId } = channel;
output += `#EXTINF:-1 channel-id="${channelId}" tvg-id="${channelId}" tvg-chno="${chno || ''}" tvg-name="${name}" tvg-logo="${logo}" group-title="${group}", ${name}\n`;
output += STREAM_URL_TEMPLATE.replace('{id}', originalId) + '\n';
});
output = output.replace(/tvg-id="(.*?)-\w{2}"/g, 'tvg-id="$1"');
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
function handleSamsungTVPlus(region, sort) {
const SAMSUNG_URL = 'https://i.mjh.nz/SamsungTVPlus/.channels.json';
const STREAM_URL_TEMPLATE = 'https://jmp2.uk/sam-{id}.m3u8';
// Set a default for `sort` if not provided
sort = sort || 'name';
let data;
try {
Logger.log('Fetching new SamsungTVPlus data from URL: ' + SAMSUNG_URL);
// Fetch JSON data directly
const response = UrlFetchApp.fetch(SAMSUNG_URL);
const extractedData = response.getContentText();
// Parse JSON data
data = JSON.parse(extractedData);
} catch (error) {
Logger.log('Error fetching or processing SamsungTVPlus data: ' + error.message);
return handleError('Error fetching SamsungTVPlus data: ' + error.message);
}
let output = `#EXTM3U url-tvg="https://github.com/matthuisman/i.mjh.nz/raw/master/SamsungTVPlus/${region}.xml.gz"\n`;
let channels = {};
// If "all" is specified, gather channels from each region
if (region === 'all') {
for (const regionKey in data.regions) {
const regionData = data.regions[regionKey];
const regionFullName = regionData.name || regionKey.toUpperCase();
for (const channelKey in regionData.channels) {
const channel = { ...regionData.channels[channelKey], region: regionFullName };
const uniqueChannelId = `${channelKey}-${regionKey}`;
channels[uniqueChannelId] = channel;
}
}
} else {
// Handle a single specified region
if (!data.regions[region]) {
return handleError(`Error: Region '${region}' not found in SamsungTVPlus data.`);
}
channels = data.regions[region].channels || {};
}
// Sort channels based on the specified sorting criteria
const sortedChannelIds = Object.keys(channels).sort((a, b) => {
const channelA = channels[a];
const channelB = channels[b];
if (sort === 'chno') {
return channelA.chno - channelB.chno;
} else {
return channelA.name.localeCompare(channelB.name);
}
});
sortedChannelIds.forEach(channelId => {
const channel = channels[channelId];
const { chno, name, description, group, logo, region: channelRegion } = channel;
// Include region name in group title when "all" is specified
const groupTitle = region === 'all' ? `${channelRegion}` : group;
output += `#EXTINF:-1 channel-id="${channelId}" tvg-id="${channelId}" tvg-chno="${chno}" tvg-name="${name}" tvg-logo="${logo}" group-title="${groupTitle}", ${name}\n`;
output += STREAM_URL_TEMPLATE.replace('{id}', channelId.split('-')[0]) + '\n';
});
output = output.replace(/tvg-id="(.*?)-\w{2}"/g, 'tvg-id="$1"');
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
function handleRoku(sort) {
const ROKU_URL = 'https://i.mjh.nz/Roku/.channels.json';
// Set a default for `sort` if not provided
sort = sort || 'name';
let data;
try {
Logger.log('Fetching new Roku data from URL: ' + ROKU_URL);
// Fetch JSON data directly
const response = UrlFetchApp.fetch(ROKU_URL);
const extractedData = response.getContentText();
// Parse JSON data
data = JSON.parse(extractedData);
} catch (error) {
Logger.log('Error fetching or processing Roku data: ' + error.message);
return handleError('Error fetching Roku data: ' + error.message);
}
let output = `#EXTM3U url-tvg="https://github.com/matthuisman/i.mjh.nz/raw/master/Roku/all.xml.gz"\n`;
let channels = data.channels || {};
// Sort channels based on the specified sorting criteria
const sortedChannelIds = Object.keys(channels).sort((a, b) => {
const channelA = channels[a];
const channelB = channels[b];
if (sort === 'chno') {
return channelA.chno - channelB.chno;
} else {
return channelA.name.localeCompare(channelB.name);
}
});
sortedChannelIds.forEach(channelId => {
const channel = channels[channelId];
const { chno, name, description, groups, logo, mjh_url } = channel;
// Use the first group in `groups` array for `group-title`
const groupTitle = groups && groups.length > 0 ? groups[0] : 'Uncategorized';
output += `#EXTINF:-1 channel-id="${channelId}" tvg-id="${channelId}" tvg-chno="${chno}" tvg-name="${name}" tvg-logo="${logo}" group-title="", ${name}\n`;
output += `${mjh_url}\n`;
});
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
function handleStirr(sort) {
const STIRR_URL = 'https://i.mjh.nz/Stirr/.channels.json';
// Set a default for `sort` if not provided
sort = sort || 'name';
let data;
try {
Logger.log('Fetching new Stirr data from URL: ' + STIRR_URL);
// Fetch JSON data directly
const response = UrlFetchApp.fetch(STIRR_URL);
const extractedData = response.getContentText();
// Parse JSON data
data = JSON.parse(extractedData);
} catch (error) {
Logger.log('Error fetching or processing Stirr data: ' + error.message);
return handleError('Error fetching Stirr data: ' + error.message);
}
let output = `#EXTM3U url-tvg="https://i.mjh.nz/Stirr/all.xml.gz"\n`;
let channels = data.channels || {};
// Sort channels based on the specified sorting criteria
const sortedChannelIds = Object.keys(channels).sort((a, b) => {
const channelA = channels[a];
const channelB = channels[b];
if (sort === 'chno') {
return channelA.chno - channelB.chno;
} else {
return channelA.name.localeCompare(channelB.name);
}
});
sortedChannelIds.forEach(channelId => {
const channel = channels[channelId];
const { chno, name, groups, logo } = channel;
// Concatenate all groups, separated by commas
const groupTitle = groups && groups.length > 0 ? groups.join(', ') : 'Uncategorized';
// Generate the stream URL using the template
const streamUrl = `https://jmp2.uk/str-${channelId}.m3u8`;
output += `#EXTINF:-1 channel-id="${channelId}" tvg-id="${channelId}" tvg-chno="${chno}" tvg-name="${name}" tvg-logo="${logo}" group-title="${groupTitle}", ${name}\n`;
output += `${streamUrl}\n`;
});
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
function handleTubi(service) {
let data;
try {
Logger.log('Fetching new Tubi data');
const playlistUrl = 'https://github.com/dtankdempse/tubi-m3u/raw/refs/heads/main/tubi_playlist_us.m3u';
const response = UrlFetchApp.fetch(playlistUrl);
data = response.getContentText();
let epgUrl = 'https://raw.githubusercontent.com/dtankdempse/tubi-m3u/refs/heads/main/tubi_epg_us.xml';
let output = `#EXTM3U url-tvg="${epgUrl}"\n`;
output += data;
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
} catch (error) {
Logger.log('Error fetching Tubi data: ' + error.message);
return handleError('Error fetching Tubi data: ' + error.message);
}
}
function handlePBSKids(service) {
if (service.toLowerCase() !== 'pbskids') return;
let data;
try {
Logger.log('Fetching new PBS Kids data');
const APP_URL = 'https://i.mjh.nz/PBS/.kids_app.json';
const response = UrlFetchApp.fetch(APP_URL);
data = JSON.parse(response.getContentText());
let output = `#EXTM3U url-tvg="https://github.com/matthuisman/i.mjh.nz/raw/master/PBS/kids_all.xml.gz"\n`;
// Sort the channels by name before iterating
const sortedKeys = Object.keys(data.channels).sort((a, b) => {
const channelA = data.channels[a].name.toLowerCase();
const channelB = data.channels[b].name.toLowerCase();
return channelA.localeCompare(channelB); // Sort alphabetically by name
});
sortedKeys.forEach(key => {
const channel = data.channels[key];
const { logo, name, url } = channel;
output += `#EXTINF:-1 channel-id="pbskids-${key}" tvg-id="${key}" tvg-name="${name}" tvg-logo="${logo}", ${name}\n${url}\n`;
});
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
} catch (error) {
Logger.log('Error fetching PBS Kids data: ' + error.message);
return handleError('Error fetching PBS Kids data: ' + error.message);
}
}
function handlePBS() {
const DATA_URL = 'https://i.mjh.nz/PBS/.app.json';
const EPG_URL = 'https://i.mjh.nz/PBS/all.xml.gz';
let data;
try {
Logger.log('Fetching new PBS data from URL: ' + DATA_URL);
// Fetch JSON data directly
const response = UrlFetchApp.fetch(DATA_URL);
const extractedData = response.getContentText();
// Parse JSON data
data = JSON.parse(extractedData);
} catch (error) {
Logger.log('Error fetching or processing PBS data: ' + error.message);
return handleError('Error fetching PBS data: ' + error.message);
}
// Format data for M3U8
let output = `#EXTM3U x-tvg-url="${EPG_URL}"\n`;
Object.keys(data.channels).forEach(key => {
const channel = data.channels[key];
output += `#EXTINF:-1 channel-id="pbs-${key}" tvg-id="${key}" tvg-name="${channel.name}" tvg-logo="${channel.logo}", ${channel.name}\n`;
output += `#KODIPROP:inputstream.adaptive.manifest_type=mpd\n`;
output += `#KODIPROP:inputstream.adaptive.license_type=com.widevine.alpha\n`;
output += `#KODIPROP:inputstream.adaptive.license_key=${channel.license}|Content-Type=application%2Foctet-stream&user-agent=okhttp%2F4.9.0|R{SSM}|\n`;
output += `${channel.url}|user-agent=okhttp%2F4.9.0\n`;
});
// Return output directly to the browser
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.TEXT);
}
//------ Other Functions ------//
function handleError(errorMessage) {
return ContentService.createTextOutput(errorMessage)
.setMimeType(ContentService.MimeType.TEXT);
}