scraper/scrapers/more.js

198 lines
9.3 KiB
JavaScript
Raw Permalink Normal View History

2026-04-21 20:21:04 +08:00
import { load } from 'cheerio'
import http from './utils/http.js'
// ─── Tribunnews ───────────────────────────────────────────────────────────────
export async function searchTribun(keyword, limit = 10) {
try {
const url = `https://www.tribunnews.com/search?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('li.lsi').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h4 a, h3 a').first().text().trim()
const link = $(el).find('h4 a, h3 a').first().attr('href') ?? null
const time = $(el).find('.time').text().trim() || null
const source = 'Tribunnews'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'tribun', data: results }
} catch (err) {
return { status: false, source: 'tribun', error: err.response?.status || err.message }
}
}
// ─── Tempo ────────────────────────────────────────────────────────────────────
export async function searchTempo(keyword, limit = 10) {
try {
const url = `https://www.tempo.co/search?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, .card-box').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, .title').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date').first().text().trim() || null
const source = 'Tempo'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'tempo', data: results }
} catch (err) {
return { status: false, source: 'tempo', error: err.response?.status || err.message }
}
}
// ─── Republika ────────────────────────────────────────────────────────────────
export async function searchRepublika(keyword, limit = 10) {
try {
const url = `https://republika.co.id/search?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, .item-berita, .search-result-item').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, h4').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date, span').first().text().trim() || null
const source = 'Republika'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'republika', data: results }
} catch (err) {
return { status: false, source: 'republika', error: err.response?.status || err.message }
}
}
// ─── Antara News ──────────────────────────────────────────────────────────────
export async function searchAntara(keyword, limit = 10) {
try {
const url = `https://www.antaranews.com/search?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, .list-berita li, .simple-list li').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, h4, .title').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date, span').first().text().trim() || null
const source = 'Antara'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'antara', data: results }
} catch (err) {
return { status: false, source: 'antara', error: err.response?.status || err.message }
}
}
// ─── Okezone ──────────────────────────────────────────────────────────────────
export async function searchOkezone(keyword, limit = 10) {
try {
const url = `https://search.okezone.com/search?search=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('.list-berita li, article, .content-news').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, h4, .title').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date, .waktu').first().text().trim() || null
const source = 'Okezone'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'okezone', data: results }
} catch (err) {
return { status: false, source: 'okezone', error: err.response?.status || err.message }
}
}
// ─── Sindonews ────────────────────────────────────────────────────────────────
export async function searchSindonews(keyword, limit = 10) {
try {
const url = `https://search.sindonews.com/?query=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, .most-popular-item, .news-item').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, h4').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date').first().text().trim() || null
const source = 'Sindonews'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'sindonews', data: results }
} catch (err) {
return { status: false, source: 'sindonews', error: err.response?.status || err.message }
}
}
// ─── Merdeka.com ──────────────────────────────────────────────────────────────
export async function searchMerdeka(keyword, limit = 10) {
try {
const url = `https://www.merdeka.com/search/?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, .search-result, .list-berita li').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, h4').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, .date, span').first().text().trim() || null
const source = 'Merdeka'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) results.push({ title, link, time, source, thumb })
})
return { status: true, total: results.length, source: 'merdeka', data: results }
} catch (err) {
return { status: false, source: 'merdeka', error: err.response?.status || err.message }
}
}
// ─── Kumparan ─────────────────────────────────────────────────────────────────
export async function searchKumparan(keyword, limit = 10) {
try {
const url = `https://kumparan.com/search?q=${encodeURIComponent(keyword)}`
const { data } = await http.get(url)
const $ = load(data)
const results = []
$('article, [class*="StoryItem"], [class*="story-item"]').each((_, el) => {
if (results.length >= limit) return false
const title = $(el).find('h2, h3, [class*="title"]').first().text().trim()
const link = $(el).find('a').first().attr('href') ?? null
const time = $(el).find('time, [class*="date"]').first().text().trim() || null
const source = 'Kumparan'
const thumb = $(el).find('img').attr('src') ?? null
if (title && link) {
const fullLink = link.startsWith('http') ? link : `https://kumparan.com${link}`
results.push({ title, link: fullLink, time, source, thumb })
}
})
return { status: true, total: results.length, source: 'kumparan', data: results }
} catch (err) {
return { status: false, source: 'kumparan', error: err.response?.status || err.message }
}
}