diff --git a/lib/router.js b/lib/router.js index d97f9b49a..405d9939d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -294,7 +294,7 @@ router.get('/fzu_min/:type', lazyloadRouteHandler('./routes/universities/fzu/new router.get('/xmu/aero/:type', lazyloadRouteHandler('./routes/universities/xmu/aero')); // ifanr -router.get('/ifanr/:channel?', lazyloadRouteHandler('./routes/ifanr/index')); +// router.get('/ifanr/:channel?', lazyloadRouteHandler('./routes/ifanr/index')); // IPSW.me // router.get('/ipsw/index/:ptype/:pname', lazyloadRouteHandler('./routes/ipsw/index')); diff --git a/lib/routes-deprecated/ifanr/index.js b/lib/routes-deprecated/ifanr/index.js deleted file mode 100644 index 0e4723931..000000000 --- a/lib/routes-deprecated/ifanr/index.js +++ /dev/null @@ -1,71 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); - -module.exports = async (ctx) => { - let host = 'https://www.ifanr.com'; - - let channel; - if (ctx.params.channel) { - channel = ctx.params.channel.toLowerCase(); - channel = channel.split('-').join('/'); - - // 兼容旧版路由 - host = channel === 'appso' ? `${host}/app` : `${host}/${channel}`; - } else { - host = `${host}/app`; - channel = 'app'; - } - - const response = await got.get(host); - - const $ = cheerio.load(response.data); - - let list; - switch (channel) { - case 'dasheng': - list = $('#articles-collection') - .find('.c-bricks__brick a.c-dasheng') - .map((i, e) => $(e).attr('href')) - .get(); - break; - default: - list = $('#articles-collection') - .find('.article-item .article-info h3 a') - .map((i, e) => $(e).attr('href')) - .get(); - break; - } - - const out = await Promise.all( - list.map(async (itemUrl) => { - const cache = await ctx.cache.get(itemUrl); - if (cache) { - return JSON.parse(cache); - } - - const response = await got.get(itemUrl); - const $ = cheerio.load(response.data); - - const item = { - title: $('.c-single-normal__title').text(), - link: itemUrl, - author: $('.c-card-author__name').text(), - description: $('article').html(), - pubDate: new Date($('.c-article-header-meta__time').attr('data-timestamp') * 1000), - }; - // "大声" 标题 - if (/^https?:\/\/www\.ifanr\.com\/dasheng\/.*$/.test(itemUrl)) { - item.title = $('.c-dasheng_header__title').text(); - } - - ctx.cache.set(itemUrl, JSON.stringify(item)); - return item; - }) - ); - - ctx.state.data = { - title: `iFanr - ${$('h1.c-archive-header__title').text()}:${$('div.c-archive-header__desc').text()}`, - link: host, - item: out, - }; -}; diff --git a/lib/routes/ifanr/digest.ts b/lib/routes/ifanr/digest.ts new file mode 100644 index 000000000..2a1cba8ac --- /dev/null +++ b/lib/routes/ifanr/digest.ts @@ -0,0 +1,104 @@ +import { type CheerioAPI, load } from 'cheerio'; +import { type Context } from 'hono'; + +import { type DataItem, type Route, type Data, ViewType } from '@/types'; + +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +export const handler = async (ctx: Context): Promise => { + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '20', 10); + + const baseUrl: string = 'https://www.ifanr.com'; + const apiBaseUrl: string = 'https://sso.ifanr.com'; + const targetUrl: string = new URL('digest', baseUrl).href; + const apiUrl: string = new URL('api/v5/wp/buzz', apiBaseUrl).href; + + const response = await ofetch(apiUrl, { + query: { + limit, + offset: 0, + }, + }); + + const targetResponse = await ofetch(targetUrl); + const $: CheerioAPI = load(targetResponse); + const language: string = $('html').attr('lang') ?? 'zh-CN'; + + const items: DataItem[] = response.objects.slice(0, limit).map((item): DataItem => { + const title: string = item.post_title; + const description: string = item.post_content; + const pubDate: number | string = item.created_at; + const linkUrl: string = `digest/${item.post_id}`; + const guid: string = `ifanr-digest-${item.post_id}`; + const updated: number | string = item.updated_at ?? pubDate; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDate ? parseDate(pubDate, 'X') : undefined, + link: new URL(linkUrl, baseUrl).href, + guid, + id: guid, + content: { + html: description, + text: item.post_content ?? description, + }, + updated: updated ? parseDate(updated) : undefined, + language, + _extra: { + links: [ + { + url: item.buzz_original_url, + type: 'via', + content_html: item.post_content, + }, + ], + }, + }; + + return processedItem; + }); + + const title: string = $('title').text(); + + return { + title, + description: title, + link: targetUrl, + item: items, + allowEmpty: true, + image: $('img.c-header-navbar__logo').attr('src'), + author: $('meta[property="og:site_name"]').attr('content'), + language, + id: $('meta[property="og:url"]').attr('content'), + }; +}; + +export const route: Route = { + path: '/digest', + name: '快讯', + url: 'www.ifanr.com', + maintainers: ['nczitzk'], + handler, + example: '/ifanr/digest', + parameters: undefined, + description: undefined, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.ifanr.comdigest'], + target: '/digest', + }, + ], + view: ViewType.Articles, +};