Files changed (1) hide show
  1. index.js +59 -0
index.js CHANGED
@@ -449,6 +449,58 @@ let isRefreshingFast = false;
449
  let isRefreshingSlow = false;
450
  let lastRefresh = { fast: null, slow: null };
451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
  async function refreshTier(ids, tierName) {
453
  const results = { ok: 0, fail: 0, errors: [] };
454
  for (const id of ids) {
@@ -467,13 +519,16 @@ async function refreshTier(ids, tierName) {
467
  clearkey: data.clearkey,
468
  });
469
  results.ok++;
 
470
  } else {
471
  results.fail++;
472
  results.errors.push({ channel: id, error: (data && data.error) || 'no stream' });
 
473
  }
474
  } catch (err) {
475
  results.fail++;
476
  results.errors.push({ channel: id, error: err.message });
 
477
  }
478
  }
479
  lastRefresh[tierName] = { time: new Date().toISOString(), ...results };
@@ -575,6 +630,10 @@ app.get('/refresh', (req, res) => {
575
  });
576
  });
577
 
 
 
 
 
578
  app.get('/status', (req, res) => {
579
  res.json({
580
  success: true,
 
449
  let isRefreshingSlow = false;
450
  let lastRefresh = { fast: null, slow: null };
451
 
452
+ // ═══════════════════════════════════════════════════════════
453
+ // TELEGRAM ALERTING — arifa ikiwa channel inashindwa refresh
454
+ // mfululizo (dalili ya chanzo (kraudius/liveblog) kimebadilika
455
+ // muundo, au tatizo la kudumu zaidi ya la muda).
456
+ // ═══════════════════════════════════════════════════════════
457
+ const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN || '';
458
+ const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID || '';
459
+ const FAIL_STREAK_THRESHOLD = 3; // arifa baada ya kushindwa mara 3 mfululizo
460
+
461
+ const channelFailStreak = new Map(); // channel id -> idadi ya kushindwa mfululizo
462
+ const channelAlerted = new Set(); // channels ambazo tayari tumetuma arifa (usirudie)
463
+
464
+ async function sendTelegramAlert(message) {
465
+ if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) return; // haijawekwa, ruka kimya
466
+ try {
467
+ await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`, {
468
+ method: 'POST',
469
+ headers: { 'Content-Type': 'application/json' },
470
+ body: JSON.stringify({
471
+ chat_id: TELEGRAM_CHAT_ID,
472
+ text: message,
473
+ parse_mode: 'HTML',
474
+ }),
475
+ });
476
+ } catch (err) {
477
+ console.error('Telegram alert imeshindwa kutumwa:', err.message);
478
+ }
479
+ }
480
+
481
+ function recordChannelResult(channelId, ok) {
482
+ if (ok) {
483
+ if (channelFailStreak.get(channelId) > 0 || channelAlerted.has(channelId)) {
484
+ // ilikuwa inashindwa, sasa imepona — tuma "all clear" ikiwa tulishatuma alert
485
+ if (channelAlerted.has(channelId)) {
486
+ sendTelegramAlert(`✅ JaynesTV: Channel ${channelId} imepona — refresh inafanya kazi tena.`);
487
+ channelAlerted.delete(channelId);
488
+ }
489
+ }
490
+ channelFailStreak.set(channelId, 0);
491
+ } else {
492
+ const streak = (channelFailStreak.get(channelId) || 0) + 1;
493
+ channelFailStreak.set(channelId, streak);
494
+ if (streak >= FAIL_STREAK_THRESHOLD && !channelAlerted.has(channelId)) {
495
+ channelAlerted.add(channelId);
496
+ sendTelegramAlert(
497
+ `⚠️ JaynesTV: Channel ${channelId} imeshindwa refresh mara ${streak} mfululizo. ` +
498
+ `Huenda muundo wa chanzo (kraudius/liveblog) umebadilika au channel imekufa.`
499
+ );
500
+ }
501
+ }
502
+ }
503
+
504
  async function refreshTier(ids, tierName) {
505
  const results = { ok: 0, fail: 0, errors: [] };
506
  for (const id of ids) {
 
519
  clearkey: data.clearkey,
520
  });
521
  results.ok++;
522
+ recordChannelResult(id, true);
523
  } else {
524
  results.fail++;
525
  results.errors.push({ channel: id, error: (data && data.error) || 'no stream' });
526
+ recordChannelResult(id, false);
527
  }
528
  } catch (err) {
529
  results.fail++;
530
  results.errors.push({ channel: id, error: err.message });
531
+ recordChannelResult(id, false);
532
  }
533
  }
534
  lastRefresh[tierName] = { time: new Date().toISOString(), ...results };
 
630
  });
631
  });
632
 
633
+ app.get('/ping', (req, res) => {
634
+ res.json({ status: 'ok', time: new Date().toISOString() });
635
+ });
636
+
637
  app.get('/status', (req, res) => {
638
  res.json({
639
  success: true,