-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathqqmusicsong.cpp
214 lines (193 loc) · 6.6 KB
/
qqmusicsong.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
#include "qqmusicsong.h"
qqMusicSong::qqMusicSong(QObject *parent) : QObject(parent)
{
d=new downloader();
d->setUserAgent(USERAGENT);
connect(d,SIGNAL(downloadError(QString)),this,SLOT(htmlGotFailed(QString)));
connect(d,SIGNAL(redirected(QString)),this,SLOT(htmlGotFailed(QString)));
connect(d,SIGNAL(finished()),this,SLOT(htmlGot()));
m=new downloader();
connect(m,SIGNAL(downloadError(QString)),this,SLOT(musicDownloadFailed(QString)));
connect(m,SIGNAL(redirected(QString)),this,SLOT(musicDownloadFailed(QString)));
connect(m,SIGNAL(finished()),this,SLOT(musicDownloaded()));
connect(m,SIGNAL(progress(qint64,qint64)),this,SIGNAL(progress(qint64,qint64)));
p=new downloader();
connect(p,SIGNAL(downloadError(QString)),this,SLOT(picDownloadFailed(QString)));
connect(p,SIGNAL(redirected(QString)),this,SLOT(picDownloadFailed(QString)));
connect(p,SIGNAL(finished()),this,SLOT(picDownloaded()));
}
void qqMusicSong::doJob(QString inMid)
{
sizeape=0;
size320=0;
songMid=inMid;
htmlLink=QString(SINGLESONGHEAD)+songMid+QString(SINGLESONGTAIL);
d->init(htmlLink,QString(SONGINFOFILE));
d->doGet();
}
void qqMusicSong::htmlGotFailed(QString errorString)
{
emit status(songMid+QString("-html下载失败,失败原因:")+errorString);
finished(-1);
}
/****************************
* html文件已经下载
* 找出mediaMid,songName,albumMid,singerName
* **************************/
void qqMusicSong::htmlGot()
{
QFile file(SONGINFOFILE);
if(file.open(QIODevice::ReadOnly))
{
QByteArray b=file.readAll();
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(b,&jsonError);
if(jsonError.error!=QJsonParseError::NoError)
{
qDebug() << "read singleSong json file failed";
emit status("read singleSong json file failed");
file.close();
}
QJsonObject obj=doucment.object();
QJsonArray tmpArray=obj["data"].toArray();
obj=tmpArray.first().toObject();
QJsonObject tmpObj=obj["album"].toObject();
albumMid=tmpObj["mid"].toString();
qDebug()<<albumMid;
albumName=tmpObj["name"].toString();
qDebug()<<albumName;
picUrl=QString(ALBUMPICLINKHEAD)+albumMid+QString(ALBUMPICLINKTAIL);
qDebug()<<picUrl;
songName=obj["name"].toString();
songName.remove("\\");
singerName.remove("/");
qDebug()<<songName;
tmpObj=obj["file"].toObject();
sizeape=tmpObj["size_ape"].toInt();
size320=tmpObj["size_320mp3"].toInt();
qDebug()<<size320;
mediaMid=tmpObj["media_mid"].toString();
qDebug()<<mediaMid;
tmpArray=obj["singer"].toArray();
tmpObj=tmpArray.first().toObject();
singerName=tmpObj["name"].toString();
singerName.remove("\\");
singerName.remove("/");
qDebug()<<singerName;
tmpArray.removeFirst();
while(tmpArray.count()>0)
{
tmpObj=tmpArray.first().toObject();
singerName=singerName+QString("/")+tmpObj["name"].toString();
tmpArray.removeFirst();
qDebug()<<singerName;
}
file.close();
downloadSong();
}
}
void qqMusicSong::musicDownloadFailed(QString errorString)
{
emit status(QString("下载失败:")+errorString);
finished(-2);
}
void qqMusicSong::musicDownloaded()
{
if(songQuality==1 || songQuality==2)
{
qDebug()<<"准备下载图片";
qDebug()<<picUrl;
emit status("准备下载封面图片");
p->init(picUrl,"tmp.jpg");
p->doGet();
}
else
{
emit finished(0);
}
}
void qqMusicSong::picDownloadFailed(QString errorString)
{
emit status(QString("封面图片下载失败:")+errorString+QString(",不设置封面图"));
finished(0);
}
void qqMusicSong::picDownloaded()
{
tagtmp=new ID3tag(mp3FileName);
connect(tagtmp,SIGNAL(finished()),this,SLOT(tagDone()));
tagtmp->setTitle(songName);
tagtmp->setArtist(singerName.replace("-","/"));
tagtmp->setAlbum(albumName);
tagtmp->setPic(QString("tmp.jpg"));
tagtmp->doJob();
}
void qqMusicSong::tagDone()
{
delete tagtmp;
emit status("封面图设置结束");
emit finished(0);
}
void qqMusicSong::getSongLink()
{
if(size320==0 || songQuality == 1)
{
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("M500")+mediaMid+QString(".mp3")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
return ;
}
if(sizeape==0 || songQuality == 2)
{
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("M800")+mediaMid+QString(".mp3")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
return;
}
switch (songQuality) {
case 2:
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("M800")+mediaMid+QString(".mp3")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
break;
case 3:
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("F000")+mediaMid+QString(".flac")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
break;
case 4:
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("A000")+mediaMid+QString(".ape")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
break;
default:
mp3Link=QString(NDOWNLOADLINKHEAD)+QString("M500")+mediaMid+QString(".mp3")+QString(NDOWNLOADLINKMID)+vkey2+QString(NDOWNLOADLINKTAIL);
break;
}
}
void qqMusicSong::downloadSong()
{
getSongLink();
if(songNameType==2)
mp3FileName=mp3Dir+QString("/")+singerName.replace("/","-")+QString("-")+songName;
else
mp3FileName=mp3Dir+QString("/")+songName+QString("-")+singerName.replace("/","-");
switch (songQuality) {
case 1:
mp3FileName=mp3FileName+QString("-128")+".mp3";
break;
case 2:
mp3FileName=mp3FileName+".mp3";
break;
case 3:
mp3FileName=mp3FileName+".flac";
break;
case 4:
mp3FileName=mp3FileName+".ape";
break;
}
qDebug()<<"mp3link:"<<mp3Link;
qDebug()<<"mp3filename:"<<mp3FileName;
QFile a(mp3FileName);
if(a.exists())
{
qDebug()<<mp3FileName<<"已存在";
emit status(mp3FileName+QString("已经存在"));
emit finished(0);
}
else
{
m->init(mp3Link,mp3FileName);
emit status(QString("准备下载")+mp3FileName);
m->doGet();
}
}