fix(route): 知乎时间线 (#9485) (#15063)

* fix(route): 知乎时间线 (#9485)

* fix(知乎): use query parameter limit with default 15
This commit is contained in:
Ernest Dong
2024-04-03 12:05:13 +08:00
committed by GitHub
parent b93dfabe1a
commit 71fa40962c
2 changed files with 17 additions and 9 deletions
+11 -7
View File
@@ -30,18 +30,21 @@ export const route: Route = {
:::`,
};
async function handler() {
async function handler(ctx) {
const cookie = config.zhihu.cookies;
if (cookie === undefined) {
throw new Error('缺少知乎用户登录后的 Cookie 值');
}
const response = await got({
method: 'get',
url: `https://www.zhihu.com/api/v3/moments?desktop=true`,
url: `https://www.zhihu.com/api/v3/moments`,
headers: {
Cookie: cookie,
},
searchParams: {
desktop: true,
limit: ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 15,
},
});
const feeds = response.data.data;
@@ -99,18 +102,19 @@ async function handler() {
if (!e || !e.target) {
return {};
}
const link = buildLink(e);
return {
title: `${e.action_text_tpl.replace('{}', buildActors(e))}: ${getOne([e.target.title, e.target.question ? e.target.question.title : ''])}`,
description: utils.ProcessImage(`<div>${getOne([e.target.content_html, getContent(e.target.content), e.target.detail, e.target.excerpt, ''])}</div>`),
pubDate: parseDate(e.updated_time * 1000),
link: buildLink(e),
link,
author: e.target.author ? e.target.author.name : '',
guid: this.link,
guid: link,
};
};
const out = feeds
.filter((e) => e && e.type && e.type !== 'feed_advert')
.filter((e) => e.verb && e.verb !== 'MEMBER_VOTEUP_ARTICLE' && e.verb !== 'MEMBER_VOTEUP_ANSWER')
.map((e) => {
if (e && e.type && e.type === 'feed_group') {
// A feed group contains a list of feeds whose structure is the same as a single feed
@@ -123,7 +127,7 @@ async function handler() {
.join('')
: '';
const pubDate = e.list && Array.isArray(e.list) && e.list.length > 0 ? parseDate(e.list[0].updated_time * 1000) : new Date();
const guid = `${title} ${pubDate}`;
const guid = e.link;
return {
title,
description,
+6 -2
View File
@@ -15,8 +15,12 @@ export default {
const href = $(elem).attr('href');
if (href?.startsWith('https://link.zhihu.com/?target=')) {
const url = new URL(href);
const target = url.searchParams.get('target');
$(elem).attr('href', decodeURIComponent(target));
const target = url.searchParams.get('target') || '';
try {
$(elem).attr('href', decodeURIComponent(target));
} catch {
// sometimes the target is not a valid url
}
}
});