Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -1203,6 +1203,89 @@ async function scrapeHAnime(query) {
|
|
1203 |
}
|
1204 |
}
|
1205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1206 |
app.get('/hanime/search', async (req, res) => {
|
1207 |
const { query } = req.query;
|
1208 |
if (!query) {
|
@@ -1212,7 +1295,20 @@ app.get('/hanime/search', async (req, res) => {
|
|
1212 |
const result = await scrapeHAnime(query);
|
1213 |
res.json(result);
|
1214 |
} catch (error) {
|
1215 |
-
res.status(500).send('Error processing request');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1216 |
}
|
1217 |
});
|
1218 |
|
|
|
1203 |
}
|
1204 |
}
|
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);
|
1220 |
+
return element ? element.textContent.trim() : null;
|
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 {
|
1230 |
+
title: getText("body > main > article > aside.anime-cn.clb > header > h1"),
|
1231 |
+
tahunRilis: getText("body > main > article > aside.anime-cn.clb > header > div > p > a.mgr.mgb.fwb"),
|
1232 |
+
tahunUpload: getText("body > main > article > aside.anime-cn.clb > header > div > p > span:nth-child(9)"),
|
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 |
+
// Ambil video jika link iframe ditemukan
|
1256 |
+
let video = null;
|
1257 |
+
if (iframeSrc) {
|
1258 |
+
const { data: html } = await axios.get(iframeSrc);
|
1259 |
+
const $ = cheerio.load(html);
|
1260 |
+
const scripts = $('script');
|
1261 |
+
|
1262 |
+
scripts.each((i, script) => {
|
1263 |
+
const scriptContent = $(script).html();
|
1264 |
+
if (scriptContent && scriptContent.includes('jwplayer.setup')) {
|
1265 |
+
const match = scriptContent.match(/file:\s*"(https?:\/\/[^"]+\.mp4)"/);
|
1266 |
+
if (match && match[1]) {
|
1267 |
+
video = match[1];
|
1268 |
+
}
|
1269 |
+
}
|
1270 |
+
});
|
1271 |
+
}
|
1272 |
+
|
1273 |
+
// Tambahkan video ke hasil
|
1274 |
+
result.video = video;
|
1275 |
+
|
1276 |
+
console.log(video)
|
1277 |
+
|
1278 |
+
await browser.close();
|
1279 |
+
return result;
|
1280 |
+
|
1281 |
+
} catch (error) {
|
1282 |
+
console.log("Error scraping details:", error);
|
1283 |
+
await browser.close();
|
1284 |
+
return error;
|
1285 |
+
}
|
1286 |
+
}
|
1287 |
+
|
1288 |
+
|
1289 |
app.get('/hanime/search', async (req, res) => {
|
1290 |
const { query } = req.query;
|
1291 |
if (!query) {
|
|
|
1295 |
const result = await scrapeHAnime(query);
|
1296 |
res.json(result);
|
1297 |
} catch (error) {
|
1298 |
+
res.status(500).send('Error processing request \n\n' + error);
|
1299 |
+
}
|
1300 |
+
});
|
1301 |
+
|
1302 |
+
app.get('/hanime/download', async (req, res) => {
|
1303 |
+
const { url } = req.query;
|
1304 |
+
if (!url) {
|
1305 |
+
return res.status(400).send('url is required');
|
1306 |
+
}
|
1307 |
+
try {
|
1308 |
+
const result = await scrapeHAnimeDetails(url);
|
1309 |
+
res.json(result);
|
1310 |
+
} catch (error) {
|
1311 |
+
res.status(500).send('Error processing request \n\n' + error);
|
1312 |
}
|
1313 |
});
|
1314 |
|