From 9807609a4deb94087dcb1fd3e20a481f14c16074 Mon Sep 17 00:00:00 2001 From: Tony Date: Sat, 20 Jun 2026 23:36:02 +0800 Subject: [PATCH] refactor: use cache utils directly (#22316) --- lib/routes/163/dy.ts | 3 +-- lib/routes/163/dy2.ts | 3 +-- lib/routes/163/utils.tsx | 5 +++-- lib/routes/1point3acres/category.ts | 3 +-- lib/routes/1point3acres/section.ts | 3 +-- lib/routes/1point3acres/thread.ts | 3 +-- lib/routes/1point3acres/utils.tsx | 5 +++-- lib/routes/36kr/hot-list.ts | 3 +-- lib/routes/36kr/index.ts | 3 +-- lib/routes/36kr/utils.ts | 4 ++-- lib/routes/3dmgame/game.ts | 3 +-- lib/routes/3dmgame/news-center.ts | 3 +-- lib/routes/3dmgame/utils.ts | 5 +++-- lib/routes/4gamers/category.ts | 2 +- lib/routes/4gamers/utils.tsx | 5 +++-- lib/routes/56kog/class.ts | 3 +-- lib/routes/56kog/top.ts | 3 +-- lib/routes/56kog/util.tsx | 5 +++-- lib/routes/aisixiang/column.ts | 3 +-- lib/routes/aisixiang/thinktank.ts | 3 +-- lib/routes/aisixiang/toplist.ts | 3 +-- lib/routes/aisixiang/utils.ts | 5 +++-- lib/routes/aisixiang/zhuanti.ts | 3 +-- lib/routes/bangumi.tv/calendar/_base.ts | 19 ++++++++--------- lib/routes/bangumi.tv/calendar/today.tsx | 3 +-- lib/routes/bsky/feeds.ts | 7 +++---- lib/routes/bsky/posts.ts | 7 +++---- lib/routes/bsky/utils.ts | 21 ++++++++++--------- lib/routes/cartoonmad/comic.tsx | 6 +++--- lib/routes/cnbeta/common.ts | 3 +-- lib/routes/cnbeta/utils.ts | 5 +++-- lib/routes/creative-comic/book.tsx | 2 +- lib/routes/creative-comic/utils.ts | 5 +++-- lib/routes/cw/utils.ts | 14 ++++++------- lib/routes/cyzone/author.ts | 5 ++--- lib/routes/cyzone/index.ts | 4 +--- lib/routes/cyzone/label.ts | 5 ++--- lib/routes/cyzone/util.ts | 13 ++++++------ lib/routes/dxy/board.ts | 2 +- lib/routes/dxy/profile/thread.ts | 2 +- lib/routes/dxy/special.ts | 2 +- lib/routes/dxy/utils.ts | 5 +++-- lib/routes/followin/index.ts | 7 +++---- lib/routes/followin/kol.ts | 5 ++--- lib/routes/followin/news.ts | 5 ++--- lib/routes/followin/tag.ts | 6 +++--- lib/routes/followin/topic.ts | 5 ++--- lib/routes/followin/utils.tsx | 13 ++++++------ lib/routes/gelonghui/home.ts | 3 +-- lib/routes/gelonghui/hot-article.ts | 3 +-- lib/routes/gelonghui/keyword.ts | 3 +-- lib/routes/gelonghui/subject.ts | 3 +-- lib/routes/gelonghui/user.ts | 3 +-- lib/routes/gelonghui/utils.ts | 5 +++-- lib/routes/gov/chinamine-safety/util.ts | 6 +++--- lib/routes/gov/chinamine-safety/xw.ts | 3 +-- lib/routes/gov/chinamine-safety/zfxxgk.ts | 3 +-- lib/routes/hk01/channel.ts | 3 +-- lib/routes/hk01/hot.ts | 3 +-- lib/routes/hk01/issue.ts | 3 +-- lib/routes/hk01/latest.ts | 3 +-- lib/routes/hk01/tag.ts | 3 +-- lib/routes/hk01/utils.tsx | 5 +++-- lib/routes/hk01/zone.ts | 3 +-- lib/routes/hkepc/index.ts | 3 +-- lib/routes/hoyolab/news.tsx | 4 ++-- lib/routes/hoyolab/utils.ts | 13 ++++++------ lib/routes/huxiu/channel.ts | 3 +-- lib/routes/huxiu/club.ts | 3 +-- lib/routes/huxiu/collection.ts | 3 +-- lib/routes/huxiu/member.ts | 3 +-- lib/routes/huxiu/moment.ts | 3 +-- lib/routes/huxiu/search.ts | 3 +-- lib/routes/huxiu/tag.ts | 3 +-- lib/routes/huxiu/util.ts | 6 +++--- lib/routes/instagram/private-api/index.ts | 14 ++++++------- lib/routes/javlibrary/bestrated.ts | 3 +-- lib/routes/javlibrary/bestreviews.ts | 3 +-- lib/routes/javlibrary/genre.ts | 3 +-- lib/routes/javlibrary/maker.ts | 3 +-- lib/routes/javlibrary/mostwanted.ts | 3 +-- lib/routes/javlibrary/newentries.ts | 3 +-- lib/routes/javlibrary/newrelease.ts | 3 +-- lib/routes/javlibrary/star.ts | 3 +-- lib/routes/javlibrary/update.ts | 3 +-- lib/routes/javlibrary/user.ts | 3 +-- lib/routes/javlibrary/utils.tsx | 5 +++-- lib/routes/meteor/boards.ts | 3 +-- lib/routes/meteor/index.ts | 2 +- lib/routes/meteor/utils.ts | 5 +++-- lib/routes/newzmz/index.ts | 4 ++-- lib/routes/newzmz/util.ts | 11 +++++----- lib/routes/oeeee/app/channel.ts | 3 +-- lib/routes/oeeee/app/reporter.ts | 3 +-- lib/routes/oeeee/utils.ts | 5 +++-- lib/routes/oeeee/web.ts | 3 +-- lib/routes/otobanana/cast.ts | 3 +-- lib/routes/otobanana/livestream.ts | 3 +-- lib/routes/otobanana/timeline.ts | 3 +-- lib/routes/otobanana/utils.tsx | 5 +++-- lib/routes/pixiv/bookmarks.ts | 3 +-- lib/routes/pixiv/illustfollow.ts | 3 +-- lib/routes/pixiv/novel-api/series/nsfw.ts | 3 +-- .../pixiv/novel-api/user-novels/nsfw.ts | 3 +-- lib/routes/pixiv/ranking.ts | 3 +-- lib/routes/pixiv/search.ts | 3 +-- lib/routes/pixiv/token.ts | 9 ++++---- lib/routes/pixiv/user.ts | 3 +-- lib/routes/pku/ss/admission.ts | 3 +-- lib/routes/pku/ss/common.ts | 5 +++-- lib/routes/pku/ss/notice.ts | 3 +-- lib/routes/pku/ss/pg-admin.ts | 3 +-- lib/routes/plurk/anonymous.ts | 3 +-- lib/routes/plurk/hotlinks.ts | 3 +-- lib/routes/plurk/news.ts | 3 +-- lib/routes/plurk/search.ts | 3 +-- lib/routes/plurk/top.ts | 3 +-- lib/routes/plurk/topic.ts | 3 +-- lib/routes/plurk/user.ts | 3 +-- lib/routes/plurk/utils.ts | 6 +++--- lib/routes/qianp/news.ts | 2 +- lib/routes/qianp/utils.ts | 5 +++-- lib/routes/readhub/daily.ts | 3 +-- lib/routes/readhub/index.ts | 3 +-- lib/routes/readhub/util.ts | 6 +++--- lib/routes/science/current.ts | 3 +-- lib/routes/science/early.ts | 3 +-- lib/routes/science/utils.tsx | 5 +++-- lib/routes/segmentfault/blogs.ts | 5 ++--- lib/routes/segmentfault/channel.ts | 5 ++--- lib/routes/segmentfault/user.ts | 5 ++--- lib/routes/segmentfault/utils.ts | 9 ++++---- lib/routes/sina/chuangshiji.ts | 3 +-- lib/routes/sina/discovery.ts | 3 +-- lib/routes/sina/finance/china.ts | 3 +-- lib/routes/sina/finance/rollnews.ts | 3 +-- lib/routes/sina/finance/stock/usstock.ts | 3 +-- lib/routes/sina/rollnews.ts | 3 +-- lib/routes/sina/sports.ts | 3 +-- lib/routes/sina/utils.tsx | 5 +++-- lib/routes/southcn/nfapp/column.ts | 3 +-- lib/routes/southcn/nfapp/reporter.ts | 3 +-- lib/routes/southcn/nfapp/utils.ts | 5 +++-- lib/routes/tgbus/list.ts | 3 +-- lib/routes/tgbus/utils.ts | 5 +++-- lib/routes/vocus/publication.ts | 2 +- lib/routes/vocus/user.ts | 2 +- lib/routes/vocus/utils.ts | 5 +++-- lib/routes/whu/hyxt.ts | 3 +-- lib/routes/whu/news.ts | 3 +-- lib/routes/whu/util.ts | 6 +++--- lib/routes/woshipm/popular.ts | 3 +-- lib/routes/woshipm/user-article.ts | 3 +-- lib/routes/woshipm/utils.ts | 5 +++-- lib/routes/xinpianchang/index.ts | 5 ++--- lib/routes/xinpianchang/rank.ts | 5 ++--- lib/routes/xinpianchang/util.tsx | 13 ++++++------ lib/routes/yicai/author.ts | 3 +-- lib/routes/yicai/carousel.ts | 4 +--- lib/routes/yicai/feed.ts | 3 +-- lib/routes/yicai/headline.ts | 3 +-- lib/routes/yicai/latest.ts | 3 +-- lib/routes/yicai/news.ts | 3 +-- lib/routes/yicai/utils.ts | 9 ++++---- lib/routes/yicai/video.ts | 3 +-- lib/routes/yicai/vip.ts | 3 +-- lib/routes/zuvio/boards.ts | 3 +-- lib/routes/zuvio/student5.ts | 2 +- lib/routes/zuvio/utils.tsx | 5 +++-- 169 files changed, 318 insertions(+), 407 deletions(-) diff --git a/lib/routes/163/dy.ts b/lib/routes/163/dy.ts index 8b77df942..2720d0052 100644 --- a/lib/routes/163/dy.ts +++ b/lib/routes/163/dy.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -40,7 +39,7 @@ async function handler(ctx) { imgsrc: e.imgsrc, })); - const items = await Promise.all(list.map((e) => parseDyArticle(e, cache.tryGet))); + const items = await Promise.all(list.map((e) => parseDyArticle(e))); return { title: `网易号 - ${list[0].author}`, diff --git a/lib/routes/163/dy2.ts b/lib/routes/163/dy2.ts index 6c65b2878..06ff453d9 100644 --- a/lib/routes/163/dy2.ts +++ b/lib/routes/163/dy2.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -49,7 +48,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseDyArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseDyArticle(item))); return { title: `${$('head title').text()} - 网易号`, diff --git a/lib/routes/163/utils.tsx b/lib/routes/163/utils.tsx index df91f2032..2c2f109a8 100644 --- a/lib/routes/163/utils.tsx +++ b/lib/routes/163/utils.tsx @@ -2,6 +2,7 @@ import { load } from 'cheerio'; import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; const renderDescription = (imgsrc, postBody) => @@ -17,8 +18,8 @@ const renderDescription = (imgsrc, postBody) => ); -const parseDyArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseDyArticle = (item) => + cache.tryGet(item.link, async () => { const response = await got(item.link, { responseType: 'buffer', }); diff --git a/lib/routes/1point3acres/category.ts b/lib/routes/1point3acres/category.ts index 70e51ad01..4066df478 100644 --- a/lib/routes/1point3acres/category.ts +++ b/lib/routes/1point3acres/category.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, ProcessThreads, rootUrl, types } from './utils'; @@ -53,6 +52,6 @@ async function handler(ctx) { return { title: `一亩三分地 - ${id}${types[type]}`, link: currentUrl, - item: await ProcessThreads(cache.tryGet, apiUrl, order), + item: await ProcessThreads(apiUrl, order), }; } diff --git a/lib/routes/1point3acres/section.ts b/lib/routes/1point3acres/section.ts index e9a304b49..2eb3713b4 100644 --- a/lib/routes/1point3acres/section.ts +++ b/lib/routes/1point3acres/section.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, ProcessThreads, rootUrl, types } from './utils'; @@ -73,6 +72,6 @@ async function handler(ctx) { return { title: `一亩三分地 - ${Object.hasOwn(sections, id) ? sections[id] : id}${types[type]}`, link: currentUrl, - item: await ProcessThreads(cache.tryGet, apiUrl, order), + item: await ProcessThreads(apiUrl, order), }; } diff --git a/lib/routes/1point3acres/thread.ts b/lib/routes/1point3acres/thread.ts index 624f4550a..8bf500569 100644 --- a/lib/routes/1point3acres/thread.ts +++ b/lib/routes/1point3acres/thread.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, ProcessThreads, rootUrl, types } from './utils'; @@ -35,6 +34,6 @@ async function handler(ctx) { return { title: `一亩三分地 - ${types[type]}`, link: rootUrl, - item: await ProcessThreads(cache.tryGet, apiUrl, order), + item: await ProcessThreads(apiUrl, order), }; } diff --git a/lib/routes/1point3acres/utils.tsx b/lib/routes/1point3acres/utils.tsx index 746732d26..eee2f2f23 100644 --- a/lib/routes/1point3acres/utils.tsx +++ b/lib/routes/1point3acres/utils.tsx @@ -3,6 +3,7 @@ import presetHTML5 from '@bbob/preset-html5'; import type { BBobCoreTagNodeTree } from '@bbob/types'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -25,7 +26,7 @@ const swapLinebreak = (tree: BBobCoreTagNodeTree) => return node; }); -const ProcessThreads = async (tryGet, apiUrl, order) => { +const ProcessThreads = async (apiUrl, order) => { const response = await got({ method: 'get', url: apiUrl, @@ -46,7 +47,7 @@ const ProcessThreads = async (tryGet, apiUrl, order) => { category: [item.forum_name, ...(item.tags ? item.tags.map((t) => t.displayname) : [])], }; - return tryGet(result.link, async () => { + return cache.tryGet(result.link, async () => { try { const detailResponse = await got({ method: 'get', diff --git a/lib/routes/36kr/hot-list.ts b/lib/routes/36kr/hot-list.ts index 7712bdcc6..e7c486c4f 100644 --- a/lib/routes/36kr/hot-list.ts +++ b/lib/routes/36kr/hot-list.ts @@ -1,6 +1,5 @@ import InvalidParameterError from '@/errors/types/invalid-parameter'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -95,7 +94,7 @@ async function handler(ctx) { }; }); - items = await Promise.all(items.map((item) => ProcessItem(item, cache.tryGet))); + items = await Promise.all(items.map((item) => ProcessItem(item))); return { title: `36氪 - ${categories[category].title}`, diff --git a/lib/routes/36kr/index.ts b/lib/routes/36kr/index.ts index d5790121a..604d9e721 100644 --- a/lib/routes/36kr/index.ts +++ b/lib/routes/36kr/index.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getSubPath } from '@/utils/common-utils'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -65,7 +64,7 @@ async function handler(ctx) { }); if (!/^\/(?:search|newsflashes)/.test(path)) { - items = await Promise.all(items.map((item) => ProcessItem(item, cache.tryGet))); + items = await Promise.all(items.map((item) => ProcessItem(item))); } return { diff --git a/lib/routes/36kr/utils.ts b/lib/routes/36kr/utils.ts index e20b6b7e6..918ee0971 100644 --- a/lib/routes/36kr/utils.ts +++ b/lib/routes/36kr/utils.ts @@ -7,8 +7,8 @@ import ofetch from '@/utils/ofetch'; export const rootUrl = 'https://www.36kr.com'; -export const ProcessItem = (item, tryGet) => - tryGet(item.link, async () => { +export const ProcessItem = (item) => + cache.tryGet(item.link, async () => { const detailResponse = await ofetch(item.link); const cipherTextList = detailResponse.match(/\{"state":"(.*)","isEncrypt":true\}/) ?? []; diff --git a/lib/routes/3dmgame/game.ts b/lib/routes/3dmgame/game.ts index 9904396f0..c6ebcd845 100644 --- a/lib/routes/3dmgame/game.ts +++ b/lib/routes/3dmgame/game.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -40,7 +39,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: $('head title').text().split('_', 1)[0], diff --git a/lib/routes/3dmgame/news-center.ts b/lib/routes/3dmgame/news-center.ts index 09ea2c102..8006f074c 100644 --- a/lib/routes/3dmgame/news-center.ts +++ b/lib/routes/3dmgame/news-center.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -61,7 +60,7 @@ async function handler(ctx) { }; }); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: '3DM - ' + $('title').text().split('_', 1)[0], diff --git a/lib/routes/3dmgame/utils.ts b/lib/routes/3dmgame/utils.ts index e714f989c..fff6bccaf 100644 --- a/lib/routes/3dmgame/utils.ts +++ b/lib/routes/3dmgame/utils.ts @@ -1,11 +1,12 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const { data: response } = await got(item.link); const $ = load(response); diff --git a/lib/routes/4gamers/category.ts b/lib/routes/4gamers/category.ts index e2584f699..da6c33f73 100644 --- a/lib/routes/4gamers/category.ts +++ b/lib/routes/4gamers/category.ts @@ -35,7 +35,7 @@ async function handler(ctx) { let categoryName = '最新消息'; if (!isLatest) { - const categories = await getCategories(cache.tryGet); + const categories = await getCategories(); categoryName = categories.find((c) => c.id === Number.parseInt(category)).name; } diff --git a/lib/routes/4gamers/utils.tsx b/lib/routes/4gamers/utils.tsx index ca7ea1404..a3358f3b4 100644 --- a/lib/routes/4gamers/utils.tsx +++ b/lib/routes/4gamers/utils.tsx @@ -2,11 +2,12 @@ import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; import InvalidParameterError from '@/errors/types/invalid-parameter'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; -const getCategories = (tryGet) => - tryGet('4gamers:categories', async () => { +const getCategories = () => + cache.tryGet('4gamers:categories', async () => { const { data: response } = await got('https://www.4gamers.com.tw/site/api/news/category'); return response.data.map((category) => ({ diff --git a/lib/routes/56kog/class.ts b/lib/routes/56kog/class.ts index 19d60a0f5..ab97dccfd 100644 --- a/lib/routes/56kog/class.ts +++ b/lib/routes/56kog/class.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { fetchItems, rootUrl } from './util'; @@ -34,5 +33,5 @@ async function handler(ctx) { const currentUrl = new URL(`class/${category}.html`, rootUrl).href; - return await fetchItems(limit, currentUrl, cache.tryGet); + return await fetchItems(limit, currentUrl); } diff --git a/lib/routes/56kog/top.ts b/lib/routes/56kog/top.ts index bca65ba70..13605e02c 100644 --- a/lib/routes/56kog/top.ts +++ b/lib/routes/56kog/top.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { fetchItems, rootUrl } from './util'; @@ -30,5 +29,5 @@ async function handler(ctx) { const currentUrl = new URL(`top/${category.split(/_/, 1)[0]}_1.html`, rootUrl).href; - return await fetchItems(limit, currentUrl, cache.tryGet); + return await fetchItems(limit, currentUrl); } diff --git a/lib/routes/56kog/util.tsx b/lib/routes/56kog/util.tsx index e0b67ece7..985c31117 100644 --- a/lib/routes/56kog/util.tsx +++ b/lib/routes/56kog/util.tsx @@ -2,13 +2,14 @@ import { load } from 'cheerio'; import { renderToString } from 'hono/jsx/dom/server'; import iconv from 'iconv-lite'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; const rootUrl = 'https://www.56kog.com'; -const fetchItems = async (limit, currentUrl, tryGet) => { +const fetchItems = async (limit, currentUrl) => { const { data: response } = await got(currentUrl, { responseType: 'buffer', }); @@ -31,7 +32,7 @@ const fetchItems = async (limit, currentUrl, tryGet) => { items = await Promise.all( items.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { try { const { data: detailResponse } = await got(item.link, { responseType: 'buffer', diff --git a/lib/routes/aisixiang/column.ts b/lib/routes/aisixiang/column.ts index af373beaa..a3d9607f5 100644 --- a/lib/routes/aisixiang/column.ts +++ b/lib/routes/aisixiang/column.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -55,7 +54,7 @@ async function handler(ctx) { }); return { - item: await ProcessFeed(limit, cache.tryGet, items), + item: await ProcessFeed(limit, items), title: `爱思想 - ${title}`, link: currentUrl, description: $('div.tips').text(), diff --git a/lib/routes/aisixiang/thinktank.ts b/lib/routes/aisixiang/thinktank.ts index 5bf2966f2..427e27ae4 100644 --- a/lib/routes/aisixiang/thinktank.ts +++ b/lib/routes/aisixiang/thinktank.ts @@ -2,7 +2,6 @@ import { load } from 'cheerio'; import InvalidParameterError from '@/errors/types/invalid-parameter'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ossUrl, ProcessFeed, rootUrl } from './utils'; @@ -62,7 +61,7 @@ async function handler(ctx) { }); return { - item: await ProcessFeed(limit, cache.tryGet, items), + item: await ProcessFeed(limit, items), title: `爱思想 - ${title}`, link: currentUrl, description: $('div.thinktank-author-description-box p').text(), diff --git a/lib/routes/aisixiang/toplist.ts b/lib/routes/aisixiang/toplist.ts index 510f90f37..ede852f5e 100644 --- a/lib/routes/aisixiang/toplist.ts +++ b/lib/routes/aisixiang/toplist.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -46,7 +45,7 @@ async function handler(ctx) { }); return { - item: await ProcessFeed(limit, cache.tryGet, items), + item: await ProcessFeed(limit, items), title: `爱思想 - ${title}`, link: currentUrl, language: 'zh-cn', diff --git a/lib/routes/aisixiang/utils.ts b/lib/routes/aisixiang/utils.ts index 2521fca4b..befc488ba 100644 --- a/lib/routes/aisixiang/utils.ts +++ b/lib/routes/aisixiang/utils.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -7,10 +8,10 @@ import timezone from '@/utils/timezone'; const ossUrl = 'https://oss.aisixiang.com'; const rootUrl = 'https://www.aisixiang.com'; -const ProcessFeed = (limit, tryGet, items) => +const ProcessFeed = (limit, items) => Promise.all( items.slice(0, limit).map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const { data: detailResponse } = await got(item.link); const content = load(detailResponse); diff --git a/lib/routes/aisixiang/zhuanti.ts b/lib/routes/aisixiang/zhuanti.ts index 2ecf4e9c0..b08713dd4 100644 --- a/lib/routes/aisixiang/zhuanti.ts +++ b/lib/routes/aisixiang/zhuanti.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -58,7 +57,7 @@ async function handler(ctx) { }); return { - item: await ProcessFeed(limit, cache.tryGet, items), + item: await ProcessFeed(limit, items), title: `爱思想 - ${title}`, link: currentUrl, description: $('div.tips p').text(), diff --git a/lib/routes/bangumi.tv/calendar/_base.ts b/lib/routes/bangumi.tv/calendar/_base.ts index a06945bba..9c32e4eeb 100644 --- a/lib/routes/bangumi.tv/calendar/_base.ts +++ b/lib/routes/bangumi.tv/calendar/_base.ts @@ -1,7 +1,8 @@ import { config } from '@/config'; +import cache from '@/utils/cache'; import got from '@/utils/got'; -export const getData = (tryGet) => { +export const getData = () => { const bgmCalendarUrl = 'https://api.bgm.tv/calendar'; const bgmDataUrl = 'https://cdn.jsdelivr.net/npm/bangumi-data/dist/data.json'; @@ -9,21 +10,19 @@ export const getData = (tryGet) => { return Promise.all( urls.map((item, i) => - tryGet( + cache.tryGet( item, async () => { const { data } = await got(item); if (i === 1) { // 只保留有 bangumi id 的番剧 - const items = []; - for (const item of data.items) { - const bgmSite = item.sites.find((s) => s.site === 'bangumi'); - if (bgmSite) { - item.bgmId = bgmSite.id; - items.push(item); - } - } + const items = data.items + .map((item) => { + const bgmSite = item.sites.find((s) => s.site === 'bangumi'); + return bgmSite ? { ...item, bgmId: bgmSite.id } : null; + }) + .filter(Boolean); data.items = items; } diff --git a/lib/routes/bangumi.tv/calendar/today.tsx b/lib/routes/bangumi.tv/calendar/today.tsx index c33f9ec20..f329bf89e 100644 --- a/lib/routes/bangumi.tv/calendar/today.tsx +++ b/lib/routes/bangumi.tv/calendar/today.tsx @@ -1,7 +1,6 @@ import { renderToString } from 'hono/jsx/dom/server'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getData } from './_base'; @@ -49,7 +48,7 @@ const renderTodayDescription = (bgm, siteMeta) => ); async function handler() { - const [list, data] = await getData(cache.tryGet); + const [list, data] = await getData(); const siteMeta = data.siteMeta; const today = new Date(Date.now()); diff --git a/lib/routes/bsky/feeds.ts b/lib/routes/bsky/feeds.ts index 458c15747..cba53d1fc 100644 --- a/lib/routes/bsky/feeds.ts +++ b/lib/routes/bsky/feeds.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import { renderPost } from './templates/post'; @@ -32,10 +31,10 @@ async function handler(ctx) { const handle = ctx.req.param('handle'); const space = ctx.req.param('space'); - const DID = await resolveHandle(handle, cache.tryGet); + const DID = await resolveHandle(handle); const uri = `at://${DID}/app.bsky.feed.generator/${space}`; - const profile = await getFeedGenerator(uri, cache.tryGet); - const feeds = await getFeed(uri, cache.tryGet); + const profile = await getFeedGenerator(uri); + const feeds = await getFeed(uri); const items = feeds.feed.map(({ post }) => ({ title: post.record.text.split('\n', 1)[0], diff --git a/lib/routes/bsky/posts.ts b/lib/routes/bsky/posts.ts index 98f6efcb1..b5e40c933 100644 --- a/lib/routes/bsky/posts.ts +++ b/lib/routes/bsky/posts.ts @@ -2,7 +2,6 @@ import querystring from 'node:querystring'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import { renderPost } from './templates/post'; @@ -52,9 +51,9 @@ async function handler(ctx) { const routeParams = querystring.parse(ctx.req.param('routeParams')); const filter = routeParams.filter || 'posts_and_author_threads'; - const DID = await resolveHandle(handle, cache.tryGet); - const profile = await getProfile(DID, cache.tryGet); - const authorFeed = await getAuthorFeed(DID, filter, cache.tryGet); + const DID = await resolveHandle(handle); + const profile = await getProfile(DID); + const authorFeed = await getAuthorFeed(DID, filter); const items = authorFeed.feed.map(({ post }) => ({ title: post.record.text.split('\n', 1)[0], diff --git a/lib/routes/bsky/utils.ts b/lib/routes/bsky/utils.ts index f167308a2..099643bb0 100644 --- a/lib/routes/bsky/utils.ts +++ b/lib/routes/bsky/utils.ts @@ -1,4 +1,5 @@ import { config } from '@/config'; +import cache from '@/utils/cache'; import got from '@/utils/got'; /** @@ -6,8 +7,8 @@ import got from '@/utils/got'; */ // https://github.com/bluesky-social/atproto/blob/main/lexicons/com/atproto/identity/resolveHandle.json -const resolveHandle = (handle, tryGet) => - tryGet(`bsky:${handle}`, async () => { +const resolveHandle = (handle) => + cache.tryGet(`bsky:${handle}`, async () => { const { data } = await got('https://public.api.bsky.app/xrpc/com.atproto.identity.resolveHandle', { searchParams: { handle, @@ -17,8 +18,8 @@ const resolveHandle = (handle, tryGet) => }); // https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/actor/getProfile.json -const getProfile = (did, tryGet) => - tryGet(`bsky:profile:${did}`, async () => { +const getProfile = (did) => + cache.tryGet(`bsky:profile:${did}`, async () => { const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile', { searchParams: { actor: did, @@ -28,8 +29,8 @@ const getProfile = (did, tryGet) => }); // https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/getAuthorFeed.json -const getAuthorFeed = (did, filter, tryGet) => - tryGet( +const getAuthorFeed = (did, filter) => + cache.tryGet( `bsky:authorFeed:${did}:${filter}`, async () => { const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.feed.getAuthorFeed', { @@ -46,8 +47,8 @@ const getAuthorFeed = (did, filter, tryGet) => ); // https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/getFeed.json -const getFeed = (uri, tryGet) => - tryGet( +const getFeed = (uri) => + cache.tryGet( `bsky:feed:${uri}`, async () => { const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.feed.getFeed', { @@ -63,8 +64,8 @@ const getFeed = (uri, tryGet) => ); // https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/getFeedGenerator.json -const getFeedGenerator = (uri, tryGet) => - tryGet( +const getFeedGenerator = (uri) => + cache.tryGet( `bsky:feedGenerator:${uri}`, async () => { const { data } = await got('https://public.api.bsky.app/xrpc/app.bsky.feed.getFeedGenerator', { diff --git a/lib/routes/cartoonmad/comic.tsx b/lib/routes/cartoonmad/comic.tsx index 61e24bf89..a8009be8c 100644 --- a/lib/routes/cartoonmad/comic.tsx +++ b/lib/routes/cartoonmad/comic.tsx @@ -27,10 +27,10 @@ const loadContent = (id, { chapter, pages }) => { return description; }; -const getChapters = (id, list, tryGet) => +const getChapters = (id, list) => Promise.all( list.map((item) => - tryGet(item.link, () => { + cache.tryGet(item.link, () => { item.description = loadContent(id, item); return item; @@ -91,7 +91,7 @@ async function handler(ctx) { }) .toReversed(); - const chapters = await getChapters(id, list, cache.tryGet); + const chapters = await getChapters(id, list); return { title: $('head title').text(), diff --git a/lib/routes/cnbeta/common.ts b/lib/routes/cnbeta/common.ts index 08a261ace..20267dd71 100644 --- a/lib/routes/cnbeta/common.ts +++ b/lib/routes/cnbeta/common.ts @@ -1,6 +1,5 @@ import { load } from 'cheerio'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -39,6 +38,6 @@ export async function handler(ctx) { return { title: $('title').text(), link: currentUrl, - item: await ProcessItems(items, ctx.req.query('limit'), cache.tryGet), + item: await ProcessItems(items, ctx.req.query('limit')), }; } diff --git a/lib/routes/cnbeta/utils.ts b/lib/routes/cnbeta/utils.ts index 96f78a1f4..66d349eef 100644 --- a/lib/routes/cnbeta/utils.ts +++ b/lib/routes/cnbeta/utils.ts @@ -1,15 +1,16 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; const rootUrl = 'https://www.cnbeta.com.tw'; -const ProcessItems = (items, limit, tryGet) => +const ProcessItems = (items, limit) => Promise.all( items.slice(0, limit ? Number.parseInt(limit) : 60).map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const detailResponse = await got(item.link); const content = load(detailResponse.data); diff --git a/lib/routes/creative-comic/book.tsx b/lib/routes/creative-comic/book.tsx index 37323d863..ccf805478 100644 --- a/lib/routes/creative-comic/book.tsx +++ b/lib/routes/creative-comic/book.tsx @@ -32,7 +32,7 @@ export const route: Route = { async function handler(ctx) { const { id, coverOnly = 'true', quality = '1' } = ctx.req.param(); - const uuid = await getUuid(cache.tryGet); + const uuid = await getUuid(); const { data: { data: book }, } = await getBook(id, uuid); diff --git a/lib/routes/creative-comic/utils.ts b/lib/routes/creative-comic/utils.ts index 115136b89..cc1bdef32 100644 --- a/lib/routes/creative-comic/utils.ts +++ b/lib/routes/creative-comic/utils.ts @@ -1,5 +1,6 @@ import CryptoJS from 'crypto-js'; +import cache from '@/utils/cache'; import got from '@/utils/got'; const apiHost = 'https://api.creative-comic.tw'; @@ -48,8 +49,8 @@ const getImgKey = (pageId, uuid) => }, }); -const getUuid = (tryGet) => - tryGet('creative-comic:uuid', async () => { +const getUuid = () => + cache.tryGet('creative-comic:uuid', async () => { const { data } = await got(`${apiHost}/guest`, { headers: { device, diff --git a/lib/routes/cw/utils.ts b/lib/routes/cw/utils.ts index e9935dc07..514064e45 100644 --- a/lib/routes/cw/utils.ts +++ b/lib/routes/cw/utils.ts @@ -30,9 +30,9 @@ const pathMap = { }, }; -const getCookie = async (context, tryGet) => { +const getCookie = async (context) => { if (!cookie) { - cookie = await tryGet('cw:cookie', async () => { + cookie = await cache.tryGet('cw:cookie', async () => { const page = await context.newPage(); await page.route('**/*', (route) => { const request = route.request(); @@ -53,7 +53,7 @@ const getCookie = async (context, tryGet) => { const parsePage = async (path, context, ctx) => { const pageUrl = `${baseUrl}${pathMap[path].pageUrl(ctx.req.param('channel'))}`; - const cookie = await getCookie(context, cache.tryGet); + const cookie = await getCookie(context); const page = await context.newPage(); await page.route('**/*', (route) => { const request = route.request(); @@ -71,7 +71,7 @@ const parsePage = async (path, context, ctx) => { const $ = load(response); const list = parseList($, ctx.req.query('limit') ? Number(ctx.req.query('limit')) : pathMap[path].limit); - const items = await parseItems(list, context, cache.tryGet); + const items = await parseItems(list, context); return { $, items }; }; @@ -89,13 +89,13 @@ const parseList = ($, limit) => }) .slice(0, limit); -const parseItems = (list, context, tryGet) => +const parseItems = (list, context) => Promise.all( list.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const response = await ofetch(item.link, { headers: { - Cookie: await getCookie(context, tryGet), + Cookie: await getCookie(context), 'User-Agent': config.ua, }, }); diff --git a/lib/routes/cyzone/author.ts b/lib/routes/cyzone/author.ts index 7c320b029..760055725 100644 --- a/lib/routes/cyzone/author.ts +++ b/lib/routes/cyzone/author.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, getInfo, processItems, rootUrl } from './util'; @@ -33,12 +32,12 @@ async function handler(ctx) { const apiUrl = new URL('v2/author/author/detail', apiRootUrl).href; const currentUrl = new URL(`author/${id}`, rootUrl).href; - const items = await processItems(apiUrl, limit, cache.tryGet, { + const items = await processItems(apiUrl, limit, { author_id: id, }); return { item: items, - ...(await getInfo(currentUrl, cache.tryGet)), + ...(await getInfo(currentUrl)), }; } diff --git a/lib/routes/cyzone/index.ts b/lib/routes/cyzone/index.ts index 735150462..c359bdb18 100644 --- a/lib/routes/cyzone/index.ts +++ b/lib/routes/cyzone/index.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, getInfo, processItems, rootUrl } from './util'; @@ -37,7 +36,6 @@ async function handler(ctx) { const items = await processItems( apiUrl, limit, - cache.tryGet, id === 'news' ? {} : { @@ -47,6 +45,6 @@ async function handler(ctx) { return { item: items, - ...(await getInfo(currentUrl, cache.tryGet)), + ...(await getInfo(currentUrl)), }; } diff --git a/lib/routes/cyzone/label.ts b/lib/routes/cyzone/label.ts index e9de525a6..55fae5981 100644 --- a/lib/routes/cyzone/label.ts +++ b/lib/routes/cyzone/label.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { apiRootUrl, getInfo, processItems, rootUrl } from './util'; @@ -33,12 +32,12 @@ async function handler(ctx) { const apiUrl = new URL('v2/content/tag/tagList', apiRootUrl).href; const currentUrl = new URL(`label/${name}`, rootUrl).href; - const items = await processItems(apiUrl, limit, cache.tryGet, { + const items = await processItems(apiUrl, limit, { tag: name, }); return { item: items, - ...(await getInfo(currentUrl, cache.tryGet)), + ...(await getInfo(currentUrl)), }; } diff --git a/lib/routes/cyzone/util.ts b/lib/routes/cyzone/util.ts index 76f8a6c5f..e3c7ed609 100644 --- a/lib/routes/cyzone/util.ts +++ b/lib/routes/cyzone/util.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -8,13 +9,12 @@ const apiRootUrl = 'https://api1.cyzone.cn'; const apiShowUrl = new URL('v2/content/app_content/show', apiRootUrl).href; /** - * Retrieves information from a given URL using a provided tryGet function. + * Retrieves information from a given URL. * @param {string} url - The URL to retrieve information from. - * @param {Function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise} - A promise that resolves to an object containing the retrieved information. */ -const getInfo = (url, tryGet) => - tryGet(url, async () => { +const getInfo = (url) => + cache.tryGet(url, async () => { const { data: response } = await got(url); const $ = load(response); @@ -40,11 +40,10 @@ const getInfo = (url, tryGet) => * Process the item list and return the resulting array. * @param {string} apiUrl - The URL of the API. * @param {number} limit - The limit of the results. - * @param {function} tryGet - The tryGet function that handles the retrieval process. * @param {...Object} searchParams - The search parameter objects. * @returns {Promise} - The processed item array. */ -const processItems = async (apiUrl, limit, tryGet, ...params) => { +const processItems = async (apiUrl, limit, ...params) => { // Merge search parameters let searchParams = { size: limit, @@ -76,7 +75,7 @@ const processItems = async (apiUrl, limit, tryGet, ...params) => { items = await Promise.all( items.map((item) => - tryGet(`cyzone-${item.guid}`, async () => { + cache.tryGet(`cyzone-${item.guid}`, async () => { const { data: detailResponse } = await got.post(apiShowUrl, { json: { content_id: item.guid, diff --git a/lib/routes/dxy/board.ts b/lib/routes/dxy/board.ts index 11552325b..7c5654c1e 100644 --- a/lib/routes/dxy/board.ts +++ b/lib/routes/dxy/board.ts @@ -85,7 +85,7 @@ async function handler(ctx) { postId: item.postId, })); - const items = await Promise.all(list.map((item) => getPost(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => getPost(item))); return { title: boardDetail.title, diff --git a/lib/routes/dxy/profile/thread.ts b/lib/routes/dxy/profile/thread.ts index 3aae46ea1..ccdfce162 100644 --- a/lib/routes/dxy/profile/thread.ts +++ b/lib/routes/dxy/profile/thread.ts @@ -96,7 +96,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => getPost(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => getPost(item))); return { title: `${userInfo.nickname} 的个人主页 - 丁香园论坛 - 专业医生社区,医学、药学、生命科学、科研学术交流`, diff --git a/lib/routes/dxy/special.ts b/lib/routes/dxy/special.ts index dbe27a8ba..1a9b0a178 100644 --- a/lib/routes/dxy/special.ts +++ b/lib/routes/dxy/special.ts @@ -89,7 +89,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => getPost(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => getPost(item))); return { title: specialDetail.name, diff --git a/lib/routes/dxy/utils.ts b/lib/routes/dxy/utils.ts index 49f788ff9..844531d51 100644 --- a/lib/routes/dxy/utils.ts +++ b/lib/routes/dxy/utils.ts @@ -1,6 +1,7 @@ import * as cheerio from 'cheerio'; import CryptoJS from 'crypto-js'; +import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; @@ -33,8 +34,8 @@ const sign = (params) => { return CryptoJS.SHA1(searchParams.toString()).toString(); }; -const getPost = (item, tryGet) => - tryGet(item.link, async () => { +const getPost = (item) => + cache.tryGet(item.link, async () => { const postParams = { postId: item.postId, serverTimestamp: Date.now(), diff --git a/lib/routes/followin/index.ts b/lib/routes/followin/index.ts index 92bb89a11..b7e103c0f 100644 --- a/lib/routes/followin/index.ts +++ b/lib/routes/followin/index.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiUrl, favicon, getBParam, getBuildId, getGToken, parseItem, parseList } from './utils'; @@ -64,7 +63,7 @@ Language async function handler(ctx) { const { categoryId = '1', lang = 'en' } = ctx.req.param(); const { limit = 20 } = ctx.req.query(); - const gToken = await getGToken(cache.tryGet); + const gToken = await getGToken(); const bParam = getBParam(lang); const { data: response } = await got.post(`${apiUrl}/feed/list/recommended`, { @@ -81,10 +80,10 @@ async function handler(ctx) { throw new Error(response.msg); } - const buildId = await getBuildId(cache.tryGet); + const buildId = await getBuildId(); const list = parseList(response.data.list, lang, buildId); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: 'Followin', diff --git a/lib/routes/followin/kol.ts b/lib/routes/followin/kol.ts index 83b5ab69f..ebc528729 100644 --- a/lib/routes/followin/kol.ts +++ b/lib/routes/followin/kol.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getBuildId, parseItem, parseList } from './utils'; @@ -31,14 +30,14 @@ async function handler(ctx) { const { kolId, lang = 'en' } = ctx.req.param(); const { limit = 10 } = ctx.req.query(); - const buildId = await getBuildId(cache.tryGet); + const buildId = await getBuildId(); const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/kol/${kolId}.json`); const { queries } = response.pageProps.dehydratedState; const { data: profile } = queries.find((q) => q.queryKey[0] === '/user/get_profile').state; const list = parseList(queries.find((q) => q.queryKey[0] === '/feed/list/user').state.data.pages[0].list.slice(0, limit), lang, buildId); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `${profile.nickname} - Followin`, diff --git a/lib/routes/followin/news.ts b/lib/routes/followin/news.ts index 4e7bd397a..45ce9bef4 100644 --- a/lib/routes/followin/news.ts +++ b/lib/routes/followin/news.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, favicon, getBuildId, parseItem, parseList } from './utils'; @@ -44,11 +43,11 @@ async function handler(ctx) { const { lang = 'en' } = ctx.req.param(); const { limit = 20 } = ctx.req.query(); - const buildId = await getBuildId(cache.tryGet); + const buildId = await getBuildId(); const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/news.json`); const list = parseList(response.pageProps.dehydratedState.queries.find((q) => q.queryKey[0] === '/feed/list/recommended/news').state.data.pages[0].list.slice(0, limit), lang, buildId); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `${lang === 'en' ? 'News' : lang === 'vi' ? 'Bản tin' : '快讯'} - Followin`, diff --git a/lib/routes/followin/tag.ts b/lib/routes/followin/tag.ts index 7d277f3f0..903a3ad93 100644 --- a/lib/routes/followin/tag.ts +++ b/lib/routes/followin/tag.ts @@ -31,7 +31,7 @@ async function handler(ctx) { const { tagId, lang = 'en' } = ctx.req.param(); const { limit = 20 } = ctx.req.query(); - const buildId = await getBuildId(cache.tryGet); + const buildId = await getBuildId(); const tagInfo = await cache.tryGet(`followin:tag:${tagId}:${lang}`, async () => { const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/tag/${tagId}.json`); const { queries } = response.pageProps.dehydratedState; @@ -39,7 +39,7 @@ async function handler(ctx) { return tagInfo; }); - const gToken = await getGToken(cache.tryGet); + const gToken = await getGToken(); const bParam = getBParam(lang); const { data: tagResponse } = await got.post(`${apiUrl}/feed/list/tag`, { headers: { @@ -57,7 +57,7 @@ async function handler(ctx) { } const list = parseList(tagResponse.data.list.slice(0, limit), lang, buildId); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `${tagInfo.name} - Followin`, diff --git a/lib/routes/followin/topic.ts b/lib/routes/followin/topic.ts index 3d3938a46..1cc9b8dc6 100644 --- a/lib/routes/followin/topic.ts +++ b/lib/routes/followin/topic.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getBuildId, parseItem, parseList } from './utils'; @@ -31,14 +30,14 @@ async function handler(ctx) { const { topicId, lang = 'en' } = ctx.req.param(); const { limit = 20 } = ctx.req.query(); - const buildId = await getBuildId(cache.tryGet); + const buildId = await getBuildId(); const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/topic/${topicId}.json`); const { queries } = response.pageProps.dehydratedState; const { data: topicInfo } = queries.find((q) => q.queryKey[0] === '/topic/info').state; const list = parseList(queries.find((q) => q.queryKey[0] === '/feed/list/topic').state.data.pages[0].list.slice(0, limit), lang, buildId); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `${topicInfo.title} - Followin`, diff --git a/lib/routes/followin/utils.tsx b/lib/routes/followin/utils.tsx index 35f2b0bba..c9000bd19 100644 --- a/lib/routes/followin/utils.tsx +++ b/lib/routes/followin/utils.tsx @@ -3,6 +3,7 @@ import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; import { config } from '@/config'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -44,8 +45,8 @@ const getBParam = (lang) => ({ i: 'official', }); -const getBuildId = (tryGet) => - tryGet( +const getBuildId = () => + cache.tryGet( 'followin:buildId', async () => { const { data: pageResponse } = await got(baseUrl); @@ -57,8 +58,8 @@ const getBuildId = (tryGet) => false ); -const getGToken = (tryGet) => - tryGet('followin:gtoken', async () => { +const getGToken = () => + cache.tryGet('followin:gtoken', async () => { const { data } = await got.post(`${apiUrl}/user/gtoken`); return data.data.gtoken; }); @@ -74,8 +75,8 @@ const parseList = (list, lang, buildId) => nextData: `${baseUrl}/_next/data/${buildId}/${lang}/feed/${item.id}.json`, })); -const parseItem = (item, tryGet) => - tryGet(item.link, async () => { +const parseItem = (item) => + cache.tryGet(item.link, async () => { const { data } = await got(item.nextData); const { queries } = data.pageProps.dehydratedState; diff --git a/lib/routes/gelonghui/home.ts b/lib/routes/gelonghui/home.ts index b846ce062..8e7bd7800 100644 --- a/lib/routes/gelonghui/home.ts +++ b/lib/routes/gelonghui/home.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -56,7 +55,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: '格隆汇-财经资讯动态-股市行情', diff --git a/lib/routes/gelonghui/hot-article.ts b/lib/routes/gelonghui/hot-article.ts index 752c8c655..ab64a4548 100644 --- a/lib/routes/gelonghui/hot-article.ts +++ b/lib/routes/gelonghui/hot-article.ts @@ -2,7 +2,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseItem } from './utils'; @@ -60,7 +59,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `最热文章 - ${type === 0 ? '日排行' : '周排行'} - 格隆汇`, diff --git a/lib/routes/gelonghui/keyword.ts b/lib/routes/gelonghui/keyword.ts index 90a4f6b89..94921c5ae 100644 --- a/lib/routes/gelonghui/keyword.ts +++ b/lib/routes/gelonghui/keyword.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -46,7 +45,7 @@ async function handler(ctx) { pubDate: parseDate(item.timestamp, 'X'), })); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `格隆汇 - 关键词 “${keyword}” 的文章`, diff --git a/lib/routes/gelonghui/subject.ts b/lib/routes/gelonghui/subject.ts index 6f8fdd76a..975406aa2 100644 --- a/lib/routes/gelonghui/subject.ts +++ b/lib/routes/gelonghui/subject.ts @@ -2,7 +2,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -53,7 +52,7 @@ async function handler(ctx) { pubDate: parseDate(item.timestamp, 'X'), })); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `格隆汇 - 主题 ${$('span.user-nick').text()} 的文章`, diff --git a/lib/routes/gelonghui/user.ts b/lib/routes/gelonghui/user.ts index 3132d56b1..20540bf8a 100644 --- a/lib/routes/gelonghui/user.ts +++ b/lib/routes/gelonghui/user.ts @@ -1,6 +1,5 @@ import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -49,7 +48,7 @@ async function handler(ctx) { pubDate: parseDate(item.createTimestamp, 'X'), })); - const items = await Promise.all(list.map((item) => parseItem(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItem(item))); return { title: `格隆汇 - 用户 ${data.result[0].user.nick} 的文章`, diff --git a/lib/routes/gelonghui/utils.ts b/lib/routes/gelonghui/utils.ts index d2608b561..15104a7cf 100644 --- a/lib/routes/gelonghui/utils.ts +++ b/lib/routes/gelonghui/utils.ts @@ -1,11 +1,12 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate, parseRelativeDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; -const parseItem = (item, tryGet) => - tryGet(item.link, async () => { +const parseItem = (item) => + cache.tryGet(item.link, async () => { const { data: res } = await got(item.link); const $ = load(res); if (item.link.startsWith('https://www.gelonghui.com/live/')) { diff --git a/lib/routes/gov/chinamine-safety/util.ts b/lib/routes/gov/chinamine-safety/util.ts index 19664ff4a..15bca0aab 100644 --- a/lib/routes/gov/chinamine-safety/util.ts +++ b/lib/routes/gov/chinamine-safety/util.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -10,13 +11,12 @@ const rootUrl = 'https://www.chinamine-safety.gov.cn'; * Process the item list and return the resulting array. * * @param {Object[]} items - The items to process. - * @param {Function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise} - A promise that resolves to an array of processed items. */ -const processItems = async (items, tryGet) => +const processItems = async (items) => await Promise.all( items.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { if (!item.link.endsWith('html')) { return item; } diff --git a/lib/routes/gov/chinamine-safety/xw.ts b/lib/routes/gov/chinamine-safety/xw.ts index 7fa1e9d2b..040d2c713 100644 --- a/lib/routes/gov/chinamine-safety/xw.ts +++ b/lib/routes/gov/chinamine-safety/xw.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -80,7 +79,7 @@ async function handler(ctx) { }; }); - items = await processItems(items, cache.tryGet); + items = await processItems(items); return { item: items, diff --git a/lib/routes/gov/chinamine-safety/zfxxgk.ts b/lib/routes/gov/chinamine-safety/zfxxgk.ts index d6c8a124f..db622b9b4 100644 --- a/lib/routes/gov/chinamine-safety/zfxxgk.ts +++ b/lib/routes/gov/chinamine-safety/zfxxgk.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -232,7 +231,7 @@ async function handler(ctx) { }; }); - items = await processItems(items, cache.tryGet); + items = await processItems(items); return { item: items, diff --git a/lib/routes/hk01/channel.ts b/lib/routes/hk01/channel.ts index fd3d0b71e..80d28867b 100644 --- a/lib/routes/hk01/channel.ts +++ b/lib/routes/hk01/channel.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -27,7 +26,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.items, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.items, ctx.req.query('limit')); return { title: response.data.category ? `${response.data.category.publishName} | 香港01` : `Channel: ${id} | 香港01`, diff --git a/lib/routes/hk01/hot.ts b/lib/routes/hk01/hot.ts index e58df8ee7..8bea90e29 100644 --- a/lib/routes/hk01/hot.ts +++ b/lib/routes/hk01/hot.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -37,7 +36,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.items, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.items, ctx.req.query('limit')); return { title: '熱門新聞、全城熱話及社會時事 | 香港01', diff --git a/lib/routes/hk01/issue.ts b/lib/routes/hk01/issue.ts index c46ebd34a..01aadcc38 100644 --- a/lib/routes/hk01/issue.ts +++ b/lib/routes/hk01/issue.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -27,7 +26,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.blocks[0].articles, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.blocks[0].articles, ctx.req.query('limit')); return { title: `${response.data.title} | 香港01`, diff --git a/lib/routes/hk01/latest.ts b/lib/routes/hk01/latest.ts index 8b873291e..439b85cb4 100644 --- a/lib/routes/hk01/latest.ts +++ b/lib/routes/hk01/latest.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -37,7 +36,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.items, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.items, ctx.req.query('limit')); return { title: '即時 | 香港01', diff --git a/lib/routes/hk01/tag.ts b/lib/routes/hk01/tag.ts index d7a580d97..34b7bed10 100644 --- a/lib/routes/hk01/tag.ts +++ b/lib/routes/hk01/tag.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -27,7 +26,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.items, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.items, ctx.req.query('limit')); return { title: `${id} | 香港01`, diff --git a/lib/routes/hk01/utils.tsx b/lib/routes/hk01/utils.tsx index 52ede4cd3..05d2a7b15 100644 --- a/lib/routes/hk01/utils.tsx +++ b/lib/routes/hk01/utils.tsx @@ -1,5 +1,6 @@ import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -91,7 +92,7 @@ const renderDescription = ({ image, teasers, blocks }) => ); -const ProcessItems = (items, limit, tryGet) => +const ProcessItems = (items, limit) => Promise.all( items .filter((item) => item.type !== 2) @@ -104,7 +105,7 @@ const ProcessItems = (items, limit, tryGet) => author: item.data.authors.map((a) => a.publishName).join(', '), })) .map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, diff --git a/lib/routes/hk01/zone.ts b/lib/routes/hk01/zone.ts index 237a85272..04bfc7f70 100644 --- a/lib/routes/hk01/zone.ts +++ b/lib/routes/hk01/zone.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, ProcessItems, rootUrl } from './utils'; @@ -27,7 +26,7 @@ async function handler(ctx) { url: apiUrl, }); - const items = await ProcessItems(response.data.items, ctx.req.query('limit'), cache.tryGet); + const items = await ProcessItems(response.data.items, ctx.req.query('limit')); return { title: `${response.data.category.publishName} | 香港01`, diff --git a/lib/routes/hkepc/index.ts b/lib/routes/hkepc/index.ts index b51686deb..8aab23c60 100644 --- a/lib/routes/hkepc/index.ts +++ b/lib/routes/hkepc/index.ts @@ -1,4 +1,3 @@ -/* eslint-disable unicorn/prefer-code-point */ import { load } from 'cheerio'; import type { Route } from '@/types'; @@ -97,7 +96,7 @@ async function handler(ctx) { // Taken from /caixin/blog.js content .find('#view > p') - .filter((_, e) => e.children[0]?.data === String.fromCharCode(160)) + .filter((_, e) => e.children[0]?.data === String.fromCodePoint(160)) .remove(); // fix lazyload image diff --git a/lib/routes/hoyolab/news.tsx b/lib/routes/hoyolab/news.tsx index 4e4a132db..ac23d8bac 100644 --- a/lib/routes/hoyolab/news.tsx +++ b/lib/routes/hoyolab/news.tsx @@ -124,8 +124,8 @@ async function handler(ctx) { language, size: Number.parseInt(ctx.req.query('limit')) || 15, }; - const gameInfo = await getI18nGameInfo(gids, language, cache.tryGet); - const typeInfo = await getI18nType(language, cache.tryGet); + const gameInfo = await getI18nGameInfo(gids, language); + const typeInfo = await getI18nType(language); const list = await getEventList(params); const items = await getPostContent(list, params); return { diff --git a/lib/routes/hoyolab/utils.ts b/lib/routes/hoyolab/utils.ts index 358bf5946..81d2c6eb9 100644 --- a/lib/routes/hoyolab/utils.ts +++ b/lib/routes/hoyolab/utils.ts @@ -1,13 +1,14 @@ +import cache from '@/utils/cache'; import got from '@/utils/got'; -const getGameInfoList = (tryGet) => - tryGet('hoyolab:gameNameList', async () => { +const getGameInfoList = () => + cache.tryGet('hoyolab:gameNameList', async () => { const { data } = await got('https://bbs-api-os-static.hoyolab.com/community/apihub/static/api/getAppConfig'); return JSON.parse(data.data.config.hoyolab_game_info_list); }); -const getI18nGameInfo = async (gid, language, tryGet) => { - const gameNameList = await getGameInfoList(tryGet); +const getI18nGameInfo = async (gid, language) => { + const gameNameList = await getGameInfoList(); const game = gameNameList.find((item) => item.game_id === Number(gid)); return { name: game?.game_name_list.find((item) => item.locale === language)?.raw_name ?? game?.game_name_list.find((item) => item.locale === 'en-us')?.raw_name, @@ -15,8 +16,8 @@ const getI18nGameInfo = async (gid, language, tryGet) => { }; }; -const getI18nType = (language, tryGet) => - tryGet(`hoyolab:type:${language}`, async () => { +const getI18nType = (language) => + cache.tryGet(`hoyolab:type:${language}`, async () => { const { data } = await got(`https://webstatic.hoyoverse.com/admin/mi18n/bbs_oversea/m07281525151831/m07281525151831-${language}.json`); return { diff --git a/lib/routes/huxiu/channel.ts b/lib/routes/huxiu/channel.ts index 0581248f8..23b1a508d 100644 --- a/lib/routes/huxiu/channel.ts +++ b/lib/routes/huxiu/channel.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiArticleRootUrl, buildFeedMetadata, buildHuxiuRouteTitlePrefix, processItems, rootUrl } from './util'; @@ -54,7 +53,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data?.dataList ?? response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data?.dataList ?? response.data.datalist, limit); const rawTitle = response.data?.share_info?.share_title ?? response.data?.name ?? '全部'; const data = buildFeedMetadata({ diff --git a/lib/routes/huxiu/club.ts b/lib/routes/huxiu/club.ts index 71fd9a28e..14b5653e1 100644 --- a/lib/routes/huxiu/club.ts +++ b/lib/routes/huxiu/club.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiClubRootUrl, buildHuxiuRouteTitlePrefix, fetchApiRouteData, processItems, rootUrl } from './util'; @@ -61,7 +60,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data.datalist, limit); return { item: items, diff --git a/lib/routes/huxiu/collection.ts b/lib/routes/huxiu/collection.ts index a0e96605a..b17f8582b 100644 --- a/lib/routes/huxiu/collection.ts +++ b/lib/routes/huxiu/collection.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiArticleRootUrl, buildHuxiuRouteTitlePrefix, fetchApiRouteData, processItems, rootUrl } from './util'; @@ -36,7 +35,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data.datalist, limit); const data = await fetchApiRouteData<{ name: string; diff --git a/lib/routes/huxiu/member.ts b/lib/routes/huxiu/member.ts index 1fcdfe874..461a25fc5 100644 --- a/lib/routes/huxiu/member.ts +++ b/lib/routes/huxiu/member.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiMemberRootUrl, buildHuxiuRouteTitlePrefix, fetchMemberData, processItems } from './util'; @@ -37,7 +36,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data.datalist, limit); const data = await fetchMemberData(id, type, response.data.datalist ?? [], buildHuxiuRouteTitlePrefix(route.name)); diff --git a/lib/routes/huxiu/moment.ts b/lib/routes/huxiu/moment.ts index 44486b5fc..f104952a5 100644 --- a/lib/routes/huxiu/moment.ts +++ b/lib/routes/huxiu/moment.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiMomentRootUrl, buildFeedMetadata, buildHuxiuRouteTitlePrefix, processItems, rootUrl } from './util'; @@ -40,7 +39,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.moment_list.datalist, limit, cache.tryGet); + const items = await processItems(response.data.moment_list.datalist, limit); const data = buildFeedMetadata({ title: '24 小时', diff --git a/lib/routes/huxiu/search.ts b/lib/routes/huxiu/search.ts index 2b824832e..897cdcdbe 100644 --- a/lib/routes/huxiu/search.ts +++ b/lib/routes/huxiu/search.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiSearchRootUrl, buildFeedMetadata, buildHuxiuRouteTitlePrefix, generateSignature, processItems, rootUrl, siteTitle } from './util'; @@ -48,7 +47,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data.datalist, limit); const data = buildFeedMetadata({ title: `搜索结果-${keyword}`, diff --git a/lib/routes/huxiu/tag.ts b/lib/routes/huxiu/tag.ts index 8f9c4315f..b069372d1 100644 --- a/lib/routes/huxiu/tag.ts +++ b/lib/routes/huxiu/tag.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiArticleRootUrl, buildHuxiuRouteTitlePrefix, fetchApiRouteData, processItems, rootUrl } from './util'; @@ -36,7 +35,7 @@ async function handler(ctx) { }, }); - const items = await processItems(response.data.datalist, limit, cache.tryGet); + const items = await processItems(response.data.datalist, limit); const data = await fetchApiRouteData<{ tag_name: string; diff --git a/lib/routes/huxiu/util.ts b/lib/routes/huxiu/util.ts index ed1c06741..6f7adf84c 100644 --- a/lib/routes/huxiu/util.ts +++ b/lib/routes/huxiu/util.ts @@ -1,6 +1,7 @@ import { load } from 'cheerio'; import CryptoJS from 'crypto-js'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -511,10 +512,9 @@ const mapItem = (item) => { * * @param {Object[]} items - The items to process. * @param {number} limit - The maximum number of items to process. - * @param {Function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise} - A promise that resolves to an array of processed items. */ -const processItems = async (items, limit, tryGet) => { +const processItems = async (items, limit) => { const processedItems = items .map((item) => mapItem(item)) .filter(Boolean) @@ -522,7 +522,7 @@ const processItems = async (items, limit, tryGet) => { return await Promise.all( processedItems.map((item) => - tryGet(item.guid, async () => { + cache.tryGet(item.guid, async () => { const isExternalLink = !new RegExp(domain, 'i').test(new URL(item.link).hostname); const isMoment = item.guid.startsWith('huxiu-moment'); diff --git a/lib/routes/instagram/private-api/index.ts b/lib/routes/instagram/private-api/index.ts index c7e8e35d6..8787a236a 100644 --- a/lib/routes/instagram/private-api/index.ts +++ b/lib/routes/instagram/private-api/index.ts @@ -9,7 +9,7 @@ import { renderItems } from '../common-utils'; import { ig, login } from './utils'; // loadContent pulls the desired user/tag/etc -async function loadContent(category, nameOrId, tryGet) { +async function loadContent(category, nameOrId) { let feedTitle, feedLink, feedDescription, feedLogo; let itemsRaw; @@ -18,11 +18,11 @@ async function loadContent(category, nameOrId, tryGet) { let userInfo, username, id; if (Number.isNaN(nameOrId)) { username = nameOrId; - id = await tryGet(`instagram:getIdByUsername:${username}`, () => ig.user.getIdByUsername(username), 31_536_000); // 1 year since it will never change - userInfo = await tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id)); + id = await cache.tryGet(`instagram:getIdByUsername:${username}`, () => ig.user.getIdByUsername(username), 31_536_000); // 1 year since it will never change + userInfo = await cache.tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id)); } else { id = nameOrId; - userInfo = await tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id)); + userInfo = await cache.tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id)); username = userInfo.username; } @@ -33,7 +33,7 @@ async function loadContent(category, nameOrId, tryGet) { feedTitle = `${fullName} (@${username}) - Instagram`; feedLink = `https://www.instagram.com/${username}`; - itemsRaw = await tryGet(`instagram:feed:${id}`, () => ig.feed.user(id).items(), config.cache.routeExpire, false); + itemsRaw = await cache.tryGet(`instagram:feed:${id}`, () => ig.feed.user(id).items(), config.cache.routeExpire, false); break; } case 'tags': { @@ -42,7 +42,7 @@ async function loadContent(category, nameOrId, tryGet) { feedTitle = `#${tag} - Instagram`; feedLink = `https://www.instagram.com/explore/tags/${tag}`; - itemsRaw = await tryGet(`instagram:tags:${tag}`, () => ig.feed.tags(tag, 'recent').items(), config.cache.routeExpire, false); + itemsRaw = await cache.tryGet(`instagram:tags:${tag}`, () => ig.feed.tags(tag, 'recent').items(), config.cache.routeExpire, false); break; } default: @@ -129,7 +129,7 @@ async function handler(ctx) { let data; try { - data = await loadContent(category, key, cache.tryGet); + data = await loadContent(category, key); } catch (error) { logger.error(`Instagram error: ${error}`); throw error; diff --git a/lib/routes/javlibrary/bestrated.ts b/lib/routes/javlibrary/bestrated.ts index e34f905b0..482f1c9bb 100644 --- a/lib/routes/javlibrary/bestrated.ts +++ b/lib/routes/javlibrary/bestrated.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -21,5 +20,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_bestrated.php?list&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/bestreviews.ts b/lib/routes/javlibrary/bestreviews.ts index 38b060de5..c557593e8 100644 --- a/lib/routes/javlibrary/bestreviews.ts +++ b/lib/routes/javlibrary/bestreviews.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -30,5 +29,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/tl_bestreviews.php?list&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/genre.ts b/lib/routes/javlibrary/genre.ts index ae53f811f..4077bfa93 100644 --- a/lib/routes/javlibrary/genre.ts +++ b/lib/routes/javlibrary/genre.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultGenre, defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -26,5 +25,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_genre.php?list&g=${genre}&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/maker.ts b/lib/routes/javlibrary/maker.ts index 1428158d3..e11e07c7b 100644 --- a/lib/routes/javlibrary/maker.ts +++ b/lib/routes/javlibrary/maker.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMaker, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -31,5 +30,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_maker.php?list&m=${maker}&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/mostwanted.ts b/lib/routes/javlibrary/mostwanted.ts index 7474ec434..69bba16e0 100644 --- a/lib/routes/javlibrary/mostwanted.ts +++ b/lib/routes/javlibrary/mostwanted.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -21,5 +20,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_mostwanted.php?list&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/newentries.ts b/lib/routes/javlibrary/newentries.ts index 6bc22aca1..40ec2e36c 100644 --- a/lib/routes/javlibrary/newentries.ts +++ b/lib/routes/javlibrary/newentries.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, ProcessItems, rootUrl } from './utils'; @@ -17,5 +16,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_newentries.php?list`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/newrelease.ts b/lib/routes/javlibrary/newrelease.ts index 0bad12e3f..def757006 100644 --- a/lib/routes/javlibrary/newrelease.ts +++ b/lib/routes/javlibrary/newrelease.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -21,5 +20,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_newrelease.php?list&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/star.ts b/lib/routes/javlibrary/star.ts index c37c255c3..f87115898 100644 --- a/lib/routes/javlibrary/star.ts +++ b/lib/routes/javlibrary/star.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, defaultMode, ProcessItems, rootUrl } from './utils'; @@ -37,5 +36,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_star.php?list&s=${id}&mode=${mode}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/update.ts b/lib/routes/javlibrary/update.ts index 5b75ed512..4079aea21 100644 --- a/lib/routes/javlibrary/update.ts +++ b/lib/routes/javlibrary/update.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, ProcessItems, rootUrl } from './utils'; @@ -17,5 +16,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/vl_update.php?list`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/user.ts b/lib/routes/javlibrary/user.ts index d64bb7f55..d1d834151 100644 --- a/lib/routes/javlibrary/user.ts +++ b/lib/routes/javlibrary/user.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { defaultLanguage, ProcessItems, rootUrl } from './utils'; @@ -22,5 +21,5 @@ async function handler(ctx) { const language = ctx.req.param('language') ?? defaultLanguage; const currentUrl = `${rootUrl}/${language}/${type}.php?list&u=${id}`; - return await ProcessItems(language, currentUrl, cache.tryGet); + return await ProcessItems(language, currentUrl); } diff --git a/lib/routes/javlibrary/utils.tsx b/lib/routes/javlibrary/utils.tsx index 6b0b10ff9..95b0b032f 100644 --- a/lib/routes/javlibrary/utils.tsx +++ b/lib/routes/javlibrary/utils.tsx @@ -2,6 +2,7 @@ import { load } from 'cheerio'; import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -32,7 +33,7 @@ const renderDescription = ({ cover, info, comment, videos, thumbs }) => {thumbs?.length ? thumbs.map((thumb) => ) : null} ); -const ProcessItems = async (language, currentUrl, tryGet) => { +const ProcessItems = async (language, currentUrl) => { const response = await got({ method: 'get', url: currentUrl, @@ -64,7 +65,7 @@ const ProcessItems = async (language, currentUrl, tryGet) => { items = await Promise.all( items.map((item) => - tryGet(item.url, async () => { + cache.tryGet(item.url, async () => { const detailResponse = await got({ method: 'get', url: item.url, diff --git a/lib/routes/meteor/boards.ts b/lib/routes/meteor/boards.ts index 3a7181e0f..9816d5912 100644 --- a/lib/routes/meteor/boards.ts +++ b/lib/routes/meteor/boards.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { baseUrl, getBoards } from './utils'; @@ -28,7 +27,7 @@ export const route: Route = { }; async function handler() { - const items = await getBoards(cache.tryGet); + const items = await getBoards(); return { title: '看板列表', diff --git a/lib/routes/meteor/index.ts b/lib/routes/meteor/index.ts index b82f93674..4ef131020 100644 --- a/lib/routes/meteor/index.ts +++ b/lib/routes/meteor/index.ts @@ -26,7 +26,7 @@ export const route: Route = { async function handler(ctx) { let { board = 'all' } = ctx.req.param(); - const boards = await getBoards(cache.tryGet); + const boards = await getBoards(); let boardInfo; if (board !== 'all') { boardInfo = boards.find((b) => b.id === board || b.alias === board); diff --git a/lib/routes/meteor/utils.ts b/lib/routes/meteor/utils.ts index 52904f16c..15930b981 100644 --- a/lib/routes/meteor/utils.ts +++ b/lib/routes/meteor/utils.ts @@ -1,11 +1,12 @@ +import cache from '@/utils/cache'; import got from '@/utils/got'; import { renderMedia } from './templates/desc'; const baseUrl = 'https://meteor.today'; -const getBoards = (tryGet) => - tryGet('meteor:boards', async () => { +const getBoards = () => + cache.tryGet('meteor:boards', async () => { const { data: response } = await got.post(`${baseUrl}/board/get_boards`, { json: { isCollege: 'false', diff --git a/lib/routes/newzmz/index.ts b/lib/routes/newzmz/index.ts index e73861d96..1404b81ab 100644 --- a/lib/routes/newzmz/index.ts +++ b/lib/routes/newzmz/index.ts @@ -61,14 +61,14 @@ async function handler(ctx) { // add that item alone to the "to be processed" array. let items = isCategory - ? await getItems(cache.tryGet, currentUrl, id, 'div.rowMod', 'ul.slides li a') + ? await getItems(currentUrl, id, 'div.rowMod', 'ul.slides li a') : [ { link: currentUrl, }, ]; - items = await Promise.all(items.slice(0, limit).map((item) => getItemInfo(cache.tryGet, item.link))); + items = await Promise.all(items.slice(0, limit).map((item) => getItemInfo(item.link))); // If the link of the entry is "#", // it indicates that there are currently no relevant resources available for that specific item. diff --git a/lib/routes/newzmz/util.ts b/lib/routes/newzmz/util.ts index eda1b193b..8800c1d1d 100644 --- a/lib/routes/newzmz/util.ts +++ b/lib/routes/newzmz/util.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -9,15 +10,14 @@ const rootUrl = 'https://nzmz.xyz'; /** * Retrieve all movies and TV shows under a specified category on the homepage and obtain their detail links. - * @param {function} tryGet - cache.tryGet * @param {string} homeUrl - Homepage URL * @param {string} id - Category id * @param {string} modSelector - Selector for mods * @param {string} itemSelector - Selector for items * @returns {Array} An array containing the links in the map. */ -const getItems = async (tryGet, homeUrl, id, modSelector, itemSelector) => { - const response = await tryGet(homeUrl, async () => { +const getItems = async (homeUrl, id, modSelector, itemSelector) => { + const response = await cache.tryGet(homeUrl, async () => { const { data: response } = await got(homeUrl); return response; @@ -40,12 +40,11 @@ const getItems = async (tryGet, homeUrl, id, modSelector, itemSelector) => { /** * Obtain the information corresponding to a given movie or TV show item based on the provided URL. - * @param {function} tryGet - cache.tryGet * @param {string} itemUrl - Item URL * @returns {Object} An object containing information of the item. */ -const getItemInfo = (tryGet, itemUrl) => - tryGet(`newzmz#${itemUrl.match(/details-(.*?)\.html/)[1]}`, async () => { +const getItemInfo = (itemUrl) => + cache.tryGet(`newzmz#${itemUrl.match(/details-(.*?)\.html/)[1]}`, async () => { const { data: detailResponse } = await got(itemUrl); const content = load(detailResponse); diff --git a/lib/routes/oeeee/app/channel.ts b/lib/routes/oeeee/app/channel.ts index 74db98ec6..12453f86e 100644 --- a/lib/routes/oeeee/app/channel.ts +++ b/lib/routes/oeeee/app/channel.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -35,7 +34,7 @@ async function handler(ctx) { const channel = list[1] ? list[1].channel : ''; - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: `南方都市报客户端 - ${channel}`, diff --git a/lib/routes/oeeee/app/reporter.ts b/lib/routes/oeeee/app/reporter.ts index 7f6a59e92..0b85643b0 100644 --- a/lib/routes/oeeee/app/reporter.ts +++ b/lib/routes/oeeee/app/reporter.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { renderDescription } from '../templates/description'; @@ -41,7 +40,7 @@ async function handler(ctx) { const author = response.data.info ? response.data.info.name : ''; - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: `南方都市报奥一网 - ${author}`, diff --git a/lib/routes/oeeee/utils.ts b/lib/routes/oeeee/utils.ts index 3d731409c..22dd547f8 100644 --- a/lib/routes/oeeee/utils.ts +++ b/lib/routes/oeeee/utils.ts @@ -1,11 +1,12 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, diff --git a/lib/routes/oeeee/web.ts b/lib/routes/oeeee/web.ts index e3a477ace..740a4d9a5 100644 --- a/lib/routes/oeeee/web.ts +++ b/lib/routes/oeeee/web.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -48,7 +47,7 @@ async function handler(ctx) { const channelEname = list[1] ? list[1].channelEname : ''; - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: '南方都市报奥一网', diff --git a/lib/routes/otobanana/cast.ts b/lib/routes/otobanana/cast.ts index 42c17cb5a..5c6846a2e 100644 --- a/lib/routes/otobanana/cast.ts +++ b/lib/routes/otobanana/cast.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiBase, baseUrl, getUserInfo, renderCast } from './utils'; @@ -30,7 +29,7 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); - const userInfo = await getUserInfo(id, cache.tryGet); + const userInfo = await getUserInfo(id); const { data: castData } = await got(`${apiBase}/users/${id}/casts/`); const casts = castData.results.map((item) => renderCast(item)); diff --git a/lib/routes/otobanana/livestream.ts b/lib/routes/otobanana/livestream.ts index 341d529a1..524684120 100644 --- a/lib/routes/otobanana/livestream.ts +++ b/lib/routes/otobanana/livestream.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiBase, baseUrl, getUserInfo, renderLive } from './utils'; @@ -30,7 +29,7 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); - const userInfo = await getUserInfo(id, cache.tryGet); + const userInfo = await getUserInfo(id); const { data: liveData } = await got(`${apiBase}/users/${id}/livestreams/`); const casts = liveData.results.map((item) => renderLive(item)); diff --git a/lib/routes/otobanana/timeline.ts b/lib/routes/otobanana/timeline.ts index e5cd0bb24..d45f68028 100644 --- a/lib/routes/otobanana/timeline.ts +++ b/lib/routes/otobanana/timeline.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiBase, baseUrl, getUserInfo, renderPost } from './utils'; @@ -30,7 +29,7 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); - const userInfo = await getUserInfo(id, cache.tryGet); + const userInfo = await getUserInfo(id); const { data: postData } = await got(`${apiBase}/users/${id}/posts/`); const posts = postData.results.map((item) => renderPost(item)); diff --git a/lib/routes/otobanana/utils.tsx b/lib/routes/otobanana/utils.tsx index 68722e361..557d6093c 100644 --- a/lib/routes/otobanana/utils.tsx +++ b/lib/routes/otobanana/utils.tsx @@ -1,6 +1,7 @@ import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -8,8 +9,8 @@ const domain = 'otobanana.com'; const apiBase = `https://api.${domain}`; const baseUrl = `https://${domain}`; -const getUserInfo = (id, tryGet) => - tryGet(`otobanana:user:${id}`, async () => { +const getUserInfo = (id) => + cache.tryGet(`otobanana:user:${id}`, async () => { const { data } = await got(`${apiBase}/users/${id}/`); return data; }); diff --git a/lib/routes/pixiv/bookmarks.ts b/lib/routes/pixiv/bookmarks.ts index dd86b2a02..fb3f6f2c7 100644 --- a/lib/routes/pixiv/bookmarks.ts +++ b/lib/routes/pixiv/bookmarks.ts @@ -1,7 +1,6 @@ import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import getBookmarks from './api/get-bookmarks'; @@ -40,7 +39,7 @@ async function handler(ctx) { const id = ctx.req.param('id'); - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/illustfollow.ts b/lib/routes/pixiv/illustfollow.ts index e04a51ae3..bccf25950 100644 --- a/lib/routes/pixiv/illustfollow.ts +++ b/lib/routes/pixiv/illustfollow.ts @@ -1,7 +1,6 @@ import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import getIllustFollows from './api/get-illust-follows'; @@ -46,7 +45,7 @@ async function handler() { throw new ConfigNotFoundError('pixiv RSS is disabled due to the lack of relevant config'); } - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/novel-api/series/nsfw.ts b/lib/routes/pixiv/novel-api/series/nsfw.ts index cb8db0832..2da223ee7 100644 --- a/lib/routes/pixiv/novel-api/series/nsfw.ts +++ b/lib/routes/pixiv/novel-api/series/nsfw.ts @@ -2,7 +2,6 @@ import queryString from 'query-string'; import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; -import cache from '@/utils/cache'; import { maskHeader } from '../../constants'; import got from '../../pixiv-got'; @@ -36,7 +35,7 @@ export async function getNSFWSeriesNovels(seriesId: string, limit: number = 10): throw new ConfigNotFoundError('This user is an R18 creator, PIXIV_REFRESHTOKEN is required.\npixiv RSS is disabled due to the lack of relevant config.\n該用戶爲 R18 創作者,需要 PIXIV_REFRESHTOKEN。'); } - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/novel-api/user-novels/nsfw.ts b/lib/routes/pixiv/novel-api/user-novels/nsfw.ts index db9a896de..c6699b6c5 100644 --- a/lib/routes/pixiv/novel-api/user-novels/nsfw.ts +++ b/lib/routes/pixiv/novel-api/user-novels/nsfw.ts @@ -3,7 +3,6 @@ import queryString from 'query-string'; import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import InvalidParameterError from '@/errors/types/invalid-parameter'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import { maskHeader } from '../../constants'; @@ -32,7 +31,7 @@ export async function getNSFWUserNovels(id: string, fullContent: boolean = false throw new ConfigNotFoundError('This user is an R18 creator, PIXIV_REFRESHTOKEN is required.\npixiv RSS is disabled due to the lack of relevant config.\n該用戶爲 R18 創作者,需要 PIXIV_REFRESHTOKEN。'); } - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/ranking.ts b/lib/routes/pixiv/ranking.ts index 4277d803c..a251431da 100644 --- a/lib/routes/pixiv/ranking.ts +++ b/lib/routes/pixiv/ranking.ts @@ -2,7 +2,6 @@ import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import getRanking from './api/get-ranking'; @@ -152,7 +151,7 @@ async function handler(ctx) { const mode = alias[ctx.req.param('mode')] ?? ctx.req.param('mode'); const date = ctx.req.param('date') ? new Date(ctx.req.param('date')) : new Date(); - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/search.ts b/lib/routes/pixiv/search.ts index e4f81e596..81066abd1 100644 --- a/lib/routes/pixiv/search.ts +++ b/lib/routes/pixiv/search.ts @@ -2,7 +2,6 @@ import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import searchIllust from './api/search-illust'; @@ -88,7 +87,7 @@ async function handler(ctx) { const mode = ctx.req.param('mode'); const includeAI = ctx.req.param('include_ai'); - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pixiv/token.ts b/lib/routes/pixiv/token.ts index c087a2700..7938aecf5 100644 --- a/lib/routes/pixiv/token.ts +++ b/lib/routes/pixiv/token.ts @@ -1,4 +1,5 @@ import { config } from '@/config'; +import cache from '@/utils/cache'; import logger from '@/utils/logger'; import { maskHeader } from './constants'; @@ -12,8 +13,8 @@ const authorizationInfo = { hash_secret: '28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c', }; -const refreshToken = (tryGet) => - tryGet( +const refreshToken = () => + cache.tryGet( 'pixiv:accessToken', () => got.post('https://oauth.secure.pixiv.net/auth/token', { @@ -31,8 +32,8 @@ const refreshToken = (tryGet) => false ); -async function getToken(tryGet) { - const { data } = await refreshToken(tryGet); +async function getToken() { + const { data } = await refreshToken(); // let expireTime; if (data && data.access_token) { logger.debug('Pixiv refresh token success.'); diff --git a/lib/routes/pixiv/user.ts b/lib/routes/pixiv/user.ts index 4506089d5..4c5c44684 100644 --- a/lib/routes/pixiv/user.ts +++ b/lib/routes/pixiv/user.ts @@ -2,7 +2,6 @@ import { config } from '@/config'; import ConfigNotFoundError from '@/errors/types/config-not-found'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; import getIllusts from './api/get-illusts'; @@ -40,7 +39,7 @@ async function handler(ctx) { } const id = ctx.req.param('id'); - const token = await getToken(cache.tryGet); + const token = await getToken(); if (!token) { throw new ConfigNotFoundError('pixiv not login'); } diff --git a/lib/routes/pku/ss/admission.ts b/lib/routes/pku/ss/admission.ts index 5fb012521..20484d14e 100644 --- a/lib/routes/pku/ss/admission.ts +++ b/lib/routes/pku/ss/admission.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { baseUrl, getArticle, getSingleRecord } from './common'; @@ -31,7 +30,7 @@ export const route: Route = { async function handler() { const items = await getSingleRecord(host); - const out = await Promise.all(items.map((item) => getArticle(item, cache.tryGet))); + const out = await Promise.all(items.map((item) => getArticle(item))); return { title: '北大软微-招生通知', diff --git a/lib/routes/pku/ss/common.ts b/lib/routes/pku/ss/common.ts index e7416191e..5a3f0c1e1 100644 --- a/lib/routes/pku/ss/common.ts +++ b/lib/routes/pku/ss/common.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -22,8 +23,8 @@ const getSingleRecord = async (url) => { }); }; -const getArticle = (item, tryGet) => - tryGet(item.link, async () => { +const getArticle = (item) => + cache.tryGet(item.link, async () => { const response = await got(item.link); const $ = load(response.data); diff --git a/lib/routes/pku/ss/notice.ts b/lib/routes/pku/ss/notice.ts index b00bf22a3..38fdb5c01 100644 --- a/lib/routes/pku/ss/notice.ts +++ b/lib/routes/pku/ss/notice.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { baseUrl, getArticle, getSingleRecord } from './common'; @@ -20,7 +19,7 @@ export const route: Route = { async function handler() { const items = await getSingleRecord(host); - const out = await Promise.all(items.map((item) => getArticle(item, cache.tryGet))); + const out = await Promise.all(items.map((item) => getArticle(item))); return { title: '北大软微-通知公告', diff --git a/lib/routes/pku/ss/pg-admin.ts b/lib/routes/pku/ss/pg-admin.ts index f16b7ccb8..42458fba1 100644 --- a/lib/routes/pku/ss/pg-admin.ts +++ b/lib/routes/pku/ss/pg-admin.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { baseUrl, getArticle, getSingleRecord } from './common'; @@ -31,7 +30,7 @@ export const route: Route = { async function handler() { const items = await getSingleRecord(host); - const out = await Promise.all(items.map((item) => getArticle(item, cache.tryGet))); + const out = await Promise.all(items.map((item) => getArticle(item))); return { title: '北大软微-硕士统考招生', diff --git a/lib/routes/plurk/anonymous.ts b/lib/routes/plurk/anonymous.ts index 7ee88d659..845fb17ac 100644 --- a/lib/routes/plurk/anonymous.ts +++ b/lib/routes/plurk/anonymous.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getPlurk } from './utils'; @@ -39,7 +38,7 @@ async function handler(ctx) { delete apiResponse.pids; delete apiResponse.count; - const items = await Promise.all(Object.values(apiResponse).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, 'ಠ_ಠ', cache.tryGet))); + const items = await Promise.all(Object.values(apiResponse).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, 'ಠ_ಠ'))); return { title: 'Anonymous - Plurk', diff --git a/lib/routes/plurk/hotlinks.ts b/lib/routes/plurk/hotlinks.ts index 122a5928a..e07fd8093 100644 --- a/lib/routes/plurk/hotlinks.ts +++ b/lib/routes/plurk/hotlinks.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getPlurk } from './utils'; @@ -36,7 +35,7 @@ async function handler(ctx) { }, }); - const items = await Promise.all(apiResponse.map((item) => getPlurk(item.link_url.startsWith('https://www.plurk.com/p/') ? item.link_url : `plurk:${item.link_url}`, item, null, cache.tryGet))); + const items = await Promise.all(apiResponse.map((item) => getPlurk(item.link_url.startsWith('https://www.plurk.com/p/') ? item.link_url : `plurk:${item.link_url}`, item, null))); return { title: 'Hot Links - Plurk', diff --git a/lib/routes/plurk/news.ts b/lib/routes/plurk/news.ts index 45557a02f..749d46e71 100644 --- a/lib/routes/plurk/news.ts +++ b/lib/routes/plurk/news.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, fetchFriends, getPlurk } from './utils'; @@ -40,7 +39,7 @@ async function handler(ctx) { const userIds = apiResponse.map((item) => item.user_id); const names = await fetchFriends(userIds); - const items = await Promise.all(apiResponse.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name, cache.tryGet))); + const items = await Promise.all(apiResponse.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name))); return { title: 'Plurk News - Plurk', diff --git a/lib/routes/plurk/search.ts b/lib/routes/plurk/search.ts index f14fcdc34..cdc17486a 100644 --- a/lib/routes/plurk/search.ts +++ b/lib/routes/plurk/search.ts @@ -1,7 +1,6 @@ import dayjs from 'dayjs'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getPlurk } from './utils'; @@ -37,7 +36,7 @@ async function handler(ctx) { const users = apiResponse.users; const plurks = apiResponse.plurks; - const items = await Promise.all(plurks.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, users[item.user_id].display_name, cache.tryGet))); + const items = await Promise.all(plurks.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, users[item.user_id].display_name))); return { title: `Search "${keyword}" - Plurk`, diff --git a/lib/routes/plurk/top.ts b/lib/routes/plurk/top.ts index afada6cea..a25f3712b 100644 --- a/lib/routes/plurk/top.ts +++ b/lib/routes/plurk/top.ts @@ -1,7 +1,6 @@ import InvalidParameterError from '@/errors/types/invalid-parameter'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, getPlurk } from './utils'; @@ -48,7 +47,7 @@ async function handler(ctx) { }, }); - const items = await Promise.all(apiResponse.stats.map((item) => item[1]).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, item.owner.display_name, cache.tryGet))); + const items = await Promise.all(apiResponse.stats.map((item) => item[1]).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, item.owner.display_name))); return { title: 'Top Plurk - Plurk', diff --git a/lib/routes/plurk/topic.ts b/lib/routes/plurk/topic.ts index b54d2a656..dd4677e2b 100644 --- a/lib/routes/plurk/topic.ts +++ b/lib/routes/plurk/topic.ts @@ -2,7 +2,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, fetchFriends, getPlurk } from './utils'; @@ -50,7 +49,7 @@ async function handler(ctx) { const userIds = Object.values(apiResponse).map((item) => item.user_id); const names = await fetchFriends(userIds); - const items = await Promise.all(Object.values(apiResponse).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name, cache.tryGet))); + const items = await Promise.all(Object.values(apiResponse).map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name))); return { title: $('head title').text(), diff --git a/lib/routes/plurk/user.ts b/lib/routes/plurk/user.ts index a099a8a26..9fb103fee 100644 --- a/lib/routes/plurk/user.ts +++ b/lib/routes/plurk/user.ts @@ -2,7 +2,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; import { ViewType } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { baseUrl, fetchFriends, getPlurk } from './utils'; @@ -43,7 +42,7 @@ async function handler(ctx) { const userIds = publicPlurks.map((item) => item.user_id); const names = await fetchFriends(userIds); - const items = await Promise.all(publicPlurks.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name, cache.tryGet))); + const items = await Promise.all(publicPlurks.map((item) => getPlurk(`plurk:${item.plurk_id}`, item, names[item.user_id].display_name))); return { title: $('head title').text(), diff --git a/lib/routes/plurk/utils.ts b/lib/routes/plurk/utils.ts index a070ac782..50aa94609 100644 --- a/lib/routes/plurk/utils.ts +++ b/lib/routes/plurk/utils.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -25,11 +26,10 @@ const fetchFriends = async (userIds) => { * @param {string} plurkGuid plurk guid, should start with 'plurk:' * @param {object} item plurk object * @param {string} author author name, can be `null` - * @param {function} tryGet cache get function * @returns {object} item object */ -const getPlurk = (plurkGuid, item, author, tryGet) => - tryGet(plurkGuid, () => { +const getPlurk = (plurkGuid, item, author) => + cache.tryGet(plurkGuid, () => { const $ = load(item.content || item.rendered, null, false); $('img').each((_, e) => { e = $(e); diff --git a/lib/routes/qianp/news.ts b/lib/routes/qianp/news.ts index 203beea20..5efe5b1cd 100644 --- a/lib/routes/qianp/news.ts +++ b/lib/routes/qianp/news.ts @@ -19,7 +19,7 @@ async function handler(ctx) { const { path = 'news/recommend' } = ctx.req.param(); const url = `${baseUrl}/${path}/`; - const { token, secret } = await getTokenAndSecret(cache.tryGet); + const { token, secret } = await getTokenAndSecret(); const headers = { cookie: token ? `t=${token}; r=${secret - 100}` : undefined, }; diff --git a/lib/routes/qianp/utils.ts b/lib/routes/qianp/utils.ts index 75da724d3..0c67da37d 100644 --- a/lib/routes/qianp/utils.ts +++ b/lib/routes/qianp/utils.ts @@ -1,7 +1,8 @@ +import cache from '@/utils/cache'; import got from '@/utils/got'; -const getTokenAndSecret = (tryGet) => - tryGet('qianp:token', async () => { +const getTokenAndSecret = () => + cache.tryGet('qianp:token', async () => { const response = await got('https://qianp.com/news/recommend/'); const token = response.headers['set-cookie'] .find((cookie) => cookie.startsWith('token=')) diff --git a/lib/routes/readhub/daily.ts b/lib/routes/readhub/daily.ts index 4f0431202..9b550bcf2 100644 --- a/lib/routes/readhub/daily.ts +++ b/lib/routes/readhub/daily.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { apiRootUrl, processItems, rootUrl } from './util'; @@ -47,7 +46,7 @@ async function handler(ctx) { guid: item.uid, })); - items = await processItems(items, cache.tryGet); + items = await processItems(items); const { data: currentHTMLResponse } = await got(infoUrl); const $ = load(currentHTMLResponse); diff --git a/lib/routes/readhub/index.ts b/lib/routes/readhub/index.ts index f76a8579c..4cae47f72 100644 --- a/lib/routes/readhub/index.ts +++ b/lib/routes/readhub/index.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -62,7 +61,7 @@ async function handler(ctx) { pubDate: parseDate(item.publishDate), })); - items = await processItems(items, cache.tryGet); + items = await processItems(items); const $ = load(currentResponse); diff --git a/lib/routes/readhub/util.ts b/lib/routes/readhub/util.ts index 825214c66..5f0d4a183 100644 --- a/lib/routes/readhub/util.ts +++ b/lib/routes/readhub/util.ts @@ -1,3 +1,4 @@ +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -12,13 +13,12 @@ const apiTopicUrl = new URL('topic/list', apiRootUrl).href; * Process items asynchronously. * * @param {Array} items - The array of items to process. - * @param {function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise>} Returns a Promise that resolves to an array of processed items. */ -const processItems = async (items, tryGet) => +const processItems = async (items) => await Promise.all( items.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { try { if (!item.link.startsWith(rootUrl)) { throw new Error(`"${item.link}" is an external URL`); diff --git a/lib/routes/science/current.ts b/lib/routes/science/current.ts index c4f189515..cf1d42456 100644 --- a/lib/routes/science/current.ts +++ b/lib/routes/science/current.ts @@ -9,7 +9,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import playwright from '@/utils/playwright'; @@ -67,7 +66,7 @@ async function handler(ctx) { .map((item) => getItem(item, $)); const context = await playwright(); - const items = await fetchDesc(list, context, cache.tryGet); + const items = await fetchDesc(list, context); await context.close(); return { diff --git a/lib/routes/science/early.ts b/lib/routes/science/early.ts index 01dfa2c6e..18558bd47 100644 --- a/lib/routes/science/early.ts +++ b/lib/routes/science/early.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import playwright from '@/utils/playwright'; @@ -48,7 +47,7 @@ async function handler(ctx) { .map((item) => getItem(item, $)); const context = await playwright(); - const items = await fetchDesc(list, context, cache.tryGet); + const items = await fetchDesc(list, context); await context.close(); return { diff --git a/lib/routes/science/utils.tsx b/lib/routes/science/utils.tsx index c4bad3631..156cfce65 100644 --- a/lib/routes/science/utils.tsx +++ b/lib/routes/science/utils.tsx @@ -2,14 +2,15 @@ import { load } from 'cheerio'; import { raw } from 'hono/html'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import { parseDate } from '@/utils/parse-date'; const baseUrl = 'https://www.science.org'; -const fetchDesc = (list, context, tryGet) => +const fetchDesc = (list, context) => Promise.all( list.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const page = await context.newPage(); await page.route('**/*', (route) => { const request = route.request(); diff --git a/lib/routes/segmentfault/blogs.ts b/lib/routes/segmentfault/blogs.ts index 6d7515248..bbde348af 100644 --- a/lib/routes/segmentfault/blogs.ts +++ b/lib/routes/segmentfault/blogs.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { acw_sc__v2, host, parseItems, parseList } from './utils'; @@ -35,9 +34,9 @@ async function handler(ctx) { const list = parseList(data); - const acwScV2Cookie = await acw_sc__v2(list[0].link, cache.tryGet); + const acwScV2Cookie = await acw_sc__v2(list[0].link); - const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item))); return { title: `segmentfault-Blogs-${tag}`, diff --git a/lib/routes/segmentfault/channel.ts b/lib/routes/segmentfault/channel.ts index 1dc6609b5..587527466 100644 --- a/lib/routes/segmentfault/channel.ts +++ b/lib/routes/segmentfault/channel.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { acw_sc__v2, host, parseItems, parseList } from './utils'; @@ -49,9 +48,9 @@ async function handler(ctx) { const list = parseList(apiResponse.rows); - const acwScV2Cookie = await acw_sc__v2(list[0].link, cache.tryGet); + const acwScV2Cookie = await acw_sc__v2(list[0].link); - const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item))); return { title: `segmentfault - ${channelName}`, diff --git a/lib/routes/segmentfault/user.ts b/lib/routes/segmentfault/user.ts index 8e16d0c7c..8ecc16398 100644 --- a/lib/routes/segmentfault/user.ts +++ b/lib/routes/segmentfault/user.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; import { acw_sc__v2, host, parseItems, parseList } from './utils'; @@ -37,9 +36,9 @@ async function handler(ctx) { const list = parseList(data); const { author } = list[0]; - const acwScV2Cookie = await acw_sc__v2(list[0].link, cache.tryGet); + const acwScV2Cookie = await acw_sc__v2(list[0].link); - const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseItems(acwScV2Cookie, item))); return { title: `segmentfault - ${author}`, diff --git a/lib/routes/segmentfault/utils.ts b/lib/routes/segmentfault/utils.ts index 517a1b85c..7030fbc85 100644 --- a/lib/routes/segmentfault/utils.ts +++ b/lib/routes/segmentfault/utils.ts @@ -1,6 +1,7 @@ import { load } from 'cheerio'; import { config } from '@/config'; +import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; @@ -8,8 +9,8 @@ import { getAcwScV2ByArg1 } from '../5eplay/utils'; const host = 'https://segmentfault.com'; -const acw_sc__v2 = (link, tryGet) => - tryGet( +const acw_sc__v2 = (link) => + cache.tryGet( 'segmentfault:acw_sc__v2', async () => { const response = await ofetch(link); @@ -34,8 +35,8 @@ const parseList = (data) => description: item.excerpt, })); -const parseItems = (cookie, item, tryGet) => - tryGet(item.link, async () => { +const parseItems = (cookie, item) => + cache.tryGet(item.link, async () => { try { const response = await ofetch(item.link, { headers: { diff --git a/lib/routes/sina/chuangshiji.ts b/lib/routes/sina/chuangshiji.ts index 50355859d..0310b4bb3 100644 --- a/lib/routes/sina/chuangshiji.ts +++ b/lib/routes/sina/chuangshiji.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getRollNewsList, parseArticle, parseRollNewsList } from './utils'; @@ -34,7 +33,7 @@ async function handler(ctx) { const response = await getRollNewsList(pageid, lid, limit); const list = parseRollNewsList(response.data.result.data); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: '新浪专栏-创事记', diff --git a/lib/routes/sina/discovery.ts b/lib/routes/sina/discovery.ts index 39af3bbd9..cd3feca3f 100644 --- a/lib/routes/sina/discovery.ts +++ b/lib/routes/sina/discovery.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getRollNewsList, parseArticle, parseRollNewsList } from './utils'; @@ -46,7 +45,7 @@ async function handler(ctx) { const response = await getRollNewsList(pageid, lid, limit); const list = parseRollNewsList(response.data.result.data); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: `${title}-新浪科技科学探索`, diff --git a/lib/routes/sina/finance/china.ts b/lib/routes/sina/finance/china.ts index b4568ff9f..bfe98f00a 100644 --- a/lib/routes/sina/finance/china.ts +++ b/lib/routes/sina/finance/china.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getRollNewsList, parseArticle, parseRollNewsList } from '../utils'; @@ -46,7 +45,7 @@ async function handler(ctx) { const response = await getRollNewsList(pageid, lid, limit); const list = parseRollNewsList(response.data.result.data); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: `新浪财经-${map[lid]}`, diff --git a/lib/routes/sina/finance/rollnews.ts b/lib/routes/sina/finance/rollnews.ts index 347b0c3b1..20b0201aa 100644 --- a/lib/routes/sina/finance/rollnews.ts +++ b/lib/routes/sina/finance/rollnews.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getRollNewsList, parseArticle, parseRollNewsList } from '../utils'; @@ -49,7 +48,7 @@ async function handler(ctx) { const response = await getRollNewsList(pageid, lid, limit); const list = parseRollNewsList(response.data.result.data); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: `新浪财经-${map[lid] ?? lid}滚动新闻`, diff --git a/lib/routes/sina/finance/stock/usstock.ts b/lib/routes/sina/finance/stock/usstock.ts index 9b036fd10..e3b488cde 100644 --- a/lib/routes/sina/finance/stock/usstock.ts +++ b/lib/routes/sina/finance/stock/usstock.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -58,7 +57,7 @@ async function handler(ctx) { author: item.media, })); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: '美股|美股行情|美股新闻 - 新浪财经', diff --git a/lib/routes/sina/rollnews.ts b/lib/routes/sina/rollnews.ts index b23e14c4f..1c593edc4 100644 --- a/lib/routes/sina/rollnews.ts +++ b/lib/routes/sina/rollnews.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getRollNewsList, parseArticle, parseRollNewsList } from './utils'; @@ -45,7 +44,7 @@ async function handler(ctx) { const response = await getRollNewsList(pageid, lid, limit); const list = parseRollNewsList(response.data.result.data); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: `新浪${map[lid]}滚动新闻`, diff --git a/lib/routes/sina/sports.ts b/lib/routes/sina/sports.ts index 6aa740db3..f02433c2a 100644 --- a/lib/routes/sina/sports.ts +++ b/lib/routes/sina/sports.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseArticle } from './utils'; @@ -46,7 +45,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: `${$('title').text().split('_', 1)[0]} - 新浪体育`, diff --git a/lib/routes/sina/utils.tsx b/lib/routes/sina/utils.tsx index 1259a25ca..5c9c6d44f 100644 --- a/lib/routes/sina/utils.tsx +++ b/lib/routes/sina/utils.tsx @@ -1,6 +1,7 @@ import { load } from 'cheerio'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -31,8 +32,8 @@ const parseRollNewsList = (data) => updated: parseDate(item.mtime, 'X'), })); -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const detailResponse = await got(item.link); const $ = load(detailResponse.data); $('#left_hzh_ad, .appendQr_wrap, .app-kaihu-qr, .tech-quotation').remove(); diff --git a/lib/routes/southcn/nfapp/column.ts b/lib/routes/southcn/nfapp/column.ts index d69359fc5..b4445353d 100644 --- a/lib/routes/southcn/nfapp/column.ts +++ b/lib/routes/southcn/nfapp/column.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -62,7 +61,7 @@ async function handler(ctx) { shareUrl: item.shareUrl, })); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: columnName, diff --git a/lib/routes/southcn/nfapp/reporter.ts b/lib/routes/southcn/nfapp/reporter.ts index b035dca11..ddd674944 100644 --- a/lib/routes/southcn/nfapp/reporter.ts +++ b/lib/routes/southcn/nfapp/reporter.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -44,7 +43,7 @@ async function handler(ctx) { shareUrl: item.shareUrl, })); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: `南方+ - ${response.data.reportInfo.reporterName}`, diff --git a/lib/routes/southcn/nfapp/utils.ts b/lib/routes/southcn/nfapp/utils.ts index 3adfb3684..d505ea059 100644 --- a/lib/routes/southcn/nfapp/utils.ts +++ b/lib/routes/southcn/nfapp/utils.ts @@ -1,9 +1,10 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, diff --git a/lib/routes/tgbus/list.ts b/lib/routes/tgbus/list.ts index 572fe6658..2ba47a3d9 100644 --- a/lib/routes/tgbus/list.ts +++ b/lib/routes/tgbus/list.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -58,7 +57,7 @@ async function handler(ctx) { }; }); - const out = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const out = await Promise.all(list.map((item) => parseArticle(item))); return { title: `${categories[category]} - 电玩巴士`, diff --git a/lib/routes/tgbus/utils.ts b/lib/routes/tgbus/utils.ts index 3dbd608c9..bad693071 100644 --- a/lib/routes/tgbus/utils.ts +++ b/lib/routes/tgbus/utils.ts @@ -1,11 +1,12 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const { data: response } = await got(item.link); const $ = load(response as unknown as string); diff --git a/lib/routes/vocus/publication.ts b/lib/routes/vocus/publication.ts index 4723f1e9d..491dcf20a 100644 --- a/lib/routes/vocus/publication.ts +++ b/lib/routes/vocus/publication.ts @@ -57,7 +57,7 @@ async function handler(ctx) { const list = processList(articles); - const items = await ProcessFeed(list, cache.tryGet); + const items = await ProcessFeed(list); return { title: `${publicationData.title} - 文章列表|方格子 vocus`, diff --git a/lib/routes/vocus/user.ts b/lib/routes/vocus/user.ts index 517652483..b89b7b362 100644 --- a/lib/routes/vocus/user.ts +++ b/lib/routes/vocus/user.ts @@ -52,7 +52,7 @@ async function handler(ctx) { const list = processList(articles); - const items = await ProcessFeed(list, cache.tryGet); + const items = await ProcessFeed(list); return { title: `${userData.fullname}|方格子 vocus`, diff --git a/lib/routes/vocus/utils.ts b/lib/routes/vocus/utils.ts index ccf6eeca8..369c036dc 100644 --- a/lib/routes/vocus/utils.ts +++ b/lib/routes/vocus/utils.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -16,10 +17,10 @@ const processList = (articleData) => _id: item._id, })); -const ProcessFeed = (list, tryGet) => +const ProcessFeed = (list) => Promise.all( list.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const { data: { article }, } = await got(`${apiUrl}/api/article/${item._id}`, { diff --git a/lib/routes/whu/hyxt.ts b/lib/routes/whu/hyxt.ts index bf23c430e..bad55684e 100644 --- a/lib/routes/whu/hyxt.ts +++ b/lib/routes/whu/hyxt.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -40,7 +39,7 @@ async function handler(ctx) { }; }); - items = await processItems(items, cache.tryGet, rootUrl); + items = await processItems(items, rootUrl); const meta = processMeta(response); const siteName = getMeta(meta, 'SiteName'); diff --git a/lib/routes/whu/news.ts b/lib/routes/whu/news.ts index edc5ed48a..0a63882c2 100644 --- a/lib/routes/whu/news.ts +++ b/lib/routes/whu/news.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -78,7 +77,7 @@ async function handler(ctx) { }; }); - items = await processItems(items, cache.tryGet, rootUrl); + items = await processItems(items, rootUrl); const meta = processMeta(response); const siteName = getMeta(meta, 'SiteName'); diff --git a/lib/routes/whu/util.ts b/lib/routes/whu/util.ts index 568030a21..1e9c56de7 100644 --- a/lib/routes/whu/util.ts +++ b/lib/routes/whu/util.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -125,18 +126,17 @@ const getItemDetail = async (item, rootUrl) => { * Process items asynchronously. * * @param {Array} items - The array of items to process. - * @param {Function} tryGet - The function to attempt to get the content of a URL. * @param {string} rootUrl - The root URL. * @returns {Array>} An array of promises that resolve to the processed items. */ -const processItems = async (items, tryGet, rootUrl) => +const processItems = async (items, rootUrl) => await Promise.all( items.map((item) => { if (!item.link.includes(domain)) { return item; } - return tryGet(item.link, async () => await getItemDetail(item, rootUrl)); + return cache.tryGet(item.link, async () => await getItemDetail(item, rootUrl)); }) ); diff --git a/lib/routes/woshipm/popular.ts b/lib/routes/woshipm/popular.ts index d4d279bba..eaf882f7c 100644 --- a/lib/routes/woshipm/popular.ts +++ b/lib/routes/woshipm/popular.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -54,7 +53,7 @@ async function handler(ctx) { }; }); - const results = await Promise.allSettled(list.map((item) => parseArticle(item, cache.tryGet))); + const results = await Promise.allSettled(list.map((item) => parseArticle(item))); const result = results.filter((result) => result.status === 'fulfilled').map((result) => result.value); return { diff --git a/lib/routes/woshipm/user-article.ts b/lib/routes/woshipm/user-article.ts index 44b947fcb..9b8c37bc1 100644 --- a/lib/routes/woshipm/user-article.ts +++ b/lib/routes/woshipm/user-article.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -52,7 +51,7 @@ async function handler(ctx) { }; }); - const items = await Promise.all(list.map((item) => parseArticle(item, cache.tryGet))); + const items = await Promise.all(list.map((item) => parseArticle(item))); return { title: `${name}的文章-人人都是产品经理`, diff --git a/lib/routes/woshipm/utils.ts b/lib/routes/woshipm/utils.ts index c81cd549f..f556cbb5c 100644 --- a/lib/routes/woshipm/utils.ts +++ b/lib/routes/woshipm/utils.ts @@ -1,11 +1,12 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; const baseUrl = 'https://www.woshipm.com'; -const parseArticle = (item, tryGet) => - tryGet(item.link, async () => { +const parseArticle = (item) => + cache.tryGet(item.link, async () => { const { data: response } = await got(item.link); const $ = load(response); diff --git a/lib/routes/xinpianchang/index.ts b/lib/routes/xinpianchang/index.ts index 0b9ba52c2..90b8bc3d8 100644 --- a/lib/routes/xinpianchang/index.ts +++ b/lib/routes/xinpianchang/index.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getData, processItems, rootUrl } from './util'; @@ -32,11 +31,11 @@ async function handler(ctx) { const currentUrl = new URL(`discover/${params}`, rootUrl).href; - const { data, response } = await getData(currentUrl, cache.tryGet); + const { data, response } = await getData(currentUrl); let items = JSON.parse(response.match(/"list":(\[.*?\]),"total"/)[1]); - items = await processItems(items.slice(0, limit), cache.tryGet); + items = await processItems(items.slice(0, limit)); return { ...data, diff --git a/lib/routes/xinpianchang/rank.ts b/lib/routes/xinpianchang/rank.ts index ce00960d6..4fe115343 100644 --- a/lib/routes/xinpianchang/rank.ts +++ b/lib/routes/xinpianchang/rank.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { getData, processItems, rootUrl } from './util'; @@ -42,7 +41,7 @@ async function handler(ctx) { const currentUrl = current.web_link; const currentName = `${current.code}-${current.year}-${current.index}`; - const { data, response: currentResponse } = await getData(currentUrl, cache.tryGet); + const { data, response: currentResponse } = await getData(currentUrl); const buildId = currentResponse.match(/\/static\/(\w+)\/_buildManifest\.js/)[1]; @@ -52,7 +51,7 @@ async function handler(ctx) { let items = response.pageProps.rankList; - items = await processItems(items.slice(0, limit), cache.tryGet); + items = await processItems(items.slice(0, limit)); return { ...data, diff --git a/lib/routes/xinpianchang/util.tsx b/lib/routes/xinpianchang/util.tsx index b8bbdc407..b8cba4e2b 100644 --- a/lib/routes/xinpianchang/util.tsx +++ b/lib/routes/xinpianchang/util.tsx @@ -1,6 +1,7 @@ import { load } from 'cheerio'; import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; @@ -23,14 +24,13 @@ const renderDescription = (content, cover, enclousure) => ); /** - * Retrieves information from a given URL using a provided tryGet function. + * Retrieves information from a given URL. * * @param {string} url - The URL to fetch information from. - * @param {Function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise} - A Promise that resolves to an object containing the retrieved information. */ -const getData = (url, tryGet) => - tryGet(url, async () => { +const getData = (url) => + cache.tryGet(url, async () => { const { data: response } = await got(url); const $ = load(response); @@ -61,10 +61,9 @@ const getData = (url, tryGet) => * Process items asynchronously. * * @param {Array} items - The array of items to process. - * @param {function} tryGet - The tryGet function that handles the retrieval process. * @returns {Promise>} Returns a Promise that resolves to an array of processed items. */ -const processItems = async (items, tryGet) => { +const processItems = async (items) => { items = items.map((item) => ({ title: item.title, link: item.web_url, @@ -82,7 +81,7 @@ const processItems = async (items, tryGet) => { return await Promise.all( items.map((item) => - tryGet(item.guid, async () => { + cache.tryGet(item.guid, async () => { const apiUrl = new URL(`mod/api/v2/media/${item.enclosure_url}?appKey=${appKey}`, rootApiUrl).href; const { data: detailResponse } = await got(apiUrl); diff --git a/lib/routes/yicai/author.ts b/lib/routes/yicai/author.ts index f343bde5b..02cce240c 100644 --- a/lib/routes/yicai/author.ts +++ b/lib/routes/yicai/author.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ProcessItems, rootUrl } from './utils'; @@ -43,7 +42,7 @@ async function handler(ctx) { const $ = load(response.data); - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: `第一财经一财号 - ${$('title').text()}`, diff --git a/lib/routes/yicai/carousel.ts b/lib/routes/yicai/carousel.ts index 7fda11549..ccace05d0 100644 --- a/lib/routes/yicai/carousel.ts +++ b/lib/routes/yicai/carousel.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import ofetch from '@/utils/ofetch'; import { fetchFullArticles, rootUrl } from './utils'; @@ -37,8 +36,7 @@ async function handler() { fetchFullArticles( $('#breaknews a') .toArray() - .map((e) => ({ link: new URL($(e).attr('href'), rootUrl).href, title: $(e).text() })), - cache.tryGet + .map((e) => ({ link: new URL($(e).attr('href'), rootUrl).href, title: $(e).text() })) ) ); diff --git a/lib/routes/yicai/feed.ts b/lib/routes/yicai/feed.ts index fcbe10a65..a6d54a7f6 100644 --- a/lib/routes/yicai/feed.ts +++ b/lib/routes/yicai/feed.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ProcessItems, rootUrl } from './utils'; @@ -46,7 +45,7 @@ async function handler(ctx) { const $ = load(response.data); - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: `第一财经主题 - ${$('title').text()}`, diff --git a/lib/routes/yicai/headline.ts b/lib/routes/yicai/headline.ts index 69b83ab14..81db03af5 100644 --- a/lib/routes/yicai/headline.ts +++ b/lib/routes/yicai/headline.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { ProcessItems, rootUrl } from './utils'; @@ -30,7 +29,7 @@ export const route: Route = { async function handler(ctx) { const apiUrl = `${rootUrl}/api/ajax/getlistbycid?cid=48&type=1&page=1&pagesize=${ctx.req.query('limit') ?? 30}`; - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: '第一财经 - 头条', diff --git a/lib/routes/yicai/latest.ts b/lib/routes/yicai/latest.ts index 5f81654b4..e1ade4f0d 100644 --- a/lib/routes/yicai/latest.ts +++ b/lib/routes/yicai/latest.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { ProcessItems, rootUrl } from './utils'; @@ -30,7 +29,7 @@ export const route: Route = { async function handler(ctx) { const apiUrl = `${rootUrl}/api/ajax/getlatest?page=1&pagesize=${ctx.req.query('limit') ?? 30}`; - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: '第一财经 - 最新', diff --git a/lib/routes/yicai/news.ts b/lib/routes/yicai/news.ts index 66fe0163f..b560b52cc 100644 --- a/lib/routes/yicai/news.ts +++ b/lib/routes/yicai/news.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ProcessItems, rootUrl } from './utils'; @@ -84,7 +83,7 @@ async function handler(ctx) { const currentUrl = `${rootUrl}/news${id ? `/${channel.slug}` : ''}`; const apiUrl = `${rootUrl}/api/ajax/${id ? `getlistbycid?cid=${channel.id}` : 'getjuhelist?action=news'}&page=1&pagesize=${ctx.req.query('limit') ?? 30}`; - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: `第一财经 - ${channel?.name ?? '新闻'}`, diff --git a/lib/routes/yicai/utils.ts b/lib/routes/yicai/utils.ts index 22ba0604d..c33c97535 100644 --- a/lib/routes/yicai/utils.ts +++ b/lib/routes/yicai/utils.ts @@ -1,5 +1,6 @@ import { load } from 'cheerio'; +import cache from '@/utils/cache'; import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -8,7 +9,7 @@ import { renderDescription } from './templates/description'; const rootUrl = 'https://www.yicai.com'; -const ProcessItems = async (apiUrl, tryGet) => { +const ProcessItems = async (apiUrl) => { const response = await got({ method: 'get', url: apiUrl, @@ -33,11 +34,11 @@ const ProcessItems = async (apiUrl, tryGet) => { }), })); - return Promise.all(fetchFullArticles(items, tryGet)); + return Promise.all(fetchFullArticles(items)); }; -function fetchFullArticles(items, tryGet) { +function fetchFullArticles(items) { return items.map((item) => - tryGet(item.link, async () => { + cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, diff --git a/lib/routes/yicai/video.ts b/lib/routes/yicai/video.ts index 9b28757ed..9b2119875 100644 --- a/lib/routes/yicai/video.ts +++ b/lib/routes/yicai/video.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ProcessItems, rootUrl } from './utils'; @@ -94,7 +93,7 @@ async function handler(ctx) { const currentUrl = `${rootUrl}/video${id ? `/${channel.slug}` : ''}`; const apiUrl = `${rootUrl}/api/ajax/${id ? `getlistbycid?cid=${channel.id}` : 'getjuhelist?action=video'}&page=1&pagesize=${ctx.req.query('limit') ?? 30}`; - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: `第一财经 - ${channel?.name ?? '视听'}`, diff --git a/lib/routes/yicai/vip.ts b/lib/routes/yicai/vip.ts index 94eecd79c..08c4d442f 100644 --- a/lib/routes/yicai/vip.ts +++ b/lib/routes/yicai/vip.ts @@ -1,7 +1,6 @@ import { load } from 'cheerio'; import type { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { ProcessItems, rootUrl } from './utils'; @@ -43,7 +42,7 @@ async function handler(ctx) { const $ = load(response.data); - const items = await ProcessItems(apiUrl, cache.tryGet); + const items = await ProcessItems(apiUrl); return { title: `第一财经VIP频道 - ${$('title').text()}`, diff --git a/lib/routes/zuvio/boards.ts b/lib/routes/zuvio/boards.ts index 3e429593b..d059dc7e1 100644 --- a/lib/routes/zuvio/boards.ts +++ b/lib/routes/zuvio/boards.ts @@ -1,5 +1,4 @@ import type { Route } from '@/types'; -import cache from '@/utils/cache'; import { getBoards, rootUrl } from './utils'; @@ -22,7 +21,7 @@ export const route: Route = { }; async function handler() { - const items = await getBoards(cache.tryGet); + const items = await getBoards(); return { title: 'Zuvio 校園話題列表 - 大學生論壇', diff --git a/lib/routes/zuvio/student5.ts b/lib/routes/zuvio/student5.ts index 971239877..adfad98d1 100644 --- a/lib/routes/zuvio/student5.ts +++ b/lib/routes/zuvio/student5.ts @@ -26,7 +26,7 @@ export const route: Route = { async function handler(ctx) { const { board = '' } = ctx.req.param(); - const title = board ? (await getBoards(cache.tryGet)).find((i) => i.boardId === board).title : '全部'; + const title = board ? (await getBoards()).find((i) => i.boardId === board).title : '全部'; const { data } = await got(`${apiUrl}/article`, { searchParams: { diff --git a/lib/routes/zuvio/utils.tsx b/lib/routes/zuvio/utils.tsx index e41e700c3..b9ce94c65 100644 --- a/lib/routes/zuvio/utils.tsx +++ b/lib/routes/zuvio/utils.tsx @@ -1,5 +1,6 @@ import { renderToString } from 'hono/jsx/dom/server'; +import cache from '@/utils/cache'; import got from '@/utils/got'; const token = @@ -47,8 +48,8 @@ const renderSections = (sections) => { return output; }; -const getBoards = (tryGet) => - tryGet('zuvio:boards', async () => { +const getBoards = () => + cache.tryGet('zuvio:boards', async () => { const { data } = await got(`${apiUrl}/board`, { searchParams: { api_token: token,