31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
|
|
import { load } from 'cheerio'
|
||
|
|
import http from './utils/http.js'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Cari berita di Detik.com
|
||
|
|
* @param {string} keyword
|
||
|
|
* @param {number} limit
|
||
|
|
*/
|
||
|
|
export async function searchDetik(keyword, limit = 10) {
|
||
|
|
try {
|
||
|
|
const url = `https://www.detik.com/search/searchall?query=${encodeURIComponent(keyword)}`
|
||
|
|
const { data } = await http.get(url)
|
||
|
|
const $ = load(data)
|
||
|
|
const results = []
|
||
|
|
|
||
|
|
$('article').each((_, el) => {
|
||
|
|
if (results.length >= limit) return false
|
||
|
|
const title = $(el).find('h3, h2, .title').first().text().trim()
|
||
|
|
const link = $(el).find('a').first().attr('href') ?? null
|
||
|
|
const time = $(el).find('.date, time').first().text().trim() || null
|
||
|
|
const source = $(el).find('.channel, .category').first().text().trim() || 'Detik'
|
||
|
|
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: 'detik', data: results }
|
||
|
|
} catch (err) {
|
||
|
|
return { status: false, source: 'detik', error: err.response?.status || err.message }
|
||
|
|
}
|
||
|
|
}
|