Skip to content

Commit

Permalink
fix(route): IT之家专题 (#15446)
Browse files Browse the repository at this point in the history
* fix(route): IT之家专题

* docs: add link

* Update lib/routes/ithome/zt.ts

---------
  • Loading branch information
nczitzk authored May 2, 2024
1 parent 5c687c7 commit aecdcd9
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 56 deletions.
13 changes: 13 additions & 0 deletions lib/routes/ithome/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}
174 changes: 118 additions & 56 deletions lib/routes/ithome/zt.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,150 @@
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 route: Route = {
path: '/zt/:id',
categories: ['new-media'],
example: '/ithome/zt/xijiayi',
parameters: { id: '专题 id' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['ithome.com/zt/:id'],
},
],
name: '专题',
maintainers: ['nczitzk'],
handler,
description: `所有专题请见[此处](https://www.ithome.com/zt)`,
};

async function handler(ctx) {
const id = ctx.req.param('id');
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 = `${rootUrl}/zt/${id}`;
const currentUrl = new URL(`zt/${id}`, rootUrl).href;

const { data: response } = await got(currentUrl);

const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response);

const $ = load(response.data);
const author = 'IT之家';
const language = 'zh';

const list = $('.newsbody a')
.map((_, item) => {
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: item.text(),
link: item.attr('href'),
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,
};
})
.get();
});

const items = await Promise.all(
list.map((item) =>
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const { data: detailResponse } = await got(item.link);

const $$ = load(detailResponse);

$$('p.ad-tips, a.topic-bar').remove();

const content = load(detailResponse.data);
const post = content('.post_content');
$$('div#paragraph p img').each((_, el) => {
el = $$(el);

post.find('img[data-original]').each((_, ele) => {
ele = $(ele);
ele.attr('src', ele.attr('data-original'));
ele.removeAttr('class');
ele.removeAttr('data-original');
const src = el.prop('data-original');

if (src) {
el.replaceWith(
art(path.join(__dirname, 'templates/description.art'), {
images: [
{
src,
alt: el.prop('alt'),
},
],
})
);
}
});

item.description = post.html();
item.author = content('#author_baidu').text().replace('作者:', '');
item.pubDate = timezone(parseDate(content('#pubtime_baidu').text()), +8);
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: `${$('title').text()} - IT之家`,
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?',
name: '专题',
url: 'ithome.com',
maintainers: ['nczitzk'],
handler,
example: '/ithome/zt/xijiayi',
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,
},
radar: [
{
source: ['ithome.com/zt/:id'],
target: '/zt/:id',
},
],
};

0 comments on commit aecdcd9

Please sign in to comment.