From 7d6bde4e2c9064f1533c5f147fea5cac8a42416a Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 18 Mar 2024 02:29:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E8=BF=BD=E6=96=B0=E7=95=AA?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E6=9B=B4=E6=96=B0=20(#14751)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(route): 追新番最近更新 * update lib/routes/fanxinzhui/index.ts Co-authored-by: Tony * update lib/routes/fanxinzhui/index.ts Co-authored-by: Tony --------- --- lib/router.js | 4 +- lib/routes-deprecated/fanxinzhui/latest.js | 52 -------- lib/routes/fanxinzhui/index.ts | 125 ++++++++++++++++++ lib/routes/fanxinzhui/namespace.ts | 6 + .../fanxinzhui/templates/description.art | 13 ++ 5 files changed, 146 insertions(+), 54 deletions(-) delete mode 100644 lib/routes-deprecated/fanxinzhui/latest.js create mode 100644 lib/routes/fanxinzhui/index.ts create mode 100644 lib/routes/fanxinzhui/namespace.ts create mode 100644 lib/routes/fanxinzhui/templates/description.art diff --git a/lib/router.js b/lib/router.js index 955ae25df36917..8bd1566d0e74a8 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1121,8 +1121,8 @@ router.get('/etoland/:bo_table', lazyloadRouteHandler('./routes/etoland/board')) router.get('/lntu/jwnews', lazyloadRouteHandler('./routes/universities/lntu/jwnews')); // 追新番 -router.get('/fanxinzhui', lazyloadRouteHandler('./routes/fanxinzhui/latest')); -router.get('/zhuixinfan/list', lazyloadRouteHandler('./routes/fanxinzhui/latest')); +// router.get('/fanxinzhui', lazyloadRouteHandler('./routes/fanxinzhui/latest')); +// router.get('/zhuixinfan/list', lazyloadRouteHandler('./routes/fanxinzhui/latest')); // blur-studio router.get('/blur-studio', lazyloadRouteHandler('./routes/blur-studio/index')); diff --git a/lib/routes-deprecated/fanxinzhui/latest.js b/lib/routes-deprecated/fanxinzhui/latest.js deleted file mode 100644 index 668b12bbfd488c..00000000000000 --- a/lib/routes-deprecated/fanxinzhui/latest.js +++ /dev/null @@ -1,52 +0,0 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); -const { parseRelativeDate } = require('@/utils/parse-date'); - -module.exports = async (ctx) => { - const rootUrl = 'http://www.fanxinzhui.com'; - const currentUrl = `${rootUrl}/lastest`; - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const $ = cheerio.load(response.data); - - const list = $('.la') - .slice(0, 10) - .map((_, item) => { - item = $(item); - const pubDate = item.find('.time'); - - pubDate.remove(); - - return { - title: item.text(), - pubDate: parseRelativeDate(pubDate.text()), - link: `${rootUrl}${item.attr('href')}`, - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - const content = cheerio.load(detailResponse.data); - - item.description = content('.middle_box').html(); - - return item; - }) - ) - ); - - ctx.state.data = { - title: '最近更新 - 追新番', - link: currentUrl, - item: items, - }; -}; diff --git a/lib/routes/fanxinzhui/index.ts b/lib/routes/fanxinzhui/index.ts new file mode 100644 index 00000000000000..e34aecae1a65c6 --- /dev/null +++ b/lib/routes/fanxinzhui/index.ts @@ -0,0 +1,125 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import timezone from '@/utils/timezone'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; +import * as path from 'node:path'; + +export const route: Route = { + path: '/', + name: '最近更新', + url: 'fanxinzhui.com/lastest', + maintainers: ['nczitzk'], + handler, + example: '/fanxinzhui', + categories: ['multimedia'], + + radar: [{ + source: ['fanxinzhui.com/lastest'], + target: '/', + }], +}; + +async function handler(ctx) { + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 30; + + const rootUrl = 'https://www.fanxinzhui.com'; + const currentUrl = new URL('lastest', rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = load(response); + + let items = $('a.la') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const season = item.find('span.season').text(); + const name = item.find('span.name').text(); + const link = new URL(item.prop('href'), rootUrl).href; + + return { + title: `${season} ${name}`, + link, + guid: `${link}#${season}`, + pubDate: timezone(parseDate(item.find('span.time').text()), +8), + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const content = load(detailResponse); + + item.author = undefined; + item.category = []; + + content('div.info ul li').each((_, el) => { + el = content(el); + + const key = el.find('span').text().split(/:/)[0]; + const value = el.contents().last().text().trim(); + + if (key === '类型') { + item.category = [...item.category, ...value.split(/\//)]; + } else if (key === '首播日期') { + return; + } else { + item.author = `${item.author ? `${item.author}/` : ''}${value}`; + item.category = [...item.category, ...value.split(/\//)].filter((c) => c !== '等'); + } + }); + + content('div.image').each((_, el) => { + el = content(el); + + const image = el.find('img').prop('src'); + + el.replaceWith( + art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image.replace(/@\d+,\d+\.\w+$/, ''), + alt: content('div.resource_title h2').text(), + }, + ] + : undefined, + }) + ); + }); + + content('a.password').each((_, el) => { + el = content(el); + + el.replaceWith(el.text()); + }); + + item.description = content('div.middle_box').html(); + item.enclosure_url = content('p.way span a').prop('href'); + + return item; + }) + ) + ); + + const title = $('title').text(); + const image = new URL($('img.logo').prop('src'), rootUrl).href; + + return { + item: items, + title, + link: currentUrl, + description: $('meta[name="description"]').prop('content'), + language: 'zh', + image, + author: title.split(/_/).pop(), + allowEmpty: true, + }; +} diff --git a/lib/routes/fanxinzhui/namespace.ts b/lib/routes/fanxinzhui/namespace.ts new file mode 100644 index 00000000000000..21e195134e39b1 --- /dev/null +++ b/lib/routes/fanxinzhui/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '追新番', + url: 'fanxinzhui.com', +}; diff --git a/lib/routes/fanxinzhui/templates/description.art b/lib/routes/fanxinzhui/templates/description.art new file mode 100644 index 00000000000000..0a7f83a6f60fb1 --- /dev/null +++ b/lib/routes/fanxinzhui/templates/description.art @@ -0,0 +1,13 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} \ No newline at end of file