免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1781 | 回复: 1
打印 上一主题 下一主题

获得百度音乐盒音乐的下载地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-12 22:33 |只看该作者 |倒序浏览

获得百度音乐盒音乐的下载地址







iphonebaidumusicbox.




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

论坛徽章:
0
2 [报告]
发表于 2012-03-12 22:34 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP