TTKMusicPlayer  4.1.3.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  TTKSemaphoreLoop 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  QJson::Parser json;
52  bool ok = false;
53  const QVariant &data = json.parse(m_reply->readAll(), &ok);
54  if(ok)
55  {
56  QVariantMap value = data.toMap();
57  if(value["code"].toInt() == 200)
58  {
59  const QVariantMap &lValue = value["lrc"].toMap();
60  const QString &lrc = lValue["lyric"].toString();
61 
62  const QVariantMap &tValue = value["tlyric"].toMap();
63  const QString &tlrc = tValue["lyric"].toString();
64 
65  if(!lrc.isEmpty() && !tlrc.isEmpty())
66  {
67  QString text;
68  QStringList orts = lrc.split(TTK_LINEFEED);
69  QStringList trts = tlrc.split(TTK_LINEFEED);
70 
71  for(QString &ort : orts)
72  {
73  const QRegExp regx("\\[.+\\]");
74  const QString &prefix = (regx.indexIn(ort) != -1) ? regx.cap(0) : QString();
75 
76  if(prefix.isEmpty())
77  {
78  continue;
79  }
80 
81  text += ort.remove(prefix) + TTK_WLINEFEED;
82 
83  for(QString &trt : trts)
84  {
85  if(trt.startsWith(prefix))
86  {
87  text += trt.remove(prefix) + TTK_WLINEFEED;
88  break;
89  }
90  }
91  }
92 
93  Q_EMIT downLoadDataChanged(TTK_AUTHOR_NAME + text);
94  deleteAll();
95  return;
96  }
97  }
98  }
99  }
100 
101  TTK_ERROR_STREAM("Translation source data error");
102  Q_EMIT downLoadDataChanged({});
103  deleteAll();
104 }
virtual void deleteAll()
void downLoadDataChanged(const QString &bytes)
QNetworkReply * m_reply
#define TTK_AUTHOR_NAME
TTK_MODULE_EXPORT void setSslConfiguration(QNetworkRequest *request, QSslSocket::PeerVerifyMode mode=QSslSocket::VerifyNone)
The class of the translation words request.
int exec(ProcessEventsFlags flags=AllEvents)
virtual void downLoadFinished()
virtual void startToRequest(const QString &data) overridefinal
MusicWYTranslationRequest(QObject *parent=nullptr)
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:68
const QVariant header(const QString &key) const
static constexpr const char * WY_SONG_LRC_OLD_URL
QNetworkAccessManager m_manager
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:181
virtual void downLoadFinished() overridefinal
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
TTK_MODULE_EXPORT QString toString(Record type)
#define TTK_WLINEFEED
Definition: ttkglobal.h:199
virtual void startToSearch(const QString &value) override
#define TTK_LINEFEED
Definition: ttkglobal.h:198
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
const TTK::MusicSongInformationList & items() const
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:70
The class of the semaphore event loop.
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