fix: switch to Dcard post api (#4041)

This commit is contained in:
Henry Wang
2020-02-20 15:21:28 +08:00
committed by GitHub
parent 87120e6fbb
commit 95592da00f
2 changed files with 12 additions and 9 deletions
+1 -3
View File
@@ -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,
+11 -6
View File
@@ -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) => `<img src="${m}">`);
body = body.replace(/(?=https?:\/\/).*(?<!jpe?g"?>?)$/gim, (m) => `<a href="${m}">${m}</a>`);
body = body.replace(/\n/g, '<br>');
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,