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:
@@ -1,29 +0,0 @@
|
||||
const got = require('@/utils/got');
|
||||
|
||||
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: new Date(base_info.ctime * 1000).toUTCString(),
|
||||
guid: base_info.booklet_id,
|
||||
}));
|
||||
|
||||
ctx.state.data = {
|
||||
title: '掘金小册',
|
||||
link: 'https://juejin.cn/books',
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
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.slice(0, 5);
|
||||
}
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,31 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,66 +0,0 @@
|
||||
const got = require('@/utils/got');
|
||||
|
||||
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 = new Date(parseInt(item.msg_Info.ctime) * 1000).toUTCString();
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,25 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,40 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,79 +0,0 @@
|
||||
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,
|
||||
};
|
||||
};
|
||||
@@ -1,48 +0,0 @@
|
||||
const got = require('@/utils/got');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const timezone = require('@/utils/timezone');
|
||||
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);
|
||||
}
|
||||
|
||||
return { description };
|
||||
}
|
||||
|
||||
const ProcessFeed = (list, caches) =>
|
||||
Promise.all(
|
||||
list.map(async (item) => {
|
||||
const pubDate = timezone(parseDate(parseInt(item.article_info.ctime) * 1000), +8);
|
||||
const link = `https://juejin.cn/post/${item.article_id}`;
|
||||
// 列表上提取到的信息
|
||||
const single = {
|
||||
title: item.article_info.title,
|
||||
description: (item.article_info.brief_content || '无描述').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, () => load(item.article_id));
|
||||
// 合并解析后的结果集作为该篇文章最终的输出结果
|
||||
return { ...single, ...other };
|
||||
})
|
||||
);
|
||||
|
||||
module.exports = {
|
||||
ProcessFeed,
|
||||
};
|
||||
Reference in New Issue
Block a user