-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlyricloader.cpp
229 lines (191 loc) · 5.29 KB
/
lyricloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "lyricloader.h"
#include <QFile>
#include <QTextStream>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtDeclarative>
#include <QDebug>
#include "qjson/parser.h"
class LyricLine
{
public:
LyricLine():time(0){}
LyricLine(int time, QString text):time(time), text(text){}
int time;
QString text;
};
LyricLoader::LyricLoader(QObject *parent) : QObject(parent),
mParser(new QJson::Parser), mNetworkManager(0), mHasTimer(false),
isComponentComplete(false)
{
}
LyricLoader::~LyricLoader()
{
delete mParser;
}
void LyricLoader::classBegin()
{
mNetworkManager = qmlEngine(this)->networkAccessManager();
}
void LyricLoader::componentComplete()
{
isComponentComplete = true;
}
bool LyricLoader::loadFromFile(const QString &fileName)
{
reset();
QFile file(fileName);
if (file.size() && file.open(QIODevice::ReadOnly)) {
QTextStream stream(&file);
stream.setCodec("UTF-8");
stream.setAutoDetectUnicode(true);
return processContent(stream.readAll());
}
return false;
}
void LyricLoader::loadFromMusicId(const QString &musicId)
{
if (!isComponentComplete) return;
reset();
QUrl url("http://music.163.com/api/song/lyric");
url.addEncodedQueryItem("os", "pc");
url.addEncodedQueryItem("id", musicId.toAscii());
url.addEncodedQueryItem("lv", "-1");
url.addEncodedQueryItem("kv", "-1");
url.addEncodedQueryItem("tv", "-1");
mReply = mNetworkManager->get(QNetworkRequest(url));
connect(mReply, SIGNAL(finished()), SLOT(requestFinished()), Qt::QueuedConnection);
emit loadingChanged();
}
void LyricLoader::saveToFile(const QString &fileName)
{
if (mLines.isEmpty() || mRawData.isEmpty())
return;
QFile file(fileName);
if (file.exists() && !file.remove())
return;
if (!file.open(QIODevice::WriteOnly))
return;
QTextStream stream(&file);
stream.setCodec("UTF-8");
stream << mRawData;
}
int LyricLoader::getLineByPosition(const int &millisec, const int &startPos) const
{
if (!mHasTimer || mLines.isEmpty())
return -1;
int result = qBound(0, startPos, mLines.size() - 1);
if (mLines.at(result)->time <= millisec) {
while (++result < mLines.size() && mLines.at(result)->time <= millisec);
return result - 1;
}
else {
while (--result >= 0 && mLines.at(result)->time > millisec);
return result;
}
}
bool LyricLoader::dataAvailable() const
{
return !mLines.isEmpty() && !mRawData.isEmpty();
}
QStringList LyricLoader::lyric() const
{
QStringList list;
foreach (LyricLine* line, mLines)
list.append(line->text);
return list;
}
bool LyricLoader::hasTimer() const
{
return mHasTimer;
}
bool LyricLoader::loading() const
{
return mReply && mReply->isRunning();
}
void LyricLoader::reset()
{
if (mReply && mReply->isRunning()) {
mReply->abort();
mReply = 0;
emit loadingChanged();
}
else {
mReply = 0;
}
mRawData.clear();
if (!mLines.isEmpty()) {
qDeleteAll(mLines);
mLines.clear();
mHasTimer = false;
emit lyricChanged();
}
}
bool lyricTimeLessThan(const LyricLine* line1, const LyricLine* line2)
{
return line1->time < line2->time;
}
bool LyricLoader::processContent(const QString &content)
{
if (!mLines.isEmpty()) {
qDeleteAll(mLines);
mLines.clear();
mHasTimer = false;
emit lyricChanged();
}
const QRegExp rx("\\[(\\d+):(\\d+(\\.\\d+)?)\\]");
mRawData = content;
int pos = rx.indexIn(content);
if (pos == -1) {
QStringList list = content.split('\n', QString::SkipEmptyParts);
foreach (QString line, list)
mLines.append(new LyricLine(0, line));
mHasTimer = false;
}
else {
int lastPos;
QList<int> timeLabels;
do {
timeLabels << (rx.cap(1).toInt() * 60 + rx.cap(2).toDouble()) * 1000;
lastPos = pos + rx.matchedLength();
pos = rx.indexIn(content, lastPos);
if (pos == -1) {
QString text = content.mid(lastPos).trimmed();
foreach (const int& time, timeLabels)
mLines.append(new LyricLine(time, text));
break;
}
QString text = content.mid(lastPos, pos - lastPos);
if (!text.isEmpty()) {
foreach (const int& time, timeLabels)
mLines.append(new LyricLine(time, text.trimmed()));
timeLabels.clear();
}
}
while (true);
qStableSort(mLines.begin(), mLines.end(), lyricTimeLessThan);
mHasTimer = true;
}
if (!mLines.isEmpty()) {
emit lyricChanged();
return true;
}
return false;
}
void LyricLoader::requestFinished()
{
sender()->deleteLater();
if (mReply != sender())
return;
if (mReply->error() == QNetworkReply::NoError) {
QVariantMap resp = mParser->parse(mReply->readAll()).toMap();
if (resp.value("code", -1).toInt() == 200) {
QString lrc = resp.value("lrc").toMap().value("lyric").toString();
if (processContent(lrc)) {
emit lyricChanged();
}
}
}
emit loadingChanged();
}