#import "testerViewController.h"
#import "ASIHTTPRequest.h"
@implementation testerViewController
+(NSArray*) KeyWordsForSongs:(NSString*)myKeyWords pageNavi:(int)myPageNavi{
int pageNavi = myPageNavi; // 页码
NSString * keyWords = myKeyWords; // 关键字
//NSString * keyWords = @" 张信哲 ";
// 创建字符串
NSString * myURL = [NSString stringWithFormat:@"http://mp3.baidu.com/m?f=ms&rf=idx&tn=baidump3&ct=134217728&lf=&rn=&word=%@&lm=0&pn=%d",keyWords,pageNavi];
//UTF8 to gb2312
myURL = [myURL stringByAddingPercentEscapesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
NSLog(@"%@",myURL);
ASIHTTPRequest *myRequest = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:myURL]] autorelease];
[myRequest startSynchronous];
// 如果发生错误,返回 nil
if ([myRequest error]) return nil;
// 返回数组定义
NSMutableArray * mySongs = [NSMutableArray arrayWithCapacity:15];
// 编码转换 gb2313 to UTF
NSData * myResponseData = [myRequest responseData];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString * myResponseStr = [[NSString alloc] initWithData:myResponseData encoding:enc];
//NSLog(@"html=%@",myResponseStr);
// 如果百度告诉我没有找到?
NSRange myRange;
myRange = [myResponseStr rangeOfString:@" 抱歉,没有找到与 "];
if (myRange.location != NSNotFound) {
return nil;
};
// 将源代码按 ” <td class=d><a href= “ 切割
NSMutableArray * myResponseArrForSinger = (NSMutableArray *)[myResponseStr componentsSeparatedByString:@"<td class=d><a href="];
// 去头去尾
if ([myResponseArrForSinger count]>=2) {
[myResponseArrForSinger removeObjectAtIndex:0];
[myResponseArrForSinger removeObjectAtIndex:[myResponseArrForSinger count]-1];
}
// 按段处理
for (int i=0; i<[myResponseArrForSinger count]; i++) {
//
@try {
// 把段按 “ .html" target="_blank"> ” 切割
NSMutableArray * mySubArr = (NSMutableArray *)[[myResponseArrForSinger objectAtIndex:i] componentsSeparatedByString:@".html" target="_blank">"];
// 处理歌曲下载页面的 URL
NSArray * myURLTmpArr = [[mySubArr objectAtIndex:0] componentsSeparatedByString:@"" title=" 请点击左键!来源网址: "];
myURLTmpArr = [[mySubArr objectAtIndex:0] componentsSeparatedByString:@"""];
NSMutableString * SongsURL = [myURLTmpArr objectAtIndex:1];
for (int j=0; j<[mySubArr count]; j++) {
NSLog(@"%d= %@",j,[mySubArr objectAtIndex:j]);
}
// 如果只有一段,表示 无 歌手名 和 专辑名
//if ([mySubArr count]<=1) {
//NSLog()
// 常量定义 段的意义
const int Const_Songs = 0;
const int Const_Singer = 1;
const int Const_Album = 4;
// 变量定义
NSMutableString * SongsName = (NSMutableString*)@"";
NSString * SongsSinger = @"";
NSString * SongsAlbum = @"";
NSString * SongsSpeed = @"";
NSString * SongsSize = @"";
// 处理 歌曲大小
NSMutableArray * mySizeArr = (NSMutableArray*)[[mySubArr objectAtIndex:([mySubArr count]-1)] componentsSeparatedByString:@"<td>"];
if ([mySizeArr count]>=2) {
SongsSize = [mySizeArr objectAtIndex:([mySizeArr count]-2)];
NSLog(@"size = %@",SongsSize);
};
// 处理 歌曲链接速度图片
@try {
if ([mySubArr count]>=3) {
if ([mySubArr objectAtIndex:([mySubArr count]-1)]!=nil) {
NSMutableArray * mySpeedArr = (NSMutableArray*)[[mySubArr objectAtIndex:([mySubArr count]-1)] componentsSeparatedByString:@"<td class=spd><img src="http://img.baidu.com/img/mp3/"];
if ([mySpeedArr count]>0)
if ([mySpeedArr objectAtIndex:1]!=nil)
mySpeedArr = (NSMutableArray*)[[mySpeedArr objectAtIndex:1] componentsSeparatedByString:@""></td>"];
if ([mySpeedArr objectAtIndex:0]!=nil)
SongsSpeed = [mySpeedArr objectAtIndex:0];
NSLog(@"test speed = %@",SongsSpeed);
}
};
}
@catch (NSException * e) {
//none
}
@finally {
//none
}
// 处理 歌曲名
NSMutableArray * myTmpArr = (NSMutableArray *)[[mySubArr objectAtIndex:Const_Songs] componentsSeparatedByString:@""return ow(event,this)" target="_blank">"];
myTmpArr = (NSMutableArray *)[[myTmpArr objectAtIndex:1] componentsSeparatedByString:@"</a></td>"];
SongsName = [myTmpArr objectAtIndex:0];
NSString * mySubStr;
// 处理 歌手名
if ([mySubArr count]>1) {
mySubStr = [mySubArr objectAtIndex:Const_Singer];
SongsSinger = mySubStr;
};
// 处理 专辑名
if ([mySubArr count]>=5) {
mySubStr = [mySubArr objectAtIndex:Const_Album];
NSMutableArray * mySubSubArr = (NSMutableArray *)[mySubStr componentsSeparatedByString:@"</a>"];
mySubStr = [mySubSubArr objectAtIndex:0];
SongsAlbum = mySubStr;
} else {
SongsAlbum = @"";
};
待续...