TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicwytranslationrequest.cpp
Go to the documentation of this file.
3 #include "musicwyqueryrequest.h"
4 #include "musicotherdefine.h"
5 
8 {
9 
10 }
11 
13 {
14  TTK_INFO_STREAM(className() << __FUNCTION__);
15 
16  Q_UNUSED(data);
18 
19  TTKEventLoop loop;
20  MusicWYQueryRequest query(this), *d = &query;
21  connect(d, SIGNAL(downLoadDataChanged(QString)), &loop, SLOT(quit()));
23  d->startToSearch(QFileInfo(header("name").toString()).baseName());
24  loop.exec();
25 
26  if(d->isEmpty())
27  {
28  TTK_INFO_STREAM(className() << "downLoadFinished");
29  Q_EMIT downLoadDataChanged({});
30  deleteAll();
31  return;
32  }
33 
34  QNetworkRequest request;
35  request.setUrl(TTK::Algorithm::mdII(WY_SONG_LRC_OLD_URL, false).arg(d->items().front().m_songId));
36  TTK::setSslConfiguration(&request);
38 
39  m_reply = m_manager.get(request);
40  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
42 }
43 
45 {
46  TTK_INFO_STREAM(className() << __FUNCTION__);
47 
49  if(m_reply && m_reply->error() == QNetworkReply::NoError)
50  {
51  QJsonParseError ok;
52  const QJsonDocument &json = QJsonDocument::fromJson(m_reply->readAll(), &ok);
54  {
55  QVariantMap value = json.toVariant().toMap();
56  if(value["code"].toInt() == 200)
57  {
58  const QVariantMap &lValue = value["lrc"].toMap();
59  const QString &lrc = lValue["lyric"].toString();
60 
61  const QVariantMap &tValue = value["tlyric"].toMap();
62  const QString &tlrc = tValue["lyric"].toString();
63 
64  if(!lrc.isEmpty() && !tlrc.isEmpty())
65  {
66  QString text;
67  QStringList orts = lrc.split(TTK_LINEFEED);
68  QStringList trts = tlrc.split(TTK_LINEFEED);
69 
70  for(QString &ort : orts)
71  {
72  const QRegExp regx("\\[.+\\]");
73  const QString &prefix = (regx.indexIn(ort) != -1) ? regx.cap(0) : QString();
74 
75  if(prefix.isEmpty())
76  {
77  continue;
78  }
79 
80  text += ort.remove(prefix) + TTK_WLINEFEED;
81 
82  for(QString &trt : trts)
83  {
84  if(trt.startsWith(prefix))
85  {
86  text += trt.remove(prefix) + TTK_WLINEFEED;
87  break;
88  }
89  }
90  }
91 
92  Q_EMIT downLoadDataChanged(TTK_AUTHOR_NAME + text);
93  deleteAll();
94  return;
95  }
96  }
97  }
98  }
99 
100  TTK_ERROR_STREAM("Translation source data error");
101  Q_EMIT downLoadDataChanged({});
102  deleteAll();
103 }
const TTK::MusicSongInformationList & items() const noexcept
virtual void deleteAll()
void downLoadDataChanged(const QString &bytes)
void setQueryMode(QueryMode mode) noexcept
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone) noexcept
QNetworkReply * m_reply
#define TTK_AUTHOR_NAME
const QVariant header(const QString &key) const noexcept
QVariant toVariant() const
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=0)
int exec(ProcessEventsFlags flags=AllEvents)
The class of the translation words request.
virtual void downLoadFinished()
virtual void startToRequest(const QString &data) overridefinal
The class of the ttk event loop.
Definition: ttkeventloop.h:29
ParseError error
MusicWYTranslationRequest(QObject *parent=nullptr)
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={}) noexcept
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
static constexpr const char * WY_SONG_LRC_OLD_URL
QNetworkAccessManager m_manager
virtual void replyError(QNetworkReply::NetworkError error) override
#define TTK_SLOT
Definition: ttkqtglobal.h:177
virtual void downLoadFinished() overridefinal
TTK_MODULE_EXPORT QString toString(Record type) noexcept
#define TTK_WLINEFEED
Definition: ttkglobal.h:272
virtual void startToSearch(const QString &value) override
#define TTK_LINEFEED
Definition: ttkglobal.h:271
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
TTK_MODULE_EXPORT QString mdII(const QString &data, bool encode)
The class of the wangyi query download data from net.
#define QtNetworkErrorConnect(p, q, f, s)
Network error connect.
Definition: ttkqtcompat.h:59