TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicunityqueryinterface.cpp
Go to the documentation of this file.
2 
3 static constexpr const char *QUERY_PLUGINS_URL = "resource/plugins";
4 static constexpr const char *QUERY_MODULE_A = "A";
5 static constexpr const char *QUERY_MODULE_B = "B";
6 
7 static QString makeQualityValue(const QString &type, int bitrate)
8 {
9  if(type == QUERY_MODULE_A)
10  {
11  switch(bitrate)
12  {
13  case TTK_BN_128: return "128k";
14  case TTK_BN_320: return "320k";
15  case TTK_BN_1000: return "flac";
16  default: return {};
17  }
18  }
19  else if(type == QUERY_MODULE_B)
20  {
21  switch(bitrate)
22  {
23  case TTK_BN_128: return "128";
24  case TTK_BN_320: return "320";
25  case TTK_BN_1000: return "999";
26  default: return {};
27  }
28  }
29  return {};
30 }
31 
32 static QString makeModuleValue(const QString &type, const QString &module)
33 {
34  if(type == QUERY_MODULE_A)
35  {
36  if(module == QUERY_WY_INTERFACE) return "wy";
37  else if(module == QUERY_KG_INTERFACE) return "kg";
38  else if(module == QUERY_KW_INTERFACE) return "kw";
39  else return {};
40  }
41  else if(type == QUERY_MODULE_B)
42  {
43  if(module == QUERY_WY_INTERFACE) return "wangyi";
44  else if(module == QUERY_KG_INTERFACE) return "kugou";
45  else if(module == QUERY_KW_INTERFACE) return "kuwo";
46  else return {};
47  }
48  return {};
49 }
50 
51 //OVpCbTNnaHljODBhZ0hHUzNvejBGTmFJREdXVzRWWDJwREw4ckNSZGNrbz0=
52 //Ukg1OFg2VUIzWDVQbWxYSkdYUHY1d21mOVZ3SlpERHhFYjFyQzREb29NTUFTZW55eVN1YVRnPT0=
53 static void parseSongPropertyA(TTK::MusicSongInformation *info, const QString &key, const QString &url, const QString &quality, int bitrate)
54 {
55  for(const TTK::MusicSongProperty &prop : qAsConst(info->m_songProps))
56  {
57  if(prop.m_bitrate == bitrate)
58  {
59  return;
60  }
61  }
62 
63  TTK_INFO_STREAM("Parse song in A module, url:" << url);
64 
65  QNetworkRequest request;
66  request.setRawHeader("X-Request-Key", TTK::Algorithm::mdII(key, false).toUtf8());
67  request.setUrl(url);
68  TTK::setSslConfiguration(&request);
69 
70  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
71  if(bytes.isEmpty())
72  {
73  return;
74  }
75 
76  QJson::Parser json;
77  bool ok = false;
78  const QVariant &data = json.parse(bytes, &ok);
79  if(ok)
80  {
81  QVariantMap value = data.toMap();
82  if(value["code"].toInt() == 0)
83  {
85  prop.m_url = value["data"].toString();
86  prop.m_size = TTK_DEFAULT_STR;
88  prop.m_bitrate = bitrate;
89 
90  if(prop.isEmpty())
91  {
92  prop.m_url = value["url"].toString();
93  }
94 
95  if(prop.isEmpty())
96  {
97  return;
98  }
99 
100  value = value["extra"].toMap();
101  if(value.isEmpty())
102  {
103  return;
104  }
105 
106  value = value["quality"].toMap();
107  if(value.isEmpty())
108  {
109  return;
110  }
111 
112  if(value["target"].toString().contains(quality, Qt::CaseInsensitive) && value["result"].toString().contains(quality, Qt::CaseInsensitive))
113  {
114  info->m_songProps.append(prop);
115  }
116  }
117  }
118 }
119 
120 //aFB0VHBaY3lpMHNKWTBjVk5Dd3JrZlpkeVRYamhnNDVPVHR0dlZhSTFDVW5rWm5PVjJtcWhrWVJweU09
121 static void parseSongPropertyB(TTK::MusicSongInformation *info, const QString &url, int bitrate)
122 {
123  for(const TTK::MusicSongProperty &prop : qAsConst(info->m_songProps))
124  {
125  if(prop.m_bitrate == bitrate)
126  {
127  return;
128  }
129  }
130 
131  TTK_INFO_STREAM("Parse song in B module, url:" << url);
132 
133  QNetworkRequest request;
134  request.setUrl(url);
135  TTK::setSslConfiguration(&request);
136 
137  const QByteArray &bytes = TTK::syncNetworkQueryForGet(&request);
138  if(bytes.isEmpty())
139  {
140  return;
141  }
142 
143  QJson::Parser json;
144  bool ok = false;
145  const QVariant &data = json.parse(bytes, &ok);
146  if(ok)
147  {
148  QVariantMap value = data.toMap();
149  if(value.contains("url"))
150  {
152  prop.m_url = value["url"].toString();
153  prop.m_size = TTK_DEFAULT_STR;
155  prop.m_bitrate = bitrate;
156 
157  if(!prop.isEmpty())
158  {
159  info->m_songProps.append(prop);
160  }
161  }
162  }
163 }
164 
165 
166 void ReqUnityInterface::parseFromSongProperty(TTK::MusicSongInformation *info, const QString &type, const QString &id, int bitrate)
167 {
168  QByteArray bytes;
169  QFile file(APPCACHE_DIR_FULL + QUERY_PLUGINS_URL);
170  if(file.open(QIODevice::ReadOnly))
171  {
172  TTK_INFO_STREAM("Load unity plugins using local resource config");
173  bytes = file.readAll();
174  file.close();
175  }
176  else
177  {
178  TTK_ERROR_STREAM("Load unity plugins resource config failed");
179  return;
180  }
181 
182  QJson::Parser json;
183  bool ok = false;
184  const QVariant &data = json.parse(bytes, &ok);
185  if(ok)
186  {
187  const QVariantList &datas = data.toList();
188  for(const QVariant &var : qAsConst(datas))
189  {
190  if(var.isNull())
191  {
192  continue;
193  }
194 
195  const QVariantMap &value = var.toMap();
196  const QString &key = value["key"].toString();
197  const bool option = value["option"].toBool();
198 
199  if(!option)
200  {
201  continue;
202  }
203 
204  for(const QString &module : value.keys())
205  {
206  const QString &url = value[module].toString();
207  if(url.isEmpty())
208  {
209  continue;
210  }
211 
212  const QString &server = makeModuleValue(module, type);
213  const QString &quality = makeQualityValue(module, bitrate);
214 
215  if(server.isEmpty() || quality.isEmpty())
216  {
217  continue;
218  }
219 
220  if(module == QUERY_MODULE_A)
221  {
222  parseSongPropertyA(info, key, TTK::Algorithm::mdII(url, false).arg(server, id, quality), quality, bitrate);
223  }
224  else if(module == QUERY_MODULE_B)
225  {
226  parseSongPropertyB(info, TTK::Algorithm::mdII(url, false).arg(server, id, quality), bitrate);
227  }
228  }
229  }
230  }
231 }
#define TTK_DEFAULT_STR
Definition: ttkglobal.h:203
static QString makeQualityValue(const QString &type, int bitrate)
The class of the music song property.
Definition: musicobject.h:225
static constexpr const char * QUERY_PLUGINS_URL
static constexpr const char * QUERY_KG_INTERFACE
static constexpr wchar_t key[]
static constexpr const char * QUERY_MODULE_B
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone)
static void parseSongPropertyB(TTK::MusicSongInformation *info, const QString &url, int bitrate)
static QString makeModuleValue(const QString &type, const QString &module)
MusicSongPropertyList m_songProps
Definition: musicobject.h:297
TTK_MODULE_EXPORT QByteArray syncNetworkQueryForGet(QNetworkRequest *request)
#define TTK_BN_1000
Definition: ttkglobal.h:359
#define MP3_FILE_SUFFIX
Definition: musicobject.h:51
#define APPCACHE_DIR_FULL
Definition: musicobject.h:123
#define qAsConst
Definition: ttkqtglobal.h:51
static constexpr const char * QUERY_WY_INTERFACE
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:68
bool isEmpty() const noexcept
Definition: musicobject.h:238
static constexpr const char * QUERY_MODULE_A
#define TTK_BN_320
Definition: ttkglobal.h:356
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
static constexpr const char * QUERY_KW_INTERFACE
TTK_MODULE_EXPORT QString toString(Record type)
static void parseSongPropertyA(TTK::MusicSongInformation *info, const QString &key, const QString &url, const QString &quality, int bitrate)
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
#define FLAC_FILE_SUFFIX
Definition: musicobject.h:53
void parseFromSongProperty(TTK::MusicSongInformation *info, const QString &type, const QString &id, int bitrate)
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:70
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)
The class of the music song information.
Definition: musicobject.h:283
#define TTK_BN_128
Definition: ttkglobal.h:353