From 5516dac61f7914bcbc627a6edbf8bf2eec22bbd9 Mon Sep 17 00:00:00 2001 From: Cloud Date: Sat, 17 Aug 2019 12:32:34 +0800 Subject: [PATCH] feat: add latexstudio (#2869) --- docs/other.md | 6 ++++ lib/router.js | 3 ++ lib/routes/latexstudio/home.js | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 lib/routes/latexstudio/home.js diff --git a/docs/other.md b/docs/other.md index 9d7240a93..42534e656 100644 --- a/docs/other.md +++ b/docs/other.md @@ -185,6 +185,12 @@ pageClass: routes +## LaTeX 开源小屋 + +### 首页 + + + ## MobData ### 分析报告 diff --git a/lib/router.js b/lib/router.js index 306b3cd2f..fa2d569ff 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1631,4 +1631,7 @@ router.get('/sf/sffq-announce', require('./routes/sf/sffq-announce')); router.get('/queshu/sale', require('./routes/queshu/sale')); router.get('/queshu/book/:bookid', require('./routes/queshu/book')); +// LaTeX 开源小屋 +router.get('/latexstudio/home', require('./routes/latexstudio/home')); + module.exports = router; diff --git a/lib/routes/latexstudio/home.js b/lib/routes/latexstudio/home.js new file mode 100644 index 000000000..11ba14c7d --- /dev/null +++ b/lib/routes/latexstudio/home.js @@ -0,0 +1,66 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { addNoReferrer } = require('@/utils/common-utils'); + +module.exports = async (ctx) => { + const res = await got({ + method: 'get', + url: 'https://www.latexstudio.net/', + }); + const $ = cheerio.load(res.data); + const list = $('div.article-latest').find('div.item'); + + const ProcessFeed = async (link) => { + const detail = await got({ + method: 'get', + url: link, + }); + const $ = cheerio.load(detail.data); + $('.tac').remove(); + addNoReferrer($, '.article-content'); + const author = $('.article-meta .item:last-child') + .text() + .replace('稿源:', ''); + const description = $('.article-content').html(); + return { description, author }; + }; + + const items = await Promise.all( + list + .slice(0, 10) + .map(async (index, item) => { + const $item = $(item); + const originalUrl = $item + .find('h2') + .find('a') + .attr('href') + .replace('http', 'https'); + + const cache = await ctx.cache.get(originalUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const { description, author } = await ProcessFeed(originalUrl); + + const single = { + title: `${$item + .find('a.cat') + .text() + .trim()} - ${$item.find('h2', 'a').text()}`, + link: originalUrl, + description, + author, + }; + ctx.cache.set(originalUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + .get() + ); + + ctx.state.data = { + title: 'LaTeX 开源小屋', + link: 'http://www.latexstudio.net/', + item: items, + }; +};