TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musickgqueryinterface.cpp
Go to the documentation of this file.
3 
4 void ReqKGInterface::makeRequestRawHeader(QNetworkRequest *request)
5 {
6  request->setRawHeader("User-Agent", TTK::Algorithm::mdII(KG_UA_URL, ALG_UA_KEY, false).toUtf8());
9 }
10 
11 QString ReqKGInterface::makeSongArtist(const QString &name)
12 {
13  return TTK::String::charactersReplace(name).replace("+", ";");
14 }
15 
17 {
18  if(info->m_songId.isEmpty())
19  {
20  return;
21  }
22 
23  QNetworkRequest request;
24  request.setUrl(TTK::Algorithm::mdII(KG_SONG_INFO_URL, false).arg(info->m_songId));
26 
27  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
28  if(bytes.isEmpty())
29  {
30  return;
31  }
32 
33  QJson::Parser json;
34  bool ok = false;
35  const QVariant &data = json.parse(bytes, &ok);
36  if(ok)
37  {
38  QVariantMap value = data.toMap();
39  if(value["errcode"].toInt() == 0 && value.contains("data"))
40  {
41  value = value["data"].toMap();
42  info->m_songName = TTK::String::charactersReplace(value["songname"].toString());
43  info->m_artistId = value["singerid"].toString();
44  info->m_artistName = ReqKGInterface::makeSongArtist(value["singername"].toString());
45  info->m_coverUrl = value["imgurl"].toString().replace("{size}", "480");
46  info->m_lrcUrl = TTK::Algorithm::mdII(KG_SONG_LRC_URL, false).arg(value["songname"].toString(), info->m_songId).arg(value["duration"].toInt() * TTK_DN_S2MS);
47  }
48  }
49 }
50 
52 {
53  if(album.isEmpty())
54  {
55  return;
56  }
57 
59  parseFromSongAlbumInfo(&item, info->m_songId, album);
60 
61  info->m_albumId = item.m_id;
62  info->m_albumName = item.m_name;
63 }
64 
65 void ReqKGInterface::parseFromSongAlbumInfo(MusicResultDataItem *item, const QString &hash, const QString &album)
66 {
67  if(hash.isEmpty() || album.isEmpty())
68  {
69  return;
70  }
71 
72  QNetworkRequest request;
73  request.setUrl(TTK::Algorithm::mdII(KG_ALBUM_INFO_URL, false).arg(hash, album));
75 
76  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
77  if(bytes.isEmpty())
78  {
79  return;
80  }
81 
82  QJson::Parser json;
83  bool ok = false;
84  const QVariant &data = json.parse(bytes, &ok);
85  if(ok)
86  {
87  QVariantMap value = data.toMap();
88  if(value["error_code"].toInt() == 0 && value.contains("data"))
89  {
90  QVariantList datas = value["data"].toList();
91  for(const QVariant &var : qAsConst(datas))
92  {
93  if(var.isNull())
94  {
95  continue;
96  }
97 
98  for(const QVariant &al : var.toList())
99  {
100  if(al.isNull())
101  {
102  continue;
103  }
104 
105  value = al.toMap();
106  item->m_id = value["album_id"].toString();
107  item->m_name = TTK::String::charactersReplace(value["album_name"].toString());
108  item->m_description = value["publish_company"].toString();
109  item->m_updateTime = value["publish_date"].toString();
110  item->m_category = value["language"].toString();
111  break;
112  }
113  }
114  }
115  }
116 }
117 
118 static void parseSongProperty(TTK::MusicSongInformation *info, const QString &hash, int bitrate)
119 {
120  if(hash.isEmpty())
121  {
122  return;
123  }
124 
125  for(const TTK::MusicSongProperty &prop : qAsConst(info->m_songProps))
126  {
127  if(prop.m_bitrate == bitrate)
128  {
129  return;
130  }
131  }
132 
133  bool foundProp = false;
134  {
135  const QByteArray &encodedData = TTK::Algorithm::md5(QString("%1kgcloudv2").arg(hash).toUtf8());
136 
137  QNetworkRequest request;
138  request.setUrl(TTK::Algorithm::mdII(KG_SONG_DETAIL_URL, false).arg(hash, encodedData.constData()));
140 
141  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
142  if(!bytes.isEmpty())
143  {
144  QJson::Parser json;
145  bool ok = false;
146  const QVariant &data = json.parse(bytes, &ok);
147  if(ok)
148  {
149  const QVariantMap &value = data.toMap();
150  if(value.contains("status") && value["status"].toInt() == 1)
151  {
153  prop.m_url = value["url"].toString();
154  prop.m_size = TTK::Number::sizeByteToLabel(value["fileSize"].toInt());
155  prop.m_format = value["extName"].toString();
156  prop.m_bitrate = TTK::Number::bitrateToNormal(bitrate);
157  info->m_songProps.append(prop);
158  foundProp = true;
159  }
160  }
161  }
162  }
163 
164  if(!foundProp)
165  {
166  const QString &mid = TTK::Algorithm::mdII("Wk51dktMOHJXUTdmM1VsVUVXTFM5RTlYQ05laDE0Z2lZMzFPL1M1VUJSaHd1N0kwRDQxdkpWVFJPZTQ9", false);
167  const QString &sign = TTK::Algorithm::mdII("SVhlNmFTaWpqdVhYVTAwaHh4QllwRkFGSmJpY0VSZUhXQmQrV2Q4WHo0eXVCWm1zK1p0RkVRPT0=", false);
168  const QString &user = "0";
169  const QByteArray &encodedData = TTK::Algorithm::md5((hash + sign + mid + user).toUtf8());
170 
171  QNetworkRequest request;
172  request.setUrl(TTK::Algorithm::mdII(KG_SONG_DETAIL_OLD_URL, false).arg(mid, hash, user, encodedData.constData()));
173  request.setRawHeader("x-router", TTK::Algorithm::mdII("MTJnUGtpL0hqWXhZQmlCNE9hVzVyREF0QXZmeVBNNVc=", false).toUtf8());
175 
176  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
177  if(!bytes.isEmpty())
178  {
179  QJson::Parser json;
180  bool ok = false;
181  const QVariant &data = json.parse(bytes, &ok);
182  if(ok)
183  {
184  const QVariantMap &value = data.toMap();
185  if(value.contains("status") && value["status"].toInt() == 1)
186  {
188  prop.m_size = TTK::Number::sizeByteToLabel(value["fileSize"].toInt());
189  prop.m_format = value["extName"].toString();
190  prop.m_bitrate = TTK::Number::bitrateToNormal(bitrate);
191 
192  const QVariantList &datas = value["url"].toList();
193  if(!datas.isEmpty())
194  {
195  prop.m_url = datas.front().toString();
196  info->m_songProps.append(prop);
197  }
198  }
199  }
200  }
201  }
202 }
203 
204 
206 {
207  const QStringList &formats = info->m_formatProps.split("|");
208  if(info->m_formatProps.isEmpty() || formats.count() < 4)
209  {
210  parseSongProperty(info, info->m_songId, TTK_BN_128);
211  return;
212  }
213 
214  if(bitrate == TTK_BN_0)
215  {
216  parseSongProperty(info, formats[0], TTK_BN_128);
217  parseSongProperty(info, formats[1], TTK_BN_192);
218  parseSongProperty(info, formats[2], TTK_BN_320);
219  parseSongProperty(info, formats[3], TTK_BN_1000);
220  }
221  else
222  {
223  int index = 0;
224  switch(bitrate)
225  {
226  case TTK_BN_128: index = 0; break;
227  case TTK_BN_192: index = 1; break;
228  case TTK_BN_320: index = 2; break;
229  case TTK_BN_1000: index = 3; break;
230  }
231 
232  parseSongProperty(info, formats[index], bitrate);
233  }
234 }
235 
237 {
238  info->m_formatProps = key["hash"].toString() + "|" + key["128hash"].toString() + "|" + key["320hash"].toString() + "|" + key["sqhash"].toString();
239 }
static constexpr const char * KG_UA_URL
cookie url
TTK_MODULE_EXPORT QString charactersReplace(const QString &value)
TTK_MODULE_EXPORT QByteArray md5(const QByteArray &data)
The class of the search result data item.
static constexpr const char * KG_SONG_INFO_URL
The class of the music song property.
Definition: musicobject.h:223
static constexpr wchar_t key[]
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone)
MusicSongPropertyList m_songProps
Definition: musicobject.h:295
QString makeSongArtist(const QString &name)
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
TTK_MODULE_EXPORT QByteArray syncNetworkQueryForGet(QNetworkRequest *request)
#define TTK_BN_1000
Definition: ttkglobal.h:354
void makeRequestRawHeader(QNetworkRequest *request)
static constexpr const char * KG_SONG_DETAIL_OLD_URL
static constexpr const char * ALG_UA_KEY
#define qAsConst
Definition: ttkqtglobal.h:53
const char * name
Definition: http_parser.c:458
static void parseSongProperty(TTK::MusicSongInformation *info, const QString &hash, int bitrate)
static constexpr const char * KG_SONG_LRC_URL
#define TTK_BN_0
Definition: ttkglobal.h:344
#define TTK_BN_320
Definition: ttkglobal.h:351
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
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
TTK_MODULE_EXPORT QString toString(Record type)
static constexpr const char * KG_ALBUM_INFO_URL
void parseFromSongAlbumLrc(TTK::MusicSongInformation *info)
static constexpr const char * KG_SONG_DETAIL_URL
TTK_MODULE_EXPORT int bitrateToNormal(int bitrate)
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
void parseFromSongAlbumInfo(TTK::MusicSongInformation *info, const QString &album)
#define TTK_BN_192
Definition: ttkglobal.h:349
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 TTK_BN_128
Definition: ttkglobal.h:348