Skip to content

Commit 38ec0d4

Browse files
authored
fix(route): juejin post (#18127)
* fix(route): juejin * refactor: split metadata and article parsing * fix: clean up * fix: more cleanup
1 parent ddd2dbb commit 38ec0d4

File tree

12 files changed

+490
-190
lines changed

12 files changed

+490
-190
lines changed

lib/routes/juejin/books.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Route } from '@/types';
2-
import got from '@/utils/got';
2+
import ofetch from '@/utils/ofetch';
33
import { parseDate } from '@/utils/parse-date';
44

55
export const route: Route = {
@@ -28,14 +28,12 @@ export const route: Route = {
2828
};
2929

3030
async function handler() {
31-
const response = await got({
32-
method: 'post',
33-
url: 'https://api.juejin.cn/booklet_api/v1/booklet/listbycategory',
34-
json: { category_id: '0', cursor: '0', limit: 20 },
31+
const response = await ofetch('https://api.juejin.cn/booklet_api/v1/booklet/listbycategory', {
32+
method: 'POST',
33+
body: { category_id: '0', cursor: '0', limit: 20 },
3534
});
3635

37-
const { data } = response.data;
38-
const items = data.map(({ base_info }) => ({
36+
const items = response.data.map(({ base_info }) => ({
3937
title: base_info.title,
4038
link: `https://juejin.cn/book/${base_info.booklet_id}`,
4139
description: `

lib/routes/juejin/category.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import util from './utils';
2+
import ofetch from '@/utils/ofetch';
3+
import { getCategoryBrief, parseList, ProcessFeed } from './utils';
54

65
export const route: Route = {
76
path: '/category/:category',
@@ -16,29 +15,33 @@ export const route: Route = {
1615
supportPodcast: false,
1716
supportScihub: false,
1817
},
18+
radar: [
19+
{
20+
source: ['juejin.cn/:category'],
21+
},
22+
],
1923
name: '分类',
2024
maintainers: ['DIYgod'],
2125
handler,
2226
description: `| 后端 | 前端 | Android | iOS | 人工智能 | 开发工具 | 代码人生 | 阅读 |
23-
| ------- | -------- | ------- | --- | -------- | -------- | -------- | ------- |
24-
| backend | frontend | android | ios | ai | freebie | career | article |`,
27+
| ------- | -------- | ------- | --- | -------- | -------- | -------- | ------- |
28+
| backend | frontend | android | ios | ai | freebie | career | article |`,
2529
};
2630

2731
async function handler(ctx) {
2832
const category = ctx.req.param('category');
2933

30-
const idResponse = await got({
31-
method: 'get',
32-
url: 'https://api.juejin.cn/tag_api/v1/query_category_briefs?show_type=0',
33-
});
34+
const idResponse = await getCategoryBrief();
3435

35-
const cat = idResponse.data.data.find((item) => item.category_url === category);
36+
const cat = idResponse.find((item) => item.category_url === category);
37+
if (!cat) {
38+
throw new Error('分类不存在');
39+
}
3640
const id = cat.category_id;
3741

38-
const response = await got({
39-
method: 'post',
40-
url: 'https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed',
41-
json: {
42+
const response = await ofetch('https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed', {
43+
method: 'POST',
44+
body: {
4245
id_type: 2,
4346
sort_type: 300,
4447
cate_id: id,
@@ -47,11 +50,8 @@ async function handler(ctx) {
4750
},
4851
});
4952

50-
let originalData = [];
51-
if (response.data.data) {
52-
originalData = response.data.data;
53-
}
54-
const resultItems = await util.ProcessFeed(originalData, cache);
53+
const list = parseList(response.data);
54+
const resultItems = await ProcessFeed(list);
5555

5656
return {
5757
title: `掘金 ${cat.category_name}`,

lib/routes/juejin/collection.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import util from './utils';
2+
import { getCollection, parseList, ProcessFeed } from './utils';
53

64
export const route: Route = {
75
path: '/collection/:collectionId',
@@ -22,27 +20,21 @@ export const route: Route = {
2220
},
2321
],
2422
name: '单个收藏夹',
25-
maintainers: ['isQ'],
23+
maintainers: ['yang131323'],
2624
handler,
2725
};
2826

2927
async function handler(ctx) {
3028
const collectionId = ctx.req.param('collectionId');
3129

32-
const collectPage = await got({
33-
method: 'get',
34-
url: `https://api.juejin.cn/interact_api/v1/collectionSet/get?tag_id=${collectionId}&cursor=0`,
35-
});
30+
const collectPage = await getCollection(collectionId);
3631

37-
let items = [];
38-
if (collectPage.data.data && collectPage.data.data.article_list) {
39-
items = collectPage.data.data.article_list.slice(0, 10);
40-
}
32+
const items = parseList(collectPage.article_list);
4133

42-
const result = await util.ProcessFeed(items, cache);
34+
const result = await ProcessFeed(items);
4335

4436
return {
45-
title: '掘金 - 单个收藏夹',
37+
title: `${collectPage.detail.tag_name} - ${collectPage.create_user.user_name}的收藏集 - 掘金`,
4638
link: `https://juejin.cn/collection/${collectionId}`,
4739
description: '掘金,用户单个收藏夹',
4840
item: result,

lib/routes/juejin/favorites.ts renamed to lib/routes/juejin/collections.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import util from './utils';
2+
import ofetch from '@/utils/ofetch';
3+
import { getCollection, parseList, ProcessFeed } from './utils';
4+
import { Article } from './types';
55

66
export const route: Route = {
77
path: '/collections/:userId',
@@ -23,37 +23,29 @@ export const route: Route = {
2323
},
2424
],
2525
name: '收藏集',
26-
maintainers: ['isQ'],
26+
maintainers: ['yang131323'],
2727
handler,
2828
};
2929

30+
// 获取所有收藏夹文章内容
31+
async function getArticleList(collectionId) {
32+
const collectPage = await getCollection(collectionId);
33+
34+
return collectPage.article_list;
35+
}
36+
3037
async function handler(ctx) {
3138
const userId = ctx.req.param('userId');
32-
const response = await got({
33-
method: 'get',
34-
url: `https://api.juejin.cn/interact_api/v1/collectionSet/list?user_id=${userId}&cursor=0&limit=20`,
35-
});
39+
const response = await ofetch(`https://api.juejin.cn/interact_api/v1/collectionSet/list?user_id=${userId}&cursor=0&limit=20`);
3640

3741
// 获取用户所有收藏夹id
38-
const collectionId = response.data.data.map((item) => item.tag_id);
39-
40-
// 获取所有收藏夹文章内容
41-
async function getPostId(item) {
42-
const collectPage = await got({
43-
method: 'get',
44-
url: `https://api.juejin.cn/interact_api/v1/collectionSet/get?tag_id=${item}&cursor=0`,
45-
});
46-
47-
return (Array.isArray(collectPage.data.data.article_list) && collectPage.data.data.article_list.slice(0, 10)) || [];
48-
}
42+
const collectionId = response.data.map((item) => item.tag_id);
4943

50-
const temp = await Promise.all(collectionId.map((element) => getPostId(element)));
51-
const posts = [];
52-
for (const item of temp) {
53-
posts.push(...item);
54-
}
44+
const temp = (await Promise.all(collectionId.map((id) => getArticleList(id)))) as Article[][];
45+
const posts = temp.flat();
46+
const list = parseList(posts);
5547

56-
const result = await util.ProcessFeed(posts, cache);
48+
const result = await ProcessFeed(list);
5749

5850
return {
5951
title: '掘金 - 收藏集',

lib/routes/juejin/column.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import util from './utils';
2+
import ofetch from '@/utils/ofetch';
3+
import { parseList, ProcessFeed } from './utils';
54

65
export const route: Route = {
76
path: '/column/:id',
@@ -28,29 +27,25 @@ export const route: Route = {
2827

2928
async function handler(ctx) {
3029
const id = ctx.req.param('id');
31-
const detail = await got({
32-
method: 'get',
33-
url: `https://api.juejin.cn/content_api/v1/column/detail?column_id=${id}`,
34-
});
35-
const response = await got({
36-
method: 'post',
37-
url: 'https://api.juejin.cn/content_api/v1/column/articles_cursor',
38-
json: {
30+
const columnDetail = await ofetch(`https://api.juejin.cn/content_api/v1/column/detail?column_id=${id}`);
31+
const response = await ofetch('https://api.juejin.cn/content_api/v1/column/articles_cursor', {
32+
method: 'POST',
33+
body: {
3934
column_id: id,
40-
limit: 20,
4135
cursor: '0',
36+
limit: 20,
4237
sort: 0,
4338
},
4439
});
45-
const { data } = response.data;
46-
const detailData = detail.data.data;
47-
const columnName = detailData && detailData.column_version && detailData.column_version.title;
48-
const resultItems = await util.ProcessFeed(data, cache);
40+
const detailData = columnDetail.data;
41+
const list = parseList(response.data);
42+
const resultItems = await ProcessFeed(list);
4943

5044
return {
51-
title: `掘金专栏-${columnName}`,
45+
title: `${detailData.column_version.title} - ${detailData.author.user_name}的专栏 - 掘金`,
5246
link: `https://juejin.cn/column/${id}`,
53-
description: `掘金专栏-${columnName}`,
47+
description: detailData.column_version.description,
48+
image: columnDetail.data.column_version.cover,
5449
item: resultItems,
5550
};
5651
}

lib/routes/juejin/dynamic.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Route } from '@/types';
2-
import got from '@/utils/got';
2+
import ofetch from '@/utils/ofetch';
33
import { parseDate } from '@/utils/parse-date';
44

55
export const route: Route = {
@@ -28,17 +28,16 @@ export const route: Route = {
2828
async function handler(ctx) {
2929
const id = ctx.req.param('id');
3030

31-
const response = await got({
32-
method: 'get',
33-
url: 'https://api.juejin.cn/user_api/v1/user/dynamic',
34-
searchParams: {
31+
const response = await ofetch('https://api.juejin.cn/user_api/v1/user/dynamic', {
32+
query: {
3533
user_id: id,
3634
cursor: 0,
3735
},
3836
});
39-
const list = response.data.data.list;
37+
const list = response.data.list;
4038

41-
const username = list[0].user.user_name;
39+
const user = list[0].user;
40+
const username = user.user_name;
4241

4342
const items = list.map((e) => {
4443
const { target_type, target_data, action, time } = e; // action: 0.发布文章;1.点赞文章;2.发布沸点;3.点赞沸点;4.关注用户
@@ -107,7 +106,8 @@ async function handler(ctx) {
107106
return {
108107
title: `掘金用户动态-${username}`,
109108
link: `https://juejin.cn/user/${id}/`,
110-
description: `掘金用户动态-${username}`,
109+
description: user.description || `掘金用户动态-${username}`,
110+
image: user.avatar_large,
111111
item: items,
112112
author: username,
113113
};

lib/routes/juejin/pins.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Route } from '@/types';
2-
import got from '@/utils/got';
2+
import ofetch from '@/utils/ofetch';
33
import { parseDate } from '@/utils/parse-date';
44

55
export const route: Route = {
@@ -38,7 +38,7 @@ async function handler(ctx) {
3838
};
3939

4040
let url = '';
41-
let json = null;
41+
let json = {};
4242
if (/^\d+$/.test(type)) {
4343
url = `https://api.juejin.cn/recommend_api/v1/short_msg/topic`;
4444
json = { id_type: 4, sort_type: 500, cursor: '0', limit: 20, topic_id: type };
@@ -47,10 +47,9 @@ async function handler(ctx) {
4747
json = { id_type: 4, sort_type: 200, cursor: '0', limit: 20 };
4848
}
4949

50-
const response = await got({
51-
method: 'post',
52-
url,
53-
json,
50+
const response = await ofetch(url, {
51+
method: 'POST',
52+
body: json,
5453
});
5554

5655
const items = response.data.data.map((item) => {

lib/routes/juejin/posts.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Route } from '@/types';
2-
import cache from '@/utils/cache';
3-
import got from '@/utils/got';
4-
import util from './utils';
2+
import ofetch from '@/utils/ofetch';
3+
import { parseList, ProcessFeed } from './utils';
4+
import { Article, AuthorUserInfo } from './types';
55

66
export const route: Route = {
77
path: '/posts/:id',
@@ -26,25 +26,32 @@ export const route: Route = {
2626
handler,
2727
};
2828

29+
const getUserInfo = (data: AuthorUserInfo) => ({
30+
username: data.user_name,
31+
description: data.description,
32+
avatar: data.avatar_large,
33+
});
34+
2935
async function handler(ctx) {
3036
const id = ctx.req.param('id');
3137

32-
const response = await got({
33-
method: 'post',
34-
url: 'https://api.juejin.cn/content_api/v1/article/query_list',
35-
json: {
38+
const response = await ofetch('https://api.juejin.cn/content_api/v1/article/query_list', {
39+
method: 'POST',
40+
body: {
3641
user_id: id,
3742
sort_type: 2,
3843
},
3944
});
40-
const { data } = response.data;
41-
const username = data[0] && data[0].author_user_info && data[0].author_user_info.user_name;
42-
const resultItems = await util.ProcessFeed(data, cache);
45+
const data = response.data as Article[];
46+
const list = parseList(data);
47+
const authorInfo = getUserInfo(data[0].author_user_info);
48+
const resultItems = await ProcessFeed(list);
4349

4450
return {
45-
title: `掘金专栏-${username}`,
51+
title: `掘金专栏-${authorInfo.username}`,
4652
link: `https://juejin.cn/user/${id}/posts`,
47-
description: `掘金专栏-${username}`,
53+
description: authorInfo.description,
54+
image: authorInfo.avatar,
4855
item: resultItems,
4956
};
5057
}

0 commit comments

Comments
 (0)