From e91d9968a1b5783a922b3de3f6a97f9500a8a1b2 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 24 Jan 2021 11:37:26 +0800 Subject: [PATCH] feat: add telecompaper search (#6734) --- docs/traditional-media.md | 20 +++++++- lib/router.js | 3 +- lib/routes/telecompaper/search.js | 77 +++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 lib/routes/telecompaper/search.js diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 1e27c3dabd..dd1e18cbd8 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -222,7 +222,7 @@ Solidot 提供的 feed: ### News - + 可选分类如下 @@ -240,6 +240,24 @@ Solidot 提供的 feed: +### Search + + + +排序 + +| Date Ascending | Date Descending | +| -------------- | --------------- | +| 1 | 2 | + +发表在时间段内 + +| 1 month | 3 months | 6 months | 12 months | 24 months | +| ------- | -------- | -------- | --------- | --------- | +| 1 | 3 | 6 | 12 | 24 | + + + ## The Economist ### 分类 diff --git a/lib/router.js b/lib/router.js index 7ee8df8417..fc40fd5355 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3224,7 +3224,8 @@ router.get('/141ppv/:type/:key?', require('./routes/141ppv/141ppv')); router.get('/curiouscat/user/:id', require('./routes/curiouscat/user')); // Telecompaper -router.get('/telecompaper/news/:caty/:year?/:country?/:type?/:keyword?', require('./routes/telecompaper/news')); +router.get('/telecompaper/news/:caty/:year?/:country?/:type?', require('./routes/telecompaper/news')); +router.get('/telecompaper/search/:keyword?/:company?/:sort?/:period?', require('./routes/telecompaper/search')); // 水木社区 router.get('/newsmth/account/:id', require('./routes/newsmth/account')); diff --git a/lib/routes/telecompaper/search.js b/lib/routes/telecompaper/search.js new file mode 100644 index 0000000000..e297eb3746 --- /dev/null +++ b/lib/routes/telecompaper/search.js @@ -0,0 +1,77 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const keyword = ctx.params.keyword || ''; + const company = ctx.params.company || ''; + const sort = ctx.params.sort || '2'; + const period = ctx.params.period || '12'; + + const rootUrl = `https://www.telecompaper.com/search/index.aspx?search=${keyword}`; + + let response = await got({ + method: 'get', + url: rootUrl, + }), + $ = cheerio.load(response.data); + + response = await got({ + method: 'post', + url: rootUrl, + data: JSON.stringify({ + __EVENTTARGET: '', + __EVENTARGUMENT: '', + __VSTATE: $('#__VSTATE').attr('value'), + __VIEWSTATE: '', + ctl00$header$searchText: 'Search keywords', + ctl00$header$searchTextMobile: 'Search keywords', + ctl00$MainPlaceHolder$SearchText: keyword, + ctl00$MainPlaceHolder$CompanyText: company, + ctl00$MainPlaceHolder$Sort: parseInt(sort), + ctl00$MainPlaceHolder$Results: 20, + ctl00$MainPlaceHolder$Date: 'Timeframe1', + ctl00$MainPlaceHolder$Period: parseInt(period), + ctl00$MainPlaceHolder$txtStartDate: '', + ctl00$MainPlaceHolder$txtEndDate: '', + ctl00$MainPlaceHolder$chkEnglish: 'on', + ctl00$MainPlaceHolder$Submit: 'Search', + }), + }); + $ = cheerio.load(response.data); + + const list = $('table.details_rows tbody tr') + .slice(0, 15) + .map((_, item) => { + item = $(item); + const a = item.find('a'); + return { + title: a.text(), + link: a.attr('href'), + pubDate: new Date(item.find('span.source').text().split(' | ')[0] + ' GMT+1').toUTCString(), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('#pageContainer').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `Telecompaper Search - ${keyword}`, + link: rootUrl, + item: items, + }; +};