www.pudn.com > winnap.rar > abao_lrcdownload.cpp
//////////////////////////////////////////////
/// @defgroup abaolrcdownload lrc歌词下载类
/// @author abao++
/// @version 1.0
/// @date 2006-9-26 19:39:07
/// @{
//////////////////////////////////////////////
#include"abao_lrcdownload.h"
#include"abao_lrcfile.h"
char* AbaoHttp::UrlEncode(const char* in,char* out)
{
//简单的urlEncode,简单的十六进制汉字内码
char* p=out;
for(;*in;in++)
{
if(*in>0)
{
switch(*in)
{
case ' ':
*p++='+';
break;
default:
*p++=*in;
}
}
else
{
sprintf(p,"%%%2x",*in&0xff);
p+=3;
}
}
*(p+1)=0;
return out;
}
int AbaoHttp::GetHttp(const char* head,string& hostname)
{
//sockect 连接网站 ,并读取返回数据到httpbuf
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
if(WSAStartup(winsock_version,&wsadata)) return 0;
if(!hostname.empty())
{
lphostent=gethostbyname(hostname.c_str());
}
else
{
if(!this->HostName.empty()) lphostent=gethostbyname(this->HostName.c_str());
else
return 0;
}
if(lphostent==NULL) return 0;
hsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
saServer.sin_family = AF_INET;
// Use def. now, need to handle general case
saServer.sin_port = htons(80);
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list);
nRet = connect(hsocket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
if (nRet == SOCKET_ERROR)
{
closesocket(hsocket);
return 0;
}
nRet = send(hsocket, head, strlen(head), 0);
if (nRet == SOCKET_ERROR)
{
closesocket(hsocket);
return 0;
}
char dest[10001];
nRet=1;
this->buf="";
while(nRet>0)
{
nRet=recv(hsocket,(LPSTR)dest,10000,0);
if(nRet>0)
dest[nRet]=0;
else
dest[0]=0;
this->buf+=dest;
}
return 1;
}
AbaoLrcDownload::AbaoLrcDownload()
{
//初始化网站以及http头
httpheadformat="POST %s HTTP/1.1\r\n"
//"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*\r\n"
//"Referer: http://www.jpwy.net/gc/\r\n"
"Accept-Language: zh-cn\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
//"Accept-Encoding: gzip, deflate\r\n"
//"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: www.jpwy.net\r\n"
"Content-Length: 32\r\n"
//"Connection: Keep-Alive\r\n"
"Cache-Control: no-cache\r\n"
//"Cookie: cck_lasttime=1159152815187; cck_count=1; is_use_cookie=yes; is_get_4088=yes\r\n"
"\r\n"
//"keyword=%D6%DC%BD%DC%C2%D7&R1=V1\r\n";
"%s\r\n";
http.HostName="www.jpwy.net";
}
int AbaoLrcDownload::Search()
{
//根据id3标题以及文件名分离标题提取信息
if(this->Download(lrcfile->id3v2title.c_str())) return 1;
if(this->Download(lrcfile->id3v1title.c_str())) return 1;
return this->Download(lrcfile->Mp3Name.c_str());
}
int AbaoLrcDownload::Download(const char * keyword)
{
//根据关键字网站读取信息
if(*keyword==0) return 0;
string k=keyword;
int kpos=k.rfind('-');
if(kpos!=-1)
{
k=k.substr(kpos+1,k.size()-kpos);
}
else
{
kpos=k.rfind('.');
k=k.substr(kpos+1,k.size()-kpos);
}
char url[256];
char reqbuf[1024];
http.UrlEncode(k.c_str(),reqbuf);
//sprintf(reqbuf,searchformat,url);
sprintf(url,"R1=V1&keyword=%s\r\n",reqbuf);
sprintf(reqbuf,httpheadformat,"/gc/search.php",url);
if(!http.GetHttp(reqbuf))
return 0;
//printf("%s",http.buf.c_str());
int pos;
pos=http.buf.find("对不起,没有找到与歌曲",0);
if(pos>0) return 0;
pos=http.buf.find("down.php",0);
int endpos;
string downloadurl;
while(pos>0)
{
int arpos=http.buf.rfind("search.php",pos);
arpos=http.buf.find(">",arpos);
int arposend=http.buf.find("<",arpos);
string ar=http.buf.substr(arpos+1,arposend-arpos-1);
if(this->lrcfile->id3v2artist.find(ar)!=-1||this->lrcfile->id3v1artist.find(ar)!=-1)
{
int endpos=http.buf.find('\"',pos);
downloadurl=http.buf.substr(pos+9,endpos-10-pos);
//printf("%s",downloadurl.c_str());
//sprintf(reqbuf,downloadformat,downloadurl.c_str());
sprintf(reqbuf,httpheadformat,"/gc/down.php",downloadurl.c_str());
if(!http.GetHttp(reqbuf)) return 0;
pos=http.buf.find("stream\r\n\r\n");
pos=http.buf.find("\r\n",pos+11);
this->lrcstr=http.buf.substr(pos+2,http.buf.size()-12-pos);
return 1;
}
pos=http.buf.find("down.php",pos+10);
}
pos=http.buf.find("down.php",0);
if(pos>0)
{
endpos=http.buf.find('\"',pos);
string downloadurl=http.buf.substr(pos+9,endpos-10-pos);
//printf("%s",downloadurl.c_str());
sprintf(reqbuf,httpheadformat,"/gc/down.php",downloadurl.c_str());
if(!http.GetHttp(reqbuf)) return 0;
pos=http.buf.find("stream\r\n\r\n");
pos=http.buf.find("\r\n",pos+11);
this->lrcstr=http.buf.substr(pos+2,http.buf.size()-12-pos);
return 1;
}
return 0;
}
/// @} 文件注释完毕