|
| 1 | +const got = require('@/utils/got'); |
| 2 | +const cheerio = require('cheerio'); |
| 3 | +const iconv = require('iconv-lite'); |
| 4 | +const { parseDate } = require('@/utils/parse-date'); |
| 5 | +const baseUrl = 'http://wlwz.changsha.gov.cn'; |
| 6 | + |
| 7 | +module.exports = async (ctx) => { |
| 8 | + const listPage = await got('http://wlwz.changsha.gov.cn/webapp/cs2020/email/index.jsp', { |
| 9 | + responseType: 'buffer', |
| 10 | + }); |
| 11 | + listPage.data = iconv.decode(listPage.data, 'gbk'); |
| 12 | + const $ = cheerio.load(listPage.data); |
| 13 | + const list = $('.table1 tbody tr') |
| 14 | + .slice(1) |
| 15 | + .map((_, tr) => { |
| 16 | + tr = $(tr); |
| 17 | + |
| 18 | + return { |
| 19 | + title: tr.find('td[title]').attr('title'), |
| 20 | + link: baseUrl + tr.find('td[title] > a').attr('href'), |
| 21 | + author: tr.find('td:last').text(), |
| 22 | + }; |
| 23 | + }) |
| 24 | + .get(); |
| 25 | + |
| 26 | + const items = await Promise.all( |
| 27 | + list.map((item) => |
| 28 | + ctx.cache.tryGet(item.link, async () => { |
| 29 | + const postPage = await got(item.link, { |
| 30 | + responseType: 'buffer', |
| 31 | + }); |
| 32 | + postPage.data = iconv.decode(postPage.data, 'gbk'); |
| 33 | + const $ = cheerio.load(postPage.data); |
| 34 | + |
| 35 | + const data = { |
| 36 | + title: item.title, |
| 37 | + description: $('.letter-details').html().trim(), |
| 38 | + pubDate: parseDate($('.letter-details div:first table tr:nth-child(2) > .td_label2').text() + ' +0800', 'YYYY-MM-DD HH:mm:ss ZZ'), |
| 39 | + link: item.link, |
| 40 | + author: item.author, |
| 41 | + }; |
| 42 | + return data; |
| 43 | + }) |
| 44 | + ) |
| 45 | + ); |
| 46 | + |
| 47 | + ctx.state.data = { |
| 48 | + title: '来信反馈 - 长沙市市长信箱', |
| 49 | + link: `${baseUrl}/webapp/cs2020/email/index.jsp`, |
| 50 | + item: items, |
| 51 | + }; |
| 52 | +}; |
0 commit comments