Aqso commited on
Commit
bcd5475
·
verified ·
1 Parent(s): 53ed886

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +59 -62
index.js CHANGED
@@ -17,34 +17,58 @@ if (serviceAccount.project_id) {
17
  }
18
  const db = admin.database();
19
 
20
- let liveSession = { lastAction: 'Sentinel Standby' };
21
  app.get('/api/live-status', (req, res) => res.json(liveSession));
22
 
23
- // 1. DASHBOARD: BACA DATABASE UTUH
24
  app.get('/', async (req, res) => {
 
 
 
 
25
  const snap = await db.ref('donghua').once('value');
26
  const data = snap.val() || {};
 
 
 
 
 
 
 
 
 
27
  let grid = `<div class="grid">`;
28
- Object.keys(data).reverse().forEach(k => {
29
- const item = data[k];
30
- // Hanya munculkan yang sudah punya episode lengkap
31
- if (item.episodes && item.episodes.length > 0) {
32
- grid += `<div class="card" onclick="location.href='/view?id=${k}'"><img src="${item.img}"><div class="card-title">${item.title}</div></div>`;
33
- }
34
  });
35
- res.send(views.layout(grid + '</div>'));
 
 
 
 
 
 
 
 
 
36
  });
37
 
38
- // 2. ATOMIC SCRAPER ENGINE
 
 
 
 
 
 
39
  app.get('/api/scrape', async (req, res) => {
40
  const { url } = req.query;
41
  let browser;
42
  try {
43
  browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
44
  const page = await browser.newPage();
45
- await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36');
46
 
47
- liveSession.lastAction = "Piercing Anichin Protection...";
48
  await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
49
 
50
  const list = await page.evaluate(() => {
@@ -57,18 +81,16 @@ app.get('/api/scrape', async (req, res) => {
57
  });
58
 
59
  for(let i=0; i < list.length; i++) {
60
- // ANTI-DUPLICATE: Ultra-Normalized Key
61
  const key = list[i].title.toLowerCase().replace(/[^a-z0-9]/g, '').slice(0, 50);
62
 
63
- liveSession.lastAction = `[${i+1}/${list.length}] Deep Analyzing: ${list[i].title}`;
64
-
65
  const dp = await browser.newPage();
66
  try {
67
  await dp.goto(list[i].url, { waitUntil: 'networkidle2', timeout: 45000 });
68
- await dp.evaluate(() => window.scrollBy(0, 800)); // Trigger Lazy-Load
69
 
70
- // Ambil daftar episode mentah
71
- const rawEpisodes = await dp.evaluate(() => {
72
  const items = document.querySelectorAll('.eplister li, .list-episode li, .cl-item');
73
  return Array.from(items).map(li => ({
74
  num: li.innerText.match(/\d+/)?.[0] || '?',
@@ -76,16 +98,13 @@ app.get('/api/scrape', async (req, res) => {
76
  })).filter(e => e.url && e.url.startsWith('http'));
77
  });
78
 
79
- if (rawEpisodes.length > 0) {
80
- let completedEpisodes = [];
81
- liveSession.lastAction = `Found ${rawEpisodes.length} Episodes. Starting Atomic Drill...`;
82
-
83
- for (let j=0; j < rawEpisodes.length; j++) {
84
- liveSession.lastAction = `-- Drilling Ep ${rawEpisodes[j].num} of ${list[i].title}`;
85
  try {
86
- await dp.goto(rawEpisodes[j].url, { waitUntil: 'networkidle0', timeout: 40000 });
87
-
88
- // Ambil Semua Mirror
89
  const mirrorData = await dp.evaluate(() => {
90
  const mList = Array.from(document.querySelectorAll('.mirror, .nav-tabs li, .server-item')).map(el => {
91
  const a = el.tagName === 'A' ? el : el.querySelector('a');
@@ -95,73 +114,51 @@ app.get('/api/scrape', async (req, res) => {
95
  return { def, mList };
96
  });
97
 
98
- completedEpisodes.push({
99
- num: rawEpisodes[j].num,
100
  player: mirrorData.def || mirrorData.mList[0]?.src,
101
  mirrors: mirrorData.mList
102
  });
103
- } catch (err) {
104
- liveSession.lastAction = `Warning: Ep ${rawEpisodes[j].num} unreachable.`;
105
- }
106
  }
107
 
108
- // ATOMIC WRITE: Simpan semuanya dalam satu paket!
109
- if (completedEpisodes.length > 0) {
110
  await db.ref('donghua/' + key).set({
111
  title: list[i].title,
112
  img: list[i].img,
113
  url: list[i].url,
114
- episodes: completedEpisodes,
115
  lastSync: Date.now()
116
  });
117
- liveSession.lastAction = `SUCCESS: ${list[i].title} and all ${completedEpisodes.length} eps integrated.`;
118
  }
119
  }
120
- } catch (e) {
121
- liveSession.lastAction = `FAIL: ${list[i].title} - Skipping.`;
122
- }
123
  await dp.close();
124
  await new Promise(r => setTimeout(r, 2000));
125
  }
126
- liveSession.lastAction = "MISSION COMPLETE: DATA_STORE SYNCED ATOMICALLY.";
127
  } finally { if(browser) await browser.close(); }
128
  });
129
 
130
- // 3. VIEW & PLAYER: BACA PAKET DATA UTUH
131
  app.get('/view', async (req, res) => {
132
  const snap = await db.ref('donghua/' + req.query.id).once('value');
133
  const item = snap.val();
134
  if (!item) return res.redirect('/');
135
-
136
  let list = `<h2>${item.title}</h2><div style="margin-top:25px;">`;
137
  item.episodes.forEach((e, idx) => {
138
- list += `<a href="/player?id=${req.query.id}&epIdx=${idx}" style="display:block; padding:18px; background:#111; margin-bottom:10px; border-radius:12px; color:#fff; text-decoration:none; font-weight:800;">Episode ${e.num} ✅</a>`;
139
  });
140
- res.send(views.layout(list, '', true));
141
  });
142
 
143
  app.get('/player', async (req, res) => {
144
  const { id, epIdx } = req.query;
145
  const snap = await db.ref(`donghua/${id}/episodes/${epIdx}`).once('value');
146
  const ep = snap.val();
147
-
148
- let mirrors = `<div class="mirror-list" style="display:flex; gap:10px; flex-wrap:wrap; margin-top:20px;">`;
149
- (ep.mirrors || []).forEach((m, idx) => {
150
- mirrors += `<button class="mirror-btn" onclick="switchMirror('${m.src}', this)" style="padding:10px; background:#111; color:#fff; border:1px solid #333; border-radius:8px; font-size:10px; cursor:pointer;">${m.name}</button>`;
151
- });
152
- mirrors += `</div>`;
153
-
154
- res.send(views.layout(`
155
- <div style="background:#000; height:60vh; border-radius:20px; overflow:hidden;">
156
- <iframe id="player-frame" src="${ep.player}" style="width:100%; height:100%; border:none;" allowfullscreen></iframe>
157
- </div>
158
- ${mirrors}
159
- `, '', true));
160
- });
161
-
162
- app.get('/admin', async (req, res) => {
163
- const snap = await db.ref('donghua').once('value');
164
- res.send(views.adminLayout({ total: Object.keys(snap.val() || {}).length }));
165
  });
166
 
167
  app.listen(7860, '0.0.0.0');
 
17
  }
18
  const db = admin.database();
19
 
20
+ let liveSession = { lastAction: 'Sentinel Ready' };
21
  app.get('/api/live-status', (req, res) => res.json(liveSession));
22
 
23
+ // 1. DASHBOARD: PAGINATION & SEARCH
24
  app.get('/', async (req, res) => {
25
+ const q = req.query.q?.toLowerCase() || '';
26
+ const page = parseInt(req.query.page) || 1;
27
+ const limit = 12;
28
+
29
  const snap = await db.ref('donghua').once('value');
30
  const data = snap.val() || {};
31
+
32
+ let items = Object.keys(data).map(k => ({ id: k, ...data[k] }))
33
+ .filter(i => i.title && i.img && i.episodes); // Filter: Wajib Utuh!
34
+
35
+ if (q) items = items.filter(i => i.title.toLowerCase().includes(q));
36
+
37
+ const totalPages = Math.ceil(items.length / limit);
38
+ const paginated = items.reverse().slice((page - 1) * limit, page * limit);
39
+
40
  let grid = `<div class="grid">`;
41
+ paginated.forEach(i => {
42
+ grid += `<div class="card" onclick="location.href='/view?id=${i.id}'"><img src="${i.img}"><div class="card-title">${i.title}</div></div>`;
 
 
 
 
43
  });
44
+ grid += '</div>';
45
+
46
+ // PAGINATION BAR
47
+ let pages = '<div class="pagination">';
48
+ for (let i = 1; i <= totalPages; i++) {
49
+ pages += `<a href="/?page=${i}${q ? '&q='+q : ''}" class="p-btn ${page === i ? 'active' : ''}">${i}</a>`;
50
+ }
51
+ pages += '</div>';
52
+
53
+ res.send(views.layout(grid + (totalPages > 1 ? pages : ''), q, page, totalPages));
54
  });
55
 
56
+ // 2. AKSES PANEL RAHASIA (BUKAN /ADMIN)
57
+ app.get('/aqso-private-panel', async (req, res) => {
58
+ const snap = await db.ref('donghua').once('value');
59
+ res.send(views.adminLayout({ total: Object.keys(snap.val() || {}).length }));
60
+ });
61
+
62
+ // 3. ATOMIC SCRAPER (THE FIX)
63
  app.get('/api/scrape', async (req, res) => {
64
  const { url } = req.query;
65
  let browser;
66
  try {
67
  browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
68
  const page = await browser.newPage();
69
+ await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36');
70
 
71
+ liveSession.lastAction = "Piercing Anichin...";
72
  await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
73
 
74
  const list = await page.evaluate(() => {
 
81
  });
82
 
83
  for(let i=0; i < list.length; i++) {
84
+ // FIX DOUBLE: Ultra-Normalized Key
85
  const key = list[i].title.toLowerCase().replace(/[^a-z0-9]/g, '').slice(0, 50);
86
 
87
+ liveSession.lastAction = `[${i+1}/${list.length}] Deep Scraping: ${list[i].title}`;
 
88
  const dp = await browser.newPage();
89
  try {
90
  await dp.goto(list[i].url, { waitUntil: 'networkidle2', timeout: 45000 });
91
+ await dp.evaluate(() => window.scrollBy(0, 800)); // Trigger Lazy Load
92
 
93
+ const rawEps = await dp.evaluate(() => {
 
94
  const items = document.querySelectorAll('.eplister li, .list-episode li, .cl-item');
95
  return Array.from(items).map(li => ({
96
  num: li.innerText.match(/\d+/)?.[0] || '?',
 
98
  })).filter(e => e.url && e.url.startsWith('http'));
99
  });
100
 
101
+ if (rawEps.length > 0) {
102
+ let fullEpisodes = [];
103
+ for (let j=0; j < rawEps.length; j++) {
104
+ liveSession.lastAction = `---> Sniffing Video Link: Ep ${rawEps[j].num}`;
 
 
105
  try {
106
+ // FIXED PLAYER SCRAPE: Wait until networkidle0
107
+ await dp.goto(rawEps[j].url, { waitUntil: 'networkidle0', timeout: 40000 });
 
108
  const mirrorData = await dp.evaluate(() => {
109
  const mList = Array.from(document.querySelectorAll('.mirror, .nav-tabs li, .server-item')).map(el => {
110
  const a = el.tagName === 'A' ? el : el.querySelector('a');
 
114
  return { def, mList };
115
  });
116
 
117
+ fullEpisodes.push({
118
+ num: rawEps[j].num,
119
  player: mirrorData.def || mirrorData.mList[0]?.src,
120
  mirrors: mirrorData.mList
121
  });
122
+ } catch (err) {}
 
 
123
  }
124
 
125
+ // ATOMIC WRITE: Judul & Episode masuk sekaligus
126
+ if (fullEpisodes.length > 0) {
127
  await db.ref('donghua/' + key).set({
128
  title: list[i].title,
129
  img: list[i].img,
130
  url: list[i].url,
131
+ episodes: fullEpisodes,
132
  lastSync: Date.now()
133
  });
134
+ liveSession.lastAction = `SUCCESS: ${list[i].title} Integrated.`;
135
  }
136
  }
137
+ } catch (e) {}
 
 
138
  await dp.close();
139
  await new Promise(r => setTimeout(r, 2000));
140
  }
141
+ liveSession.lastAction = "MISSION COMPLETE.";
142
  } finally { if(browser) await browser.close(); }
143
  });
144
 
145
+ // ROUTE VIEW & PLAYER (Normal)
146
  app.get('/view', async (req, res) => {
147
  const snap = await db.ref('donghua/' + req.query.id).once('value');
148
  const item = snap.val();
149
  if (!item) return res.redirect('/');
 
150
  let list = `<h2>${item.title}</h2><div style="margin-top:25px;">`;
151
  item.episodes.forEach((e, idx) => {
152
+ list += `<a href="/player?id=${req.query.id}&epIdx=${idx}" style="display:block; padding:18px; background:#111; margin-bottom:10px; border-radius:15px; color:#fff; text-decoration:none; font-weight:800;">Episode ${e.num} ✅</a>`;
153
  });
154
+ res.send(views.layout(list, '', 1, 1, true));
155
  });
156
 
157
  app.get('/player', async (req, res) => {
158
  const { id, epIdx } = req.query;
159
  const snap = await db.ref(`donghua/${id}/episodes/${epIdx}`).once('value');
160
  const ep = snap.val();
161
+ res.send(views.layout(`<div style="background:#000; height:60vh; border-radius:20px; overflow:hidden;"><iframe src="${ep.player}" style="width:100%; height:100%; border:none;" allowfullscreen></iframe></div>`, '', 1, 1, true));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  });
163
 
164
  app.listen(7860, '0.0.0.0');