TTKMusicPlayer  3.7.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() << "startToRequest");
15 
16  Q_UNUSED(data);
18 
19  TTKSemaphoreLoop loop;
20  MusicWYQueryRequest query(this), *d = &query;
21  connect(d, SIGNAL(downLoadDataChanged(QString)), &loop, SLOT(quit()));
24  d->startToSearch(QFileInfo(header("name").toString()).baseName());
25  loop.exec();
26 
27  if(d->isEmpty())
28  {
29  TTK_INFO_STREAM(className() << "downLoadFinished");
30  Q_EMIT downLoadDataChanged({});
31  deleteAll();
32  return;
33  }
34 
35  QNetworkRequest request;
36  request.setUrl(TTK::Algorithm::mdII(WY_SONG_LRC_OLD_URL, false).arg(d->items().front().m_songId));
37  TTK::setSslConfiguration(&request);
39 
40  m_reply = m_manager.get(request);
41  connect(m_reply, SIGNAL(finished()), SLOT(downLoadFinished()));
43 }
44 
46 {
47  TTK_INFO_STREAM(className() << "downLoadFinished");
48 
50  if(m_reply && m_reply->error() == QNetworkReply::NoError)
51  {
52  QJson::Parser json;
53  bool ok = false;
54  const QVariant &data = json.parse(m_reply->readAll(), &ok);
55  if(ok)
56  {
57  QVariantMap value = data.toMap();
58  if(value["code"].toInt() == 200)
59  {
60  const QVariantMap &lValue = value["lrc"].toMap();
61  const QString &lrc = lValue["lyric"].toString();
62 
63  const QVariantMap &tValue = value["tlyric"].toMap();
64  const QString &tlrc = tValue["lyric"].toString();
65 
66  if(!lrc.isEmpty() && !tlrc.isEmpty())
67  {
68  QString text;
69  QStringList orts = lrc.split("\n");
70  QStringList trts = tlrc.split("\n");
71 
72  for(QString &ort : orts)
73  {
74  const QRegExp regx("\\[.+\\]");
75  const QString &prefix = (regx.indexIn(ort) != -1) ? regx.cap(0) : QString();
76 
77  if(prefix.isEmpty())
78  {
79  continue;
80  }
81 
82  text += ort.remove(prefix) + "\r\n";
83 
84  for(QString &trt : trts)
85  {
86  if(trt.startsWith(prefix))
87  {
88  text += trt.remove(prefix) + "\r\n";
89  break;
90  }
91  }
92  }
93 
94  Q_EMIT downLoadDataChanged(TTK_AUTHOR_NAME + text);
95  deleteAll();
96  return;
97  }
98  }
99  }
100  }
101 
102  TTK_ERROR_STREAM("Translation source data error");
103  Q_EMIT downLoadDataChanged({});
104  deleteAll();
105 }
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:67
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:165
virtual void downLoadFinished() overridefinal
TTK_MODULE_EXPORT void makeContentTypeHeader(QNetworkRequest *request, const QByteArray &data={})
TTK_MODULE_EXPORT QString toString(Record type)
virtual void startToSearch(const QString &value) override
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:69
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