TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musickgqueryrequest.cpp
Go to the documentation of this file.
1 #include "musickgqueryrequest.h"
2 
4  : MusicQueryRequest(parent)
5 {
8 }
9 
11 {
12  TTK_INFO_STREAM(className() << "startToPage" << offset);
13 
14  deleteAll();
15  m_totalSize = 0;
17 
18  QNetworkRequest request;
19  request.setUrl(TTK::Algorithm::mdII(KG_SONG_SEARCH_URL, false).arg(m_queryValue).arg(offset + 1).arg(m_pageSize));
21 
22  m_reply = m_manager.get(request);
23  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
25 }
26 
27 void MusicKGQueryRequest::startToSearchByID(const QString &value)
28 {
29  TTK_INFO_STREAM(className() << "startToSearchByID" << value);
30 
31  deleteAll();
32 
33  QNetworkRequest request;
34  request.setUrl(TTK::Algorithm::mdII(KG_SONG_INFO_URL, false).arg(value));
36 
37  QNetworkReply *reply = m_manager.get(request);
38  connect(reply, SIGNAL(finished()), SLOT(downLoadSingleFinished()));
40 }
41 
43 {
44  TTK_INFO_STREAM(className() << "startToQueryResult" << info->m_songId << bitrate << "kbps");
45 
51 }
52 
54 {
55  TTK_INFO_STREAM(className() << "downLoadFinished");
56 
58  if(m_reply && m_reply->error() == QNetworkReply::NoError)
59  {
60  QJson::Parser json;
61  bool ok = false;
62  const QVariant &data = json.parse(m_reply->readAll(), &ok);
63  if(ok)
64  {
65  QVariantMap value = data.toMap();
66  if(value.contains("data"))
67  {
68  value = value["data"].toMap();
69  m_totalSize = value["total"].toInt();
70 
71  const QVariantList &datas = value["info"].toList();
72  for(const QVariant &var : qAsConst(datas))
73  {
74  if(var.isNull())
75  {
76  continue;
77  }
78 
79  value = var.toMap();
81 
83  info.m_songId = value["hash"].toString();
84  info.m_songName = TTK::String::charactersReplace(value["songname"].toString());
85 
86  info.m_artistName = ReqKGInterface::makeSongArtist(value["singername"].toString());
87 
88  info.m_albumId = value["album_id"].toString();
89  info.m_albumName = TTK::String::charactersReplace(value["album_name"].toString());
90 
91  info.m_duration = TTKTime::formatDuration(value["duration"].toInt() * TTK_DN_S2MS);
92  info.m_year.clear();
93  info.m_trackNumber = "0";
94 
98 
100  {
104 
105  Q_EMIT createResultItem({info, serverToString()});
106  }
107 
108  m_items << info;
109  }
110  }
111  }
112  }
113 
114  Q_EMIT downLoadDataChanged({});
115  deleteAll();
116 }
117 
119 {
120  TTK_INFO_STREAM(className() << "downLoadSingleFinished");
121 
123  QNetworkReply *reply = TTKObjectCast(QNetworkReply*, sender());
124  if(reply && reply->error() == QNetworkReply::NoError)
125  {
126  QJson::Parser json;
127  bool ok = false;
128  const QVariant &data = json.parse(reply->readAll(), &ok);
129  if(ok)
130  {
131  QVariantMap value = data.toMap();
132  if(value["errcode"].toInt() == 0 && value.contains("data"))
133  {
134  value = value["data"].toMap();
135 
137  info.m_songId = value["hash"].toString();
138  info.m_songName = TTK::String::charactersReplace(value["songname"].toString());
139 
140  info.m_artistId = value["singerid"].toString();
141  info.m_artistName = ReqKGInterface::makeSongArtist(value["singername"].toString());
142 
143  const QVariantList &albumArray = value["album"].toList();
144  for(const QVariant &var : qAsConst(albumArray))
145  {
146  if(var.isNull())
147  {
148  continue;
149  }
150 
151  const QVariantMap &albumObject = var.toMap();
153  ReqKGInterface::parseFromSongAlbumInfo(&info, albumObject["album_audio_id"].toString());
155  }
156 
157  info.m_coverUrl = value["imgurl"].toString().replace("{size}", "480");
158  info.m_lrcUrl = TTK::Algorithm::mdII(KG_SONG_LRC_URL, false).arg(info.m_songName, info.m_songId).arg(value["duration"].toInt() * TTK_DN_S2MS);
159  info.m_duration = TTKTime::formatDuration(value["duration"].toInt() * TTK_DN_S2MS);
160  info.m_year.clear();
161  info.m_trackNumber = "0";
162 
164  ReqKGInterface::parseFromSongProperty(&info, value["extra"].toMap());
166 
167  Q_EMIT createResultItem({info, serverToString()});
168  m_items << info;
169  }
170  }
171  }
172 
173  Q_EMIT downLoadDataChanged({});
174  deleteAll();
175 }
TTK_MODULE_EXPORT QString charactersReplace(const QString &value)
#define TTK_NETWORK_QUERY_CHECK(VALUE)
virtual void deleteAll()
static constexpr const char * KG_SONG_INFO_URL
void downLoadDataChanged(const QString &bytes)
QNetworkReply * m_reply
static constexpr const char * QUERY_KG_INTERFACE
QString makeSongArtist(const QString &name)
virtual void downLoadFinished()
void makeRequestRawHeader(QNetworkRequest *request)
voidpf uLong offset
Definition: ioapi.h:142
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate)
TTK::MusicSongInformationList m_items
#define qAsConst
Definition: ttkqtglobal.h:53
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate) overridefinal
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
QNetworkAccessManager m_manager
void createResultItem(const MusicResultInfoItem &songItem)
static constexpr const char * KG_SONG_LRC_URL
static constexpr const char * KG_SONG_SEARCH_URL
song url
virtual void replyError(QNetworkReply::NetworkError error) override
QVariant parse(QIODevice *io, bool *ok=0)
Read JSON string from the I/O Device and converts it to a QVariant object.
Definition: parser.cpp:69
#define TTK_SLOT
Definition: ttkqtglobal.h:165
virtual void downLoadFinished() override
virtual void downLoadFinished() overridefinal
#define SONG_PAGE_SIZE
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
MusicKGQueryRequest(QObject *parent=nullptr)
TTK_MODULE_EXPORT QString toString(Record type)
void parseFromSongAlbumLrc(TTK::MusicSongInformation *info)
virtual void startToPage(int offset) overridefinal
virtual void startToSearchByID(const QString &value) overridefinal
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
void parseFromSongAlbumInfo(TTK::MusicSongInformation *info, const QString &album)
void parseFromSongProperty(TTK::MusicSongInformation *info, int bitrate)
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)
The class of the music song information.
Definition: musicobject.h:281
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123
#define TTKObjectCast(x, y)
Definition: ttkqtglobal.h:60
The class of the query download data from net.