-
Notifications
You must be signed in to change notification settings - Fork 2
/
network.cpp
217 lines (194 loc) · 6.89 KB
/
network.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
212
213
214
215
216
217
#include "network.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
Network::Network(QObject *parent) :
QObject(parent)
{
this->networkAccessManager = new QNetworkAccessManager(this);
}
Network::~Network()
{
delete networkAccessManager;
}
void Network::getContentOfURL(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleGetFinished()));
}
void Network::fetchChannels(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleFetchChannelsFinished()));
}
void Network::fetchMediaInfoList(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleFetchMediaInfoListFinished()));
}
void Network::fetchSongInfo(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleFetchSongInfoFinished()));
}
void Network::fetchImage(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleFetchImageFinished()));
}
void Network::fetchLyrics(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "KylinPlayer 1.0");
QNetworkReply *reply = this->networkAccessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleFetchLyricsFinished()));
}
void Network::handleGetFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
qDebug() << strLocation;
this->getContentOfURL(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
emit this->contentGetted(new QByteArray(strHttpData));
}
void Network::handleFetchChannelsFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
// qDebug() << strLocation;
this->fetchChannels(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(strHttpData);
if(jsonDoc.isNull() ||!jsonDoc.isArray())
{// null or not array format
return;
}
QJsonArray channels = jsonDoc.array();
QList<KChannel> *channelList = new QList<KChannel>();
for(int i = 0; i < channels.count(); i++)
{
QJsonValue value = channels[i];
QString id = value.toObject().value("id").toString();
QString title = value.toObject().value("title").toString();
// qDebug()<<id<<"----"<<title;
KChannel channel;
channel.id = id;
channel.title = title;
channelList->append(channel);
}
emit this->musicChannelsFetched(channelList);
}
void Network::handleFetchMediaInfoListFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
qDebug() << strLocation;
this->fetchMediaInfoList(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(strHttpData);
if(jsonDoc.isNull() ||!jsonDoc.isObject())
{// null or not object format
return;
}
QJsonObject jsonObj = jsonDoc.object();
QJsonArray mediaArray = jsonObj.value("list").toArray();
QList<KMediaInfo> *mediaList = new QList<KMediaInfo>();
for(int i = 0; i < mediaArray.count(); i++)
{
KMediaInfo mediaInfo;
QJsonObject jsonObj2 = mediaArray[i].toObject();
mediaInfo.id = jsonObj2.value("id").toInt();
mediaList->append(mediaInfo);
// qDebug()<<"id:"<<mediaInfo.id;
}
emit this->mediaInfoListFetched(mediaList);
}
void Network::handleFetchSongInfoFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
qDebug() << strLocation;
this->fetchMediaInfoList(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
QJsonDocument jsonDoc = QJsonDocument::fromJson(strHttpData);
if(jsonDoc.isNull() ||!jsonDoc.isObject())
{// null or not object format
return;
}
QJsonObject jsonObj = jsonDoc.object();
QJsonArray songArray = jsonObj.value("data").toObject().value("songList").toArray();
if(songArray.count()<=0)
{
return;
}
QJsonObject infoObj = songArray.first().toObject();
KMediaInfo mediaInfo;
mediaInfo.id = infoObj.value("songId").toInt();
mediaInfo.songName = infoObj.value("songName").toString();
mediaInfo.artistName = infoObj.value("artistName").toString();
mediaInfo.albumName = infoObj.value("albumName").toString();
mediaInfo.songPicUrl = infoObj.value("songPicRadio").toString();
mediaInfo.mp3Url = infoObj.value("songLink").toString();
mediaInfo.lrcUrl = infoObj.value("lrcLink").toString();
emit this->mediaInfoFetched(mediaInfo);
}
void Network::handleFetchImageFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
qDebug() << strLocation;
this->fetchMediaInfoList(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
emit this->imageDataFetched(new QByteArray(strHttpData));
}
void Network::handleFetchLyricsFinished()
{
QNetworkReply *reply = (QNetworkReply *)sender();
if(reply->hasRawHeader("Location")){
QString strLocation = reply->header(QNetworkRequest::LocationHeader).toString();
qDebug() << strLocation;
this->fetchMediaInfoList(strLocation);
return;
}
QByteArray strHttpData;
strHttpData = reply->readAll();
emit this->lyricsDataFetched(new QByteArray(strHttpData));
}