Closure-RI commited on
Commit
e1645d2
·
verified ·
1 Parent(s): 37ff572

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +24 -25
index.js CHANGED
@@ -1205,15 +1205,17 @@ async function scrapeHAnime(query) {
1205
 
1206
  async function scrapeHAnimeDetails(url) {
1207
  const browser = await puppeteer.launch({
1208
- headless: true,
1209
- args: ['--no-sandbox', '--disable-setuid-sandbox']
1210
  });
1211
  const page = await browser.newPage();
1212
- await page.setUserAgent("Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]");
 
 
1213
 
1214
  try {
1215
- await page.goto(url, { waitUntil: 'domcontentloaded' });
1216
- // Ambil data dari halaman
1217
  const result = await page.evaluate(() => {
1218
  const getText = (selector) => {
1219
  const element = document.querySelector(selector);
@@ -1221,9 +1223,9 @@ async function scrapeHAnimeDetails(url) {
1221
  };
1222
 
1223
  const getGenre = () => {
1224
- return [...document.querySelectorAll(
1225
- "body > main > article > aside.anime-cn.clb > div.genres.mgt.df.fww.por > a"
1226
- )].map(genre => genre.textContent.trim());
1227
  };
1228
 
1229
  return {
@@ -1233,37 +1235,37 @@ async function scrapeHAnimeDetails(url) {
1233
  subtitleLang: getText("body > main > article > aside.anime-cn.clb > header > div > p > a:nth-child(5)"),
1234
  quality: getText("body > main > article > aside.anime-cn.clb > header > div > p > a:nth-child(3)"),
1235
  description: getText("body > main > article > aside.anime-cn.clb > div.description.link-co.mgb2"),
1236
- genre: getGenre()
1237
  };
1238
  });
1239
-
1240
- console.log(result)
1241
 
1242
- // Klik tombol play jika ada
 
1243
  const playButtonSelector = "body > div > div.play.p-pulse";
1244
  if (await page.$(playButtonSelector)) {
 
1245
  await page.click(playButtonSelector);
1246
- await page.waitForTimeout(10000); // Tunggu iframe muncul
 
 
1247
  }
1248
 
1249
- // Ambil link dari iframe
1250
  const iframeSrc = await page.evaluate(() => {
1251
  const iframe = document.querySelector("body > div > iframe");
1252
  return iframe ? iframe.src : null;
1253
  });
1254
-
1255
- console.log(iframeSrc)
1256
 
1257
- // Ambil video jika link iframe ditemukan
 
1258
  let video = null;
1259
  if (iframeSrc) {
1260
  const { data: html } = await axios.get(iframeSrc);
1261
  const $ = cheerio.load(html);
1262
- const scripts = $('script');
1263
 
1264
  scripts.each((i, script) => {
1265
  const scriptContent = $(script).html();
1266
- if (scriptContent && scriptContent.includes('jwplayer.setup')) {
1267
  const match = scriptContent.match(/file:\s*"(https?:\/\/[^"]+\.mp4)"/);
1268
  if (match && match[1]) {
1269
  video = match[1];
@@ -1271,23 +1273,20 @@ async function scrapeHAnimeDetails(url) {
1271
  }
1272
  });
1273
  }
1274
-
1275
- // Tambahkan video ke hasil
1276
  result.video = video;
1277
-
1278
-
1279
 
1280
  await browser.close();
1281
  return result;
1282
-
1283
  } catch (error) {
1284
- console.log("Error scraping details:", error);
1285
  await browser.close();
1286
  return error;
1287
  }
1288
  }
1289
 
1290
 
 
1291
  app.get('/hanime/search', async (req, res) => {
1292
  const { query } = req.query;
1293
  if (!query) {
 
1205
 
1206
  async function scrapeHAnimeDetails(url) {
1207
  const browser = await puppeteer.launch({
1208
+ headless: false, // Set ke false untuk debugging
1209
+ args: ["--no-sandbox", "--disable-setuid-sandbox"],
1210
  });
1211
  const page = await browser.newPage();
1212
+ await page.setUserAgent(
1213
+ "Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]"
1214
+ );
1215
 
1216
  try {
1217
+ await page.goto(url, { waitUntil: "domcontentloaded" });
1218
+
1219
  const result = await page.evaluate(() => {
1220
  const getText = (selector) => {
1221
  const element = document.querySelector(selector);
 
1223
  };
1224
 
1225
  const getGenre = () => {
1226
+ return [...document.querySelectorAll("body > main > article > aside.anime-cn.clb > div.genres.mgt.df.fww.por > a")].map((genre) =>
1227
+ genre.textContent.trim()
1228
+ );
1229
  };
1230
 
1231
  return {
 
1235
  subtitleLang: getText("body > main > article > aside.anime-cn.clb > header > div > p > a:nth-child(5)"),
1236
  quality: getText("body > main > article > aside.anime-cn.clb > header > div > p > a:nth-child(3)"),
1237
  description: getText("body > main > article > aside.anime-cn.clb > div.description.link-co.mgb2"),
1238
+ genre: getGenre(),
1239
  };
1240
  });
 
 
1241
 
1242
+ console.log("Result Metadata:", result);
1243
+
1244
  const playButtonSelector = "body > div > div.play.p-pulse";
1245
  if (await page.$(playButtonSelector)) {
1246
+ console.log("Klik tombol play...");
1247
  await page.click(playButtonSelector);
1248
+ await page.waitForSelector("body > div > iframe", { timeout: 20000 });
1249
+ } else {
1250
+ console.log("Tombol play tidak ditemukan");
1251
  }
1252
 
 
1253
  const iframeSrc = await page.evaluate(() => {
1254
  const iframe = document.querySelector("body > div > iframe");
1255
  return iframe ? iframe.src : null;
1256
  });
 
 
1257
 
1258
+ console.log("Iframe Source:", iframeSrc);
1259
+
1260
  let video = null;
1261
  if (iframeSrc) {
1262
  const { data: html } = await axios.get(iframeSrc);
1263
  const $ = cheerio.load(html);
1264
+ const scripts = $("script");
1265
 
1266
  scripts.each((i, script) => {
1267
  const scriptContent = $(script).html();
1268
+ if (scriptContent && scriptContent.includes("jwplayer.setup")) {
1269
  const match = scriptContent.match(/file:\s*"(https?:\/\/[^"]+\.mp4)"/);
1270
  if (match && match[1]) {
1271
  video = match[1];
 
1273
  }
1274
  });
1275
  }
1276
+
 
1277
  result.video = video;
 
 
1278
 
1279
  await browser.close();
1280
  return result;
 
1281
  } catch (error) {
1282
+ console.error("Error scraping details:", error);
1283
  await browser.close();
1284
  return error;
1285
  }
1286
  }
1287
 
1288
 
1289
+
1290
  app.get('/hanime/search', async (req, res) => {
1291
  const { query } = req.query;
1292
  if (!query) {