32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
import { load } from 'cheerio'
|
|
import http from './utils/http.js'
|
|
|
|
/**
|
|
* Cari berita di CNN Indonesia
|
|
* @param {string} keyword
|
|
* @param {number} limit
|
|
*/
|
|
export async function searchCNN(keyword, limit = 10) {
|
|
try {
|
|
const url = `https://www.cnnindonesia.com/search?query=${encodeURIComponent(keyword)}`
|
|
const { data } = await http.get(url)
|
|
const $ = load(data)
|
|
const results = []
|
|
|
|
$('.list_content article, .collection_list article, article').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 = 'CNN Indonesia'
|
|
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: 'cnnindonesia', data: results }
|
|
} catch (err) {
|
|
return { status: false, source: 'cnnindonesia', error: err.response?.status || err.message }
|
|
}
|
|
}
|
|
|