Skip to content

Commit

Permalink
fix(route): 知乎时间线 (#9485) (#15063)
Browse files Browse the repository at this point in the history
* fix(route): 知乎时间线 (#9485)

* fix(知乎): use query parameter limit with default 15
  • Loading branch information
ErnestDong committed Apr 3, 2024
1 parent b93dfab commit 71fa409
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 11 additions & 7 deletions lib/routes/zhihu/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions lib/routes/zhihu/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
});

Expand Down

0 comments on commit 71fa409

Please sign in to comment.