forked from iwxyi/Bilibili-MagicalDanmaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongbeans.h
450 lines (402 loc) · 12.4 KB
/
songbeans.h
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#ifndef SONGBEANS_H
#define SONGBEANS_H
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <QList>
#include <QDateTime>
#include <QDebug>
#define JSON_VAL_LONG(json, x) static_cast<qint64>(json.value(#x).toDouble())
#define JVAL_LONG(x) static_cast<qint64>(json.value(#x).toDouble())
#define JVAL_INT(x) json.value(#x).toInt()
#define JVAL_STR(x) json.value(#x).toString()
#define snum(x) QString::number(x)
#define cast(x,y) static_cast<x>(y)
enum MusicSource
{
UnknowMusic = -1,
NeteaseCloudMusic = 0,
QQMusic = 1,
MiguMusic = 2,
KugouMusic = 3,
LocalMusic = 10
};
struct Artist
{
qint64 id = 0;
QString mid;
QString name;
QString faceUrl;
Artist()
{}
Artist(QString name)
{
this->name = name;
}
static Artist fromJson(QJsonObject json)
{
Artist artist;
artist.id = JVAL_LONG(id);
if (json.contains("mid"))
artist.mid = JVAL_STR(mid);
artist.name = JVAL_STR(name);
artist.faceUrl = JVAL_STR(img1v1Url);
return artist;
}
static Artist fromQQMusicJson(QJsonObject json)
{
Artist artist;
artist.id = JVAL_LONG(id);
artist.mid = JVAL_STR(mid);
artist.name = JVAL_STR(name);
return artist;
}
static Artist fromMiguMusicJson(QJsonObject json)
{
Artist artist;
artist.id = JVAL_LONG(id);
artist.name = JVAL_STR(name);
return artist;
}
QJsonObject toJson() const
{
QJsonObject json;
if (id)
json.insert("id", id);
if (!mid.isEmpty())
json.insert("mid", mid);
json.insert("name", name);
if (!faceUrl.isEmpty())
json.insert("faceUrl", faceUrl);
return json;
}
};
struct Album
{
qint64 id = 0;
QString mid;
qint64 audio_id = 0;
QString name;
int size = 0;
int mark = 0;
QString picUrl;
static Album fromJson(QJsonObject json)
{
Album album;
album.id = JVAL_LONG(id);
if (json.contains("mid"))
album.mid = JVAL_STR(mid);
album.name = JVAL_STR(name);
album.size = JVAL_INT(size);
album.mark = JVAL_INT(mark);
return album;
}
static Album fromQQMusicJson(QJsonObject json)
{
Album album;
album.id = JVAL_LONG(albumid);
album.mid = JVAL_STR(albummid);
album.name = JVAL_STR(albumname);
return album;
}
static Album fromMiguMusicJson(QJsonObject json)
{
Album album;
album.id = JVAL_LONG(id);
album.mid = JVAL_STR(mid);
album.name = JVAL_STR(name);
album.picUrl = JVAL_STR(picUrl);
return album;
}
static Album fromKugouMusicJson(const QJsonObject& json)
{
Album album;
album.id = JVAL_STR(album_id).toLongLong();
album.audio_id = JVAL_LONG(album_audio_id);
album.name = JVAL_STR(album_name);
return album;
}
QJsonObject toJson() const
{
QJsonObject json;
if (id)
json.insert("id", id);
if (!mid.isEmpty())
json.insert("mid", mid);
json.insert("name", name);
if (size)
json.insert("size", size);
if (mark)
json.insert("mark", mark);
if (!picUrl.isEmpty())
json.insert("picUrl", picUrl);
if (audio_id)
json.insert("audio_id", audio_id);
return json;
}
};
struct Song
{
qint64 id = 0;
QString mid;
QString name;
int duration = 0; // 毫秒
int mark = 0;
QList<Artist> artists;
Album album;
QString artistNames;
QString url;
qint64 addTime;
QString mediaId;
QString addBy;
MusicSource source = NeteaseCloudMusic;
char m_padding2[4];
QString filePath;
static Song fromJson(QJsonObject json)
{
Song song;
song.id = JVAL_LONG(id);
song.mid = JVAL_STR(mid);
song.name = JVAL_STR(name);
QJsonArray array = json.value("artists").toArray();
if (array.empty())
array = json.value("ar").toArray();
QStringList artistNameList;
foreach (QJsonValue val, array)
{
Artist artist = Artist::fromJson(val.toObject());
song.artists.append(artist);
artistNameList.append(artist.name);
}
song.artistNames = artistNameList.join("/");
QJsonObject album = json.value("album").toObject();
if (album.isEmpty())
album = json.value("al").toObject();
song.album = Album::fromJson(album);
song.duration = JVAL_INT(duration);
if (song.duration == 0)
song.duration = JVAL_INT(dt);
song.mark = JVAL_INT(mark);
if (json.contains("mediaId"))
song.mediaId = JVAL_STR(mediaId);
if (json.contains("addTime"))
song.addTime = JVAL_LONG(addTime);
if (json.contains("addBy"))
song.addBy = JVAL_STR(addBy);
if (json.contains("source"))
song.source = static_cast<MusicSource>(JVAL_INT(source));
if (json.contains("filePath"))
song.filePath = JVAL_STR(filePath);
return song;
}
static Song fromQQMusicJson(QJsonObject json)
{
Song song;
song.id = JVAL_LONG(songid);
if (song.id == 0)
song.id = JVAL_LONG(id);
if (json.contains("songmid"))
song.mid = JVAL_STR(songmid);
else if (json.contains("mid"))
song.mid = JVAL_STR(mid);
song.name = JVAL_STR(songname);
if(song.name.isEmpty())
song.name = JVAL_STR(name);
QJsonArray array = json.value("singer").toArray();
QStringList artistNameList;
foreach (QJsonValue val, array)
{
Artist artist = Artist::fromQQMusicJson(val.toObject());
song.artists.append(artist);
artistNameList.append(artist.name);
}
song.artistNames = artistNameList.join("/");
QJsonObject album = json.value("album").toObject();
if (album.isEmpty())
song.album = Album::fromQQMusicJson(json);
else
song.album = Album::fromJson(album);
song.duration = JVAL_INT(interval) * 1000; // 秒数,转毫秒
song.mediaId = JVAL_STR(media_mid);
if (json.contains("addTime"))
song.addTime = JVAL_LONG(addTime);
if (json.contains("addBy"))
song.addBy = JVAL_STR(addBy);
song.source = QQMusic;
return song;
}
static Song fromMiguMusicJson(QJsonObject json)
{
Song song;
song.id = JVAL_STR(id).toLongLong();
if (json.contains("cid"))
song.mid = JVAL_STR(cid);
song.name = JVAL_STR(name);
song.url = JVAL_STR(url);
QJsonArray array = json.value("artists").toArray();
QStringList artistNameList;
foreach (QJsonValue val, array)
{
Artist artist = Artist::fromMiguMusicJson(val.toObject());
song.artists.append(artist);
artistNameList.append(artist.name);
}
song.artistNames = artistNameList.join("/");
song.album = Album::fromMiguMusicJson(json.value("album").toObject());
if (json.contains("addTime"))
song.addTime = JVAL_LONG(addTime);
if (json.contains("addBy"))
song.addBy = JVAL_STR(addBy);
song.source = MiguMusic;
return song;
}
static Song fromKugouMusicJson(QJsonObject json)
{
Song song;
song.id = qMax(0, json.value("trans_param").toObject().value("cid").toInt());
song.mid = JVAL_STR(hash);
song.name = JVAL_STR(songname).replace("<em>", "").replace("</em>", "");
song.artistNames = JVAL_STR(singername); // 没有歌手详细信息,直接设置了
song.artists.append(Artist(song.artistNames));
song.duration = JVAL_INT(duration) * 1000; // 秒数,转毫秒
song.album = Album::fromKugouMusicJson(json);
if (json.contains("addTime"))
song.addTime = JVAL_LONG(addTime);
if (json.contains("addBy"))
song.addBy = JVAL_STR(addBy);
song.source = KugouMusic;
return song;
}
static Song fromNeteaseShareJson(QJsonObject json)
{
Song song;
song.id = JVAL_LONG(id);
song.name = JVAL_STR(name);
QJsonArray array = json.value("ar").toArray();
QStringList artistNameList;
foreach (QJsonValue val, array)
{
Artist artist = Artist::fromJson(val.toObject());
song.artists.append(artist);
artistNameList.append(artist.name);
}
song.artistNames = artistNameList.join("/");
song.album = Album::fromJson(json.value("al").toObject());
song.duration = JVAL_INT(dt); // 毫秒
song.mark = JVAL_INT(mark);
song.source = NeteaseCloudMusic;
return song;
}
static Song fromKugouShareJson(QJsonObject json)
{
Song song;
song.id = qMax(0, json.value("trans_param").toObject().value("cid").toInt());
song.mid = JVAL_STR(hash);
QString fileName = JVAL_STR(filename); // 歌手2、歌手2 - 歌名
if (fileName.isEmpty())
fileName = JVAL_STR(name);
int pos = fileName.indexOf("-");
if (pos > -1)
{
song.name = fileName.right(fileName.length() - pos - 1).trimmed();
QStringList ars = fileName.left(pos).trimmed().split("、", QString::SkipEmptyParts);
foreach (auto a, ars)
song.artists.append(Artist(a));
song.artistNames = ars.join("/");
}
song.duration = JVAL_INT(duration) * 1000; // 秒数,转毫秒
if (song.duration == 0)
song.duration = JVAL_INT(timelen);
song.album = Album::fromKugouMusicJson(json);
song.album.name = JVAL_STR(remark);
if (json.contains("addTime"))
song.addTime = JVAL_LONG(addTime);
if (json.contains("addBy"))
song.addBy = JVAL_STR(addBy);
song.source = KugouMusic;
return song;
}
QJsonObject toJson() const
{
QJsonObject json;
json.insert("id", id);
json.insert("mid", mid);
json.insert("name", name);
if (!url.isEmpty())
json.insert("url", url);
json.insert("duration", duration);
if (mark)
json.insert("mark", mark);
QJsonArray array;
foreach (Artist artist, artists)
array.append(artist.toJson());
json.insert("artists", array);
json.insert("album", album.toJson());
if (!mediaId.isEmpty())
json.insert("mediaId", mediaId);
if (addTime)
json.insert("addTime", addTime);
if (!addBy.isEmpty())
json.insert("addBy", addBy);
json.insert("source", static_cast<int>(source));
if (!filePath.isEmpty())
json.insert("filePath", filePath);
return json;
}
bool isValid() const
{
return id || !mid.isEmpty();
}
bool operator==(const Song& song) const
{
return this->id == song.id && this->mid == song.mid;
}
bool operator!=(const Song& song) const
{
return this->id != song.id || this->mid != song.mid;
}
bool isSame(const Song& song, bool art = false) const
{
if (art)
if (!this->artistNames.contains(song.artistNames)
&& !song.artistNames.contains(this->artistNames))
return false;
return this->name.contains(song.name)
|| song.name.contains(this->name);
}
QString simpleString() const
{
if (artistNames.isEmpty())
return name;
return name + " - " + artistNames;
}
void setAddDesc(QString by)
{
this->addBy = by;
this->addTime = QDateTime::currentSecsSinceEpoch();
}
bool is(MusicSource source) const
{
return this->source == source;
}
};
struct PlayListCreator
{
QString nickname;
QString signature;
QString avatarUrl;
};
struct PlayList
{
QString name;
qint64 id;
QString description;
QStringList tags;
int playCount;
PlayListCreator creator;
MusicSource source = NeteaseCloudMusic;
};
typedef QList<Song> SongList;
typedef QList<PlayList> PlayListList;
#endif // SONGBEANS_H