mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 03:09:14 +08:00
d7c8a38d88
fixed #20
30 lines
736 B
TypeScript
30 lines
736 B
TypeScript
import * as cheerio from "cheerio"
|
|
import type { NewsItem } from "@shared/types"
|
|
|
|
export default defineSource(async () => {
|
|
const baseURL = "https://news.ycombinator.com"
|
|
const html: any = await myFetch(baseURL)
|
|
const $ = cheerio.load(html)
|
|
const $main = $(".athing")
|
|
const news: NewsItem[] = []
|
|
$main.each((_, el) => {
|
|
const a = $(el).find(".titleline a").first()
|
|
// const url = a.attr("href")
|
|
const title = a.text()
|
|
const id = $(el).attr("id")
|
|
const score = $(`#score_${id}`).text()
|
|
const url = `${baseURL}/item?id=${id}`
|
|
if (url && id && title) {
|
|
news.push({
|
|
url,
|
|
title,
|
|
id,
|
|
extra: {
|
|
info: score,
|
|
},
|
|
})
|
|
}
|
|
})
|
|
return news
|
|
})
|