File size: 11,738 Bytes
2e7ea18 9f780bd e9c5527 9f780bd af34501 ac08f29 af34501 40d121e e9c5527 40d121e 9f780bd af34501 9f780bd 40d121e 9a73cbf af34501 40d121e 9a73cbf 9f780bd 9a73cbf 40d121e 9a73cbf 40d121e 9db0314 9a73cbf 9db0314 40d121e 9a73cbf 40d121e 9a73cbf 40d121e 9a73cbf 40d121e 9a73cbf 40d121e 9f780bd 9a73cbf 40d121e af34501 40d121e af34501 40d121e af34501 40d121e af34501 40d121e af34501 40d121e af34501 40d121e af34501 40d121e 9a73cbf 40d121e 9a73cbf 40d121e 9a73cbf 40d121e 9f780bd af34501 40d121e af34501 1252866 40d121e 1252866 af34501 40d121e af34501 979deb4 40d121e af34501 40d121e c209578 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
import express from 'express';
import { chromium } from 'playwright';
import cors from 'cors';
import bodyParser from 'body-parser'
const app = express();
const PORT = process.env.PORT || 7860;
app.set('json spaces', 2)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(express.json({ limit: '500mb' }));
app.use(cors());
async function extractContentFromUrl(url, browser) {
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
viewport: { width: 1920, height: 1080 },
locale: 'en-US',
timezoneId: 'America/New_York'
});
const page = await context.newPage();
try {
// Intercept dan block resource yang tidak perlu
await page.route('**/*', (route) => {
const resourceType = route.request().resourceType();
if (['font', 'media'].includes(resourceType)) {
route.abort();
} else {
route.continue();
}
});
await page.goto(url, {
waitUntil: 'networkidle',
timeout: 60000
});
// Wait for content to be visible
await page.waitForSelector('body', { state: 'visible', timeout: 30000 });
// Scroll untuk trigger lazy loading
await page.evaluate(() => {
return new Promise((resolve) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
window.scrollTo(0, 0);
resolve();
}
}, 100);
});
});
// Wait tambahan setelah scroll
await page.waitForTimeout(2000);
const content = await page.evaluate(() => {
const cleanText = (text) => {
return text ? text.replace(/\s+/g, ' ').trim() : '';
};
// Helper function untuk mendapatkan text content yang lebih baik
const getTextContent = (element) => {
if (!element) return '';
// Clone element untuk manipulasi
const clone = element.cloneNode(true);
// Remove script dan style tags
const scripts = clone.querySelectorAll('script, style, noscript');
scripts.forEach(el => el.remove());
// Get text content
return cleanText(clone.textContent || clone.innerText || '');
};
const title = document.title || '';
const metaDescription = document.querySelector('meta[name="description"]')?.content ||
document.querySelector('meta[property="og:description"]')?.content || '';
// Improved heading extraction
const h1Elements = Array.from(document.querySelectorAll('h1'))
.map(h1 => getTextContent(h1))
.filter(text => text.length > 0);
const h2Elements = Array.from(document.querySelectorAll('h2'))
.map(h2 => getTextContent(h2))
.filter(text => text.length > 0);
// Improved paragraph extraction
const paragraphs = Array.from(document.querySelectorAll('p'))
.map(p => getTextContent(p))
.filter(text => text.length > 20);
// Try multiple selectors for main content
const contentSelectors = [
'main',
'article',
'[role="main"]',
'.content',
'#content',
'.post-content',
'.entry-content',
'.article-content',
'.page-content',
'.main-content',
'[itemprop="articleBody"]',
'.story-body',
'.article-body'
];
let mainContent = null;
for (const selector of contentSelectors) {
mainContent = document.querySelector(selector);
if (mainContent) break;
}
// Fallback: jika tidak ada main content, ambil dari body
if (!mainContent) {
mainContent = document.body;
}
const mainText = getTextContent(mainContent);
// Jika mainText masih kosong, coba ambil semua text dari div yang panjang
let fallbackText = '';
if (!mainText || mainText.length < 100) {
const allDivs = Array.from(document.querySelectorAll('div'))
.map(div => getTextContent(div))
.filter(text => text.length > 200)
.sort((a, b) => b.length - a.length);
fallbackText = allDivs[0] || '';
}
const finalMainText = mainText || fallbackText;
// Extract links dengan filter yang lebih baik
const links = Array.from(document.querySelectorAll('a[href]'))
.map(a => ({
text: getTextContent(a),
href: a.href
}))
.filter(link => link.text && link.href && !link.href.startsWith('javascript:'))
.slice(0, 20);
// Extract images dengan filter yang lebih baik
const images = Array.from(document.querySelectorAll('img[src]'))
.filter(img => img.src && !img.src.includes('data:image'))
.map(img => ({
src: img.src,
alt: img.alt || '',
title: img.title || ''
}))
.slice(0, 10);
// Calculate word count
const allText = finalMainText || paragraphs.join(' ') || document.body.innerText || '';
const wordCount = allText.split(/\s+/).filter(word => word.length > 0).length;
return {
title,
metaDescription,
headings: {
h1: h1Elements,
h2: h2Elements
},
paragraphs,
mainText: finalMainText,
links,
images,
wordCount,
hasContent: wordCount > 50 // Flag untuk mengecek apakah ada konten
};
});
return {
url,
success: true,
content,
extractedAt: new Date().toISOString()
};
} catch (error) {
console.error(`Error extracting ${url}:`, error);
return {
url,
success: false,
error: error.message,
extractedAt: new Date().toISOString()
};
} finally {
await context.close();
}
}
app.post('/extract-content', async (req, res) => {
const { urls } = req.body;
if (!urls || !Array.isArray(urls)) {
return res.status(400).json({
success: false,
message: 'Body harus berisi array urls'
});
}
if (urls.length === 0) {
return res.status(400).json({
success: false,
message: 'Array urls tidak boleh kosong'
});
}
if (urls.length > 10) {
return res.status(400).json({
success: false,
message: 'Maksimal 10 URLs per request'
});
}
const validUrls = [];
const invalidUrls = [];
urls.forEach(url => {
try {
new URL(url);
validUrls.push(url);
} catch (error) {
invalidUrls.push(url);
}
});
if (invalidUrls.length > 0) {
return res.status(400).json({
success: false,
message: 'Format URL tidak valid',
invalidUrls
});
}
let browser;
try {
browser = await chromium.launch({
args: [
'--incognito',
'--single-process',
'--no-sandbox',
'--no-zygote',
'--no-cache',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--disable-accelerated-2d-canvas',
'--disable-gpu'
],
executablePath: process.env.CHROME_BIN,
headless: true,
});
const concurrencyLimit = 3;
const results = [];
for (let i = 0; i < validUrls.length; i += concurrencyLimit) {
const batch = validUrls.slice(i, i + concurrencyLimit);
const batchPromises = batch.map(url => extractContentFromUrl(url, browser));
const batchResults = await Promise.all(batchPromises);
results.push(...batchResults);
}
const successCount = results.filter(r => r.success).length;
const failCount = results.filter(r => !r.success).length;
const emptyContentCount = results.filter(r => r.success && (!r.content.hasContent || r.content.wordCount < 50)).length;
res.json({
success: true,
message: `Berhasil memproses ${validUrls.length} URLs`,
statistics: {
total: validUrls.length,
success: successCount,
failed: failCount,
emptyContent: emptyContentCount
},
results
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({
success: false,
message: 'Terjadi kesalahan saat memproses URLs',
error: error.message
});
} finally {
if (browser) {
await browser.close();
}
}
});
app.get('/health', (req, res) => {
res.json({
success: true,
message: 'Content Extractor API is running',
timestamp: new Date().toISOString()
});
});
app.get('/', (req, res) => {
res.json({
success: true,
message: 'Content Extractor API',
endpoints: {
'POST /extract-content': 'Extract content from URLs',
'GET /health': 'Health check',
'GET /': 'API information'
},
usage: {
method: 'POST',
endpoint: '/extract-content',
body: {
urls: ['https://example.com', 'https://another-site.com']
}
}
});
});
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).json({
success: false,
message: 'Internal server error',
error: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
});
});
app.use((req, res) => {
res.status(404).json({
success: false,
message: 'Endpoint not found'
});
});
app.listen(PORT, () => {
console.log(`π Content Extractor API running on port ${PORT}`);
console.log(`π API Documentation: http://localhost:${PORT}`);
console.log(`π₯ Health Check: http://localhost:${PORT}/health`);
}); |