newsnow/server/sources/zaobao.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-10-03 13:16:14 +08:00
import { Buffer } from "node:buffer"
import * as cheerio from "cheerio"
import iconv from "iconv-lite"
2024-10-04 15:36:03 +08:00
import type { NewsItem } from "@shared/types"
import { $fetch } from "ofetch"
2024-10-03 13:16:14 +08:00
2024-10-04 15:36:03 +08:00
export default defineSource(async () => {
2024-10-03 23:11:10 +08:00
const response: ArrayBuffer = await $fetch("https://www.kzaobao.com/top.html", {
2024-10-03 13:16:14 +08:00
responseType: "arrayBuffer",
})
const base = "https://www.kzaobao.com"
const utf8String = iconv.decode(Buffer.from(response), "gb2312")
const $ = cheerio.load(utf8String)
2024-10-04 15:36:03 +08:00
const $main = $("div[id^='cd0'] tr")
2024-10-03 13:16:14 +08:00
const news: NewsItem[] = []
2024-10-04 15:36:03 +08:00
$main.each((_, el) => {
2024-10-03 13:16:14 +08:00
const a = $(el).find("h3>a")
// https://www.kzaobao.com/shiju/20241002/170659.html
const url = a.attr("href")
const title = a.text()
2024-10-04 15:36:03 +08:00
const date = $(el).find("td:nth-child(3)").text()
if (url && title && date) {
2024-10-03 13:16:14 +08:00
news.push({
url: base + url,
title,
id: url,
extra: {
2024-10-07 22:35:50 +08:00
date: tranformToUTC(date),
2024-10-03 13:16:14 +08:00
},
})
}
})
2024-10-07 22:35:50 +08:00
return news.sort((m, n) => n.extra!.date > m.extra!.date ? 1 : -1)
2024-10-13 13:59:23 +08:00
.slice(0, 30)
2024-10-04 15:36:03 +08:00
})