feat(route): add juejin news (#7969)

* feat(route): add juejin news

* feat(route): add juejin news

* fix(route): juejin news pubDate

* fix: fix utility

* refactor: migrate to v2

* fix: typo

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
This commit is contained in:
CancerGary
2022-05-20 01:55:18 +08:00
committed by GitHub
co-authored by TonyRL
parent 8a8dde7432
commit 950bfa61ed
19 changed files with 163 additions and 89 deletions
+30
View File
@@ -0,0 +1,30 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
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 { data } = response.data;
const items = data.map(({ base_info }) => ({
title: base_info.title,
link: `https://juejin.cn/book/${base_info.booklet_id}`,
description: `
<img src="${base_info.cover_img}"><br>
<strong>${base_info.title}</strong><br><br>
${base_info.summary}<br>
<strong>价格:</strong> ${base_info.price / 100}元
`,
pubDate: parseDate(base_info.ctime * 1000),
guid: base_info.booklet_id,
}));
ctx.state.data = {
title: '掘金小册',
link: 'https://juejin.cn/books',
item: items,
};
};
+39
View File
@@ -0,0 +1,39 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { category } = ctx.params;
const idResponse = await got({
method: 'get',
url: 'https://api.juejin.cn/tag_api/v1/query_category_briefs?show_type=0',
});
const cat = idResponse.data.data.find((item) => item.category_url === category);
const id = cat.category_id;
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed',
json: {
id_type: 2,
sort_type: 300,
cate_id: id,
cursor: '0',
limit: 20,
},
});
let originalData = [];
if (response.data.data) {
originalData = response.data.data;
}
const resultItems = await util.ProcessFeed(originalData, ctx.cache);
ctx.state.data = {
title: `掘金 ${cat.category_name}`,
link: `https://juejin.cn/${category}`,
description: `掘金 ${cat.category_name}`,
item: resultItems,
};
};
+26
View File
@@ -0,0 +1,26 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { collectionId } = ctx.params;
const collectPage = await got({
method: 'get',
url: `https://api.juejin.cn/interact_api/v1/collectionSet/get?tag_id=${collectionId}&cursor=0`,
});
let items = [];
if (collectPage.data.data && collectPage.data.data.article_list) {
items = collectPage.data.data.article_list.slice(0, 10);
}
const result = await util.ProcessFeed(items, ctx.cache);
ctx.state.data = {
title: '掘金 - 单个收藏夹',
link: `https://juejin.cn/collection/${collectionId}`,
description: '掘金,用户单个收藏夹',
item: result,
allowEmpty: true,
};
};
+31
View File
@@ -0,0 +1,31 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
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: {
column_id: id,
limit: 20,
cursor: '0',
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, ctx.cache);
ctx.state.data = {
title: `掘金专栏-${columnName}`,
link: `https://juejin.cn/column/${id}`,
description: `掘金专栏-${columnName}`,
item: resultItems,
};
};
+39
View File
@@ -0,0 +1,39 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { userId } = ctx.params;
const response = await got({
method: 'get',
url: `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 temp = await Promise.all(collectionId.map(getPostId));
const posts = [];
temp.forEach((item) => {
posts.push(...item);
});
const result = await util.ProcessFeed(posts, ctx.cache);
ctx.state.data = {
title: '掘金 - 收藏集',
link: `https://juejin.im/user/${userId}/collections`,
description: '掘金,指定用户整个收藏集',
item: result,
allowEmpty: true,
};
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
'/books': ['xyqfer'],
'/category/:category': ['DIYgod'],
'/collection/:collectionId': ['isQ'],
'/collections/:userId': ['isQ'],
'/column/:id': ['xiangzy1'],
'/news/:id': ['CancerGary'],
'/pins/:type?': ['xyqfer', 'laampui'],
'/posts/:id': ['Maecenas'],
'/shares/:userId': ['qiwihui'],
'/tag/:tag': ['isheng5'],
'/trending/:category/:type': ['moaix'],
};
+25
View File
@@ -0,0 +1,25 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/news/list_user',
json: {
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, ctx.cache);
ctx.state.data = {
title: `掘金资讯-${username}`,
link: `https://juejin.cn/user/${id}/news`,
description: `掘金资讯-${username}`,
item: resultItems,
};
};
+67
View File
@@ -0,0 +1,67 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const type = ctx.params.type ?? 'recommend';
const title = {
recommend: '推荐',
hot: '热门',
'6824710203301167112': '上班摸鱼',
'6819970850532360206': '内推招聘',
'6824710202487472141': '一图胜千言',
'6824710202562969614': '今天学到了',
'6824710202378436621': '每天一道算法题',
'6824710202000932877': '开发工具推荐',
'6824710203112423437': '树洞一下',
};
let url = '';
let json = null;
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 };
} else {
url = `https://api.juejin.cn/recommend_api/v1/short_msg/${type}`;
json = { id_type: 4, sort_type: 200, cursor: '0', limit: 20 };
}
const response = await got({
method: 'post',
url,
json,
});
const items = response.data.data.map((item) => {
const content = item.msg_Info.content;
const title = content;
const guid = item.msg_id;
const link = `https://juejin.cn/pin/${guid}`;
const pubDate = parseDate(item.msg_Info.ctime * 1000);
const author = item.author_user_info.user_name;
const imgs = item.msg_Info.pic_list.reduce((imgs, item) => {
imgs += `
<img src="${item}"><br>
`;
return imgs;
}, '');
const description = `
${content.replace(/\n/g, '<br>')}<br>
${imgs}<br>
`;
return {
title,
link,
description,
guid,
pubDate,
author,
};
});
ctx.state.data = {
title: `沸点 - ${title[type]}`,
link: 'https://juejin.cn/pins/recommended',
item: items,
};
};
+25
View File
@@ -0,0 +1,25 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/article/query_list',
json: {
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, ctx.cache);
ctx.state.data = {
title: `掘金专栏-${username}`,
link: `https://juejin.cn/user/${id}/posts`,
description: `掘金专栏-${username}`,
item: resultItems,
};
};
+61
View File
@@ -0,0 +1,61 @@
module.exports = {
'juejin.cn': {
_name: '掘金',
'.': [
{
title: '标签',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-biao-qian',
source: '/tag/:tag',
target: '/juejin/tag/:tag',
},
{
title: '小册',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-xiao-ce',
source: '/books',
target: '/juejin/books',
},
{
title: '沸点',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-fei-dian',
source: ['/pins/:type', '/pins/topic/:type'],
target: (params) => (params.type !== 'recommended' ? '/juejin/pins/:type' : '/juejin/pins'),
},
{
title: '用户专栏',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-zhuan-lan',
source: ['/user/:id', '/user/:id/posts'],
target: '/juejin/posts/:id',
},
{
title: '收藏集',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-shou-cang-ji',
source: ['/user/:id', '/user/:id/collections'],
target: '/juejin/collections/:id',
},
{
title: '单个收藏夹',
docs: 'https://docs.rsshub.app/programming.html#jue-jin-dan-ge-shou-cang-jia',
source: '/collection/:collectionId',
target: '/juejin/collection/:collectionId',
},
{
title: '分享',
docs: 'https://docs.rsshub.app/programming.html#jue-jin',
source: ['/user/:userId', '/user/:userId/shares'],
target: '/juejin/shares/:userId',
},
{
title: '专栏',
docs: 'https://docs.rsshub.app/programming.html#jue-jin',
source: '/column/:id',
target: '/juejin/column/:id',
},
{
title: '资讯',
docs: 'https://docs.rsshub.app/programming.html#jue-jin',
source: ['/user/:id', '/user/:id/news'],
target: '/juejin/news/:userId',
},
],
},
};
+13
View File
@@ -0,0 +1,13 @@
module.exports = (router) => {
router.get('/books', require('./books'));
router.get('/category/:category', require('./category'));
router.get('/collection/:collectionId', require('./collection'));
router.get('/collections/:userId', require('./favorites'));
router.get('/column/:id', require('./column'));
router.get('/news/:id', require('./news'));
router.get('/pins/:type?', require('./pins'));
router.get('/posts/:id', require('./posts'));
router.get('/shares/:userId', require('./shares'));
router.get('/tag/:tag', require('./tag'));
router.get('/trending/:category/:type', require('./trending'));
};
+26
View File
@@ -0,0 +1,26 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const id = ctx.params.userId;
const response = await got({
method: 'get',
url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_self?src=web&targetUid=${id}&type=article&order=createdAt`,
headers: {
Host: 'timeline-merger-ms.juejin.im',
Origin: 'https://juejin.im',
Referer: `https://juejin.im/user/${id}/shares`,
},
});
const data = response.data.d.entrylist;
const username = data[0] && data[0].user && data[0].user.username;
const resultItems = await util.ProcessFeed(data, ctx.cache);
ctx.state.data = {
title: `掘金分享-${username}`,
link: `https://juejin.im/user/${id}/shares`,
description: `掘金分享-${username}`,
item: resultItems,
};
};
+40
View File
@@ -0,0 +1,40 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { tag } = ctx.params;
const idResponse = await got({
method: 'post',
url: 'https://api.juejin.cn/tag_api/v1/query_tag_detail',
json: {
key_word: tag,
},
});
const id = idResponse.data.data.tag_id;
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/recommend_api/v1/article/recommend_tag_feed',
json: {
id_type: 2,
cursor: '0',
tag_ids: [id],
sort_type: 200,
},
});
let originalData = [];
if (response.data.data) {
originalData = response.data.data.slice(0, 10);
}
const resultItems = await util.ProcessFeed(originalData, ctx.cache);
ctx.state.data = {
title: `掘金 ${tag}`,
link: `https://juejin.cn/tag/${encodeURIComponent(tag)}`,
description: `掘金 ${tag}`,
item: resultItems,
};
};
+79
View File
@@ -0,0 +1,79 @@
const got = require('@/utils/got');
const util = require('./utils');
module.exports = async (ctx) => {
const { category, type } = ctx.params;
let id = '';
let name = '';
let url = 'recommended';
const idResponse = await got({
method: 'get',
url: 'https://api.juejin.cn/tag_api/v1/query_category_briefs',
});
const cat = idResponse.data.data.find((item) => item.category_url === category);
if (cat) {
id = cat.category_id;
name = cat.category_name;
url = cat.category_url;
}
const params = {
monthly: {
period: 'month',
title: '本月',
link: 'monthly_hottest',
sort_type: 30,
},
weekly: {
period: 'week',
title: '本周',
link: 'weekly_hottest',
url: 'get_entry_by_period',
sort_type: 7,
},
historical: {
period: '',
title: '历史',
link: 'hottest',
sort_type: 0,
},
};
const p = params[type];
const title = `掘金${name}${p.title}最热`;
const link = `https://juejin.im/${url}?sort=${p.link}`;
let getUrl = 'https://api.juejin.cn/recommend_api/v1/article/recommend_all_feed';
const getJson = {
cursor: '0',
id_type: 2,
limit: 20,
sort_type: p.sort_type,
};
if (url !== 'recommended') {
getUrl = 'https://api.juejin.cn/recommend_api/v1/article/recommend_cate_feed';
getJson.cate_id = id;
}
const trendingResponse = await got({
method: 'post',
url: getUrl,
json: getJson,
});
let entrylist = trendingResponse.data.data;
if (category === 'all' || category === 'devops' || category === 'product' || category === 'design') {
entrylist = trendingResponse.data.data.filter((item) => item.item_type === 2).map((item) => item.item_info);
}
const resultItems = await util.ProcessFeed(entrylist, ctx.cache);
ctx.state.data = {
title,
link,
item: resultItems,
};
};
+56
View File
@@ -0,0 +1,56 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const md = require('markdown-it')({
html: true,
});
// 加载文章页
async function load(id) {
const response = await got({
method: 'post',
url: 'https://api.juejin.cn/content_api/v1/article/detail',
json: {
article_id: id,
},
});
let description;
if (response.data.data) {
description = md.render(response.data.data.article_info.mark_content) || response.data.data.article_info.content;
}
return { description };
}
const loadNews = async (link) => {
const response = await got(link);
const $ = cheerio.load(response.data);
$('h1.title, .main-box .message').remove();
return { description: $('.main-box .article').html() };
};
const ProcessFeed = (list, caches) =>
Promise.all(
list.map(async (item) => {
const isArticle = !!item.article_info;
const pubDate = parseDate((isArticle ? item.article_info.ctime : item.content_info.ctime) * 1000);
const link = `https://juejin.cn${isArticle ? '/post/' + item.article_id : '/news/' + item.content_id}`;
// 列表上提取到的信息
const single = {
title: isArticle ? item.article_info.title : item.content_info.title,
description: ((isArticle ? item.article_info.brief_content : item.content_info.brief) || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, ''),
pubDate,
author: item.author_user_info.user_name,
link,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。
const other = await caches.tryGet(link, () => (isArticle ? load(item.article_id) : loadNews(link)));
// 合并解析后的结果集作为该篇文章最终的输出结果
return { ...single, ...other };
})
);
module.exports = {
ProcessFeed,
};