TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musickgquerytoplistrequest.cpp
Go to the documentation of this file.
2 
3 static constexpr const char *KG_TOPLIST_INFO_URL = "b1QrYWx1TlJMWm00UHRMSUF3Ymcram5IWVRML1lXazdIR2I2RXVmZ0VzTUV3MjNEZG1nS003T2ZJV0c2Njl0NA==";
4 
7 {
10 }
11 
13 {
14  TTK_INFO_STREAM(metaObject()->className() << __FUNCTION__ << offset);
15 
16  deleteAll();
17  m_totalSize = 0;
19 
20  QNetworkRequest request;
21  request.setUrl(TTK::Algorithm::mdII(KG_TOPLIST_URL, false).arg(m_queryValue).arg(offset).arg(m_pageSize));
23 
24  m_reply = m_manager.get(request);
25  connect(m_reply, SIGNAL(finished()), SLOT(downloadFinished()));
27 }
28 
30 {
31  MusicQueryToplistRequest::startToSearch(value.isEmpty() ? "6666" : value);
32 }
33 
35 {
36  TTK_INFO_STREAM(metaObject()->className() << __FUNCTION__ << info->m_songId << bitrate << "kbps");
37 
42 
45 }
46 
48 {
49  TTK_INFO_STREAM(metaObject()->className() << __FUNCTION__);
50 
52  if(m_reply && m_reply->error() == QNetworkReply::NoError)
53  {
54  QJsonParseError ok;
55  const QJsonDocument &json = QJsonDocument::fromJson(m_reply->readAll(), &ok);
57  {
58  QVariantMap value = json.toVariant().toMap();
59  if(value.contains("data"))
60  {
61  value = value["data"].toMap();
62  m_totalSize = value["total"].toInt();
63 
64  queryToplistInfo(value);
65 
66  const QVariantList &datas = value["info"].toList();
67  for(const QVariant &var : qAsConst(datas))
68  {
69  if(var.isNull())
70  {
71  continue;
72  }
73 
74  value = var.toMap();
76 
78  info.m_songId = value["hash"].toString();
79  info.m_songName = TTK::String::charactersReplace(value["songname"].toString());
80 
81  const QVariantList &artistsArray = value["authors"].toList();
82  for(const QVariant &artistValue : qAsConst(artistsArray))
83  {
84  if(artistValue.isNull())
85  {
86  continue;
87  }
88 
89  const QVariantMap &artistObject = artistValue.toMap();
90  if(info.m_artistId.isEmpty())
91  {
92  info.m_artistId = artistObject["author_id"].toString();
93  }
94 
95  info.m_artistName = ReqKGInterface::makeSongArtist(info.m_artistName, artistObject["author_name"].toString());
96  }
97 
98  info.m_albumId = value["album_id"].toString();
99  info.m_albumName = TTK::String::charactersReplace(value["remark"].toString());
100 
101  info.m_coverUrl = value["album_sizable_cover"].toString().replace("{size}", "500");
102  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);
103  info.m_duration = TTKTime::formatDuration(value["duration"].toInt() * TTK_DN_S2MS);
104  info.m_year = TTKDateTime::format(value["addtime"].toULongLong(), TTK_YEAR_FORMAT);
105  info.m_trackNumber = "0";
106 
110 
111  Q_EMIT createResultItem({info, serverToString()});
112  m_items << info;
113  }
114  }
115  }
116  }
117 
118  Q_EMIT downloadDataChanged({});
119  deleteAll();
120 }
121 
122 void MusicKGQueryToplistRequest::queryToplistInfo(const QVariantMap &input)
123 {
124  Q_UNUSED(input);
125 
126  QNetworkRequest request;
127  request.setUrl(TTK::Algorithm::mdII(KG_TOPLIST_INFO_URL, false));
129 
130  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
131  if(bytes.isEmpty())
132  {
133  return;
134  }
135 
136  QJsonParseError ok;
137  const QJsonDocument &json = QJsonDocument::fromJson(bytes, &ok);
139  {
140  QVariantMap value = json.toVariant().toMap();
141  if(value.contains("data"))
142  {
143  value = value["data"].toMap();
144  MusicResultDataItem item;
145  item.m_time = TTKDateTime::format(value["timestamp"].toLongLong() * TTK_DN_S2MS, TTK_DATE_FORMAT);
146 
147  const QVariantList &datas = value["info"].toList();
148  for(const QVariant &var : qAsConst(datas))
149  {
150  if(var.isNull())
151  {
152  continue;
153  }
154 
155  value = var.toMap();
157 
158  if(m_queryValue != value["rankid"])
159  {
160  continue;
161  }
162 
163  item.m_name = value["rankname"].toString();
164  item.m_count = value["play_times"].toString();
165  item.m_description = value["intro"].toString();
166  item.m_coverUrl = value["banner7url"].toString().replace("{size}", "500");
167  Q_EMIT createToplistItem(item);
168  return;
169  }
170  }
171  }
172 }
TTK_MODULE_EXPORT QString charactersReplace(const QString &value)
The class of the query toplist data from net.
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate) overridefinal
#define TTK_NETWORK_QUERY_CHECK(VALUE)
The class of the search result data item.
virtual void deleteAll()
QNetworkReply * m_reply
void downloadDataChanged(const QString &bytes)
QVariant toVariant() const
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=0)
static constexpr const char * QUERY_KG_INTERFACE
#define TTK_PAGE_SIZE_30
MusicSongPropertyList m_songProps
Definition: musicobject.h:314
void createResultItem(const MusicResultInfoItem &result)
QString makeSongArtist(const QString &name)
TTK_MODULE_EXPORT QByteArray syncNetworkQueryForGet(QNetworkRequest *request)
#define TTK_YEAR_FORMAT
Definition: ttkglobal.h:305
virtual void startToPage(int offset) overridefinal
voidpf uLong offset
Definition: ioapi.h:142
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate)
ParseError error
TTK::MusicSongInformationList m_items
virtual void queryToplistInfo(const QVariantMap &input) overridefinal
#define qAsConst
Definition: ttkqtglobal.h:57
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
QNetworkAccessManager m_manager
static constexpr const char * KG_SONG_LRC_URL
MusicKGQueryToplistRequest(QObject *parent=nullptr)
virtual void startToSearch(const QString &value) override
bool fetchUrlPathSize(TTK::MusicSongProperty *prop, const QString &duration) const
virtual void replyError(QNetworkReply::NetworkError error) override
#define TTK_SLOT
Definition: ttkqtglobal.h:177
#define TTK_DATE_FORMAT
Definition: ttkglobal.h:309
static constexpr const char * KG_TOPLIST_INFO_URL
TTK_MODULE_EXPORT QString toString(Record type) noexcept
#define TTK_DN_S2MS
Definition: ttkglobal.h:355
virtual void downloadFinished()
void createToplistItem(const MusicResultDataItem &item)
static QString format(const QString &time, const QString &format)
Definition: ttktime.cpp:254
virtual void startToSearch(const QString &value) overridefinal
void parseFromSongProperty(TTK::MusicSongInformation *info, int bitrate)
static constexpr const char * KG_TOPLIST_URL
void makeRequestRawHeader(QNetworkRequest *request) noexcept
virtual void downloadFinished() overridefinal
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)
The class of the music song information.
Definition: musicobject.h:300
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123