Spaces:
Paused
Paused
Upload index.js
Browse files
index.js
CHANGED
|
@@ -1,15 +1,22 @@
|
|
| 1 |
/**
|
| 2 |
* JAYNES MAX TV β Node.js/Puppeteer backend
|
| 3 |
-
* Port ya kraudius_core.php (v3.2-split, PHP) kwenda Node.js.
|
| 4 |
*
|
| 5 |
-
*
|
| 6 |
-
*
|
| 7 |
-
*
|
| 8 |
-
*
|
| 9 |
-
*
|
| 10 |
-
*
|
| 11 |
-
*
|
| 12 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
*/
|
| 14 |
|
| 15 |
const express = require('express');
|
|
@@ -19,7 +26,7 @@ const app = express();
|
|
| 19 |
const PORT = process.env.PORT || 7860;
|
| 20 |
|
| 21 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
-
// DATA
|
| 23 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 24 |
|
| 25 |
const CHANNEL_NAMES = {
|
|
@@ -29,9 +36,10 @@ const CHANNEL_NAMES = {
|
|
| 29 |
15: 'Fubo Sports', 16: 'Premier Sports 1', 17: 'Premier Sports 2',
|
| 30 |
18: 'TNT Sports 1', 19: 'TNT Sports 2', 20: 'TNT Sports 3', 21: 'TNT Sports 4',
|
| 31 |
22: 'LaLiga TV',
|
| 32 |
-
// 23-52: zitatoka
|
| 33 |
};
|
| 34 |
|
|
|
|
| 35 |
const CLEARKEY_OVERRIDE = {
|
| 36 |
'azam sports 1': 'c31df1600afc33799ecac543331803f2:dd2101530e222f545997d4c553787f85',
|
| 37 |
'azam sports 2': '739e7499125b31cc9948da8057b84cf9:1b7d44d798c351acc02f33ddfbb7682a',
|
|
@@ -98,28 +106,17 @@ const DISPLAY_NAMES = {
|
|
| 98 |
};
|
| 99 |
|
| 100 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 101 |
-
//
|
| 102 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 103 |
-
const
|
| 104 |
-
const LIVEBLOG_MAX_CHANNEL = 52;
|
| 105 |
-
const TOTAL_CHANNELS = 52; // jumla ya app (kraudius 1-36 + liveblog 37-52)
|
| 106 |
-
|
| 107 |
-
function channelExistsForSource(source, channel) {
|
| 108 |
-
if (source === 'kraudius') return channel >= 1 && channel <= KRAUDIUS_MAX_CHANNEL;
|
| 109 |
-
if (source === 'liveblog') return channel >= 1 && channel <= LIVEBLOG_MAX_CHANNEL;
|
| 110 |
-
return false;
|
| 111 |
-
}
|
| 112 |
|
| 113 |
-
function
|
| 114 |
-
|
| 115 |
-
if (source === 'liveblog') return `https://mfumo.liveblog365.com/player.php?channel=${channel}`;
|
| 116 |
-
return '';
|
| 117 |
}
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
return source;
|
| 123 |
}
|
| 124 |
|
| 125 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -186,22 +183,34 @@ function releaseSlot() {
|
|
| 186 |
if (next) next();
|
| 187 |
}
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
await acquireSlot();
|
| 191 |
let page;
|
| 192 |
try {
|
| 193 |
const browser = await getBrowser();
|
| 194 |
page = await browser.newPage();
|
| 195 |
-
await page.setUserAgent(
|
| 196 |
-
'Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36 ' +
|
| 197 |
-
'(KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36'
|
| 198 |
-
);
|
| 199 |
const response = await page.goto(url, { waitUntil: 'networkidle2', timeout: 20000 });
|
| 200 |
-
const html = await page.content();
|
| 201 |
const code = response ? response.status() : 0;
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
} catch (err) {
|
| 204 |
-
return {
|
| 205 |
} finally {
|
| 206 |
if (page) await page.close().catch(() => {});
|
| 207 |
releaseSlot();
|
|
@@ -209,119 +218,32 @@ async function fetchPage(url) {
|
|
| 209 |
}
|
| 210 |
|
| 211 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
-
//
|
|
|
|
| 213 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 214 |
|
| 215 |
-
function
|
| 216 |
-
|
| 217 |
-
const m = html.match(p);
|
| 218 |
-
if (m) return m[1].trim();
|
| 219 |
-
}
|
| 220 |
-
return '';
|
| 221 |
-
}
|
| 222 |
-
|
| 223 |
-
function extractStream(html) {
|
| 224 |
-
const mpdUrl = firstMatch(html, [
|
| 225 |
-
/const\s+mpdUrl\s*=\s*["']([^"']+)["']/,
|
| 226 |
-
/var\s+mpdUrl\s*=\s*["']([^"']+)["']/,
|
| 227 |
-
/let\s+mpdUrl\s*=\s*["']([^"']+)["']/,
|
| 228 |
-
/mpdUrl\s*[:=]\s*["']([^"']+\.mpd[^"']*)["']/,
|
| 229 |
-
/source\s*[:=]\s*["']([^"']+\.mpd[^"']*)["']/,
|
| 230 |
-
/["'](https?:\/\/[^"']{10,}\.mpd(?:\?[^"']*)?)["']/,
|
| 231 |
-
]);
|
| 232 |
-
|
| 233 |
-
const hlsUrl = firstMatch(html, [
|
| 234 |
-
/const\s+hlsUrl\s*=\s*["']([^"']+)["']/,
|
| 235 |
-
/var\s+hlsUrl\s*=\s*["']([^"']+)["']/,
|
| 236 |
-
/let\s+hlsUrl\s*=\s*["']([^"']+)["']/,
|
| 237 |
-
/hlsUrl\s*[:=]\s*["']([^"']+\.m3u8[^"']*)["']/,
|
| 238 |
-
/["'](https?:\/\/[^"']{10,}\.m3u8(?:\?[^"']*)?)["']/,
|
| 239 |
-
]);
|
| 240 |
-
|
| 241 |
-
let mp4Url = '';
|
| 242 |
-
if (!mpdUrl && !hlsUrl) {
|
| 243 |
-
mp4Url = firstMatch(html, [
|
| 244 |
-
/["'](https?:\/\/[^"']{10,}\.mp4(?:\?[^"']*)?)["']/,
|
| 245 |
-
/<source[^>]+src=["']([^"']+\.mp4[^"']*)["']/,
|
| 246 |
-
]);
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
let clearKey = firstMatch(html, [
|
| 250 |
-
/const\s+clearKey\s*=\s*["']([a-f0-9]{32}:[a-f0-9]{32})["']/,
|
| 251 |
-
/var\s+clearKey\s*=\s*["']([a-f0-9]{32}:[a-f0-9]{32})["']/,
|
| 252 |
-
/clearKey\s*[:=]\s*["']([a-f0-9]{32}:[a-f0-9]{32})["']/,
|
| 253 |
-
/["']([a-f0-9]{32}:[a-f0-9]{32})["']/,
|
| 254 |
-
]);
|
| 255 |
-
|
| 256 |
-
if (!clearKey) {
|
| 257 |
-
const kidMatch = html.match(/["']?(?:kid|KID)["']?\s*[:=]\s*["']([a-f0-9]{32})["']/);
|
| 258 |
-
const keyMatch = html.match(/["']?(?:key|KEY)["']?\s*[:=]\s*["']([a-f0-9]{32})["']/);
|
| 259 |
-
if (kidMatch && keyMatch) clearKey = `${kidMatch[1]}:${keyMatch[1]}`;
|
| 260 |
-
}
|
| 261 |
-
|
| 262 |
-
let licenseUrl = '';
|
| 263 |
-
if (!clearKey) {
|
| 264 |
-
licenseUrl = firstMatch(html, [
|
| 265 |
-
/licenseUrl\s*[:=]\s*["']([^"']+)["']/,
|
| 266 |
-
/license_url\s*[:=]\s*["']([^"']+)["']/,
|
| 267 |
-
]);
|
| 268 |
-
}
|
| 269 |
-
|
| 270 |
-
let name = '';
|
| 271 |
-
const titleMatch = html.match(/<title[^>]*>([^<]+)<\/title>/i);
|
| 272 |
-
if (titleMatch) {
|
| 273 |
-
const raw = titleMatch[1].trim();
|
| 274 |
-
let n = raw.replace(/\s*[-β|:]\s*(player|live|stream|tv|channel|watch).*$/i, '').trim();
|
| 275 |
-
n = n.replace(/\s*[-β|]\s*.*$/, '').trim();
|
| 276 |
-
name = n.length > 60 ? raw : n;
|
| 277 |
-
}
|
| 278 |
-
if (!name) {
|
| 279 |
-
const hMatch = html.match(/<h[1-3][^>]*>([^<]+)<\/h[1-3]>/i);
|
| 280 |
-
if (hMatch) name = hMatch[1].replace(/<[^>]*>/g, '').trim();
|
| 281 |
-
}
|
| 282 |
-
|
| 283 |
-
const streamUrl = mpdUrl || hlsUrl || mp4Url;
|
| 284 |
-
|
| 285 |
-
// Amua "type" kutoka extension HALISI ya streamUrl iliyochaguliwa
|
| 286 |
-
// mwishoni (siyo kutoka variable tofauti) β hii inaepuka hitilafu
|
| 287 |
-
// ya kusema "DASH" wakati URL ni .m3u8 (au kinyume chake) endapo
|
| 288 |
-
// mpdUrl/hlsUrl zilipata match isiyo sahihi kwenye HTML.
|
| 289 |
-
let type = '';
|
| 290 |
-
if (/\.mpd(\?|$)/i.test(streamUrl)) type = 'DASH';
|
| 291 |
-
else if (/\.m3u8(\?|$)/i.test(streamUrl)) type = 'HLS';
|
| 292 |
-
else if (/\.mp4(\?|$)/i.test(streamUrl)) type = 'MP4';
|
| 293 |
-
else type = mpdUrl ? 'DASH' : (hlsUrl ? 'HLS' : (mp4Url ? 'MP4' : ''));
|
| 294 |
-
|
| 295 |
-
const drm = clearKey ? 'CLEARKEY' : (licenseUrl ? 'WIDEVINE' : 'NONE');
|
| 296 |
-
|
| 297 |
-
return { name, stream_url: streamUrl, type, drm, clearkey: clearKey, license_url: licenseUrl };
|
| 298 |
-
}
|
| 299 |
-
|
| 300 |
-
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 301 |
-
// PROCESS CHANNEL β port ya processChannel() ya PHP
|
| 302 |
-
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 303 |
-
|
| 304 |
-
async function processChannel(src, chNo) {
|
| 305 |
-
const url = sourceUrl(src, chNo);
|
| 306 |
const t0 = Date.now();
|
| 307 |
-
const
|
| 308 |
const ms = Date.now() - t0;
|
| 309 |
|
| 310 |
-
if (!
|
|
|
|
|
|
|
|
|
|
| 311 |
return {
|
| 312 |
-
ok: false, channel: chNo, source:
|
| 313 |
-
error:
|
| 314 |
};
|
| 315 |
}
|
| 316 |
|
| 317 |
-
const
|
| 318 |
-
|
| 319 |
-
const rawName = CHANNEL_NAMES[chNo] || ext.name || `Channel ${chNo}`;
|
| 320 |
const norm = normName(rawName);
|
| 321 |
const finalName = DISPLAY_NAMES[norm] || rawName;
|
| 322 |
|
| 323 |
-
let ck =
|
| 324 |
-
let drm =
|
| 325 |
if (!ck && CLEARKEY_OVERRIDE[norm]) {
|
| 326 |
ck = CLEARKEY_OVERRIDE[norm];
|
| 327 |
drm = 'CLEARKEY';
|
|
@@ -329,48 +251,30 @@ async function processChannel(src, chNo) {
|
|
| 329 |
|
| 330 |
let clearkeyObj = null;
|
| 331 |
if (ck && ck.includes(':')) {
|
| 332 |
-
const
|
| 333 |
-
clearkeyObj = { kid, key };
|
| 334 |
}
|
| 335 |
|
| 336 |
return {
|
| 337 |
-
ok: !!
|
| 338 |
channel: chNo,
|
| 339 |
-
source:
|
| 340 |
name: finalName,
|
| 341 |
norm,
|
| 342 |
-
stream_url:
|
| 343 |
-
type:
|
| 344 |
drm,
|
| 345 |
clearkey: clearkeyObj,
|
| 346 |
-
license_url:
|
| 347 |
-
code:
|
| 348 |
-
ms,
|
| 349 |
};
|
| 350 |
}
|
| 351 |
|
| 352 |
-
/**
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
async function getChannelData(chNo, preferredSource) {
|
| 357 |
-
let sourcesToTry;
|
| 358 |
-
if (preferredSource) {
|
| 359 |
-
sourcesToTry = [preferredSource];
|
| 360 |
-
} else if (chNo <= KRAUDIUS_MAX_CHANNEL) {
|
| 361 |
-
sourcesToTry = ['kraudius', 'liveblog'];
|
| 362 |
-
} else {
|
| 363 |
-
sourcesToTry = ['liveblog'];
|
| 364 |
-
}
|
| 365 |
-
|
| 366 |
-
let lastError = null;
|
| 367 |
-
for (const src of sourcesToTry) {
|
| 368 |
-
if (!channelExistsForSource(src, chNo)) continue;
|
| 369 |
-
const data = await processChannel(src, chNo);
|
| 370 |
-
if (data.ok && data.stream_url) return data;
|
| 371 |
-
lastError = data;
|
| 372 |
-
}
|
| 373 |
-
return lastError; // ya mwisho iliyojaribiwa (yenye error), au null
|
| 374 |
}
|
| 375 |
|
| 376 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -453,7 +357,7 @@ async function refreshTier(ids, tierName) {
|
|
| 453 |
const results = { ok: 0, fail: 0, errors: [] };
|
| 454 |
for (const id of ids) {
|
| 455 |
try {
|
| 456 |
-
const data = await getChannelData(id
|
| 457 |
if (data && data.ok && data.stream_url) {
|
| 458 |
const isDash = data.type === 'DASH';
|
| 459 |
await upsertChannelToSupabase({
|
|
@@ -514,8 +418,8 @@ setInterval(() => { runSlowRefresh().catch((e) => console.error('slow refresh er
|
|
| 514 |
|
| 515 |
app.get('/', (req, res) => {
|
| 516 |
res.json({
|
| 517 |
-
name: 'JAYNES MAX TV backend (Node.js/Puppeteer)',
|
| 518 |
-
endpoints: ['/api/content/channels', '/api/channel/:id'],
|
| 519 |
total_channels: TOTAL_CHANNELS,
|
| 520 |
});
|
| 521 |
});
|
|
@@ -585,28 +489,27 @@ app.get('/status', (req, res) => {
|
|
| 585 |
|
| 586 |
app.get('/api/channel/:id', async (req, res) => {
|
| 587 |
const id = parseInt(req.params.id, 10);
|
| 588 |
-
const preferredSource = req.query.source ? String(req.query.source) : null;
|
| 589 |
|
| 590 |
-
if (!id
|
| 591 |
return res.status(404).json({
|
| 592 |
success: false,
|
| 593 |
error: `Channel id lazima iwe kati ya 1 na ${TOTAL_CHANNELS}`,
|
| 594 |
});
|
| 595 |
}
|
| 596 |
|
| 597 |
-
const cacheKey =
|
| 598 |
const cached = getCached(cacheKey);
|
| 599 |
if (cached) {
|
| 600 |
return res.json({ ...cached, cached: true });
|
| 601 |
}
|
| 602 |
|
| 603 |
-
const data = await getChannelData(id
|
| 604 |
|
| 605 |
if (!data || !data.ok || !data.stream_url) {
|
| 606 |
return res.status(502).json({
|
| 607 |
success: false,
|
| 608 |
channel: id,
|
| 609 |
-
error: (data && data.error) || 'Imeshindwa kupata stream kutoka
|
| 610 |
});
|
| 611 |
}
|
| 612 |
|
|
|
|
| 1 |
/**
|
| 2 |
* JAYNES MAX TV β Node.js/Puppeteer backend
|
|
|
|
| 3 |
*
|
| 4 |
+
* v4: badala ya kutembelea kraudiustv.iceiy.com / mfumo.liveblog365.com moja
|
| 5 |
+
* kwa moja, Puppeteer sasa inatembelea relay yetu wenyewe (kraudius_fetch_lite.php,
|
| 6 |
+
* kwenye jaynes.freedev.app) β relay hiyo tayari imefanya curl+extraction (PHP)
|
| 7 |
+
* na inarudisha JSON. Puppeteer inahitajika kwa sababu InfinityFree (hosting ya
|
| 8 |
+
* relay) wakati mwingine inaonyesha JS-challenge yake (aes.js) badala ya kupitisha
|
| 9 |
+
* ombi moja kwa moja kwenye PHP script β curl/fetch ya kawaida haiwezi kuitatua,
|
| 10 |
+
* lakini Chromium halisi (Puppeteer) inaendesha JS hiyo na kupita kiotomatiki.
|
| 11 |
+
*
|
| 12 |
+
* Tofauti kuu kutoka toleo la awali:
|
| 13 |
+
* - Chanzo kimoja pekee (LiveBlog365, kupitia relay) β "kraudius" kama chanzo
|
| 14 |
+
* tofauti kimeondolewa (relay yenyewe haikitumii tena); channel zote 1-52
|
| 15 |
+
* sasa zinapitia njia moja.
|
| 16 |
+
* - extractStream()/firstMatch() (regex dhidi ya HTML ya tovuti asili) zimeondolewa
|
| 17 |
+
* kabisa β relay tayari inarudisha JSON iliyochakatwa, hatuhitaji kuchambua HTML.
|
| 18 |
+
* - Browser instance MOJA inabaki wazi (reuse) badala ya kufunguliwa upya kila
|
| 19 |
+
* request β bado ni muhimu kwa ufanisi.
|
| 20 |
*/
|
| 21 |
|
| 22 |
const express = require('express');
|
|
|
|
| 26 |
const PORT = process.env.PORT || 7860;
|
| 27 |
|
| 28 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
+
// DATA
|
| 30 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
|
| 32 |
const CHANNEL_NAMES = {
|
|
|
|
| 36 |
15: 'Fubo Sports', 16: 'Premier Sports 1', 17: 'Premier Sports 2',
|
| 37 |
18: 'TNT Sports 1', 19: 'TNT Sports 2', 20: 'TNT Sports 3', 21: 'TNT Sports 4',
|
| 38 |
22: 'LaLiga TV',
|
| 39 |
+
// 23-52: zitatoka kwenye data ya relay (hazina jina tuli bado)
|
| 40 |
};
|
| 41 |
|
| 42 |
+
// Hifadhi ya ziada β relay ikikosa clearkey kwa channel inayojulikana, tumia hii.
|
| 43 |
const CLEARKEY_OVERRIDE = {
|
| 44 |
'azam sports 1': 'c31df1600afc33799ecac543331803f2:dd2101530e222f545997d4c553787f85',
|
| 45 |
'azam sports 2': '739e7499125b31cc9948da8057b84cf9:1b7d44d798c351acc02f33ddfbb7682a',
|
|
|
|
| 106 |
};
|
| 107 |
|
| 108 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
// CHANNEL RANGE β chanzo kimoja (relay/LiveBlog365), 1-52
|
| 110 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 111 |
+
const TOTAL_CHANNELS = 52;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
function channelValid(channel) {
|
| 114 |
+
return Number.isInteger(channel) && channel >= 1 && channel <= TOTAL_CHANNELS;
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
+
const RELAY_BASE = 'https://jaynes.freedev.app/kraudius_fetch_lite.php';
|
| 118 |
+
function relayUrl(channel) {
|
| 119 |
+
return `${RELAY_BASE}?channel=${channel}`;
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 183 |
if (next) next();
|
| 184 |
}
|
| 185 |
|
| 186 |
+
const UA = 'Mozilla/5.0 (Linux; Android 13; SM-G991B) AppleWebKit/537.36 ' +
|
| 187 |
+
'(KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36';
|
| 188 |
+
|
| 189 |
+
/**
|
| 190 |
+
* Inatembelea relay (kraudius_fetch_lite.php) kwa Puppeteer. InfinityFree
|
| 191 |
+
* wakati mwingine inaonyesha JS-challenge (aes.js) kabla ya kufikisha JSON
|
| 192 |
+
* halisi β Puppeteer inaiendesha hiyo JS kiotomatiki (waitUntil: networkidle2
|
| 193 |
+
* inasubiri hadi redirect ya challenge ikamilike), kisha tunasoma maandishi
|
| 194 |
+
* ghafi ya ukurasa (Chrome inaonyesha JSON ndani ya <pre> ikiwa Content-Type
|
| 195 |
+
* ni application/json β innerText inatupa JSON safi bila escaping ya HTML).
|
| 196 |
+
*/
|
| 197 |
+
async function fetchRelayJson(url) {
|
| 198 |
await acquireSlot();
|
| 199 |
let page;
|
| 200 |
try {
|
| 201 |
const browser = await getBrowser();
|
| 202 |
page = await browser.newPage();
|
| 203 |
+
await page.setUserAgent(UA);
|
|
|
|
|
|
|
|
|
|
| 204 |
const response = await page.goto(url, { waitUntil: 'networkidle2', timeout: 20000 });
|
|
|
|
| 205 |
const code = response ? response.status() : 0;
|
| 206 |
+
const text = await page.evaluate(() => (document.body && (document.body.innerText || document.body.textContent)) || '');
|
| 207 |
+
try {
|
| 208 |
+
return { data: JSON.parse(text), code, error: '' };
|
| 209 |
+
} catch (_e) {
|
| 210 |
+
return { data: null, code, error: 'Relay haijarudisha JSON halali (challenge haijatatuliwa?)' };
|
| 211 |
+
}
|
| 212 |
} catch (err) {
|
| 213 |
+
return { data: null, code: 0, error: err.message };
|
| 214 |
} finally {
|
| 215 |
if (page) await page.close().catch(() => {});
|
| 216 |
releaseSlot();
|
|
|
|
| 218 |
}
|
| 219 |
|
| 220 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 221 |
+
// PROCESS CHANNEL β inatumia JSON ya relay moja kwa moja (relay
|
| 222 |
+
// tayari imefanya extraction; hatuhitaji tena ku-regex HTML hapa)
|
| 223 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 224 |
|
| 225 |
+
async function processChannel(chNo) {
|
| 226 |
+
const url = relayUrl(chNo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
const t0 = Date.now();
|
| 228 |
+
const { data, code, error } = await fetchRelayJson(url);
|
| 229 |
const ms = Date.now() - t0;
|
| 230 |
|
| 231 |
+
if (!data) {
|
| 232 |
+
return { ok: false, channel: chNo, source: 'LiveBlog365', error: error || 'Relay haijarudisha JSON', code, ms };
|
| 233 |
+
}
|
| 234 |
+
if (!data.ok) {
|
| 235 |
return {
|
| 236 |
+
ok: false, channel: chNo, source: 'LiveBlog365',
|
| 237 |
+
error: data.error || 'Relay imeshindwa kupata stream', code: data.code ?? code, ms: data.ms ?? ms,
|
| 238 |
};
|
| 239 |
}
|
| 240 |
|
| 241 |
+
const rawName = CHANNEL_NAMES[chNo] || data.name || `Channel ${chNo}`;
|
|
|
|
|
|
|
| 242 |
const norm = normName(rawName);
|
| 243 |
const finalName = DISPLAY_NAMES[norm] || rawName;
|
| 244 |
|
| 245 |
+
let ck = data.clearkey;
|
| 246 |
+
let drm = data.drm;
|
| 247 |
if (!ck && CLEARKEY_OVERRIDE[norm]) {
|
| 248 |
ck = CLEARKEY_OVERRIDE[norm];
|
| 249 |
drm = 'CLEARKEY';
|
|
|
|
| 251 |
|
| 252 |
let clearkeyObj = null;
|
| 253 |
if (ck && ck.includes(':')) {
|
| 254 |
+
const parts = ck.split(':');
|
| 255 |
+
if (parts.length === 2) clearkeyObj = { kid: parts[0], key: parts[1] };
|
| 256 |
}
|
| 257 |
|
| 258 |
return {
|
| 259 |
+
ok: !!data.stream_url,
|
| 260 |
channel: chNo,
|
| 261 |
+
source: 'LiveBlog365',
|
| 262 |
name: finalName,
|
| 263 |
norm,
|
| 264 |
+
stream_url: data.stream_url,
|
| 265 |
+
type: data.type,
|
| 266 |
drm,
|
| 267 |
clearkey: clearkeyObj,
|
| 268 |
+
license_url: data.license_url || undefined,
|
| 269 |
+
code: data.code ?? code,
|
| 270 |
+
ms: data.ms ?? ms,
|
| 271 |
};
|
| 272 |
}
|
| 273 |
|
| 274 |
+
/** Chanzo kimoja pekee sasa (relay/LiveBlog365) β hakuna fallback ya vyanzo. */
|
| 275 |
+
async function getChannelData(chNo) {
|
| 276 |
+
if (!channelValid(chNo)) return null;
|
| 277 |
+
return processChannel(chNo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
}
|
| 279 |
|
| 280 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 357 |
const results = { ok: 0, fail: 0, errors: [] };
|
| 358 |
for (const id of ids) {
|
| 359 |
try {
|
| 360 |
+
const data = await getChannelData(id);
|
| 361 |
if (data && data.ok && data.stream_url) {
|
| 362 |
const isDash = data.type === 'DASH';
|
| 363 |
await upsertChannelToSupabase({
|
|
|
|
| 418 |
|
| 419 |
app.get('/', (req, res) => {
|
| 420 |
res.json({
|
| 421 |
+
name: 'JAYNES MAX TV backend (Node.js/Puppeteer β relay)',
|
| 422 |
+
endpoints: ['/api/content/channels', '/api/channel/:id', '/refresh', '/status'],
|
| 423 |
total_channels: TOTAL_CHANNELS,
|
| 424 |
});
|
| 425 |
});
|
|
|
|
| 489 |
|
| 490 |
app.get('/api/channel/:id', async (req, res) => {
|
| 491 |
const id = parseInt(req.params.id, 10);
|
|
|
|
| 492 |
|
| 493 |
+
if (!channelValid(id)) {
|
| 494 |
return res.status(404).json({
|
| 495 |
success: false,
|
| 496 |
error: `Channel id lazima iwe kati ya 1 na ${TOTAL_CHANNELS}`,
|
| 497 |
});
|
| 498 |
}
|
| 499 |
|
| 500 |
+
const cacheKey = String(id);
|
| 501 |
const cached = getCached(cacheKey);
|
| 502 |
if (cached) {
|
| 503 |
return res.json({ ...cached, cached: true });
|
| 504 |
}
|
| 505 |
|
| 506 |
+
const data = await getChannelData(id);
|
| 507 |
|
| 508 |
if (!data || !data.ok || !data.stream_url) {
|
| 509 |
return res.status(502).json({
|
| 510 |
success: false,
|
| 511 |
channel: id,
|
| 512 |
+
error: (data && data.error) || 'Imeshindwa kupata stream kutoka relay',
|
| 513 |
});
|
| 514 |
}
|
| 515 |
|