forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathznews.js
81 lines (68 loc) · 2.71 KB
/
znews.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const baseUrl = 'https://www.zaobao.com.sg';
const axios_ins = axios.create({
headers: {
Referer: baseUrl,
},
});
module.exports = async (ctx) => {
const type = ctx.params.type || 'greater-china';
let info = '中港台';
let word = '/znews/greater-china';
if (type === 'singapore') {
info = '新加坡';
word = '/znews/singapore';
} else if (type === 'international') {
info = '国际';
word = '/znews/international';
} else if (type === 'sea') {
info = '东南亚';
word = '/znews/sea';
} else if (type === 'sports') {
info = '体育';
word = '/znews/sports';
}
const response = await axios_ins.get(baseUrl + word);
const $ = cheerio.load(response.data);
const data = $('.row.list', '.post-list').find('.col-md-8.col-sm-8.col-xs-8.content');
const resultItems = await Promise.all(
data.toArray().map(async (item) => {
const $item = $(item);
const link = baseUrl + $item.find('a')[1].attribs.href;
let resultItem = {};
const value = await ctx.cache.get(link);
if (value) {
resultItem = JSON.parse(value);
} else {
const article = await axios_ins.get(link);
const $1 = cheerio.load(article.data);
const res = $1('.datestamp.date-published.meta-date-published', '.body-content')
.contents()
.text()
.replace('年', '-')
.replace('月', '-')
.replace('日', '');
const date = res.replace('发布/', '').toString();
let description = '';
$1('p', '.article-content-container').each(function() {
description = description + '<p>' + $(this).html() + '</p>';
});
resultItem = {
title: $1('h1', '.body-content').text(),
description: description,
pubDate: new Date(date).toUTCString(),
link: link,
};
ctx.cache.set(link, JSON.stringify(resultItem), 24 * 60 * 60);
}
return Promise.resolve(resultItem);
})
);
ctx.state.data = {
title: `《联合早报》${info} 新闻`,
link: baseUrl + word,
description: '《联合早报》被公认是一份素质高、负责任、报道客观、言论公正、可信度高的报纸,对中国的发展采取积极的态度,在华人世界中享有崇高的信誉。',
item: resultItems,
};
};