From 95592da00ff33767667a6b5b62d4a599f42d1b3b Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 20 Feb 2020 07:21:28 +0000 Subject: [PATCH] fix: switch to Dcard post api (#4041) --- lib/routes/dcard/section.js | 4 +--- lib/routes/dcard/utils.js | 17 +++++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/routes/dcard/section.js b/lib/routes/dcard/section.js index 982a856155..194c6ece6a 100644 --- a/lib/routes/dcard/section.js +++ b/lib/routes/dcard/section.js @@ -36,9 +36,7 @@ module.exports = async (ctx) => { }, }); - const data = response.data; - - const items = await utils.ProcessFeed(data, ctx.cache); + const items = await utils.ProcessFeed(response.data, ctx.cache); ctx.state.data = { title, diff --git a/lib/routes/dcard/utils.js b/lib/routes/dcard/utils.js index e081acda38..33270536da 100644 --- a/lib/routes/dcard/utils.js +++ b/lib/routes/dcard/utils.js @@ -1,10 +1,9 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); const ProcessFeed = async (list, cache) => { const result = await Promise.all( list.map(async (item) => { - const link = `https://www.dcard.tw/f/${item.forumAlias}/p/${item.id}`; + const url = `https://www.dcard.tw/_api/posts/${item.id}`; const content = await cache.tryGet(`dcard${item.id}`, async () => { let response; @@ -14,15 +13,21 @@ const ProcessFeed = async (list, cache) => { try { response = await got({ method: 'get', - url: link, + url, headers: { Referer: 'https://www.dcard.tw/f', }, }); - const $ = cheerio.load(response.data); + let body = response.data.content; - return $('.Post_content_NKEl9d > div:nth-child(1)').html(); + body = body.replace(/(?=https?:\/\/).*?(?<=\.(jpe?g|gif|png))/gi, (m) => ``); + + body = body.replace(/(?=https?:\/\/).*(??)$/gim, (m) => `${m}`); + + body = body.replace(/\n/g, '
'); + + return body; } catch (error) { return ''; } @@ -30,7 +35,7 @@ const ProcessFeed = async (list, cache) => { const single = { title: `「${item.forumName}」${item.title}`, - link, + link: `https://www.dcard.tw/f/${item.forumAlias}/p/${item.id}`, description: content, author: item.school || '匿名', pubDate: item.createdAt,