TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicsongrecommendrequest.cpp
Go to the documentation of this file.
2 
6 namespace ReqLQInterface
7 {
8  static constexpr const char *LQ_BASE_URL = "VzJWczlXM2hMeCtTZzhLRFJvZWxUUTVmZUVBLzlMWmo=";
9  static constexpr const char *LQ_RECOMMEND_URL = "NDhiOGZ6dUJWNTBvN3R5OHNOQmkyQVVwOXdWbDNBOG14MmVXWVJxWlVXRkxuNUxxdzdYTEpUYVZRNVE9";
10 
14  static void parseFromSongProperty(TTK::MusicSongInformation *info, const QVariantMap &key);
18  static void parseFromSongProperty(TTK::MusicSongInformation *info, const QString &key, int length, int bitrate);
19 
20 }
21 
23 {
24  const int length = key["length"].toInt();
25 
26  parseFromSongProperty(info, key["lqUrl"].toString(), length, TTK_BN_128);
27  parseFromSongProperty(info, key["hqUrl"].toString(), length, TTK_BN_192);
28  parseFromSongProperty(info, key["sqUrl"].toString(), length, TTK_BN_320);
29  parseFromSongProperty(info, key["apeUrl"].toString(), length, TTK_BN_750);
30  parseFromSongProperty(info, key["flacUrl"].toString(), length, TTK_BN_1000);
31 
32  if(info->m_songProps.isEmpty())
33  {
34  parseFromSongProperty(info, key["copyUrl"].toString(), length, TTK_BN_320);
35  }
36 }
37 
38 void ReqLQInterface::parseFromSongProperty(TTK::MusicSongInformation *info, const QString &key, int length, int bitrate)
39 {
40  if(key.isEmpty())
41  {
42  return;
43  }
44 
47  prop.m_size = TTK::Number::sizeByteToLabel(length * 1000 * bitrate / 8);
48  prop.m_bitrate = bitrate;
49 
50  switch(bitrate)
51  {
52  case TTK_BN_128: prop.m_format = MP3_FILE_SUFFIX; break;
53  case TTK_BN_192: prop.m_format = MP3_FILE_SUFFIX; break;
54  case TTK_BN_320: prop.m_format = MP3_FILE_SUFFIX; break;
55  case TTK_BN_750: prop.m_format = APE_FILE_SUFFIX; break;
56  case TTK_BN_1000: prop.m_format = FLAC_FILE_SUFFIX; break;
57  default: prop.m_format = MP3_FILE_SUFFIX; break;
58  }
59  info->m_songProps.append(prop);
60 }
61 
62 
63 
66 {
68 }
69 
70 void MusicSongRecommendRequest::startToSearch(const QString &value)
71 {
72  TTK_INFO_STREAM(className() << "startToSearch" << value);
73 
74  deleteAll();
75 
76  QNetworkRequest request;
78  TTK::setSslConfiguration(&request);
80 
81  m_reply = m_manager.get(request);
82  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
84 }
85 
87 {
88  TTK_INFO_STREAM(className() << "startToQueryResult" << info->m_songId << bitrate << "kbps");
89 
91  for(const TTK::MusicSongInformation &var : m_items)
92  {
93  if(info->m_songId == var.m_songId)
94  {
95  info->m_songProps = var.m_songProps;
96  break;
97  }
98  }
99 }
100 
102 {
103  TTK_INFO_STREAM(className() << "downLoadFinished");
104 
106  if(m_reply && m_reply->error() == QNetworkReply::NoError)
107  {
108  QJson::Parser json;
109  bool ok = false;
110  const QVariant &data = json.parse(m_reply->readAll(), &ok);
111  if(ok)
112  {
113  const QVariantList &datas = data.toList();
114  for(const QVariant &var : qAsConst(datas))
115  {
116  if(var.isNull())
117  {
118  continue;
119  }
120 
121  if(m_items.count() >= m_totalSize)
122  {
123  break;
124  }
125 
126  QVariantMap value = var.toMap();
128 
130  info.m_songId = value["id"].toString();
131  info.m_songName = TTK::String::charactersReplace(value["name"].toString());
132 
133  const QVariantList &artistsArray = value["artistInfo"].toList();
134  for(const QVariant &artistValue : qAsConst(artistsArray))
135  {
136  if(artistValue.isNull())
137  {
138  continue;
139  }
140 
141  const QVariantMap &artistObject = artistValue.toMap();
142  if(info.m_artistId.isEmpty())
143  {
144  info.m_artistId = artistObject["id"].toString();
145  }
146 
147  const QString &artistName = TTK::String::charactersReplace(artistObject["name"].toString());
148  info.m_artistName = info.m_artistName.isEmpty() ? artistName : (info.m_artistName + ";" + artistName);
149  }
150 
151  const QVariantMap &albumObject = value["albumInfo"].toMap();
152  info.m_albumId = albumObject["id"].toString();
153  info.m_albumName = TTK::String::charactersReplace(albumObject["name"].toString());
154 
155  info.m_coverUrl = value["picUrl"].toString();
156  info.m_lrcUrl = TTK::Algorithm::mdII(ReqLQInterface::LQ_BASE_URL, false) + value["lrcUrl"].toString();
157  info.m_duration = value["songLength"].toString();
158  info.m_year = value["year"].toString();
159  info.m_trackNumber = value["trackNum"].toString();
160 
164 
165  if(info.m_songProps.isEmpty())
166  {
167  continue;
168  }
169 
170  Q_EMIT createResultItem({info, serverToString()});
171  m_items << info;
172  }
173  }
174  }
175 
176  Q_EMIT downLoadDataChanged({});
177  deleteAll();
178 }
TTK_MODULE_EXPORT QString charactersReplace(const QString &value)
#define TTK_NETWORK_QUERY_CHECK(VALUE)
virtual void deleteAll()
void downLoadDataChanged(const QString &bytes)
The namespace of the lq request interface.
QNetworkReply * m_reply
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)
static void parseFromSongProperty(TTK::MusicSongInformation *info, const QString &key, int length, int bitrate)
MusicSongPropertyList m_songProps
Definition: musicobject.h:295
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
#define TTK_BN_1000
Definition: ttkglobal.h:354
virtual void downLoadFinished()
#define MP3_FILE_SUFFIX
Definition: musicobject.h:50
static constexpr const char * LQ_BASE_URL
static void parseFromSongProperty(TTK::MusicSongInformation *info, const QVariantMap &key)
TTK::MusicSongInformationList m_items
#define qAsConst
Definition: ttkqtglobal.h:53
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
QNetworkAccessManager m_manager
void createResultItem(const MusicResultInfoItem &songItem)
#define TTK_BN_320
Definition: ttkglobal.h:351
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
MusicSongRecommendRequest(QObject *parent=nullptr)
virtual void downLoadFinished() overridefinal
virtual void startToQueryResult(TTK::MusicSongInformation *info, int bitrate) overridefinal
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
#define SONG_PAGE_SIZE
TTK_MODULE_EXPORT QString toString(Record type)
static constexpr const char * LQ_RECOMMEND_URL
virtual void startToSearch(const QString &value) overridefinal
The class of the abstract query download data from net.
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
#define TTK_BN_192
Definition: ttkglobal.h:349
#define FLAC_FILE_SUFFIX
Definition: musicobject.h:52
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)
#define APE_FILE_SUFFIX
Definition: musicobject.h:51
The class of the music song information.
Definition: musicobject.h:281
#define TTK_BN_750
Definition: ttkglobal.h:353
#define TTK_BN_128
Definition: ttkglobal.h:348
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59