diff --git a/lib/routes/ithome/templates/description.art b/lib/routes/ithome/templates/description.art new file mode 100644 index 000000000..0a7f83a6f --- /dev/null +++ b/lib/routes/ithome/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 diff --git a/lib/routes/ithome/zt.ts b/lib/routes/ithome/zt.ts index a33e5270c..4e7a251dd 100644 --- a/lib/routes/ithome/zt.ts +++ b/lib/routes/ithome/zt.ts @@ -1,19 +1,142 @@ import { Route } from '@/types'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + 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 path from 'node:path'; + +export const handler = async (ctx) => { + const { id = 'xijiayi' } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 50; + + const rootUrl = 'https://www.ithome.com'; + const currentUrl = new URL(`zt/${id}`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = load(response); + + const author = 'IT之家'; + const language = 'zh'; + + let items = $('div.newsbody') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const title = item.find('h2').text(); + const image = item.find('img').prop('data-original') ?? item.find('img').prop('src'); + + return { + title, + pubDate: timezone( + parseDate( + item + .find('span.time script') + .text() + .match(/'(.*?)'/) + ), + +8 + ), + link: item.find('a').first().prop('href'), + author: item.find('div.editor').contents().first().text(), + image, + banner: image, + language, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + $$('p.ad-tips, a.topic-bar').remove(); + + $$('div#paragraph p img').each((_, el) => { + el = $$(el); + + const src = el.prop('data-original'); + + if (src) { + el.replaceWith( + art(path.join(__dirname, 'templates/description.art'), { + images: [ + { + src, + alt: el.prop('alt'), + }, + ], + }) + ); + } + }); + + const title = $$('h1').text(); + const description = $$('div#paragraph').html(); + const image = $$('div#paragraph img').first().prop('src'); + + item.title = title; + item.description = description; + item.pubDate = timezone(parseDate($$('span#pubtime_baidu').text()), +8); + item.category = $$('div.cv a') + .toArray() + .map((c) => $$(c).text()) + .slice(1); + item.author = $$('span#author_baidu').contents().last().text() || $$('span#source_baidu').contents().last().text() || $$('span#editor_baidu').contents().last().text(); + item.content = { + html: description, + text: $$('div#paragraph').text(), + }; + item.image = image; + item.banner = image; + item.language = language; + + return item; + }) + ) + ); + + const image = new URL($('meta[property="og:image"]').prop('content'), rootUrl).href; + + return { + title: `${author} - ${$('title').text()}`, + description: $('meta[name="description"]').prop('content'), + link: currentUrl, + item: items, + allowEmpty: true, + image, + author, + language, + }; +}; export const route: Route = { - path: '/zt/:id', - categories: ['new-media'], + path: '/zt/:id?', + name: '专题', + url: 'ithome.com', + maintainers: ['nczitzk'], + handler, example: '/ithome/zt/xijiayi', - parameters: { id: '专题 id' }, + parameters: { category: '专题 id,默认为 xijiayi,即 [喜加一](https://www.ithome.com/zt/xijiayi),可在对应专题页 URL 中找到' }, + description: `:::tip + 更多专题请见 [IT之家专题](https://www.ithome.com/zt) + :::`, + categories: ['new-media'], + features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, + supportRadar: true, supportBT: false, supportPodcast: false, supportScihub: false, @@ -21,68 +144,7 @@ export const route: Route = { radar: [ { source: ['ithome.com/zt/:id'], + target: '/zt/:id', }, ], - name: '专题', - maintainers: ['nczitzk'], - handler, - description: `所有专题请见[此处](https://www.ithome.com/zt)`, }; - -async function handler(ctx) { - const id = ctx.req.param('id'); - - const rootUrl = 'https://www.ithome.com'; - const currentUrl = `${rootUrl}/zt/${id}`; - - const response = await got({ - method: 'get', - url: currentUrl, - }); - - const $ = load(response.data); - - const list = $('.newsbody a') - .map((_, item) => { - item = $(item); - - return { - title: item.text(), - link: item.attr('href'), - }; - }) - .get(); - - const items = await Promise.all( - list.map((item) => - cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - - const content = load(detailResponse.data); - const post = content('.post_content'); - - post.find('img[data-original]').each((_, ele) => { - ele = $(ele); - ele.attr('src', ele.attr('data-original')); - ele.removeAttr('class'); - ele.removeAttr('data-original'); - }); - - item.description = post.html(); - item.author = content('#author_baidu').text().replace('作者:', ''); - item.pubDate = timezone(parseDate(content('#pubtime_baidu').text()), +8); - - return item; - }) - ) - ); - - return { - title: `${$('title').text()} - IT之家`, - link: currentUrl, - item: items, - }; -}