Skip to content

Commit

Permalink
fix(route): juejin post (DIYgod#18127)
Browse files Browse the repository at this point in the history
* fix(route): juejin

* refactor: split metadata and article parsing

* fix: clean up

* fix: more cleanup
  • Loading branch information
TonyRL authored Jan 15, 2025
1 parent ddd2dbb commit 38ec0d4
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 190 deletions.
12 changes: 5 additions & 7 deletions lib/routes/juejin/books.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -28,14 +28,12 @@ export const route: Route = {
};

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

const { data } = response.data;
const items = data.map(({ base_info }) => ({
const items = response.data.map(({ base_info }) => ({
title: base_info.title,
link: `https://juejin.cn/book/${base_info.booklet_id}`,
description: `
Expand Down
38 changes: 19 additions & 19 deletions lib/routes/juejin/category.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import util from './utils';
import ofetch from '@/utils/ofetch';
import { getCategoryBrief, parseList, ProcessFeed } from './utils';

export const route: Route = {
path: '/category/:category',
Expand All @@ -16,29 +15,33 @@ export const route: Route = {
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['juejin.cn/:category'],
},
],
name: '分类',
maintainers: ['DIYgod'],
handler,
description: `| 后端 | 前端 | Android | iOS | 人工智能 | 开发工具 | 代码人生 | 阅读 |
| ------- | -------- | ------- | --- | -------- | -------- | -------- | ------- |
| backend | frontend | android | ios | ai | freebie | career | article |`,
| ------- | -------- | ------- | --- | -------- | -------- | -------- | ------- |
| backend | frontend | android | ios | ai | freebie | career | article |`,
};

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

const idResponse = await got({
method: 'get',
url: 'https://api.juejin.cn/tag_api/v1/query_category_briefs?show_type=0',
});
const idResponse = await getCategoryBrief();

const cat = idResponse.data.data.find((item) => item.category_url === category);
const cat = idResponse.find((item) => item.category_url === category);
if (!cat) {
throw new Error('分类不存在');
}
const id = cat.category_id;

const response = await got({
method: 'post',
url: 'https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed',
json: {
const response = await ofetch('https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed', {
method: 'POST',
body: {
id_type: 2,
sort_type: 300,
cate_id: id,
Expand All @@ -47,11 +50,8 @@ async function handler(ctx) {
},
});

let originalData = [];
if (response.data.data) {
originalData = response.data.data;
}
const resultItems = await util.ProcessFeed(originalData, cache);
const list = parseList(response.data);
const resultItems = await ProcessFeed(list);

return {
title: `掘金 ${cat.category_name}`,
Expand Down
20 changes: 6 additions & 14 deletions lib/routes/juejin/collection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import util from './utils';
import { getCollection, parseList, ProcessFeed } from './utils';

export const route: Route = {
path: '/collection/:collectionId',
Expand All @@ -22,27 +20,21 @@ export const route: Route = {
},
],
name: '单个收藏夹',
maintainers: ['isQ'],
maintainers: ['yang131323'],
handler,
};

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

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

let items = [];
if (collectPage.data.data && collectPage.data.data.article_list) {
items = collectPage.data.data.article_list.slice(0, 10);
}
const items = parseList(collectPage.article_list);

const result = await util.ProcessFeed(items, cache);
const result = await ProcessFeed(items);

return {
title: '掘金 - 单个收藏夹',
title: `${collectPage.detail.tag_name} - ${collectPage.create_user.user_name}的收藏集 - 掘金`,
link: `https://juejin.cn/collection/${collectionId}`,
description: '掘金,用户单个收藏夹',
item: result,
Expand Down
42 changes: 17 additions & 25 deletions lib/routes/juejin/favorites.ts → lib/routes/juejin/collections.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import util from './utils';
import ofetch from '@/utils/ofetch';
import { getCollection, parseList, ProcessFeed } from './utils';
import { Article } from './types';

export const route: Route = {
path: '/collections/:userId',
Expand All @@ -23,37 +23,29 @@ export const route: Route = {
},
],
name: '收藏集',
maintainers: ['isQ'],
maintainers: ['yang131323'],
handler,
};

// 获取所有收藏夹文章内容
async function getArticleList(collectionId) {
const collectPage = await getCollection(collectionId);

return collectPage.article_list;
}

async function handler(ctx) {
const userId = ctx.req.param('userId');
const response = await got({
method: 'get',
url: `https://api.juejin.cn/interact_api/v1/collectionSet/list?user_id=${userId}&cursor=0&limit=20`,
});
const response = await ofetch(`https://api.juejin.cn/interact_api/v1/collectionSet/list?user_id=${userId}&cursor=0&limit=20`);

// 获取用户所有收藏夹id
const collectionId = response.data.data.map((item) => item.tag_id);

// 获取所有收藏夹文章内容
async function getPostId(item) {
const collectPage = await got({
method: 'get',
url: `https://api.juejin.cn/interact_api/v1/collectionSet/get?tag_id=${item}&cursor=0`,
});

return (Array.isArray(collectPage.data.data.article_list) && collectPage.data.data.article_list.slice(0, 10)) || [];
}
const collectionId = response.data.map((item) => item.tag_id);

const temp = await Promise.all(collectionId.map((element) => getPostId(element)));
const posts = [];
for (const item of temp) {
posts.push(...item);
}
const temp = (await Promise.all(collectionId.map((id) => getArticleList(id)))) as Article[][];
const posts = temp.flat();
const list = parseList(posts);

const result = await util.ProcessFeed(posts, cache);
const result = await ProcessFeed(list);

return {
title: '掘金 - 收藏集',
Expand Down
31 changes: 13 additions & 18 deletions lib/routes/juejin/column.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import util from './utils';
import ofetch from '@/utils/ofetch';
import { parseList, ProcessFeed } from './utils';

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

async function handler(ctx) {
const id = ctx.req.param('id');
const detail = await got({
method: 'get',
url: `https://api.juejin.cn/content_api/v1/column/detail?column_id=${id}`,
});
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/column/articles_cursor',
json: {
const columnDetail = await ofetch(`https://api.juejin.cn/content_api/v1/column/detail?column_id=${id}`);
const response = await ofetch('https://api.juejin.cn/content_api/v1/column/articles_cursor', {
method: 'POST',
body: {
column_id: id,
limit: 20,
cursor: '0',
limit: 20,
sort: 0,
},
});
const { data } = response.data;
const detailData = detail.data.data;
const columnName = detailData && detailData.column_version && detailData.column_version.title;
const resultItems = await util.ProcessFeed(data, cache);
const detailData = columnDetail.data;
const list = parseList(response.data);
const resultItems = await ProcessFeed(list);

return {
title: `掘金专栏-${columnName}`,
title: `${detailData.column_version.title} - ${detailData.author.user_name}的专栏 - 掘金`,
link: `https://juejin.cn/column/${id}`,
description: `掘金专栏-${columnName}`,
description: detailData.column_version.description,
image: columnDetail.data.column_version.cover,
item: resultItems,
};
}
16 changes: 8 additions & 8 deletions lib/routes/juejin/dynamic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -28,17 +28,16 @@ export const route: Route = {
async function handler(ctx) {
const id = ctx.req.param('id');

const response = await got({
method: 'get',
url: 'https://api.juejin.cn/user_api/v1/user/dynamic',
searchParams: {
const response = await ofetch('https://api.juejin.cn/user_api/v1/user/dynamic', {
query: {
user_id: id,
cursor: 0,
},
});
const list = response.data.data.list;
const list = response.data.list;

const username = list[0].user.user_name;
const user = list[0].user;
const username = user.user_name;

const items = list.map((e) => {
const { target_type, target_data, action, time } = e; // action: 0.发布文章;1.点赞文章;2.发布沸点;3.点赞沸点;4.关注用户
Expand Down Expand Up @@ -107,7 +106,8 @@ async function handler(ctx) {
return {
title: `掘金用户动态-${username}`,
link: `https://juejin.cn/user/${id}/`,
description: `掘金用户动态-${username}`,
description: user.description || `掘金用户动态-${username}`,
image: user.avatar_large,
item: items,
author: username,
};
Expand Down
11 changes: 5 additions & 6 deletions lib/routes/juejin/pins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -38,7 +38,7 @@ async function handler(ctx) {
};

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

const response = await got({
method: 'post',
url,
json,
const response = await ofetch(url, {
method: 'POST',
body: json,
});

const items = response.data.data.map((item) => {
Expand Down
31 changes: 19 additions & 12 deletions lib/routes/juejin/posts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import util from './utils';
import ofetch from '@/utils/ofetch';
import { parseList, ProcessFeed } from './utils';
import { Article, AuthorUserInfo } from './types';

export const route: Route = {
path: '/posts/:id',
Expand All @@ -26,25 +26,32 @@ export const route: Route = {
handler,
};

const getUserInfo = (data: AuthorUserInfo) => ({
username: data.user_name,
description: data.description,
avatar: data.avatar_large,
});

async function handler(ctx) {
const id = ctx.req.param('id');

const response = await got({
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/article/query_list',
json: {
const response = await ofetch('https://api.juejin.cn/content_api/v1/article/query_list', {
method: 'POST',
body: {
user_id: id,
sort_type: 2,
},
});
const { data } = response.data;
const username = data[0] && data[0].author_user_info && data[0].author_user_info.user_name;
const resultItems = await util.ProcessFeed(data, cache);
const data = response.data as Article[];
const list = parseList(data);
const authorInfo = getUserInfo(data[0].author_user_info);
const resultItems = await ProcessFeed(list);

return {
title: `掘金专栏-${username}`,
title: `掘金专栏-${authorInfo.username}`,
link: `https://juejin.cn/user/${id}/posts`,
description: `掘金专栏-${username}`,
description: authorInfo.description,
image: authorInfo.avatar,
item: resultItems,
};
}
Loading

0 comments on commit 38ec0d4

Please sign in to comment.