-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathservice.js
85 lines (79 loc) · 1.66 KB
/
service.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
82
83
84
85
const axios = require('axios')
const cheerio = require('cheerio')
const ICON_MAP = {
boil: '沸',
new: '新',
recommend: '荐',
hot: '热',
}
function searchWeibo(q) {
return [
{
type: 'text',
data: {
text: `https://s.weibo.com/weibo?q=${encodeURIComponent(q)}`,
},
},
]
}
async function listResou(cookie) {
const { data } = await axios({
url: 'https://s.weibo.com/top/summary?cate=realtimehot',
headers: {
cookie
}
})
const $ = cheerio.load(data)
return [...$('.list_a > li')]
.slice(0, 20)
.map((item, index) => {
const title = $(item).find('span').text()
const number = $(item).find('span > em').text()
return {
title: title.replace(number, '').trim(),
number,
type:
index === 0
? '顶'
: ICON_MAP[
($(item).find('i').attr('class') || '').replace(
'icon icon_',
''
)
] || '',
url: $(item).find('a').attr('href'),
}
})
.filter(item => item.type !== '荐')
.slice(1, 11)
}
async function getList(search, options = {}) {
try {
if (search) {
return searchWeibo(search)
}
return [
{
type: 'text',
data: {
text:
'微博热搜\n\n' +
(await listResou(options.cookie)).map(item => item.title.trim()).join('\n'),
},
},
]
} catch (e) {
console.error('[weibo]', e)
return [
{
type: 'text',
data: {
text: '微博服务器瘫痪了~',
},
},
]
}
}
module.exports = {
getList,
}