-
Notifications
You must be signed in to change notification settings - Fork 53
/
neteaseplaylist.cpp
150 lines (140 loc) · 4.13 KB
/
neteaseplaylist.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
#include "neteaseplaylist.h"
neteasePlaylist::neteasePlaylist(QObject *parent) : QObject(parent)
{
isGray=false;
d=new downloader();
d->setUserAgent(UA);
connect(d,SIGNAL(finished()),this,SLOT(htmlGot()));
connect(d,SIGNAL(downloadError(QString)),this,SLOT(htmlGotFailed(QString)));
connect(d,SIGNAL(redirected(QString)),this,SLOT(htmlGotRedirected(QString)));
s=new neteaseSong();
connect(s,SIGNAL(finished(int,bool,QString)),this,SLOT(neteaseSongFinished(int,bool,QString)));
}
void neteasePlaylist::doJob(bool inIsGray,QString inUrl)
{
emit status("下载列表所有歌曲信息,此处需要耐心等待片刻,曲目数越多,等待时间越长");
isGray=inIsGray;
infoList.clear();
grayList.clear();
songMidList.clear();
listTitle="";
int pos;
if(inUrl.contains("playlist?id=")&&inUrl.contains("music.163.com"))
{
if(inUrl.contains("&"))
{
char a[20];
getStringBetweenAandB(inUrl.toStdString().c_str(),"=","&",a);
mid=QString(a);
}else{
pos=inUrl.indexOf(QString("="));
mid=inUrl.mid(pos+1);
}
}else if(inUrl.contains("playlist/"))
{
char a[20];
getStringBetweenAandB(inUrl.toStdString().c_str(),"playlist/","/",a);
mid=QString(a);
}
else
{
finished(-1,infoList);
}
QString listUrl=QString("http://music.163.com/playlist?id=")+mid;
d->init(listUrl,SONGHTMLFILE);
d->doGet();
}
void neteasePlaylist::htmlGotFailed(QString errorString)
{
emit status(QString("neteasePlaylist html failed:")+errorString);
finished(-1,infoList);
}
void neteasePlaylist::htmlGotRedirected(QString redirectString)
{
d->init(redirectString,SONGHTMLFILE);
d->doGet();
}
void neteasePlaylist::htmlGot()
{
QFile file(SONGHTMLFILE);
QString line;
int pos;
char tmpString[200];
if(file.open(QIODevice::ReadOnly))
{
while(!file.atEnd())
{
line=file.readLine();
if(line.contains("appid"))
{
line=file.readLine();
getStringBetweenAandB(line.toStdString().c_str(),"\"title\": \"","\"",tmpString);
listTitle=QString(tmpString);
break;
}
}
qDebug()<<listTitle;
while(!file.atEnd())
{
line=file.readLine();
if(line.contains("class=\"f-hide\""))
{
while((pos=line.indexOf(QString("song?id")))!=-1)
{
getStringBetweenAandB(line.toStdString().c_str(),"song?id=","\"",tmpString);
songMidList.append(QString(tmpString));
line=line.mid(pos+10);
qDebug()<<QString(tmpString);
}
break;
}
}
}
file.close();
if(listTitle.isEmpty())
{
emit status("失败");
finished(-2,infoList);
return;
}
listTitle.remove(QChar('|'));
listTitle.remove(QChar('>'));
listTitle.remove(QChar('<'));
listTitle.remove(QChar('*'));
listTitle.remove(QChar('?'));
listTitle.remove(QChar('/'));
listTitle.remove(QChar('\\'));
listTitle.remove(QChar(':'));
mp3Dir=mp3Dir+QString("/")+listTitle;
QDir tmp(mp3Dir);
if(!tmp.exists())
{
tmp.mkdir(mp3Dir);
}
s->doJob(songMidList.at(0));
}
void neteasePlaylist::neteaseSongFinished(int inFlag,bool inIsGray,QString inString)
{
if(inFlag)
{
emit status(songMidList.at(0)+QString("html下载失败:")+inString);
}
else{
if(inIsGray)
grayList.append(inString);
infoList.append(inString);
songMidList.removeAt(0);
if(songMidList.count()>0)
{
s->doJob(songMidList.at(0));
}
else{
if(isGray){
emit finished(0,grayList);
}
else{
finished(0,infoList);
}
}
}
}