From cdb74c278a52b34ef10440957051b764c798e315 Mon Sep 17 00:00:00 2001 From: e <41101885+rde9@users.noreply.github.com> Date: Sat, 22 Jan 2022 12:31:19 -0800 Subject: [PATCH] Add rss feed for mirror (fix bugs in previous PR for this site) (#8742) --- docs/new-media.md | 4 +++ lib/router.js | 3 ++ lib/routes/mirror/entries.js | 64 ++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 lib/routes/mirror/entries.js diff --git a/docs/new-media.md b/docs/new-media.md index f2fb7d06f9..2d309e5a69 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -716,6 +716,10 @@ IPFS 网关有可能失效,那时候换成其他网关。 +## Mirror + + + ## MIT 科技评论 ### 首页 diff --git a/lib/router.js b/lib/router.js index db6fbcef22..f5c4ef9b23 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4248,6 +4248,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 // KBS diff --git a/lib/routes/mirror/entries.js b/lib/routes/mirror/entries.js new file mode 100644 index 0000000000..8b975b2ae1 --- /dev/null +++ b/lib/routes/mirror/entries.js @@ -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, + }; +}; \ No newline at end of file