fix(route): matters pubDate and API

add pubDate to /matters/* and move /matters/tags to graphql api. (#8056)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
FledgeShiu
2021-09-09 01:45:22 -07:00
committed by GitHub
parent d94894be87
commit 10bc69d3fe
4 changed files with 58 additions and 48 deletions
+5 -5
View File
@@ -497,11 +497,11 @@ IPFS 网关有可能失效,那时候换成其他网关。
## Matters
### 最新、熱議、精華
### 最新、热门、精华
<Route author="xyqfer Cerebrater xosdy" example="/matters/latest/heat" path="/matters/latest/:type?" :paramsDesc="['默認爲 latest, 見下表']" radar="1" rssbud="1">
<Route author="xyqfer Cerebrater xosdy" example="/matters/latest/heat" path="/matters/latest/:type?" :paramsDesc="['默认为 latest, 见下表']" radar="1" rssbud="1">
| 最新 | 熱議 | 精華 |
| 最新 | 热门 | 精华 |
| ------ | ---- | ------- |
| latest | heat | essence |
@@ -509,11 +509,11 @@ IPFS 网关有可能失效,那时候换成其他网关。
### 标签
<Route author="Cerebrater" example="/matters/tags/VGFnOjk3Mg" path="/matters/tags/:tid" :paramsDesc="['標籤 id,可在標籤所在的 URL 找到']" radar="1" rssbud="1"/>
<Route author="Cerebrater" example="/matters/tags/VGFnOjk3Mg" path="/matters/tags/:tid" :paramsDesc="['标签 id,可在标签所在的 URL 找到']" radar="1" rssbud="1"/>
### 作者
<Route author="Cerebrater xosdy" example="/matters/author/az" path="/matters/author/:uid" :paramsDesc="['作者 id,可在作者主頁的 URL 找到']" radar="1" rssbud="1"/>
<Route author="Cerebrater xosdy" example="/matters/author/az" path="/matters/author/:uid" :paramsDesc="['作者 id,可在作者主页的 URL 找到']" radar="1" rssbud="1"/>
## MIT 科技评论
+3
View File
@@ -1,4 +1,5 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const uid = ctx.params.uid;
@@ -22,6 +23,7 @@ module.exports = async (ctx) => {
mediaHash
title
content
createdAt
}
}
}
@@ -41,6 +43,7 @@ module.exports = async (ctx) => {
author: user.displayName,
description: article.content,
link: `${host}/@${uid}/${article.slug}-${article.mediaHash}`,
pubDate: parseDate(article.createdAt),
})),
};
};
+2 -1
View File
@@ -1,4 +1,5 @@
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const type = ctx.params.type || 'latest';
@@ -56,7 +57,7 @@ module.exports = async (ctx) => {
link,
description: article,
author: node.author.displayName,
pubDate: node.createdAt,
pubDate: parseDate(node.createdAt),
};
});
+48 -42
View File
@@ -1,50 +1,56 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const url = `https://matters.news/tags/${ctx.params.tid}`;
const tid = ctx.params.tid;
const host = `https://matters.news`;
const url = `https://server.matters.news/graphql`;
const response = await got({
method: 'post',
url,
json: {
query: `
{
node(input: { id: "${tid}" }) {
... on Tag {
content
articles(input:{first: 20}){
edges {
node {
id
title
slug
cover
summary
mediaHash
content
createdAt
author {
id
userName
displayName
}
}
}
}
}
}
}`,
},
});
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('section.list-item').get();
const node = response.data.data.node;
const proList = [];
const indexList = [];
const out = await Promise.all(
list.map(async (item, i) => {
const $ = cheerio.load(item);
const time = $('time').attr('datetime');
const title = $('h2').text();
const author = $('span.displayname').text();
const postfix = encodeURI($('section.title > a').attr('href'));
const address = `https://matters.news${postfix}`;
const cache = await ctx.cache.get(address);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const single = {
title,
pubDate: new Date(time).toUTCString(),
author,
link: address,
guid: address,
};
proList.push(got.get(address));
indexList.push(i);
return Promise.resolve(single);
})
);
const responses = await got.all(proList);
for (let i = 0; i < responses.length; i++) {
const res = responses[i];
const $ = cheerio.load(res.data);
out[indexList[i]].description = $('.u-content').html();
ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i]));
}
ctx.state.data = {
title: $('title').text(),
link: url,
item: out,
title: `Matters | ${node.content}`,
link: `${host}/tags/${tid}`,
description: node.content,
item: node.articles.edges.map(({ node: article }) => ({
title: article.title,
author: article.author.displayName,
description: article.content,
link: `${host}/@${article.author.id}/${article.slug}-${article.mediaHash}`,
pubDate: parseDate(article.createdAt),
})),
};
};