Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -283,81 +283,12 @@ function generateRandomID(length = 8) {
|
|
| 283 |
return result;
|
| 284 |
}
|
| 285 |
|
| 286 |
-
async function Shinigami_download(url) {
|
| 287 |
-
const instanceID = generateRandomID();
|
| 288 |
-
const tempDir = path.join(tempDirBase, instanceID);
|
| 289 |
-
await fs.promises.mkdir(tempDir, { recursive: true });
|
| 290 |
-
|
| 291 |
-
const title = url.replace(/.*\/([^\/]+)\/([^\/]+)\/$/, "$1_$2");
|
| 292 |
-
|
| 293 |
-
const browser = await puppeteer.launch({
|
| 294 |
-
headless: true,
|
| 295 |
-
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
| 296 |
-
});
|
| 297 |
-
const page = await browser.newPage();
|
| 298 |
-
|
| 299 |
-
await page.setRequestInterception(true);
|
| 300 |
-
page.on('request', request => {
|
| 301 |
-
request.continue({
|
| 302 |
-
headers: {
|
| 303 |
-
...request.headers(),
|
| 304 |
-
'User-Agent': 'Mozilla/5.0 (Linux; Android 11; M2004J19C Build/RP1A.200720.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.129 Mobile Safari/537.36 WhatsApp/1.2.3',
|
| 305 |
-
'Referer': 'https://shinigami03.com/',
|
| 306 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
| 307 |
-
'Accept-Encoding': 'gzip, deflate, br'
|
| 308 |
-
}
|
| 309 |
-
});
|
| 310 |
-
});
|
| 311 |
-
|
| 312 |
-
await page.goto(url, { waitUntil: 'networkidle2' });
|
| 313 |
-
|
| 314 |
-
try {
|
| 315 |
-
const imgList = await page.evaluate(() => {
|
| 316 |
-
const elements = document.querySelectorAll("body > div.wrap > div > div > div > div > div > div > div > div > div.c-blog-post > div.entry-content > div > div > div.reading-content > div");
|
| 317 |
-
let data = [];
|
| 318 |
-
elements.forEach(element => {
|
| 319 |
-
const img = element.querySelector('span > img');
|
| 320 |
-
if (img) {
|
| 321 |
-
data.push({ path: img.src });
|
| 322 |
-
}
|
| 323 |
-
});
|
| 324 |
-
return data;
|
| 325 |
-
});
|
| 326 |
-
|
| 327 |
-
console.log(imgList)
|
| 328 |
-
|
| 329 |
-
const imagePaths = await downloadImages(imgList, tempDir, instanceID);
|
| 330 |
-
const pdfPath = await createPDF(instanceID, tempDir);
|
| 331 |
-
|
| 332 |
-
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 333 |
-
return {
|
| 334 |
-
path: pdfPath,
|
| 335 |
-
title: title,
|
| 336 |
-
url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPath)}`
|
| 337 |
-
};
|
| 338 |
-
} catch (error) {
|
| 339 |
-
console.error(error);
|
| 340 |
-
throw error;
|
| 341 |
-
} finally {
|
| 342 |
-
await browser.close();
|
| 343 |
-
await fs.promises.rmdir(tempDir, { recursive: true });
|
| 344 |
-
}
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
async function downloadImages(imgList, tempDir, instanceID) {
|
| 348 |
-
const imagePaths = [];
|
| 349 |
-
for (const img of imgList) {
|
| 350 |
-
const imagePath = await downloadImage(img, tempDir, instanceID);
|
| 351 |
-
imagePaths.push(imagePath);
|
| 352 |
-
}
|
| 353 |
-
return imagePaths;
|
| 354 |
-
}
|
| 355 |
-
|
| 356 |
async function komiku_download(url) {
|
| 357 |
const instanceID = generateRandomID();
|
| 358 |
const tempDir = path.join(tempDirBase, instanceID);
|
| 359 |
-
await
|
| 360 |
|
|
|
|
| 361 |
const title = url.split('/').filter(part => part).pop();
|
| 362 |
|
| 363 |
try {
|
|
@@ -370,34 +301,30 @@ async function komiku_download(url) {
|
|
| 370 |
const src = $(element).attr('src');
|
| 371 |
imgList.push({ path: src });
|
| 372 |
});
|
| 373 |
-
|
| 374 |
-
console.log(imgList)
|
| 375 |
|
| 376 |
await processImages(imgList, tempDir, instanceID);
|
| 377 |
const pdfPath = await createPDF(instanceID, tempDir);
|
| 378 |
|
| 379 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 380 |
-
return {
|
| 381 |
-
path: pdfPath,
|
| 382 |
-
title: title,
|
| 383 |
-
url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPath)}`
|
| 384 |
-
};
|
| 385 |
} catch (error) {
|
| 386 |
console.log(error);
|
| 387 |
throw error;
|
| 388 |
} finally {
|
| 389 |
-
await
|
| 390 |
}
|
| 391 |
}
|
| 392 |
|
| 393 |
async function downloadImage(image, tempDir, instanceID) {
|
| 394 |
-
const response = await axios.get(image.path, { responseType: 'arraybuffer'
|
| 395 |
-
|
| 396 |
-
'Referer': 'https://komiku.id/'
|
| 397 |
-
}});
|
| 398 |
-
const imagePath = `${tempDir}/image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`;
|
| 399 |
await writeFileAsync(imagePath, response.data);
|
| 400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
return imagePath;
|
| 402 |
}
|
| 403 |
|
|
@@ -407,7 +334,7 @@ async function getImageHeight(imagePath) {
|
|
| 407 |
}
|
| 408 |
|
| 409 |
async function processImages(imgList, tempDir, instanceID) {
|
| 410 |
-
const maxImagesPerPage = 10;
|
| 411 |
let partIndex = 0;
|
| 412 |
let partImages = [];
|
| 413 |
|
|
@@ -422,6 +349,7 @@ async function processImages(imgList, tempDir, instanceID) {
|
|
| 422 |
}
|
| 423 |
}
|
| 424 |
|
|
|
|
| 425 |
if (partImages.length > 0) {
|
| 426 |
await combineAndSave(partImages, partIndex, tempDir, instanceID);
|
| 427 |
}
|
|
@@ -449,45 +377,27 @@ async function createPDF(instanceID, tempDir) {
|
|
| 449 |
}
|
| 450 |
|
| 451 |
app.get('/komiku/download', async (req, res) => {
|
| 452 |
-
try {
|
| 453 |
-
const { url } = req.query;
|
| 454 |
-
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
| 455 |
-
|
| 456 |
-
let result = await komiku_download(url);
|
| 457 |
-
res.json(result);
|
| 458 |
-
|
| 459 |
try {
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
console.log(`File ${result.path} deleted.`);
|
| 463 |
-
} catch (error) {
|
| 464 |
-
console.error(`Error deleting file ${result.path}:`, error);
|
| 465 |
-
}
|
| 466 |
-
} catch (error) {
|
| 467 |
-
console.error('Error processing request:', error);
|
| 468 |
-
res.status(500).json({ error: 'Failed to process request\n' + error });
|
| 469 |
-
}
|
| 470 |
-
});
|
| 471 |
-
|
| 472 |
-
app.get('/shinigami/download', async (req, res) => {
|
| 473 |
-
try {
|
| 474 |
-
const { url } = req.query;
|
| 475 |
-
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
| 479 |
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
} catch (error) {
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
| 486 |
}
|
| 487 |
-
} catch (error) {
|
| 488 |
-
console.error('Error processing request:', error);
|
| 489 |
-
res.status(500).json({ error: 'Failed to process request\n' + error });
|
| 490 |
-
}
|
| 491 |
});
|
| 492 |
|
| 493 |
/***********/
|
|
|
|
| 283 |
return result;
|
| 284 |
}
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
async function komiku_download(url) {
|
| 287 |
const instanceID = generateRandomID();
|
| 288 |
const tempDir = path.join(tempDirBase, instanceID);
|
| 289 |
+
await fss.mkdir(tempDir);
|
| 290 |
|
| 291 |
+
// Extracting the title from the URL
|
| 292 |
const title = url.split('/').filter(part => part).pop();
|
| 293 |
|
| 294 |
try {
|
|
|
|
| 301 |
const src = $(element).attr('src');
|
| 302 |
imgList.push({ path: src });
|
| 303 |
});
|
|
|
|
|
|
|
| 304 |
|
| 305 |
await processImages(imgList, tempDir, instanceID);
|
| 306 |
const pdfPath = await createPDF(instanceID, tempDir);
|
| 307 |
|
| 308 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 309 |
+
return { path: pdfPath, title: title, url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPathq)}` };
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
} catch (error) {
|
| 311 |
console.log(error);
|
| 312 |
throw error;
|
| 313 |
} finally {
|
| 314 |
+
await fss.rmdir(tempDir, { recursive: true });
|
| 315 |
}
|
| 316 |
}
|
| 317 |
|
| 318 |
async function downloadImage(image, tempDir, instanceID) {
|
| 319 |
+
const response = await axios.get(image.path, { responseType: 'arraybuffer' });
|
| 320 |
+
const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
|
|
|
|
|
|
|
|
|
|
| 321 |
await writeFileAsync(imagePath, response.data);
|
| 322 |
|
| 323 |
+
const imageHeight = await getImageHeight(imagePath);
|
| 324 |
+
const newHeight = Math.floor(imageHeight * 0.7);
|
| 325 |
+
const command = `convert ${imagePath} -resize 720x${newHeight}! -quality 75 -background white -gravity center -extent 720x${newHeight} ${imagePath}`;
|
| 326 |
+
await execPromise(command);
|
| 327 |
+
|
| 328 |
return imagePath;
|
| 329 |
}
|
| 330 |
|
|
|
|
| 334 |
}
|
| 335 |
|
| 336 |
async function processImages(imgList, tempDir, instanceID) {
|
| 337 |
+
const maxImagesPerPage = 10; // Maksimal 10 gambar per halaman
|
| 338 |
let partIndex = 0;
|
| 339 |
let partImages = [];
|
| 340 |
|
|
|
|
| 349 |
}
|
| 350 |
}
|
| 351 |
|
| 352 |
+
// Jika masih ada gambar yang belum diproses
|
| 353 |
if (partImages.length > 0) {
|
| 354 |
await combineAndSave(partImages, partIndex, tempDir, instanceID);
|
| 355 |
}
|
|
|
|
| 377 |
}
|
| 378 |
|
| 379 |
app.get('/komiku/download', async (req, res) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
try {
|
| 381 |
+
const { url } = req.query;
|
| 382 |
+
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
+
let result = await komiku_download(url);
|
| 385 |
+
res.json(result);
|
| 386 |
|
| 387 |
+
// Menghapus file setelah 10 menit
|
| 388 |
+
try {
|
| 389 |
+
await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
|
| 390 |
+
await fss.unlink(result.path);
|
| 391 |
+
console.log(`File ${result.path} deleted.`);
|
| 392 |
+
} catch (error) {
|
| 393 |
+
console.error(`Error deleting file ${result.path}:`, error);
|
| 394 |
+
}
|
| 395 |
} catch (error) {
|
| 396 |
+
console.error('Error processing request:', error);
|
| 397 |
+
res.status(500).json({
|
| 398 |
+
error: 'Failed to process request\n' + error
|
| 399 |
+
});
|
| 400 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
});
|
| 402 |
|
| 403 |
/***********/
|