-
Notifications
You must be signed in to change notification settings - Fork 56
/
baidumusic.cpp
211 lines (168 loc) · 5.73 KB
/
baidumusic.cpp
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
#include <QDebug>
#include <QUrl>
#include <QNetworkRequest>
#include <QByteArray>
#include <QStringList>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRegularExpressionMatchIterator>
#include "baidumusic.h"
//页面大小
const int PAGESIZE = 20;
//音乐搜索API
//参数%1:搜索关键字
//参数%2: 起始位置,为(page-1)*20
const QString ApiOfSearch = "http://music.baidu.com/search?key=%1&start=%2&size=20&s=1";
//搜索建议API
//参数%1:歌曲id
const QString ApiOfSuggestion = "http://sug.music.baidu.com/info/suggestion?format=json&word=%1&version=2&from=0";
//歌曲信息API
//参数%1:歌曲id
const QString ApiOfSongInfo = "http://play.baidu.com/data/music/songinfo?songIds=%1";
//歌曲链接API
//参数%1:歌曲id
const QString ApiOfSongLink = "http://play.baidu.com/data/music/songlink?songIds=%1&type=m4a,mp3";
BaiduMusic::BaiduMusic(QObject *parent) : QObject(parent)
{
searchReply = 0;
suggestionReply = 0;
songInfoReply = 0;
songLinkReply = 0;
lyricReply = 0;
manager.setCookieJar(&cookieJar);
}
BaiduMusic::~BaiduMusic()
{
}
//搜索关键字
void BaiduMusic::search(const QString &keyword, int page)
{
//删除原来的响应
if(searchReply){
searchReply->deleteLater();
}
//起始位置
int start = (page-1)*PAGESIZE;
//构造请求链接url
QUrl url = QUrl(ApiOfSearch.arg(keyword).arg(start));
searchReply = manager.get(QNetworkRequest(url));
connect(searchReply,SIGNAL(finished()),this,SLOT(searchReplyFinished()));
}
void BaiduMusic::getSuggestion(QString keyword)
{
if(suggestionReply){
suggestionReply->deleteLater();
}
QUrl url = QUrl(ApiOfSuggestion.arg(keyword));
suggestionReply = manager.get(QNetworkRequest(url));
connect(suggestionReply,SIGNAL(finished()),this,SLOT(suggestionReplyFinished()));
}
void BaiduMusic::getSongInfo(QString songId)
{
if(songInfoReply){
songInfoReply->deleteLater();
}
QUrl url = QUrl(ApiOfSongInfo.arg(songId));
songInfoReply = manager.get(QNetworkRequest(url));
connect(songInfoReply,SIGNAL(finished()),this,SLOT(songInfoReplyFinished()));
}
void BaiduMusic::getSongLink(QString songId)
{
if(songLinkReply){
songLinkReply->deleteLater();
}
QUrl url = QUrl(ApiOfSongLink.arg(songId));
songLinkReply = manager.get(QNetworkRequest(url));
connect(songLinkReply,SIGNAL(finished()),this,SLOT(songLinkReplyFinished()));
}
void BaiduMusic::getLyric(QString url)
{
if(lyricReply){
lyricReply->deleteLater();
}
lyricReply = manager.get(QNetworkRequest(QUrl(url)));
connect(lyricReply,SIGNAL(finished()),this,SLOT(lyricReplyFinished()));
}
QString BaiduMusic::unifyResult(QString r)
{
return r.replace(QRegularExpression("songid|songId"),"sid")
.replace(QRegularExpression("author|artistname"),"singer")
.replace(QRegularExpression("songname|songName"),"sname");
}
void BaiduMusic::searchReplyFinished()
{
QString url = searchReply->request().url().toString();
int keywordBegin = url.indexOf("key=") + 4;
int keywordEnd = url.indexOf("&start=");
int pageBeginPos = url.indexOf("start=") + 6;
int pageEndPos = url.indexOf("&size=");
//当前页
int currentPage = url.mid(pageBeginPos,pageEndPos-pageBeginPos).toInt()/PAGESIZE + 1;
//关键字
QString keyword = url.mid(keywordBegin,keywordEnd-keywordBegin);
if(searchReply->error()){
//如果出错,pageCount为-1;
emit searchComplete(currentPage,1,keyword,"{error:"+searchReply->errorString()+"}");
return;
}
//TODO:未搜索到内容的判断
QString html = searchReply->readAll();
QStringList songList;
QRegularExpression re("<li data-songitem = '(.+?)'");
QRegularExpressionMatchIterator i = re.globalMatch(html);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString songData = match.captured(1);
//" 替换为 " ;删除<em>和</em>
songData = songData.replace(""","\"").replace("<em>","").replace("<\\/em>","");
songList << songData;
}
//构造json数组
QString songArray = "[" + songList.join(",") + "]";
QString result = unifyResult(songArray);
//匹配总页数
QRegularExpression pageCountRe("\">(\\d+)</a>\\s*<a class=\"page-navigator-next\"");
QRegularExpressionMatch match = pageCountRe.match(html);
//页面总数
int pageCount = match.captured(1).toInt();
//如果没有 pageCount,则 pageCount 设为 1;
pageCount = pageCount>0 ? pageCount : 1;
emit searchComplete(currentPage,pageCount,keyword,result);
}
void BaiduMusic::suggestionReplyFinished()
{
if(suggestionReply->error()){
emit getSuggestionComplete("{error:"+suggestionReply->errorString()+"}");
return;
}
QString sug = suggestionReply->readAll();
emit getSuggestionComplete(unifyResult(sug));
}
void BaiduMusic::songInfoReplyFinished()
{
if(songInfoReply->error()){
emit getSongInfoComplete("{error:" + songInfoReply->errorString() + "}");
return;
}
QString songinfo = songInfoReply->readAll();
emit getSongInfoComplete(songinfo);
}
void BaiduMusic::songLinkReplyFinished()
{
if(songLinkReply->error()){
emit getSongLinkComplete("{error:" + songLinkReply->errorString() + "}");
return;
}
QString songlink = songLinkReply->readAll();
emit getSongLinkComplete(unifyResult(songlink));
}
void BaiduMusic::lyricReplyFinished()
{
QString url = lyricReply->url().toString();
if(lyricReply->error()){
emit getLyricComplete(url, "");
return;
}
qDebug()<<lyricReply->rawHeaderList();
emit getLyricComplete(url, lyricReply->readAll());
}