diff --git a/lib/routes/juejin/books.js b/lib/routes/juejin/books.js index cefcc6ca7a..e8843c95ea 100644 --- a/lib/routes/juejin/books.js +++ b/lib/routes/juejin/books.js @@ -2,26 +2,28 @@ const got = require('@/utils/got'); module.exports = async (ctx) => { const response = await got({ - method: 'get', - url: 'https://xiaoce-timeline-api-ms.juejin.im/v1/getListByLastTime?uid=&client_id=&token=&src=web&alias=&pageNum=1', + method: 'post', + url: 'https://apinew.juejin.im/booklet_api/v1/booklet/listbycategory', + json: { category_id: '0', cursor: '0', limit: 20 }, }); - const data = response.data.d; + const { data } = response.data; + const items = data.map(({ base_info }) => ({ + title: base_info.title, + link: `https://juejin.im/book/${base_info.booklet_id}`, + description: ` +
+ ${base_info.title}

+ ${base_info.summary}
+ 价格: ${base_info.price / 100}元 + `, + pubDate: new Date(base_info.ctime * 1000).toUTCString(), + guid: base_info.booklet_id, + })); ctx.state.data = { title: '掘金小册', link: 'https://juejin.im/books', - item: data.map(({ title, id, img, desc, createdAt, price }) => ({ - title, - link: `https://juejin.im/book/${id}`, - description: ` -
- ${title}

- ${desc}
- 价格: ${price}元 - `, - pubDate: new Date(createdAt).toUTCString(), - guid: id, - })), + item: items, }; }; diff --git a/lib/routes/juejin/category.js b/lib/routes/juejin/category.js index 2c04f16c20..653a921c85 100644 --- a/lib/routes/juejin/category.js +++ b/lib/routes/juejin/category.js @@ -2,41 +2,38 @@ const got = require('@/utils/got'); const util = require('./utils'); module.exports = async (ctx) => { - const category = ctx.params.category; + const { category } = ctx.params; const idResponse = await got({ method: 'get', - url: 'https://gold-tag-ms.juejin.im/v1/categories', - headers: { - Referer: `https://juejin.im/welcome/${category}`, - 'X-Juejin-Client': '', - 'X-Juejin-Src': 'web', - 'X-Juejin-Token': '', - 'X-Juejin-Uid': '', - }, + url: 'https://apinew.juejin.im/tag_api/v1/query_category_briefs?show_type=0', }); - const cat = idResponse.data.d.categoryList.find((item) => item.title === category); - const id = cat.id; + const cat = idResponse.data.data.find((item) => item.category_url === category); + const id = cat.category_id; const response = await got({ - method: 'get', - url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_timeline?src=web&limit=20&category=${id}`, - headers: { - Referer: `https://juejin.im/welcome/${category}`, + method: 'post', + url: 'https://apinew.juejin.im/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.d && response.data.d.entrylist) { - originalData = response.data.d.entrylist.slice(0, 5); + if (response.data.data) { + originalData = response.data.data.slice(0, 5); } const resultItems = await util.ProcessFeed(originalData, ctx.cache); ctx.state.data = { - title: `掘金${cat.name}`, - link: `https://juejin.im/welcome/${category}`, - description: `掘金${cat.name}`, + title: `掘金${cat.category_name}`, + link: `https://juejin.im/${category}`, + description: `掘金${cat.category_name}`, item: resultItems, }; }; diff --git a/lib/routes/juejin/collection.js b/lib/routes/juejin/collection.js index df86d8faa4..f62da8d3b0 100644 --- a/lib/routes/juejin/collection.js +++ b/lib/routes/juejin/collection.js @@ -2,29 +2,16 @@ const got = require('@/utils/got'); const util = require('./utils'); module.exports = async (ctx) => { - const collectionId = ctx.params.collectionId; + const { collectionId } = ctx.params; const collectPage = await got({ method: 'get', - url: `https://collection-set-ms.juejin.im/v1/getCollectionSetEntries?page=0&csId=${collectionId}&rankType=new&src=web`, - headers: { - Referer: `https://juejin.im/collection/${collectionId}`, - }, - }); - - // 获取收藏夹文章内容 - const urlArg = collectPage.data.d.join('|'); - const posts = await got({ - method: 'get', - url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_ids?src=web&page=0&pageSize=10&entryIds=${encodeURI(urlArg)}`, - headers: { - Referer: `https://juejin.im/collection/${collectionId}`, - }, + url: `https://apinew.juejin.im/interact_api/v1/collectionSet/get?tag_id=${collectionId}&cursor=0`, }); let items = []; - if (posts.data.d && posts.data.d.entrylist) { - items = posts.data.d.entrylist.slice(0, 10); + 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); diff --git a/lib/routes/juejin/favorites.js b/lib/routes/juejin/favorites.js index c886666a5b..45c594d0e1 100644 --- a/lib/routes/juejin/favorites.js +++ b/lib/routes/juejin/favorites.js @@ -2,36 +2,23 @@ const got = require('@/utils/got'); const util = require('./utils'); module.exports = async (ctx) => { - const userId = ctx.params.userId; - const response = await new got({ + const { userId } = ctx.params; + const response = await got({ method: 'get', - url: `https://collection-set-ms.juejin.im/v1/getUserCollectionSet?src=web&page=0&pageSize=30&targetUserId=${userId}`, - headers: { - Referer: `https://juejin.im/user/${userId}/collections`, - }, + url: `https://apinew.juejin.im/interact_api/v1/collectionSet/list?user_id=${userId}&cursor=0&limit=20`, }); // 获取用户所有收藏夹id - const collectionId = response.data.d.collectionSets.map((item) => item.csId); + const collectionId = response.data.data.map((item) => item.tag_id); // 获取所有收藏夹文章内容 async function getPostId(item) { const collectPage = await got({ method: 'get', - url: `https://collection-set-ms.juejin.im/v1/getCollectionSetEntries?page=0&csId=${item}&rankType=new&src=web`, - headers: { - Referer: `https://juejin.im/collection/${item}`, - }, + url: `https://apinew.juejin.im/interact_api/v1/collectionSet/get?tag_id=${item}&cursor=0`, }); - const urlArg = collectPage.data.d.join('|'); - const posts = await got({ - method: 'get', - url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_ids?src=web&page=0&pageSize=10&entryIds=${encodeURI(urlArg)}`, - headers: { - Referer: `https://juejin.im/collection/${item}`, - }, - }); - return (posts.data.d && posts.data.d.entrylist.slice(0, 10)) || []; + + return (collectPage.data.data && collectPage.data.data.article_list.slice(0, 10)) || []; } const temp = await Promise.all(collectionId.map(getPostId)); diff --git a/lib/routes/juejin/posts.js b/lib/routes/juejin/posts.js index 46713b762c..2c17927845 100644 --- a/lib/routes/juejin/posts.js +++ b/lib/routes/juejin/posts.js @@ -2,19 +2,18 @@ const got = require('@/utils/got'); const util = require('./utils'); module.exports = async (ctx) => { - const id = ctx.params.id; + const { id } = ctx.params; const response = await got({ - method: 'get', - url: `https://timeline-merger-ms.juejin.im/v1/get_entry_by_self?src=web&targetUid=${id}&type=post&order=createdAt`, - headers: { - Host: 'timeline-merger-ms.juejin.im', - Origin: 'https://juejin.im', - Referer: `https://juejin.im/user/${id}/posts`, + method: 'post', + url: 'https://apinew.juejin.im/content_api/v1/article/query_list', + json: { + user_id: id, + sort_type: 2, }, }); - const data = response.data.d.entrylist; - const username = data && data[0] && data[0].user && data[0].user.username; + const { data } = response.data; + const username = data && 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 = { diff --git a/lib/routes/juejin/tag.js b/lib/routes/juejin/tag.js index 28a605e6f3..b0d676c712 100644 --- a/lib/routes/juejin/tag.js +++ b/lib/routes/juejin/tag.js @@ -2,41 +2,39 @@ const got = require('@/utils/got'); const util = require('./utils'); module.exports = async (ctx) => { - const tag = ctx.params.tag; + const { tag } = ctx.params; const idResponse = await got({ - method: 'get', - url: 'https://gold-tag-ms.juejin.im/v1/tags', - headers: { - Referer: `https://juejin.im/tag/${encodeURI(tag)}`, - 'X-Juejin-Client': '', - 'X-Juejin-Src': 'web', - 'X-Juejin-Token': '', - 'X-Juejin-Uid': '', + method: 'post', + url: 'https://apinew.juejin.im/tag_api/v1/query_tag_detail', + json: { + key_word: tag, }, }); - const cat = idResponse.data.d.tags.filter((item) => item.title === tag)[0]; - const id = cat.id; + const id = idResponse.data.data.tag_id; const response = await got({ - method: 'get', - url: `https://timeline-merger-ms.juejin.im/v1/get_tag_entry?src=web&tagId=${id}&page=0&pageSize=10&sort=rankIndex`, - headers: { - Referer: `https://juejin.im/tag/${encodeURI(tag)}`, + method: 'post', + url: 'https://apinew.juejin.im/recommend_api/v1/article/recommend_tag_feed', + json: { + id_type: 2, + cursor: '0', + tag_ids: [id], + sort_type: 200, }, }); let originalData = []; - if (response.data.d && response.data.d.entrylist) { - originalData = response.data.d.entrylist.slice(0, 10); + if (response.data.data) { + originalData = response.data.data.slice(0, 10); } const resultItems = await util.ProcessFeed(originalData, ctx.cache); ctx.state.data = { - title: `掘金${cat.title}`, - link: `https://juejin.im/tag/${encodeURI(tag)}`, - description: `掘金${cat.title}`, + title: `掘金${tag}`, + link: `https://juejin.im/tag/${encodeURIComponent(tag)}`, + description: `掘金${tag}`, item: resultItems, }; }; diff --git a/lib/routes/juejin/trending.js b/lib/routes/juejin/trending.js index f70e77353d..1b1f22d522 100644 --- a/lib/routes/juejin/trending.js +++ b/lib/routes/juejin/trending.js @@ -7,8 +7,7 @@ const got_ins = got.extend({ }); module.exports = async (ctx) => { - const category = ctx.params.category; - const type = ctx.params.type; + const { category, type } = ctx.params; let id = 'all'; let name = ''; diff --git a/lib/routes/juejin/utils.js b/lib/routes/juejin/utils.js index 5da5c2789f..0fad946bac 100644 --- a/lib/routes/juejin/utils.js +++ b/lib/routes/juejin/utils.js @@ -31,27 +31,22 @@ const ProcessFeed = async (list, caches) => await Promise.all( list.map(async (item) => { // 解析时间,补偿时区offset - const pubdate = new Date(); - pubdate.setTime(Date.parse(item.createdAt) - 8 * 60 * 60 * 1000); + const pubdate = new Date(parseInt(item.article_info.ctime) * 1000 - 8 * 60 * 60 * 1000); + const link = `https://juejin.im/post/${item.article_id}`; // 列表上提取到的信息 const single = { - title: item.title, + title: item.article_info.title, description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`, pubDate: pubdate.toUTCString(), - author: item.user.username, - link: item.originalUrl, - guid: item.originalUrl, + author: item.author_user_info.user_name, + link, }; - if (item.type === 'post') { - // 使用tryGet方法从缓存获取内容。 - // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。 - const other = await caches.tryGet(item.originalUrl, async () => await load(item.originalUrl)); - // 合并解析后的结果集作为该篇文章最终的输出结果 - return Promise.resolve(Object.assign({}, single, other)); - } else { - return Promise.resolve(single); - } + // 使用tryGet方法从缓存获取内容。 + // 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。 + const other = await caches.tryGet(link, async () => await load(link)); + // 合并解析后的结果集作为该篇文章最终的输出结果 + return Promise.resolve(Object.assign({}, single, other)); }) );