Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -285,19 +285,17 @@ function generateRandomID(length = 8) {
|
|
| 285 |
|
| 286 |
async function Shinigami_download(url) {
|
| 287 |
const instanceID = generateRandomID();
|
| 288 |
-
const tempDir =
|
| 289 |
-
await
|
| 290 |
|
| 291 |
-
// Extracting the title from the URL
|
| 292 |
const title = url.replace(/.*\/([^\/]+)\/([^\/]+)\/$/, "$1_$2");
|
| 293 |
|
| 294 |
const browser = await puppeteer.launch({
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
const page = await browser.newPage();
|
| 299 |
|
| 300 |
-
// Enable request interception to add custom headers
|
| 301 |
await page.setRequestInterception(true);
|
| 302 |
page.on('request', request => {
|
| 303 |
request.continue({
|
|
@@ -327,16 +325,20 @@ async function Shinigami_download(url) {
|
|
| 327 |
});
|
| 328 |
|
| 329 |
const imagePaths = await downloadImages(imgList, tempDir, instanceID);
|
| 330 |
-
const pdfPath = await createPDF(
|
| 331 |
|
| 332 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 333 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
} catch (error) {
|
| 335 |
console.error(error);
|
| 336 |
throw error;
|
| 337 |
} finally {
|
| 338 |
await browser.close();
|
| 339 |
-
await
|
| 340 |
}
|
| 341 |
}
|
| 342 |
|
|
@@ -352,9 +354,8 @@ async function downloadImages(imgList, tempDir, instanceID) {
|
|
| 352 |
async function komiku_download(url) {
|
| 353 |
const instanceID = generateRandomID();
|
| 354 |
const tempDir = path.join(tempDirBase, instanceID);
|
| 355 |
-
await
|
| 356 |
|
| 357 |
-
// Extracting the title from the URL
|
| 358 |
const title = url.split('/').filter(part => part).pop();
|
| 359 |
|
| 360 |
try {
|
|
@@ -372,12 +373,16 @@ async function komiku_download(url) {
|
|
| 372 |
const pdfPath = await createPDF(instanceID, tempDir);
|
| 373 |
|
| 374 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 375 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
} catch (error) {
|
| 377 |
console.log(error);
|
| 378 |
throw error;
|
| 379 |
} finally {
|
| 380 |
-
await
|
| 381 |
}
|
| 382 |
}
|
| 383 |
|
|
@@ -400,7 +405,7 @@ async function getImageHeight(imagePath) {
|
|
| 400 |
}
|
| 401 |
|
| 402 |
async function processImages(imgList, tempDir, instanceID) {
|
| 403 |
-
const maxImagesPerPage = 10;
|
| 404 |
let partIndex = 0;
|
| 405 |
let partImages = [];
|
| 406 |
|
|
@@ -415,7 +420,6 @@ async function processImages(imgList, tempDir, instanceID) {
|
|
| 415 |
}
|
| 416 |
}
|
| 417 |
|
| 418 |
-
// Jika masih ada gambar yang belum diproses
|
| 419 |
if (partImages.length > 0) {
|
| 420 |
await combineAndSave(partImages, partIndex, tempDir, instanceID);
|
| 421 |
}
|
|
@@ -442,52 +446,49 @@ async function createPDF(instanceID, tempDir) {
|
|
| 442 |
return pdfPath;
|
| 443 |
}
|
| 444 |
|
|
|
|
|
|
|
|
|
|
| 445 |
app.get('/komiku/download', async (req, res) => {
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
console.log(`File ${result.path} deleted.`);
|
| 458 |
-
} catch (error) {
|
| 459 |
-
console.error(`Error deleting file ${result.path}:`, error);
|
| 460 |
-
}
|
| 461 |
} catch (error) {
|
| 462 |
-
|
| 463 |
-
res.status(500).json({
|
| 464 |
-
error: 'Failed to process request\n' + error
|
| 465 |
-
});
|
| 466 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
});
|
| 468 |
|
| 469 |
app.get('/shinigami/download', async (req, res) => {
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
console.log(`File ${result.path} deleted.`);
|
| 482 |
-
} catch (error) {
|
| 483 |
-
console.error(`Error deleting file ${result.path}:`, error);
|
| 484 |
-
}
|
| 485 |
} catch (error) {
|
| 486 |
-
|
| 487 |
-
res.status(500).json({
|
| 488 |
-
error: 'Failed to process request\n' + error
|
| 489 |
-
});
|
| 490 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
});
|
| 492 |
|
| 493 |
/***********/
|
|
|
|
| 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({
|
|
|
|
| 325 |
});
|
| 326 |
|
| 327 |
const imagePaths = await downloadImages(imgList, tempDir, instanceID);
|
| 328 |
+
const pdfPath = await createPDF(instanceID, tempDir);
|
| 329 |
|
| 330 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 331 |
+
return {
|
| 332 |
+
path: pdfPath,
|
| 333 |
+
title: title,
|
| 334 |
+
url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPath)}`
|
| 335 |
+
};
|
| 336 |
} catch (error) {
|
| 337 |
console.error(error);
|
| 338 |
throw error;
|
| 339 |
} finally {
|
| 340 |
await browser.close();
|
| 341 |
+
await fs.promises.rmdir(tempDir, { recursive: true });
|
| 342 |
}
|
| 343 |
}
|
| 344 |
|
|
|
|
| 354 |
async function komiku_download(url) {
|
| 355 |
const instanceID = generateRandomID();
|
| 356 |
const tempDir = path.join(tempDirBase, instanceID);
|
| 357 |
+
await fs.promises.mkdir(tempDir, { recursive: true });
|
| 358 |
|
|
|
|
| 359 |
const title = url.split('/').filter(part => part).pop();
|
| 360 |
|
| 361 |
try {
|
|
|
|
| 373 |
const pdfPath = await createPDF(instanceID, tempDir);
|
| 374 |
|
| 375 |
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 376 |
+
return {
|
| 377 |
+
path: pdfPath,
|
| 378 |
+
title: title,
|
| 379 |
+
url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPath)}`
|
| 380 |
+
};
|
| 381 |
} catch (error) {
|
| 382 |
console.log(error);
|
| 383 |
throw error;
|
| 384 |
} finally {
|
| 385 |
+
await fs.promises.rmdir(tempDir, { recursive: true });
|
| 386 |
}
|
| 387 |
}
|
| 388 |
|
|
|
|
| 405 |
}
|
| 406 |
|
| 407 |
async function processImages(imgList, tempDir, instanceID) {
|
| 408 |
+
const maxImagesPerPage = 10;
|
| 409 |
let partIndex = 0;
|
| 410 |
let partImages = [];
|
| 411 |
|
|
|
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
|
|
|
| 423 |
if (partImages.length > 0) {
|
| 424 |
await combineAndSave(partImages, partIndex, tempDir, instanceID);
|
| 425 |
}
|
|
|
|
| 446 |
return pdfPath;
|
| 447 |
}
|
| 448 |
|
| 449 |
+
// Express setup
|
| 450 |
+
const app = express();
|
| 451 |
+
|
| 452 |
app.get('/komiku/download', async (req, res) => {
|
| 453 |
+
try {
|
| 454 |
+
const { url } = req.query;
|
| 455 |
+
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
| 456 |
|
| 457 |
+
let result = await komiku_download(url);
|
| 458 |
+
res.json(result);
|
| 459 |
|
| 460 |
+
try {
|
| 461 |
+
await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
|
| 462 |
+
await fs.promises.unlink(result.path);
|
| 463 |
+
console.log(`File ${result.path} deleted.`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
} catch (error) {
|
| 465 |
+
console.error(`Error deleting file ${result.path}:`, error);
|
|
|
|
|
|
|
|
|
|
| 466 |
}
|
| 467 |
+
} catch (error) {
|
| 468 |
+
console.error('Error processing request:', error);
|
| 469 |
+
res.status(500).json({ error: 'Failed to process request\n' + error });
|
| 470 |
+
}
|
| 471 |
});
|
| 472 |
|
| 473 |
app.get('/shinigami/download', async (req, res) => {
|
| 474 |
+
try {
|
| 475 |
+
const { url } = req.query;
|
| 476 |
+
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
| 477 |
|
| 478 |
+
let result = await Shinigami_download(url);
|
| 479 |
+
res.json(result);
|
| 480 |
|
| 481 |
+
try {
|
| 482 |
+
await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
|
| 483 |
+
await fs.promises.unlink(result.path);
|
| 484 |
+
console.log(`File ${result.path} deleted.`);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
} catch (error) {
|
| 486 |
+
console.error(`Error deleting file ${result.path}:`, error);
|
|
|
|
|
|
|
|
|
|
| 487 |
}
|
| 488 |
+
} catch (error) {
|
| 489 |
+
console.error('Error processing request:', error);
|
| 490 |
+
res.status(500).json({ error: 'Failed to process request\n' + error });
|
| 491 |
+
}
|
| 492 |
});
|
| 493 |
|
| 494 |
/***********/
|