From f514f7ee5f2922c575ae997bc177396165d8edb5 Mon Sep 17 00:00:00 2001 From: ourongxing Date: Sat, 26 Apr 2025 17:18:13 +0800 Subject: [PATCH] feat: try to proxy some source by vercel chore: test proxy chore: test proxy --- server/sources/coolapk/index.ts | 1 - server/sources/ghxi.ts | 32 ++++++++++++++++++-------------- server/sources/sputniknewscn.ts | 5 ++++- server/utils/source.ts | 10 ++++++++++ shared/pre-sources.ts | 3 --- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/server/sources/coolapk/index.ts b/server/sources/coolapk/index.ts index 18bd344..ca3fc6a 100644 --- a/server/sources/coolapk/index.ts +++ b/server/sources/coolapk/index.ts @@ -27,7 +27,6 @@ export default defineSource({ headers: await genHeaders(), }) if (!r.data.length) throw new Error("Failed to fetch") - console.log(r.data[0]) return r.data.filter(k => k.id).map(i => ({ id: i.id, title: i.editor_title || load(i.message).text().split("\n")[0], diff --git a/server/sources/ghxi.ts b/server/sources/ghxi.ts index d30991c..4ccb5aa 100644 --- a/server/sources/ghxi.ts +++ b/server/sources/ghxi.ts @@ -1,7 +1,8 @@ import * as cheerio from "cheerio" import type { NewsItem } from "@shared/types" +import { proxySource } from "#/utils/source" -const relativeTimeToDate = function (timeStr) { +const relativeTimeToDate = function (timeStr: string) { const units = { 秒: 1000, 分钟: 60 * 1000, @@ -18,17 +19,17 @@ const relativeTimeToDate = function (timeStr) { } const num = Number.parseInt(match[1]) - const unit = match[2] + const unit = match[2] as keyof typeof units const msAgo = num * units[unit] return new Date(Date.now() - msAgo).valueOf() } -export default defineSource(async () => { +const source = defineSource(async () => { const html: any = await myFetch("https://www.ghxi.com/category/all") const $ = cheerio.load(html) const news: NewsItem[] = [] - $(".sec-panel .sec-panel-body .post-loop li").each((i, elem) => { + $(".sec-panel .sec-panel-body .post-loop li").each((_, elem) => { let summary_title = $(elem).find(".item-content .item-title").text() if (summary_title) { summary_title = summary_title.trim() @@ -40,18 +41,21 @@ export default defineSource(async () => { summary_description = summary_description.replaceAll("'", "''") } const date = $(elem).find(".item-content .date").text() - console.log(date) const url = $(elem).find(".item-content .item-title a").attr("href") - news.push({ - id: url, - url, - title: summary_title, - extra: { - hover: summary_description, - date: relativeTimeToDate(date), - }, - }) + if (url) { + news.push({ + id: url, + url, + title: summary_title, + extra: { + hover: summary_description, + date: relativeTimeToDate(date), + }, + }) + } }) return news }) + +export default proxySource("https://newsnow-omega-one.vercel.app/api/s?id=ghxi&latest=", source) diff --git a/server/sources/sputniknewscn.ts b/server/sources/sputniknewscn.ts index 91f2dee..b48dbc2 100644 --- a/server/sources/sputniknewscn.ts +++ b/server/sources/sputniknewscn.ts @@ -1,7 +1,8 @@ import * as cheerio from "cheerio" import type { NewsItem } from "@shared/types" +import { proxySource } from "#/utils/source" -export default defineSource(async () => { +const source = defineSource(async () => { const response: any = await myFetch("https://sputniknews.cn/services/widget/lenta/") const $ = cheerio.load(response) const $items = $(".lenta__item") @@ -25,3 +26,5 @@ export default defineSource(async () => { }) return news }) + +export default proxySource("https://newsnow-omega-one.vercel.app/api/s?id=sputniknewscn&latest=", source) diff --git a/server/utils/source.ts b/server/utils/source.ts index 26a5dc5..6e8ac8a 100644 --- a/server/utils/source.ts +++ b/server/utils/source.ts @@ -1,3 +1,4 @@ +import process from "node:process" import type { AllSourceID } from "@shared/types" import defu from "defu" import type { RSSHubOption, RSSHubInfo as RSSHubResponse, SourceGetter, SourceOption } from "#/types" @@ -44,3 +45,12 @@ export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, })) } } + +export function proxySource(proxyUrl: string, source: SourceGetter) { + return process.env.CF_PAGES + ? defineSource(async () => { + const data = await myFetch(proxyUrl) + return data.items + }) + : source +} diff --git a/shared/pre-sources.ts b/shared/pre-sources.ts index efe00a3..82b7d1a 100644 --- a/shared/pre-sources.ts +++ b/shared/pre-sources.ts @@ -139,8 +139,6 @@ export const originSources = { "sputniknewscn": { name: "卫星通讯社", color: "orange", - // cloudflare pages cannot access - disable: "cf", column: "world", home: "https://sputniknews.cn", }, @@ -340,7 +338,6 @@ export const originSources = { name: "果核剥壳", column: "china", color: "yellow", - disable: "cf", home: "https://www.ghxi.com/", }, "smzdm": {