|
1 | 1 | const got = require('@/utils/got'); |
2 | 2 | const cheerio = require('cheerio'); |
3 | | -const iconv = require('iconv-lite'); |
4 | 3 | const timezone = require('@/utils/timezone'); |
| 4 | +const { parseDate } = require('@/utils/parse-date'); |
5 | 5 |
|
6 | 6 | module.exports = async (ctx) => { |
7 | | - const id = ctx.params.id || '31'; |
| 7 | + const id = ctx.params.id ?? '31'; |
| 8 | + const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 20; |
8 | 9 |
|
9 | 10 | const rootUrl = 'https://www.right.com.cn'; |
10 | 11 | const currentUrl = `${rootUrl}/forum/forum-${id}-1.html`; |
| 12 | + |
11 | 13 | const response = await got({ |
12 | 14 | method: 'get', |
13 | 15 | url: currentUrl, |
14 | | - responseType: 'buffer', |
15 | 16 | }); |
16 | 17 |
|
17 | | - const $ = cheerio.load(iconv.decode(response.data, 'gbk')); |
| 18 | + const $ = cheerio.load(response.data); |
18 | 19 |
|
19 | 20 | $('a[title="隐藏置顶帖"]').each(function () { |
20 | 21 | $(this).parents('tbody').remove(); |
21 | 22 | }); |
22 | 23 |
|
23 | | - const list = $('.s') |
24 | | - .map((_, item) => { |
| 24 | + let items = $('.s') |
| 25 | + .slice(0, limit) |
| 26 | + .toArray() |
| 27 | + .map((item) => { |
25 | 28 | item = $(item); |
| 29 | + |
26 | 30 | return { |
27 | 31 | title: item.text(), |
28 | 32 | link: `${rootUrl}/forum/${item.attr('href')}`, |
29 | 33 | }; |
30 | | - }) |
31 | | - .get(); |
| 34 | + }); |
32 | 35 |
|
33 | | - const items = await Promise.all( |
34 | | - list.map((item) => |
| 36 | + items = await Promise.all( |
| 37 | + items.map((item) => |
35 | 38 | ctx.cache.tryGet(item.link, async () => { |
36 | 39 | const detailResponse = await got({ |
37 | 40 | method: 'get', |
38 | 41 | url: item.link, |
39 | | - responseType: 'buffer', |
40 | 42 | }); |
41 | 43 |
|
42 | | - detailResponse.data = iconv.decode(detailResponse.data, 'gbk'); |
43 | | - const postId = detailResponse.data.match(/<div id="post_(\d+)" >/)[1]; |
44 | 44 | const content = cheerio.load(detailResponse.data); |
45 | 45 |
|
46 | | - item.author = content('.authi').eq(0).text(); |
47 | | - item.description = content('#postmessage_' + postId).html(); |
48 | | - item.pubDate = timezone( |
49 | | - new Date( |
50 | | - content('#authorposton' + postId) |
51 | | - .text() |
52 | | - .replace('发表于 ', '') |
53 | | - ), |
54 | | - +8 |
55 | | - ); |
| 46 | + content('.pstatus').remove(); |
| 47 | + |
| 48 | + item.author = content('.authi').first().text(); |
| 49 | + item.description = content('.t_f').first().html(); |
| 50 | + item.pubDate = timezone(parseDate(content('.authi em').first().text().replace('发表于 ', '')), +8); |
| 51 | + item.category = content('.ptg a') |
| 52 | + .toArray() |
| 53 | + .map((a) => content(a).text()); |
56 | 54 |
|
57 | 55 | return item; |
58 | 56 | }) |
|
0 commit comments