Spaces:
Sleeping
Sleeping
import express from 'express'; | |
import path from 'path'; | |
import os from 'os'; | |
import PDFDocument from 'pdfkit'; | |
import fs from 'fs'; | |
import axios from 'axios'; | |
import cheerio from 'cheerio'; | |
import { promisify } from 'util'; | |
const require = createRequire(import.meta.url); | |
const puppeteer = require('puppeteer'); | |
import { fileURLToPath } from 'url'; | |
const PORT = process.env.PORT || 7860; | |
const app = express(); | |
const writeFileAsync = promisify(fs.writeFile); | |
const fss = fs.promises; | |
app.use('/static', express.static(os.tmpdir())); | |
function generateRandomID(length = 8) { | |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
let result = ''; | |
for (let i = 0; i < length; i++) { | |
result += characters.charAt(Math.floor(Math.random() * characters.length)); | |
} | |
return result; | |
} | |
async function komiku_download(url) { | |
const instanceID = generateRandomID(); | |
const tempDir = path.join(os.tmpdir(), instanceID); | |
await fss.mkdir(tempDir); | |
const title = url.split('/').filter(part => part).pop(); | |
try { | |
const response = await axios.get(url, { | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]', | |
'Referer': 'https://komiku.id/' | |
} | |
}); | |
const html = response.data; | |
const $ = cheerio.load(html); | |
const imgList = []; | |
$('#Baca_Komik img').each((index, element) => { | |
const src = $(element).attr('src'); | |
imgList.push({ path: src }); | |
}); | |
const imagePaths = await downloadImages(imgList, tempDir, instanceID); | |
const pdfPath = await createPDF(imagePaths, instanceID, tempDir); | |
return { path: `/static/${instanceID}.pdf`, title: title }; | |
} catch (error) { | |
console.log(error); | |
throw error; | |
} finally { | |
await fss.rmdir(tempDir, { recursive: true }); | |
} | |
} | |
async function downloadImage(image, tempDir, instanceID) { | |
const response = await axios.get(image.path, { | |
responseType: 'arraybuffer', | |
headers: { | |
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]', | |
'Referer': 'https://komiku.id/' | |
} | |
}); | |
const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`); | |
await writeFileAsync(imagePath, response.data); | |
return imagePath; | |
} | |
async function downloadImages(imgList, tempDir, instanceID) { | |
const imagePaths = []; | |
for (const img of imgList) { | |
const imagePath = await downloadImage(img, tempDir, instanceID); | |
imagePaths.push(imagePath); | |
} | |
return imagePaths; | |
} | |
async function createPDF(imagePaths, instanceID, tempDir) { | |
const pdfPath = path.join(os.tmpdir(), `${instanceID}.pdf`); | |
const doc = new PDFDocument({ autoFirstPage: false }); | |
doc.pipe(fs.createWriteStream(pdfPath)); | |
for (const imagePath of imagePaths) { | |
const { width, height } = await getImageDimensions(imagePath); | |
doc.addPage({ size: [width, height] }); | |
doc.image(imagePath, 0, 0, { width: width, height: height }); | |
} | |
doc.end(); | |
return pdfPath; | |
} | |
async function getImageDimensions(imagePath) { | |
const sizeOf = promisify(require('image-size')); | |
const dimensions = await sizeOf(imagePath); | |
return dimensions; | |
} | |
app.get('/download', async (req, res) => { | |
const { url } = req.query; | |
if (!url) { | |
return res.status(400).send('URL is required'); | |
} | |
try { | |
const result = await komiku_download(url); | |
res.json(result); | |
} catch (error) { | |
res.status(500).send('Error processing request'); | |
} | |
}); | |
// Fungsi untuk ping website | |
async function pingWebsite() { | |
const browser = await puppeteer.launch({ | |
headless: true, | |
args: ['--no-sandbox', '--disable-setuid-sandbox'] | |
}); | |
const page = await browser.newPage(); | |
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;]"); | |
await page.goto('https://huggingface.co/spaces/ArashiCode/komik/'); | |
console.log("Ping"); | |
await browser.close(); | |
} | |
// Ping website setiap 5 jam | |
async function pingEvery5Hours() { | |
await pingWebsite(); | |
setInterval(async () => { | |
await pingWebsite(); | |
}, 5 * 60 * 60 * 1000); // 5 hours in milliseconds | |
} | |
// Mulai ping | |
pingEvery5Hours(); | |
app.listen(PORT, () => { | |
console.log(`Server is running on port ${PORT}`); | |
}); |