diff --git a/docs/bbs.md b/docs/bbs.md index 0a557cbee..989d4c97b 100644 --- a/docs/bbs.md +++ b/docs/bbs.md @@ -395,6 +395,24 @@ pageClass: routes +## 品葱 + +### 发现 + + + +| 最新 | 推荐 | 热门 | +| ---- | --------- | ---- | +| new | recommend | hot | + +### 精选 + + + +### 话题 + + + ## 三星盖乐世社区 ### 最新帖子 diff --git a/lib/router.js b/lib/router.js index 5bb0f656c..150c32d69 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3780,4 +3780,9 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog')); // Caixin Latest router.get('/caixin/latest', require('./routes/caixin/latest')); +// Pincong 品葱 +router.get('/pincong/category/:category?/:sort?', require('./routes/pincong/index')); +router.get('/pincong/hot/:category?', require('./routes/pincong/hot')); +router.get('/pincong/topic/:topic', require('./routes/pincong/topic')); + module.exports = router; diff --git a/lib/routes/pincong/hot.js b/lib/routes/pincong/hot.js new file mode 100644 index 000000000..0f65af2ab --- /dev/null +++ b/lib/routes/pincong/hot.js @@ -0,0 +1,33 @@ +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + let url = 'https://pincong.rocks/hot/list/'; + + url += ctx.params.category ? 'category-' + ctx.params.category : 'category-0'; + + // use Puppeteer due to the obstacle by cloudflare challenge + const browser = await require('@/utils/puppeteer')(); + const page = await browser.newPage(); + await page.goto(url); + const html = await page.evaluate( + // eslint-disable-next-line no-undef + () => document.documentElement.innerHTML + ); + + browser.close(); + + const $ = cheerio.load(html); + const list = $('div.aw-item'); + + ctx.state.data = { + title: '品葱 - 精选', + link: 'https://pincong.rocks/hot/', + item: list + .map((_, item) => ({ + title: $(item).find('h2 a').text().trim(), + description: $(item).find('div.markitup-box').html(), + link: 'https://pincong.rocks' + $(item).find('div.mod-head h2 a').attr('href'), + })) + .get(), + }; +}; diff --git a/lib/routes/pincong/index.js b/lib/routes/pincong/index.js new file mode 100644 index 000000000..f1553de5f --- /dev/null +++ b/lib/routes/pincong/index.js @@ -0,0 +1,40 @@ +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + let url = 'https://pincong.rocks/'; + + const sortMap = { + new: 'sort_type-new', + recommend: 'recommend-1', + hot: 'sort_type-hot__day2', + }; + + url += (ctx.params.sort && sortMap[ctx.params.sort]) || 'recommend-1'; + url += ctx.params.category ? '__category-' + ctx.params.category : ''; + + // use Puppeteer due to the obstacle by cloudflare challenge + const browser = await require('@/utils/puppeteer')(); + const page = await browser.newPage(); + await page.goto(url); + const html = await page.evaluate( + // eslint-disable-next-line no-undef + () => document.querySelector('div.aw-common-list').innerHTML + ); + + browser.close(); + + const $ = cheerio.load(html); + const list = $('div.aw-item'); + + ctx.state.data = { + title: '品葱 - 发现', + link: url, + item: list + .map((_, item) => ({ + title: $(item).find('h4 a').text().trim(), + link: 'https://pincong.rocks' + $(item).find('h4 a').attr('href'), + pubDate: new Date($(item).attr('data-timestamp') * 1000).toISOString(), + })) + .get(), + }; +}; diff --git a/lib/routes/pincong/topic.js b/lib/routes/pincong/topic.js new file mode 100644 index 000000000..2659515f8 --- /dev/null +++ b/lib/routes/pincong/topic.js @@ -0,0 +1,32 @@ +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const url = 'https://pincong.rocks/topic/' + ctx.params.topic; + + // use Puppeteer due to the obstacle by cloudflare challenge + const browser = await require('@/utils/puppeteer')(); + const page = await browser.newPage(); + await page.goto(url); + const html = await page.evaluate( + () => + // eslint-disable-next-line no-undef + (document.querySelector('div.aw-common-list') && document.querySelector('div.aw-common-list').innerHTML) || '' + ); + + browser.close(); + + const $ = cheerio.load(html); + const list = $('div.aw-item'); + + ctx.state.data = { + title: `品葱 - ${ctx.params.topic}`, + link: url, + item: list + .map((_, item) => ({ + title: $(item).find('h4 a').text().trim(), + link: 'https://pincong.rocks' + $(item).find('h4 a').attr('href'), + pubDate: new Date($(item).attr('data-timestamp') * 1000).toISOString(), + })) + .get(), + }; +};