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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const axios = require('axios');
|
| 3 |
+
const cheerio = require('cheerio');
|
| 4 |
+
|
| 5 |
+
// Mengimpor library lain sesuai request kamu (walau belum dipakai di fungsi utama)
|
| 6 |
+
const path = require('path');
|
| 7 |
+
const FormData = require('form-data');
|
| 8 |
+
const fs = require('fs');
|
| 9 |
+
const crypto = require('crypto');
|
| 10 |
+
const { v4: uuidv4 } = require('uuid');
|
| 11 |
+
// const fetch = require('node-fetch');
|
| 12 |
+
// const { OpenAI } = require('openai');
|
| 13 |
+
|
| 14 |
+
const app = express();
|
| 15 |
+
const PORT = 7860; // Port Wajib untuk Hugging Face Spaces
|
| 16 |
+
|
| 17 |
+
class komiku {
|
| 18 |
+
constructor() {
|
| 19 |
+
this.baseUrl = 'https://komiku.org';
|
| 20 |
+
this.headers = {
|
| 21 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 22 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
| 23 |
+
'Accept-Language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
|
| 24 |
+
'Referer': 'https://komiku.org/',
|
| 25 |
+
'Cache-Control': 'no-cache'
|
| 26 |
+
};
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
async searchManga(query) {
|
| 30 |
+
try {
|
| 31 |
+
const response = await axios.get(`${this.baseUrl}/`, {
|
| 32 |
+
params: { s: query, post_type: 'manga' },
|
| 33 |
+
headers: this.headers
|
| 34 |
+
});
|
| 35 |
+
const $ = cheerio.load(response.data);
|
| 36 |
+
const results = [];
|
| 37 |
+
$('.daftar .bge, .daftar .kan, .list-update .bge').each((i, el) => {
|
| 38 |
+
const title = $(el).find('.judul2 a, h3 a').text().trim();
|
| 39 |
+
const url = $(el).find('.judul2 a, h3 a').attr('href');
|
| 40 |
+
const image = $(el).find('img').attr('src');
|
| 41 |
+
const type = $(el).find('.tipe, .type').text().trim();
|
| 42 |
+
const description = $(el).find('p').text().trim();
|
| 43 |
+
|
| 44 |
+
if (title && title.toLowerCase().includes(query.toLowerCase())) {
|
| 45 |
+
results.push({
|
| 46 |
+
title,
|
| 47 |
+
url: url ? (url.startsWith('http') ? url : this.baseUrl + url) : null,
|
| 48 |
+
image: image ? (image.startsWith('http') ? image : 'https:' + image) : null,
|
| 49 |
+
type: type || 'Manga',
|
| 50 |
+
description: description || 'No description available'
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
});
|
| 54 |
+
return results;
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error('Search error:', error.message);
|
| 57 |
+
return [];
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
async getMangaDetail(url) {
|
| 62 |
+
try {
|
| 63 |
+
const response = await axios.get(url, { headers: this.headers });
|
| 64 |
+
const $ = cheerio.load(response.data);
|
| 65 |
+
const title = $('#Judul h1 span').first().text().trim() || $('h1.entry-title').text().trim();
|
| 66 |
+
const altTitle = $('.j2').text().trim().replace(':', '').trim();
|
| 67 |
+
const cover = $('.ims img').attr('src') || $('.thumb img').attr('src');
|
| 68 |
+
const author = $('.inftable tr:contains("Pengarang") td:last-child, .inftable tr:contains("Author") td:last-child').text().trim();
|
| 69 |
+
const status = $('.inftable tr:contains("Status") td:last-child').text().trim();
|
| 70 |
+
const type = $('.inftable tr:contains("Jenis Komik") td:last-child, .inftable tr:contains("Type") td:last-child').text().trim();
|
| 71 |
+
const genres = [];
|
| 72 |
+
|
| 73 |
+
$('.genre li a, .genres a').each((i, el) => {
|
| 74 |
+
const genre = $(el).text().trim();
|
| 75 |
+
if (genre && !genre.includes('Genre')) genres.push(genre);
|
| 76 |
+
});
|
| 77 |
+
|
| 78 |
+
const synopsis = $('.desc, .sinopsis, .entry-content p').first().text().trim();
|
| 79 |
+
const chapters = [];
|
| 80 |
+
|
| 81 |
+
$('#daftarChapter tr, .chapter-list li, .clstyle li').each((i, el) => {
|
| 82 |
+
if (i === 0 && $(el).is('tr')) return;
|
| 83 |
+
const chapterEl = $(el).find('.judulseries a, .chapter-title a, a');
|
| 84 |
+
const chapterTitle = chapterEl.text().trim();
|
| 85 |
+
const chapterUrl = chapterEl.attr('href');
|
| 86 |
+
const views = $(el).find('.pembaca i, .views').text().trim();
|
| 87 |
+
const date = $(el).find('.tanggalseries, .date').text().trim();
|
| 88 |
+
|
| 89 |
+
if (chapterTitle && chapterUrl) {
|
| 90 |
+
chapters.push({
|
| 91 |
+
title: chapterTitle,
|
| 92 |
+
url: chapterUrl.startsWith('http') ? chapterUrl : this.baseUrl + chapterUrl,
|
| 93 |
+
views: views ? parseInt(views.replace(/[^0-9]/g, '')) || 0 : 0,
|
| 94 |
+
date: date || 'Unknown'
|
| 95 |
+
});
|
| 96 |
+
}
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
chapters.sort((a, b) => {
|
| 100 |
+
const numA = parseFloat(a.title.replace(/[^0-9.]/g, ''));
|
| 101 |
+
const numB = parseFloat(b.title.replace(/[^0-9.]/g, ''));
|
| 102 |
+
return numB - numA;
|
| 103 |
+
});
|
| 104 |
+
|
| 105 |
+
const firstChapterUrl = chapters.length > 0 ? chapters[chapters.length - 1].url : null;
|
| 106 |
+
const latestChapterUrl = chapters.length > 0 ? chapters[0].url : null;
|
| 107 |
+
|
| 108 |
+
return {
|
| 109 |
+
title, altTitle: altTitle || title,
|
| 110 |
+
cover: cover ? (cover.startsWith('http') ? cover : 'https:' + cover) : null,
|
| 111 |
+
author: author || 'Unknown', status: status || 'Ongoing',
|
| 112 |
+
type: type || 'Manga', genres: [...new Set(genres)],
|
| 113 |
+
synopsis: synopsis || 'No synopsis available',
|
| 114 |
+
chapters: chapters.slice(0, 10), totalChapters: chapters.length,
|
| 115 |
+
firstChapterUrl, latestChapterUrl
|
| 116 |
+
};
|
| 117 |
+
} catch (error) {
|
| 118 |
+
console.error('Detail error:', error.message);
|
| 119 |
+
return null;
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
async getChapterImages(url) {
|
| 124 |
+
try {
|
| 125 |
+
const response = await axios.get(url, { headers: this.headers });
|
| 126 |
+
const $ = cheerio.load(response.data);
|
| 127 |
+
const title = $('#Judul h1').text().trim() || $('h1.entry-title').text().trim();
|
| 128 |
+
const seriesTitle = $('a[rel="tag"]').first().text().trim() || $('.series a').first().text().trim();
|
| 129 |
+
const seriesUrl = $('a[rel="tag"]').first().attr('href') || $('.series a').first().attr('href');
|
| 130 |
+
|
| 131 |
+
const images = [];
|
| 132 |
+
$('#Baca_Komik img, .chapter-image img, .reader-area img').each((i, el) => {
|
| 133 |
+
const imgUrl = $(el).attr('src') || $(el).attr('data-src') || $(el).attr('data-lazy-src');
|
| 134 |
+
if (imgUrl && imgUrl.startsWith('http')) {
|
| 135 |
+
images.push({ url: imgUrl, page: i + 1, alt: $(el).attr('alt') || `Page ${i + 1}` });
|
| 136 |
+
}
|
| 137 |
+
});
|
| 138 |
+
|
| 139 |
+
const prevChapter = $('a[aria-label="Prev"], a.prev, a:contains("Sebelumnya")').attr('href');
|
| 140 |
+
const nextChapter = $('a[aria-label="Next"], a.next, a:contains("Selanjutnya")').attr('href');
|
| 141 |
+
|
| 142 |
+
return {
|
| 143 |
+
title: title || 'Unknown Chapter',
|
| 144 |
+
series: {
|
| 145 |
+
title: seriesTitle || 'Unknown Series',
|
| 146 |
+
url: seriesUrl ? (seriesUrl.startsWith('http') ? seriesUrl : this.baseUrl + seriesUrl) : null
|
| 147 |
+
},
|
| 148 |
+
images: images.length > 0 ? images : [],
|
| 149 |
+
totalImages: images.length,
|
| 150 |
+
navigation: {
|
| 151 |
+
prev: prevChapter ? (prevChapter.startsWith('http') ? prevChapter : this.baseUrl + prevChapter) : null,
|
| 152 |
+
next: nextChapter ? (nextChapter.startsWith('http') ? nextChapter : this.baseUrl + nextChapter) : null
|
| 153 |
+
}
|
| 154 |
+
};
|
| 155 |
+
} catch (error) {
|
| 156 |
+
console.error('Chapter error:', error.message);
|
| 157 |
+
return null;
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
async getAll(query) {
|
| 162 |
+
const search = await this.searchManga(query);
|
| 163 |
+
if (search.length === 0) {
|
| 164 |
+
const fallbackDetail = await this.getMangaDetail(`https://komiku.org/manga/${query}-id/`);
|
| 165 |
+
if (fallbackDetail) {
|
| 166 |
+
const chapter = await this.getChapterImages(fallbackDetail.latestChapterUrl);
|
| 167 |
+
return {
|
| 168 |
+
query,
|
| 169 |
+
search: [{
|
| 170 |
+
title: fallbackDetail.title, url: `https://komiku.org/manga/${query}-id/`,
|
| 171 |
+
image: fallbackDetail.cover, type: fallbackDetail.type,
|
| 172 |
+
description: fallbackDetail.synopsis.substring(0, 200)
|
| 173 |
+
}],
|
| 174 |
+
detail: fallbackDetail, chapter
|
| 175 |
+
};
|
| 176 |
+
}
|
| 177 |
+
return { query, search: [], detail: null, chapter: null, message: 'No results found' };
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
const mangaUrl = search[0].url;
|
| 181 |
+
const detail = await this.getMangaDetail(mangaUrl);
|
| 182 |
+
const chapter = detail ? await this.getChapterImages(detail.latestChapterUrl) : null;
|
| 183 |
+
|
| 184 |
+
return { query, search: search.slice(0, 5), detail, chapter };
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
const scraper = new komiku();
|
| 189 |
+
|
| 190 |
+
// Endpoint halaman utama
|
| 191 |
+
app.get('/', (req, res) => {
|
| 192 |
+
res.send(`
|
| 193 |
+
<h1>Scraper API is Running</h1>
|
| 194 |
+
<p>Gunakan endpoint <code>/api/scrape?query=nama_komik</code></p>
|
| 195 |
+
<p>Contoh: <a href="/api/scrape?query=boruto">/api/scrape?query=boruto</a></p>
|
| 196 |
+
`);
|
| 197 |
+
});
|
| 198 |
+
|
| 199 |
+
// Endpoint untuk menjalankan scraper
|
| 200 |
+
app.get('/api/scrape', async (req, res) => {
|
| 201 |
+
const query = req.query.query;
|
| 202 |
+
|
| 203 |
+
if (!query) {
|
| 204 |
+
return res.status(400).json({ error: "Parameter 'query' dibutuhkan!" });
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
try {
|
| 208 |
+
const result = await scraper.getAll(query);
|
| 209 |
+
res.json(result);
|
| 210 |
+
} catch (error) {
|
| 211 |
+
res.status(500).json({ error: error.message });
|
| 212 |
+
}
|
| 213 |
+
});
|
| 214 |
+
|
| 215 |
+
// Start Server
|
| 216 |
+
app.listen(PORT, () => {
|
| 217 |
+
console.log(`Server jalan di port ${PORT}`);
|
| 218 |
+
});
|