// // BaiduMusicBoxParser.h // // Created by [email protected] on 12-3-11. // Copyright (c) 2012年. All rights reserved. // #import <Foundation/Foundation.h> #import "ASIHTTPRequest.h" #import "ASINetworkQueue.h" #import "TFHpple.h" #import "XPathQuery.h" #import "TFHppleElement.h" @interface BaiduMusicBoxParser : NSObject + (BaiduMusicBoxParser*)shareInstance; /** 返回的数组结构: [ { album = "\U672a\U6765\U5c5e\U4e8e\U5b69\U5b50 \U4e2d\U5916\U540d\U66f2\U7cbe\U9009"; artist = "\U6768\U9e3f\U5e74"; "download_url" = "http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting.baidu.com%2Fdata2%2Fmusic%2F9405342%2F9405342.mp3%3Fxcode%3Dfdaccf3ade1f2d967110c329ab53b046"; quality = "MP3(3.9M)"; "song_name" = "we are the world"; },... ] **/ - (NSArray*)baiduSongsInfoBySearchingSongName:(NSString *)name pageNumber:(int)pgNO; //获取歌词 - (NSString*)lyricsWithSong:(NSString*)sName; //获取百度歌手列表,从大写字母A~Z,其他为0 /** [ "A"=[ AOK, Alizee,... ], "B"={ BOBO, 白雪,... },... ] **/ - (NSArray*)allArtistsListOfBaiduMp3; - (NSArray*)artistsListOfBaiduMp3ByFilterCharacter:(NSString*)character; @end
// // BaiduMusicBoxParser.m // // Created by [email protected] on 12-3-11. // Copyright (c) 2012年. All rights reserved. // #import "BaiduMusicBoxParser.h" #import "GDataXMLNode.h" static BaiduMusicBoxParser *parser_ = nil; @interface BaiduMusicBoxParser (Private) /** 返回的数组结构: [ "http://box.zhangmen.baidu.com/m?word=mp3,,,[love+of+my+life]&gate=1&ct=134217728&tn=baidumt,love+of+my+life&si=love+of+my+life;;keith%20martin;;0;;0&lm=-1&attr=0,0&rf=zb&size=1992294", ... ] sName:歌曲名称 pgNo:页数。默认为0. **/ - (NSArray*)jumpToURLBySongName:(NSString*)sName pageNumber:(int)pgNo; @end @implementation BaiduMusicBoxParser - (void)dealloc{ [super dealloc]; } + (BaiduMusicBoxParser*)shareInstance{ if (!parser_) { parser_ = [[self alloc] init]; } return parser_; } - (id)init{ if ([super init]) { } return self; } - (NSArray*)jumpToURLBySongName:(NSString*)sName pageNumber:(int)pgNo{ if(sName==nil || [sName length]==0)return nil; if (pgNo<0) { return nil; } //去除两端的空格 [sName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //把歌曲拼凑成格式如:love+of+my+life 的形式 NSArray *nameArr = [sName componentsSeparatedByString:@" "]; NSMutableString *newName = [NSMutableString stringWithCapacity:0]; if (nameArr && [nameArr count]) { for (NSString *s in nameArr) { [newName appendFormat:@"%@+",s]; } //去掉最后一个 '+'号 if ([newName length]) { [newName deleteCharactersInRange:NSMakeRange([newName length]-1, 1)]; } } //用户输入的是整个字符串,没有空格 if (nameArr && [nameArr count]==1) { [newName appendString:[nameArr objectAtIndex:0]]; } //组合地址并编码 NSString *requestURL = [NSString stringWithFormat:@"http://mp3.baidu.com/m?word=%@&lm=-1&f=ms&tn=baidump3&ct=134217728&lf=&rn=&pn=%i",newName,pgNo*30]; NSString *requestURLWithEnc = [requestURL stringByAddingPercentEscapesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)]; //NSLog(@"===%@",requestURLWithEnc); //发送请求,获得返回数据 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:requestURLWithEnc]]; [request startSynchronous]; if ([request error]) { return nil; } //接收返回数据 //获取id = "songResults"的节点 TFHpple *helper = [TFHpple hppleWithHTMLData:[request responseData]]; NSString *xPathDescription = @"//*[@id=\"songResults\"]"; TFHppleElement *divElement = [helper peekAtSearchWithXPathQuery:xPathDescription]; NSArray *children = divElement.children; //return NSMutableArray *jumpToPageURLArray = [NSMutableArray arrayWithCapacity:0]; //获得table TFHppleElement *tb = [children objectAtIndex:0]; NSArray *tbodyChildren = tb.children; if (tbodyChildren && [tbodyChildren count]) { for (TFHppleElement *trElement in tbodyChildren) { NSArray *tdArr = trElement.children; if (tdArr && [tdArr count]) { for (TFHppleElement *tdNode in tdArr) { //<td class="second"> if (tdNode && [[tdNode objectForKey:@"class"] isEqualToString:@"down"]) { NSArray *tdNodeArr = tdNode.children; if (tdNodeArr && [tdNodeArr count]) { TFHppleElement *aNode = tdNode.firstChild; if (aNode) { //获得要跳转的地址 NSString *jumpToURL = [aNode objectForKey:@"href"]; if ([jumpToURL length]) { [jumpToPageURLArray addObject:jumpToURL]; } } } } } } } } //跳转界面地址数组 //NSLog(@"共有跳转地址:%i个,它们是:%@",[jumpToPageURLArray count],jumpToPageURLArray); if ([jumpToPageURLArray count]) { return jumpToPageURLArray; } return nil; } - (NSArray*)baiduSongsInfoBySearchingSongName:(NSString *)name pageNumber:(int)pgNO{ if(name==nil || [name length]==0)return nil; if (pgNO<0) return nil; NSArray *jumpToURLArr = [self jumpToURLBySongName:name pageNumber:pgNO]; //获得跳转地址的数组 NSMutableArray *musicInfoArr = [NSMutableArray arrayWithCapacity:0]; if (jumpToURLArr && [jumpToURLArr count]) { for (NSString *urlStr in jumpToURLArr) { ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]]; [request startSynchronous]; //获得返回数据 NSData *responseData = [request responseData]; if (responseData) { TFHpple *helper = [TFHpple hppleWithHTMLData:responseData]; if (helper) { //获得歌曲的下载地址 NSString *xPath = @"//*[@id='downlink']"; TFHppleElement *downloadLinkNode = [helper peekAtSearchWithXPathQuery:xPath]; NSString *musicDownloadURI = [downloadLinkNode objectForKey:@"href"]; NSString *musicDownloadURL = [NSString stringWithFormat:@"http://mp3.baidu.com%@",musicDownloadURI]; //NSLog(@"======下载地址是:%@",musicDownloadURL); if (musicDownloadURI && [musicDownloadURI length]) { //获得歌手名字 /**<div class="songinfo-more"> <span class="singer gray">歌手: <a href="http://mp3.baidu.com/singerlist/back ii back.html" target="_blank">back ii back</a> </span> <span class="album gray">所属专辑: 《<a href="http://mp3.baidu.com/albumlist/back ii back;;;;;;back ii back.html" target="_blank">back ii back</a> 》</span> </div> **/ NSString *aritistNameXpath = @"//*[@class='songinfo-more']"; TFHppleElement *aritistNameNode = [helper peekAtSearchWithXPathQuery:aritistNameXpath]; TFHppleElement *spanNode = aritistNameNode.firstChild; TFHppleElement *aNode = spanNode.firstChild; NSString *artistName = aNode.content; //NSLog(@"*************歌手:%@",artistName); //获得专辑名字 TFHppleElement *albumNode = [aritistNameNode.children objectAtIndex:1]; TFHppleElement *AaNode = albumNode.firstChild; NSString *albumName = AaNode.content; //NSLog(@"*************专辑:%@",albumName); //获得歌曲的品质 /** <div class="format gray">品质:<b>MP3(4.0M)</b></div> **/ NSString *qulityXpath = @"//*[@class='format gray']"; TFHppleElement *qulityNode = [helper peekAtSearchWithXPathQuery:qulityXpath]; TFHppleElement *bNode = qulityNode.firstChild; NSString *qulityDescription = bNode.content; //NSLog(@"*************品质:%@",qulityDescription); if (musicDownloadURI && [musicDownloadURI length]) { NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0]; [dic setObject:musicDownloadURL forKey:@"download_url"]; [dic setObject:name forKey:@"song_name"]; [dic setObject:artistName?artistName:[NSNull null] forKey:@"artist"]; [dic setObject:albumName?albumName:[NSNull null] forKey:@"album"]; [dic setObject:qulityDescription?qulityDescription:[NSNull null] forKey:@"quality"]; if ([dic count]) { [musicInfoArr addObject:dic]; } } } } } } } //NSLog(@"歌曲信息数组-------------%@",musicInfoArr); if ([musicInfoArr count]) { return musicInfoArr; } return nil; } - (NSString*)lyricsWithSong:(NSString*)sName { return nil; } - (NSArray*)allArtistsListOfBaiduMp3{ return nil; } - (NSArray*)artistsListOfBaiduMp3ByFilterCharacter:(NSString*)character{ return nil; } @end