Skip to content

Commit

Permalink
route 人民网 今日头条一览
Browse files Browse the repository at this point in the history
  • Loading branch information
Yubin Bai committed Sep 9, 2021
1 parent d445580 commit ad446f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
6 changes: 0 additions & 6 deletions docs/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,6 @@ pageClass: routes

<Route author="yubinbai" example="/sina/finance" path="/sina/finance" />

## 新浪财经

### 新浪财经-国內

<Route author="yubinbai" example="/sina/finance" path="/sina/finance" />

## 雪球

### 用户动态
Expand Down
4 changes: 4 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ category 对应的关键词有

<Route author="nczitzk" example="/people/cpc/24h" path="/people/cpc/24h"/>

### 人民网 今日头条一览

<Route author="yubinbai" example="/people/toutiao" path="/people/toutiao"/>

## 人民日报社 国际金融报

### 栏目
Expand Down
1 change: 1 addition & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ router.get('/people/opinion/:id', require('./routes/people/opinion'));
router.get('/people/env/:id', require('./routes/people/env'));
router.get('/people/xjpjh/:keyword?/:year?', require('./routes/people/xjpjh'));
router.get('/people/cpc/24h', require('./routes/people/cpc/24h'));
router.get('/people/toutiao', lazyloadRouteHandler('./routes/people/toutiao'));

// 北极星电力网
router.get('/bjx/huanbao', require('./routes/bjx/huanbao'));
Expand Down
58 changes: 58 additions & 0 deletions lib/routes/people/toutiao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const iconv = require('iconv-lite');
const userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36';
const cookie = 'HMF_CI=1af542e784845025d7b6dbadb116e866b9d8964dc614f5f1b703405cb77295e7a2;'; // required, otherwise 403 forbidden

module.exports = async (ctx) => {
const title = '人民网 今日头条一览';
const link = `http://www.people.com.cn/GB/59476/index.html`;

const response = await got.get(link, {
responseType: 'buffer',
headers: {
'User-Agent': userAgent,
Cookie: cookie,
},
});
const $ = cheerio.load(response.data);

const list = $('body > table:nth-child(2) > tbody > tr > td > table > tbody > tr:nth-child(2) > td > table > tbody > tr > td:nth-child(1) > table:nth-child(2) > tbody > tr > td > *')
.slice(0, 20)
.map(function () {
const link = $(this).find('a').attr('href') ?? $(this).attr('href');
return link;
})
.filter((e) => !!e)
.get();

const out = await Promise.all(
list.map((itemUrl) =>
ctx.cache.tryGet(itemUrl, async () => {
const response = await got.get(itemUrl, {
responseType: 'buffer',
headers: {
'User-Agent': userAgent,
Cookie: cookie,
},
});
response.data = iconv.decode(response.data, 'gbk');
const $ = cheerio.load(response.data);
const title = $('body > div.main > div.layout.rm_txt.cf > div.col.col-1 > h1').html().trim();
const description = $('body > div.main > div.layout.rm_txt.cf > div.col.col-1').html().trim();
const single = {
title,
link: itemUrl,
description,
};
return Promise.resolve(single);
})
)
);

ctx.state.data = {
title,
link,
item: out,
};
};

0 comments on commit ad446f6

Please sign in to comment.