Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
444 lines (369 loc) · 14.1 KB
/
Copy pathindex.js
File metadata and controls
444 lines (369 loc) · 14.1 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
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
#!/usr/bin/env node
const express = require('express');
const request = require('request');
const moment = require('moment');
const fs = require('fs-extra');
const { v4: uuid4, v1: uuid1 } = require('uuid');
const { URL } = require('url');
const { create } = require('xmlbuilder2');
const favorites = require('./favorites');
// Configuración
const CONFIG = {
PORT: process.env.PORT || 3000,
UPDATE_INTERVAL: parseInt(process.env.UPDATE_INTERVAL) || 30, // minutos
EPG_HOURS: parseInt(process.env.EPG_HOURS) || 24, // horas hacia adelante
CACHE_FILE: 'cache.json',
FAVORITES_PATH: './pluto-favorites',
API_URL: 'http://api.pluto.tv/v2/channels'
};
// Estado global
let cachedData = {
channels: [],
m3u8: '',
epg: '',
lastUpdate: null,
isUpdating: false
};
// Clase principal
class PlutoIPTVProxy {
constructor() {
this.app = express();
this.setupRoutes();
}
setupRoutes() {
// Endpoint para M3U8
this.app.get('/playlist.m3u8', (req, res) => {
console.log(`[REQUEST] M3U8 playlist requested from ${req.ip}`);
if (!cachedData.m3u8) {
return res.status(503).send('Service initializing, please wait...');
}
res.setHeader('Content-Type', 'application/vnd.apple.mpegurl');
res.setHeader('Content-Disposition', 'inline; filename="playlist.m3u8"');
res.send(cachedData.m3u8);
});
// Endpoint para EPG
this.app.get('/epg.xml', (req, res) => {
console.log(`[REQUEST] EPG requested from ${req.ip}`);
if (!cachedData.epg) {
return res.status(503).send('Service initializing, please wait...');
}
res.setHeader('Content-Type', 'application/xml');
res.setHeader('Content-Disposition', 'inline; filename="epg.xml"');
res.send(cachedData.epg);
});
// Endpoint de estado
this.app.get('/status', (req, res) => {
res.json({
status: 'running',
lastUpdate: cachedData.lastUpdate,
nextUpdate: this.getNextUpdateTime(),
channelsCount: cachedData.channels.length,
updateInterval: `${CONFIG.UPDATE_INTERVAL} minutes`,
epgHours: `${CONFIG.EPG_HOURS} hours`
});
});
// Endpoint para forzar actualización
this.app.post('/refresh', async (req, res) => {
console.log(`[REQUEST] Manual refresh requested from ${req.ip}`);
if (cachedData.isUpdating) {
return res.status(429).json({
error: 'Update already in progress',
message: 'Please wait for current update to complete'
});
}
try {
await this.updateData();
res.json({
success: true,
message: 'Data updated successfully',
lastUpdate: cachedData.lastUpdate
});
} catch (error) {
res.status(500).json({
error: 'Update failed',
message: error.message
});
}
});
// Página principal con información
this.app.get('/', (req, res) => {
const baseUrl = `${req.protocol}://${req.get('host')}`;
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>PlutoTV IPTV Proxy</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
h1 { color: #333; }
.endpoint { background: #f4f4f4; padding: 15px; margin: 10px 0; border-radius: 5px; }
.endpoint code { color: #d63384; }
.status { background: #d4edda; padding: 10px; border-radius: 5px; margin: 20px 0; }
.info { color: #666; font-size: 14px; }
</style>
</head>
<body>
<h1>🛰️ PlutoTV IPTV Proxy Server</h1>
<div class="status">
<strong>Estado:</strong> ✅ Activo<br>
<strong>Última actualización:</strong> ${cachedData.lastUpdate || 'Inicializando...'}<br>
<strong>Canales disponibles:</strong> ${cachedData.channels.length}<br>
<strong>Próxima actualización:</strong> ${this.getNextUpdateTime()}
</div>
<h2>📡 Endpoints Disponibles</h2>
<div class="endpoint">
<strong>M3U8 Playlist:</strong><br>
<code>${baseUrl}/playlist.m3u8</code>
</div>
<div class="endpoint">
<strong>EPG (Guía de Programación):</strong><br>
<code>${baseUrl}/epg.xml</code>
</div>
<div class="endpoint">
<strong>Estado del servidor:</strong><br>
<code>${baseUrl}/status</code>
</div>
<div class="endpoint">
<strong>Forzar actualización:</strong><br>
<code>POST ${baseUrl}/refresh</code>
</div>
<div class="info">
<h3>⚙️ Configuración</h3>
<ul>
<li>Intervalo de actualización: ${CONFIG.UPDATE_INTERVAL} minutos</li>
<li>EPG para las próximas: ${CONFIG.EPG_HOURS} horas</li>
<li>Puerto: ${CONFIG.PORT}</li>
</ul>
</div>
</body>
</html>
`);
});
}
getNextUpdateTime() {
if (!cachedData.lastUpdate) return 'Calculando...';
const lastUpdate = moment(cachedData.lastUpdate);
const nextUpdate = lastUpdate.add(CONFIG.UPDATE_INTERVAL, 'minutes');
return nextUpdate.format('YYYY-MM-DD HH:mm:ss');
}
async grabJSON() {
return new Promise((resolve, reject) => {
console.log('[INFO] Grabbing EPG from PlutoTV API...');
// Verificar cache local (solo si es menor a UPDATE_INTERVAL)
if (fs.existsSync(CONFIG.CACHE_FILE)) {
const stat = fs.statSync(CONFIG.CACHE_FILE);
const now = Date.now() / 1000;
const mtime = new Date(stat.mtime).getTime() / 1000;
const cacheAge = (now - mtime) / 60; // minutos
if (cacheAge < CONFIG.UPDATE_INTERVAL) {
console.log(`[DEBUG] Using cache.json (${cacheAge.toFixed(1)} minutes old)`);
try {
const data = fs.readJSONSync(CONFIG.CACHE_FILE);
return resolve(data);
} catch (error) {
console.log('[WARN] Cache file corrupted, fetching from API...');
}
}
}
const startTime = encodeURIComponent(
moment().format('YYYY-MM-DD HH:00:00.000ZZ')
);
const stopTime = encodeURIComponent(
moment().add(CONFIG.EPG_HOURS, 'hours').format('YYYY-MM-DD HH:00:00.000ZZ')
);
const url = `${CONFIG.API_URL}?start=${startTime}&stop=${stopTime}`;
console.log(`[DEBUG] API URL: ${url}`);
request(url, { timeout: 30000 }, (err, response, body) => {
if (err) {
console.error('[ERROR] Failed to fetch from API:', err.message);
return reject(err);
}
if (response.statusCode !== 200) {
console.error(`[ERROR] API returned status ${response.statusCode}`);
return reject(new Error(`API returned status ${response.statusCode}`));
}
try {
const data = JSON.parse(body);
fs.writeFileSync(CONFIG.CACHE_FILE, body);
console.log('[DEBUG] Cache updated successfully');
resolve(data);
} catch (error) {
console.error('[ERROR] Failed to parse API response:', error.message);
reject(error);
}
});
});
}
generateM3U8(channels) {
console.log('[INFO] Generating M3U8 playlist...');
let m3u8 = '#EXTM3U\n';
let count = 0;
channels.forEach((channel) => {
if (!channel.isStitched) return;
try {
const deviceId = uuid1();
const sid = uuid4();
const m3uUrl = new URL(channel.stitched.urls[0].url);
const params = new URLSearchParams(m3uUrl.search);
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();
const slug = channel.slug;
const logo = channel.colorLogoPNG?.path || '';
const group = channel.category || 'General';
const name = channel.name;
m3u8 += `#EXTINF:0 tvg-id="${slug}" tvg-logo="${logo}" group-title="${group}", ${name}\n${m3uUrl.toString()}\n\n`;
count++;
} catch (error) {
console.error(`[ERROR] Failed to process channel ${channel.name}:`, error.message);
}
});
console.log(`[SUCCESS] Generated M3U8 with ${count} channels`);
return m3u8;
}
generateEPG(channels) {
console.log('[INFO] Generating EPG XML...');
const root = create({ version: '1.0', encoding: 'UTF-8' })
.ele('tv', { 'generator-info-name': 'plutoIPTV-proxy' });
let channelCount = 0;
let programmeCount = 0;
// Channels
channels.forEach((channel) => {
if (!channel.isStitched) return;
const ch = root.ele('channel', { id: channel.slug });
ch.ele('display-name').txt(channel.name);
if (channel.colorLogoPNG?.path) {
ch.ele('icon', { src: channel.colorLogoPNG.path });
}
channelCount++;
});
// Programmes
channels.forEach((channel) => {
if (!channel.isStitched || !channel.timelines) return;
channel.timelines.forEach((programme) => {
try {
const prog = root.ele('programme', {
start: moment(programme.start).format('YYYYMMDDHHmmss ZZ'),
stop: moment(programme.stop).format('YYYYMMDDHHmmss ZZ'),
channel: channel.slug,
});
prog.ele('title', { lang: 'es' }).txt(programme.title || 'Sin título');
prog.ele('desc', { lang: 'es' }).txt(programme.episode?.description || '');
if (programme.episode?.genre) {
prog.ele('category', { lang: 'es' }).txt(programme.episode.genre);
}
if (programme.episode?.poster?.path) {
prog.ele('icon', { src: programme.episode.poster.path });
}
programmeCount++;
} catch (error) {
console.error(`[ERROR] Failed to process programme in ${channel.name}:`, error.message);
}
});
});
const epg = root.end({ prettyPrint: true });
console.log(`[SUCCESS] Generated EPG with ${channelCount} channels and ${programmeCount} programmes`);
return epg;
}
async updateData() {
if (cachedData.isUpdating) {
console.log('[WARN] Update already in progress, skipping...');
return;
}
cachedData.isUpdating = true;
console.log('\n========================================');
console.log('[UPDATE] Starting data update...');
console.log('========================================');
try {
// Obtener datos de la API
let channels = await this.grabJSON();
// Aplicar filtro de favoritos si existe
const favoritesFilter = favorites.from(CONFIG.FAVORITES_PATH);
if (!favoritesFilter.isEmpty()) {
channels = channels.filter(favoritesFilter);
favoritesFilter.printSummary();
} else {
console.log(`[DEBUG] No favorites specified, loading all ${channels.length} channels`);
}
// Generar M3U8 y EPG
cachedData.channels = channels;
cachedData.m3u8 = this.generateM3U8(channels);
cachedData.epg = this.generateEPG(channels);
cachedData.lastUpdate = moment().format('YYYY-MM-DD HH:mm:ss');
// Guardar archivos locales como respaldo
fs.writeFileSync('playlist.m3u8', cachedData.m3u8);
fs.writeFileSync('epg.xml', cachedData.epg);
console.log('[SUCCESS] Data update completed successfully!');
console.log(`[INFO] Next update in ${CONFIG.UPDATE_INTERVAL} minutes`);
console.log('========================================\n');
} catch (error) {
console.error('[ERROR] Failed to update data:', error.message);
console.error(error.stack);
} finally {
cachedData.isUpdating = false;
}
}
scheduleUpdates() {
console.log(`[INFO] Scheduling automatic updates every ${CONFIG.UPDATE_INTERVAL} minutes`);
setInterval(() => {
this.updateData();
}, CONFIG.UPDATE_INTERVAL * 60 * 1000);
}
async start() {
console.log('\n🛰️ PlutoTV IPTV Proxy Server');
console.log('=====================================');
console.log(`Port: ${CONFIG.PORT}`);
console.log(`Update Interval: ${CONFIG.UPDATE_INTERVAL} minutes`);
console.log(`EPG Hours: ${CONFIG.EPG_HOURS} hours`);
console.log('=====================================\n');
// Actualización inicial
await this.updateData();
// Programar actualizaciones automáticas
this.scheduleUpdates();
// Iniciar servidor
this.app.listen(CONFIG.PORT, () => {
console.log(`\n✅ Server is running on http://localhost:${CONFIG.PORT}`);
console.log(`📺 M3U8 Playlist: http://localhost:${CONFIG.PORT}/playlist.m3u8`);
console.log(`📋 EPG Guide: http://localhost:${CONFIG.PORT}/epg.xml`);
console.log(`📊 Status: http://localhost:${CONFIG.PORT}/status\n`);
});
}
}
// Manejo de errores no capturados
process.on('uncaughtException', (error) => {
console.error('[FATAL] Uncaught Exception:', error);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('[FATAL] Unhandled Rejection at:', promise, 'reason:', reason);
});
// Manejo de señales de terminación
process.on('SIGINT', () => {
console.log('\n[INFO] Received SIGINT, shutting down gracefully...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n[INFO] Received SIGTERM, shutting down gracefully...');
process.exit(0);
});
// Iniciar servidor
const proxy = new PlutoIPTVProxy();
proxy.start().catch((error) => {
console.error('[FATAL] Failed to start server:', error);
process.exit(1);
});
module.exports = PlutoIPTVProxy;