Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rss feed for mirror (fix bugs in previous PR for this site) #8742

Merged
merged 1 commit into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ IPFS 网关有可能失效,那时候换成其他网关。

<Route author="Cerebrater xosdy" example="/matters/author/az" path="/matters/author/:uid" :paramsDesc="['作者 id,可在作者主页的 URL 找到']" radar="1" rssbud="1"/>

## Mirror

<Route author="fifteen42 rde9" example="/mirror/tingfei.eth" path="/mirror/:id" :paramsDesc="['user id']" />

## MIT 科技评论

### 首页
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4259,6 +4259,9 @@ router.get('/secrss/author/:author?', lazyloadRouteHandler('./routes/secrss/auth
// Fashion Network
router.get('/fashionnetwork/headline/:country?', lazyloadRouteHandler('./routes/fashionnetwork/headline.js'));

// mirror.xyz
router.get('/mirror/:id', lazyloadRouteHandler('./routes/mirror/entries'));

// Deprecated: DO NOT ADD ROUTE HERE

module.exports = router;
64 changes: 64 additions & 0 deletions lib/routes/mirror/entries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');

module.exports = async (ctx) => {
const id = ctx.params.id;
let link;
let subDomain = 0;
if (id.endsWith('.eth') || id.length === 42 || id.length === 40) {
link = 'https://mirror.xyz/' + id;
} else {
link = 'https://' + id + '.mirror.xyz';
subDomain = 1;
}

const response = await got({
method: 'get',
url: link,
});

const data = response.body;
const $ = cheerio.load(data);

const author = $('div#__next > div.GlobalNavigation > div.GlobalNavigationContainer > div > div > div > div > div > a').text();

// 标题
const article = $('h1').map(function() {
return $(this).text();
}).get();

// 地址
const addr = $('h1').parent().parent().parent().map(function() {
return $(this).attr('href');
}).get();

// 发布日期
const date = $('div#__next > div > div > div > div > div > div > div > div > div').map(function() {
return $(this).text();
}).get();

// 摘要
const des = $('h1').parent().parent().parent().parent().next().next().map(function() {
return $(this).text();
}).get();


const items = [];
article.forEach((item, index) => {
items.push({
title: item,
author,
description: des[index],
pubDate: date[index],
link: subDomain ? link + addr[index] : 'https://mirror.xyz' + addr[index],
});
});

const title = author + ' - Mirror';
ctx.state.data = {
title,
link,
allowEmpty: true,
item: items,
};
};