-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
233 lines (174 loc) · 6.83 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* 示例文件
*/
// 初始化 express
var app = require('express')();
// 初始化 superagent 模块
var request = require('superagent');
/**
* 开启路由
* 第一个参数指定路由地址,当前指向的是 localhost:3000/
* 如果需要其他路由,可以这样定义,比如 需要我们的获取推荐歌单的路由 /recommendLst
* app.get('/recommendLst', function(req, res){});
*/
app.get('/', function(req, res){
// 向请求 localhost:3000/ 的地址返回 Hello World 字符串
res.send('Hello World !');
});
app.get('/test', function(req, res){
request.get('http://localhost:3000/')
.end(function(err, _response){
if (!err) {
// 如果获取过程中没有发生错误
var result = '获取到的数据:'+_response.text;
console.log(result);
res.send(result);
} else {
console.log('Get data error !');
}
});
});
// 加载 cheerio 模块
var cheerio = require('cheerio');
app.get('/testCheerio', function(req, res){
var $ = cheerio.load('<h1 id="test">这是一段示例文字</h1>');
$('#test').css('color','red');
res.send( $.html() );
});
// express 开放 /recommendLst API
app.get('/recommendLst', function(req, res){
// 初始化返回对象
var resObj = {
code: 200,
data: []
};
// 使用 superagent 访问 discover 页面
request.get('http://music.163.com/discover')
.end(function(err, _response){
if (!err) {
// 请求成功
var dom = _response.text;
// 使用 cheerio 加载 dom
var $ = cheerio.load(dom);
// 定义我们要返回的数组
var recommendLst = [];
// 获得 .m-cvrlst 的 ul 元素
$('.m-cvrlst').eq(0).find('li').each(function(index, element){
// 获得 a 链接
var cvrLink = $(element).find('.u-cover').find('a');
console.log(cvrLink.html());
// 获得 cover 歌单封面
var cover = $(element).find('.u-cover').find('img').attr('src');
// 组织单个推荐歌单对象结构
var recommendItem = {
id: cvrLink.attr('data-res-id'),
title: cvrLink.attr('title'),
href: 'http://music.163.com' + cvrLink.attr('href'),
type: cvrLink.attr('data-res-type'),
cover: cover
};
// 将单个对象放在数组中
recommendLst.push(recommendItem);
});
// 替换返回对象
resObj.data = recommendLst;
} else {
resObj.code = 404;
console.log('Get data error !');
}
// 响应数据
res.send( resObj );
});
});
// 定义根据歌单id获得歌单详细信息的API
app.get('/playlist/:playlistId', function(req, res){
// 获得歌单ID
var playlistId = req.params.playlistId;
// 定义返回对象
var resObj = {
code: 200,
data: {}
};
/**
* 使用 superagent 请求
* 在这里我们为什么要请求 http://music.163.com/playlist?id=${playlistId}
* 简友们应该还记得 网易云音乐首页的 iframe
* 应该还记得去打开 调试面板的 Sources 选项卡
* 那么就可以看到在歌单页面 iframe 到底加载了什么 url
*/
request.get(`http://music.163.com/playlist?id=${playlistId}`)
.end(function(err, _response){
if (!err) {
// 定义歌单对象
var playlist = {
id: playlistId
};
// 成功返回 HTML, decodeEntities 指定不把中文字符转为 unicode 字符
// 如果不指定 decodeEntities 为 false , 例如 " 会解析为 "
var $ = cheerio.load(_response.text,{decodeEntities: false});
// 获得歌单 dom
var dom = $('#m-playlist');
// 歌单标题
playlist.title = dom.find('.tit').text();
// 歌单拥有者
playlist.owner = dom.find('.user').find('.name').text();
// 创建时间
playlist.create_time = dom.find('.user').find('.time').text();
// 歌单被收藏数量
playlist.collection_count = dom.find('#content-operation').find('.u-btni-fav').attr('data-count');
// 分享数量
playlist.share_count = dom.find('#content-operation').find('.u-btni-share').attr('data-count');
// 评论数量
playlist.comment_count = dom.find('#content-operation').find('#cnt_comment_count').html();
// 标签
playlist.tags = [];
dom.find('.tags').eq(0).find('.u-tag').each(function(index, element){
playlist.tags.push($(element).text());
});
// 歌单描述
playlist.desc = dom.find('#album-desc-more').html();
// 歌曲总数量
playlist.song_count = dom.find('#playlist-track-count').text();
// 播放总数量
playlist.play_count = dom.find('#play-count').text();
resObj.data = playlist;
} else {
resObj.code = 404 ;
console.log('Get data error!');
}
res.send( resObj );
});
});
// 定义根据歌单id获得歌单所有歌曲列表的API
app.get('/song_list/:playlistId', function(req, res){
// 获得歌单ID
var playlistId = req.params.playlistId;
// 定义返回对象
var resObj = {
code: 200,
data: []
};
request.get(`http://music.163.com/playlist?id=${playlistId}`)
.end(function(err, _response){
if (!err) {
// 成功返回 HTML
var $ = cheerio.load(_response.text,{decodeEntities: false});
// 获得歌单 dom
var dom = $('#m-playlist');
resObj.data = JSON.parse( dom.find('#song-list-pre-cache').find('textarea').html() );
} else {
resObj.code = 404 ;
console.log('Get data error!');
}
res.send( resObj );
});
});
/**
* 开启express服务,监听本机3000端口
* 第二个参数是开启成功后的回调函数
*/
var server = app.listen(3000, function(){
// 如果 express 开启成功,则会执行这个方法
var port = server.address().port;
console.log(`Express app listening at http://localhost:${port}`);
});