feat: add mastodon local public timeline (#3537)

This commit is contained in:
hoilc
2019-12-08 02:38:26 +08:00
committed by DIYgod
parent 7b96a4579c
commit 13e6ef366c
3 changed files with 58 additions and 0 deletions

View File

@@ -296,6 +296,12 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性
| ---- | ---- | ---- | ----- | ----- |
| 最新 | 日榜 | 周榜 | 月榜 | 总榜 |
## Mastodon
### 实例公共时间线
<Route author="hoilc" example="/mastodon/timeline/pawoo.net/true" path="/mastodon/timeline/:site/:only_media?" :paramsDesc="['实例地址, 仅域名, 不包括`http://``https://`协议头', '是否只显示包含媒体(图片或视频)的推文, 默认置空为否, 任意值为是']"/>
## pixiv
### 用户收藏

View File

@@ -2023,4 +2023,7 @@ router.get('/bahamut/creation_index/:category?/:subcategory?/:type?', require('.
// CentBrowser
router.get('/centbrowser/history', require('./routes/centbrowser/history'));
// Mastodon
router.get('/mastodon/timeline/:site/:only_media?', require('./routes/mastodon/timeline'));
module.exports = router;

View File

@@ -0,0 +1,49 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const site = ctx.params.site;
const only_media = ctx.params.only_media ? 'true' : 'false';
const url = `http://${site}/api/v1/timelines/public?local=true&only_media=${only_media}`;
const response = await got.get(url);
const list = response.data;
const ProcessFeed = (data) =>
data.map((item) => {
const title = item.content
.replace(/<span.*?>|<\/span.*?>/gm, '')
.replace(/<(?:.|\n)*?>/gm, '\n')
.split('\n')
.filter((s) => s !== '')[0];
const media = item.media_attachments
.map((item) => {
switch (item.type) {
case 'gifv':
return `<br><video src="${item.url}" autoplay loop>GIF</video>`;
case 'video':
return `<br><video src="${item.url}" controls loop>Video</video>`;
case 'image':
return `<br><img src="${item.url}">`;
default:
return '';
}
})
.join('');
return {
title: title,
author: item.account.display_name,
description: item.content + media,
pubDate: new Date(item.created_at).toUTCString(),
link: item.uri,
};
});
ctx.state.data = {
title: `Local Public${ctx.params.only_media ? ' Media' : ''} Timeline on ${site}`,
link: `http://${site}`,
item: ProcessFeed(list),
};
};