newsnow/server/sources/solidot.ts

27 lines
830 B
TypeScript
Raw Normal View History

2024-10-25 11:46:53 +08:00
import * as cheerio from "cheerio"
import type { NewsItem } from "@shared/types"
export default defineSource(async () => {
2024-10-25 12:05:24 +08:00
const baseURL = "https://www.solidot.org"
2024-10-25 11:46:53 +08:00
const html: any = await $fetch(baseURL)
const $ = cheerio.load(html)
const $main = $(".block_m")
const news: NewsItem[] = []
$main.each((_, el) => {
2024-10-25 12:05:24 +08:00
const a = $(el).find(".bg_htit a").last()
2024-10-25 11:46:53 +08:00
const url = a.attr("href")
const title = a.text()
const date_raw = $(el).find(".talk_time").text().match(/发表于(.*?分)/)?.[1]
const date = date_raw?.replace(/[年月]/g, "-").replace("时", ":").replace(/[分日]/g, "")
if (url && title && date) {
news.push({
url: baseURL + url,
title,
id: url,
pubDate: parseRelativeDate(date, "Asia/Shanghai").valueOf(),
})
}
})
return news
})