- 论坛徽章:
- 0
|
获得百度音乐盒音乐的下载地址
iphonebaidumusicbox.
Object-c代码- 1.//
- 2.// BaiduMusicBoxParser.h
- 3.//
- 4.// Created by scott.8an@gmail.com on 12-3-11.
- 5.// Copyright (c) 2012年. All rights reserved.
- 6.//
- 7.
- 8.#import <Foundation/Foundation.h>
- 9.#import "ASIHTTPRequest.h"
- 10.#import "ASINetworkQueue.h"
- 11.#import "TFHpple.h"
- 12.#import "XPathQuery.h"
- 13.#import "TFHppleElement.h"
- 14.
- 15.@interface BaiduMusicBoxParser : NSObject
- 16.
- 17.+ (BaiduMusicBoxParser*)shareInstance;
- 18.
- 19./**
- 20. 返回的数组结构:
- 21. [
- 22. {
- 23. album = "\U672a\U6765\U5c5e\U4e8e\U5b69\U5b50 \U4e2d\U5916\U540d\U66f2\U7cbe\U9009";
- 24. artist = "\U6768\U9e3f\U5e74";
- 25. "download_url" = "http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting.baidu.com%2Fdata2%2Fmusic%2F9405342%2F9405342.mp3%3Fxcode%3Dfdaccf3ade1f2d967110c329ab53b046";
- 26. quality = "MP3(3.9M)";
- 27. "song_name" = "we are the world";
- 28. },...
- 29. ]
- 30. **/
- 31.- (NSArray*)baiduSongsInfoBySearchingSongName:(NSString *)name pageNumber:(int)pgNO;
- 32.
- 33.//获取歌词
- 34.- (NSString*)lyricsWithSong:(NSString*)sName;
- 35.
- 36.//获取百度歌手列表,从大写字母A~Z,其他为0
- 37./**
- 38. [
- 39. "A"=[
- 40. AOK,
- 41. Alizee,...
- 42. ],
- 43. "B"={
- 44. BOBO,
- 45. 白雪,...
- 46. },...
- 47. ]
- 48. **/
- 49.- (NSArray*)allArtistsListOfBaiduMp3;
- 50.- (NSArray*)artistsListOfBaiduMp3ByFilterCharacter:(NSString*)character;
- 51.
- 52.@end
-
复制代码 Object-c代码- 1.//
- 2.// BaiduMusicBoxParser.m
- 3.//
- 4.// Created by scott.8an@gmail.com on 12-3-11.
- 5.// Copyright (c) 2012年. All rights reserved.
- 6.//
- 7.
- 8.#import "BaiduMusicBoxParser.h"
- 9.#import "GDataXMLNode.h"
- 10.
- 11.static BaiduMusicBoxParser *parser_ = nil;
- 12.
- 13.@interface BaiduMusicBoxParser (Private)
- 14./**
- 15. 返回的数组结构:
- 16. [
- 17. "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",
- 18. ...
- 19. ]
- 20.
- 21. sName:歌曲名称
- 22. pgNo:页数。默认为0.
- 23. **/
- 24.- (NSArray*)jumpToURLBySongName:(NSString*)sName pageNumber:(int)pgNo;
- 25.@end
- 26.
- 27.@implementation BaiduMusicBoxParser
- 28.
- 29.- (void)dealloc{
- 30. [super dealloc];
- 31.}
- 32.
- 33.+ (BaiduMusicBoxParser*)shareInstance{
- 34. if (!parser_) {
- 35. parser_ = [[self alloc] init];
- 36. }
- 37. return parser_;
- 38.}
- 39.
- 40.- (id)init{
- 41. if ([super init]) {
- 42.
- 43. }
- 44. return self;
- 45.}
- 46.
- 47.- (NSArray*)jumpToURLBySongName:(NSString*)sName pageNumber:(int)pgNo{
- 48. if(sName==nil || [sName length]==0)return nil;
- 49. if (pgNo<0) {
- 50. return nil;
- 51. }
- 52.
- 53. //去除两端的空格
- 54. [sName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- 55.
- 56. //把歌曲拼凑成格式如:love+of+my+life 的形式
- 57. NSArray *nameArr = [sName componentsSeparatedByString:@" "];
- 58. NSMutableString *newName = [NSMutableString stringWithCapacity:0];
- 59. if (nameArr && [nameArr count]) {
- 60. for (NSString *s in nameArr) {
- 61. [newName appendFormat:@"%@+",s];
- 62. }
- 63. //去掉最后一个 '+'号
- 64. if ([newName length]) {
- 65. [newName deleteCharactersInRange:NSMakeRange([newName length]-1, 1)];
- 66. }
- 67. }
- 68. //用户输入的是整个字符串,没有空格
- 69. if (nameArr && [nameArr count]==1) {
- 70. [newName appendString:[nameArr objectAtIndex:0]];
- 71. }
- 72.
- 73. //组合地址并编码
- 74. 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];
- 75. NSString *requestURLWithEnc = [requestURL stringByAddingPercentEscapesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
- 76. //NSLog(@"===%@",requestURLWithEnc);
- 77.
- 78. //发送请求,获得返回数据
- 79. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:requestURLWithEnc]];
- 80. [request startSynchronous];
- 81.
- 82. if ([request error]) {
- 83. return nil;
- 84. }
- 85.
- 86. //接收返回数据
- 87. //获取id = "songResults"的节点
- 88. TFHpple *helper = [TFHpple hppleWithHTMLData:[request responseData]];
- 89. NSString *xPathDescription = @"//*[@id=\"songResults\"]";
- 90. TFHppleElement *divElement = [helper peekAtSearchWithXPathQuery:xPathDescription];
- 91. NSArray *children = divElement.children;
- 92.
- 93. //return
- 94. NSMutableArray *jumpToPageURLArray = [NSMutableArray arrayWithCapacity:0];
- 95.
- 96. //获得table
- 97. TFHppleElement *tb = [children objectAtIndex:0];
- 98. NSArray *tbodyChildren = tb.children;
- 99. if (tbodyChildren && [tbodyChildren count]) {
- 100. for (TFHppleElement *trElement in tbodyChildren) {
- 101. NSArray *tdArr = trElement.children;
- 102. if (tdArr && [tdArr count]) {
- 103. for (TFHppleElement *tdNode in tdArr) {
- 104. //<td class="second">
- 105. if (tdNode && [[tdNode objectForKey:@"class"] isEqualToString:@"down"]) {
- 106. NSArray *tdNodeArr = tdNode.children;
- 107. if (tdNodeArr && [tdNodeArr count]) {
- 108. TFHppleElement *aNode = tdNode.firstChild;
- 109.
- 110. if (aNode) {
- 111. //获得要跳转的地址
- 112. NSString *jumpToURL = [aNode objectForKey:@"href"];
- 113. if ([jumpToURL length]) {
- 114. [jumpToPageURLArray addObject:jumpToURL];
- 115. }
- 116. }
- 117. }
- 118. }
- 119. }
- 120. }
- 121. }
- 122. }
- 123.
- 124. //跳转界面地址数组
- 125. //NSLog(@"共有跳转地址:%i个,它们是:%@",[jumpToPageURLArray count],jumpToPageURLArray);
- 126. if ([jumpToPageURLArray count]) {
- 127. return jumpToPageURLArray;
- 128. }
- 129. return nil;
- 130.}
- 131.
- 132.- (NSArray*)baiduSongsInfoBySearchingSongName:(NSString *)name pageNumber:(int)pgNO{
- 133. if(name==nil || [name length]==0)return nil;
- 134. if (pgNO<0) return nil;
- 135.
- 136. NSArray *jumpToURLArr = [self jumpToURLBySongName:name pageNumber:pgNO];
- 137.
- 138. //获得跳转地址的数组
- 139. NSMutableArray *musicInfoArr = [NSMutableArray arrayWithCapacity:0];
- 140. if (jumpToURLArr && [jumpToURLArr count]) {
- 141. for (NSString *urlStr in jumpToURLArr) {
- 142. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlStr]];
- 143. [request startSynchronous];
- 144.
- 145. //获得返回数据
- 146. NSData *responseData = [request responseData];
- 147. if (responseData) {
- 148. TFHpple *helper = [TFHpple hppleWithHTMLData:responseData];
- 149. if (helper) {
- 150. //获得歌曲的下载地址
- 151. NSString *xPath = @"//*[@id='downlink']";
- 152. TFHppleElement *downloadLinkNode = [helper peekAtSearchWithXPathQuery:xPath];
- 153. NSString *musicDownloadURI = [downloadLinkNode objectForKey:@"href"];
- 154. NSString *musicDownloadURL = [NSString stringWithFormat:@"http://mp3.baidu.com%@",musicDownloadURI];
- 155. //NSLog(@"======下载地址是:%@",musicDownloadURL);
- 156.
- 157. if (musicDownloadURI && [musicDownloadURI length]) {
- 158. //获得歌手名字
- 159. /**<div class="songinfo-more">
- 160. <span class="singer gray">歌手:
- 161. <a href="http://mp3.baidu.com/singerlist/back ii back.html" target="_blank">back ii back</a>
- 162. </span>
- 163. <span class="album gray">所属专辑:
- 164. 《<a href="http://mp3.baidu.com/albumlist/back ii back;;;;;;back ii back.html" target="_blank">back ii back</a>
- 165. 》</span>
- 166. </div>
- 167. **/
- 168. NSString *aritistNameXpath = @"//*[@class='songinfo-more']";
- 169. TFHppleElement *aritistNameNode = [helper peekAtSearchWithXPathQuery:aritistNameXpath];
- 170. TFHppleElement *spanNode = aritistNameNode.firstChild;
- 171. TFHppleElement *aNode = spanNode.firstChild;
- 172. NSString *artistName = aNode.content;
- 173. //NSLog(@"*************歌手:%@",artistName);
- 174.
- 175. //获得专辑名字
- 176. TFHppleElement *albumNode = [aritistNameNode.children objectAtIndex:1];
- 177. TFHppleElement *AaNode = albumNode.firstChild;
- 178. NSString *albumName = AaNode.content;
- 179. //NSLog(@"*************专辑:%@",albumName);
- 180.
- 181. //获得歌曲的品质
- 182. /**
- 183. <div class="format gray">品质:<b>MP3(4.0M)</b></div>
- 184. **/
- 185. NSString *qulityXpath = @"//*[@class='format gray']";
- 186. TFHppleElement *qulityNode = [helper peekAtSearchWithXPathQuery:qulityXpath];
- 187. TFHppleElement *bNode = qulityNode.firstChild;
- 188. NSString *qulityDescription = bNode.content;
- 189. //NSLog(@"*************品质:%@",qulityDescription);
- 190.
- 191. if (musicDownloadURI && [musicDownloadURI length]) {
- 192. NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
- 193. [dic setObject:musicDownloadURL forKey:@"download_url"];
- 194. [dic setObject:name forKey:@"song_name"];
- 195. [dic setObject:artistName?artistName:[NSNull null] forKey:@"artist"];
- 196. [dic setObject:albumName?albumName:[NSNull null] forKey:@"album"];
- 197. [dic setObject:qulityDescription?qulityDescription:[NSNull null] forKey:@"quality"];
- 198.
- 199. if ([dic count]) {
- 200. [musicInfoArr addObject:dic];
- 201. }
- 202. }
- 203. }
- 204. }
- 205. }
- 206. }
- 207. }
- 208.
- 209. //NSLog(@"歌曲信息数组-------------%@",musicInfoArr);
- 210. if ([musicInfoArr count]) {
- 211. return musicInfoArr;
- 212. }
- 213. return nil;
- 214.}
- 215.
- 216.- (NSString*)lyricsWithSong:(NSString*)sName {
- 217. return nil;
- 218.}
- 219.
- 220.- (NSArray*)allArtistsListOfBaiduMp3{
- 221. return nil;
- 222.}
- 223.- (NSArray*)artistsListOfBaiduMp3ByFilterCharacter:(NSString*)character{
- 224. return nil;
- 225.}
- 226.
- 227.@end
复制代码 |
|