关闭

音乐API

标签: 音乐api
2990人阅读 评论(1) 收藏 举报
分类:

博主在前几篇博客中介绍了小Q聊天机器人的源码及其包含的一些功能,并在应用市场上上线了一个版本,其中有一个功能是歌曲搜索,即输入格式为“歌曲#歌曲名#歌手”即可搜索出相应的歌曲并进行播放,不过最近两天在使用时发现一只提示“歌曲获取失败”,于是在代码中打印了一些log,发现该音乐搜索api(抓自百度音乐盒)失效,其实也不算失效,只是返回的数据为空了!不知道是间歇性的,还是百度发现后封了,或者更换了链接?如果因此砍掉小Q的歌曲搜索功能,那么可玩性就降低了好多,不好不好!那怎么办,接着抓呗~

至于抓包方法,不再写了,百度上多的是,博主也偷了个懒,也是参考了网上的相关方法,最终抓取到了千千静听的音乐搜索链接!不过,它这个稍有点麻烦,我们想要的是传入一个歌曲名即可得到音乐链接,而这个抓取到的没有直接通过歌曲名获得歌曲链接的方法,多了一步,即:先通过歌曲名获得歌曲id->再通过歌曲id获得歌曲链接:

第一步:获取歌曲id

URL:
http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=

参数query的value即歌曲名!

例:传入”咱们屯里的人“:
http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=咱们屯里的人

返回结果(json):

{"song":[{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人","artistname":"罗凯

楠","control":"0000000000","songid":"251817888","has_mv":"0","encrypted_songid":"0307f026fa00956e65bbcL"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人","artistname":"赵本

山","control":"0000000000","songid":"13766076","has_mv":"1","encrypted_songid":"2706d20dbc09561cfbf7L"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人","artistname":"国民美少

女","control":"0000000000","songid":"262773592","has_mv":"0","encrypted_songid":"7007fa99b580956f79be6L"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人","artistname":"吉特

巴","control":"0000000000","songid":"85496774","has_mv":"0","encrypted_songid":"770751893c609561cf1e9L"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人 - 廖弟广场舞 编舞 廖弟","artistname":"吉特

巴","control":"0000000000","songid":"85026906","has_mv":"0","encrypted_songid":"6307511685a09561d512cL"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"咱们屯里的人 - 刘晓 dj杉毛制作吉特

巴","artistname":"htall","control":"0000000000","songid":"64795854","has_mv":"0","encrypted_songid":"57073dcb4ce09561d1261L"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"0","songname":"x240","artistname":"咱们屯里的人,博白广场舞,320,反

面","control":"0000000000","songid":"85496800","has_mv":"0","encrypted_songid":"040751893e009561cf1e9L"},{"bitrate_fee":"{\"0\":\"0|0\",\"1\":\"0|0\"}","yyr_artist":"1","songname":"罗凯楠-咱们屯里的人","artistname":"Mc九

染","control":"0000000000","songid":"74120660","has_mv":"0","encrypted_songid":""}],"error_code":22000,"order":"song"}

我们看到,返回的其实是一个列表,其中的songid即为歌曲id!这么多id,id三千你只取一个听就行了,一般第一条数据与你的搜索意向最接近,所以我们取出第一条数据就行了,搜索歌曲地址时同理!(当然,也可根据自己的需求去取啦!)

第二步:获取歌曲地址

URL:
http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.song.getInfos&format=json
&ts=1408284347323&e=JoN56kTXnnbEpd9MVczkYJCSx%2FE1mkLx%2BPMIkTcOEu4%3D
&nw=2&ucf=1&res=1&songid=

参数songid即歌曲id,从第一步获得传入即可!

接着上一步的例子:
http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.song.getInfos&format=json
&ts=1408284347323&e=JoN56kTXnnbEpd9MVczkYJCSx%2FE1mkLx%2BPMIkTcOEu4%3D
&nw=2&ucf=1&res=1&songid=251817888

返回结果(json):

{"songurl":{"url":[{"show_link":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/4fae6f8e86d0a2ff3266ab48a57c3503\/255805332\/255805332.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"down_type":0,"original":0,"free":1,"replay_gain":"0.709999","song_file_id":255805332,"file_size":1097845,"file_extension":"mp3","file_duration":137,"can_see":1,"can_load":true,"preload":40,"file_bitrate":64,"file_link":"http:\/\/yinyueshiting.baidu.com\/data2\/music\/4fae6f8e86d0a2ff3266ab48a57c3503\/255805332\/255805332.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"is_udition_url":0,"hash":"3df112b1212f39acc25343da2ff11407eac52387"},{"show_link":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/fd50ddbbebe1ea4f189ac5285f7d124e\/255805294\/255805294.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"down_type":1,"original":0,"free":1,"replay_gain":"0.730003","song_file_id":255805294,"file_size":2194361,"file_extension":"mp3","file_duration":137,"can_see":1,"can_load":true,"preload":80,"file_bitrate":128,"file_link":"http:\/\/yinyueshiting.baidu.com\/data2\/music\/fd50ddbbebe1ea4f189ac5285f7d124e\/255805294\/255805294.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"is_udition_url":1,"hash":"777018a26271617c8419a9e97db81e9ac01ff69d"},{"show_link":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/d37b40f4f8ea042560ad5c0947af19a5\/255805243\/255805243.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955","down_type":0,"original":0,"free":1,"replay_gain":"0.559998","song_file_id":255805243,"file_size":3291883,"file_extension":"mp3","file_duration":137,"can_see":1,"can_load":true,"preload":120,"file_bitrate":192,"file_link":"http:\/\/yinyueshiting.baidu.com\/data2\/music\/d37b40f4f8ea042560ad5c0947af19a5\/255805243\/255805243.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"is_udition_url":0,"hash":"c4b6aa68e3652402deff50709afbbf8d379ed0f7"},{"show_link":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/a9e5349e5a23681f265c9cfb47d63fd6\/255805201\/255805201.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955","down_type":0,"original":0,"free":1,"replay_gain":"0.339996","song_file_id":255805201,"file_size":4388399,"file_extension":"mp3","file_duration":137,"can_see":1,"can_load":true,"preload":160,"file_bitrate":256,"file_link":"http:\/\/yinyueshiting.baidu.com\/data2\/music\/a9e5349e5a23681f265c9cfb47d63fd6\/255805201\/255805201.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"is_udition_url":0,"hash":"05ddb88d4ef987f56ac267cdab3e810715afed14"},{"show_link":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/4d57a2ac3e57f5ec7ee392315808cc00\/255805096\/255805096.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"down_type":2,"original":0,"free":1,"replay_gain":"0.339996","song_file_id":255805096,"file_size":5483909,"file_extension":"mp3","file_duration":137,"can_see":1,"can_load":true,"preload":200,"file_bitrate":320,"file_link":"http:\/\/yinyueshiting.baidu.com\/data2\/music\/4d57a2ac3e57f5ec7ee392315808cc00\/255805096\/255805096.mp3?xcode=3f0d256c45bc24ec7ddf74b1a6ef6955",

"is_udition_url":0,"hash":"c6fb15c83d5f9f270f3a6b9e4eafcc5f9189f56e"},{"show_link":"","down_type":0,"original":0,"free":1,"replay_gain":"1.209999","song_file_id":255804950,"file_size":18190765,"file_extension":"flac","file_duration":137,"can_see":1,"can_load":true,"preload":663.75,"file_bitrate":1062,"file_link":"","is_udition_url":0,"hash":"fa6b2a66a189001297cbc1c8f2155b40ac6edf84"}]},

"error_code":22000,"songinfo":{"resource_type_ext":"0","pic_huge":"http:\/\/musicdata.baidu.com\/data2\/pic\/255805384\/255805384.jpg","resource_type":"0","del_status":"0","album_1000_1000":"http:\/\/musicdata.baidu.com\/data2\/pic\/255805384\/255805384.jpg","pic_singer":"","album_500_500":"http:\/\/a.hiphotos.baidu.com\/ting\/pic\/item\/c75c10385343fbf2c9c771b4b67eca8064388fba.jpg","havehigh":2,"piao_id":"0","song_source":"web","korean_bb_song":"0","compose":"","toneid":"0","area":"0","original_rate":"","bitrate":"64,128,192,256,320,1062","artist_500_500":"http:\/\/musicdata.baidu.com\/data2\/pic\/106116271\/106116271.jpg","multiterminal_copytype":"1110,1101,1011,0111","has_mv":0,"file_duration":"0","album_title":"夏洛特烦恼 电影原声带","sound_effect":"0","title":"咱们屯里的人","high_rate":"320","pic_radio":"http:\/\/musicdata.baidu.com\/data2\/pic\/255805386\/255805386.jpg",

"is_first_publish":0,"hot":"125867","language":"粤语","lrclink":"http:\/\/musicdata.baidu.com\/data2\/lrc\/f17378837196eea07b2868eae2965411\/263504354\/263504354.lrc","distribution":"0000000000,0000000000,0000000000,0000000000,0000000000,0000000000,0000000000,0000001111,0000001111,0000000000","relate_status":"0","learn":0,"play_type":0,"pic_big":"http:\/\/musicdata.baidu.com\/data2\/pic\/255805392\/255805392.jpg","pic_premium":"http:\/\/a.hiphotos.baidu.com\/ting\/pic\/item\/c75c10385343fbf2c9c771b4b67eca8064388fba.jpg","artist_480_800":"http:\/\/musicdata.baidu.com\/data2\/pic\/105454692\/105454692.jpg","aliasname":"","country":"内地","artist_id":"251817690","album_id":"247551805","original":0,"compress_status":"0","versions":"影视原声",

"expire":36000,"ting_uid":"239546327","artist_1000_1000":"http:\/\/b.hiphotos.baidu.com\/ting\/pic\/item\/a1ec08fa513d269770633a5257fbb2fb4316d808.jpg","all_artist_id":"251817690","artist_640_1136":"http:\/\/musicdata.baidu.com\/data2\/pic\/105454694\/105454694.jpg","publishtime":"2015-10-02","charge":0,"copy_type":"0","songwriting":"","share_url":"http:\/\/music.baidu.com\/song\/251817888","author":"罗凯楠","has_mv_mobile":0,"all_rate":"64,128,192,256,320,flac","pic_small":"http:\/\/musicdata.baidu.com\/data2\/pic\/255805397\/255805397.jpg","album_no":"2","song_id":"251817888","is_charge":"0"}}

依然是一个列表,我们来找下其中的歌曲链接:show_link和file_link,注意我们需要取得是file_link,你可以试一下show_link,会提示401拒绝访问,原因你就不用管了!再往下看,字段songinfo下则包含的是歌曲的一些信息,其中对我们有用的两个字段:share_url分享url打开链接如图:

这里写图片描述

歌词:lrclink;
海报:pic_huge;

其他字段就自己发掘吧~

好了,说了这么多demo何在?别急,先看效果图:

这里写图片描述

当然,这个也会有失效的可能,到时来一句偶滴老嘎,继续抓包呗!

EcDemo下载地址:http://download.csdn.net/detail/baiyuliang2013/9530532

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:303948次
    • 积分:4386
    • 等级:
    • 排名:第7267名
    • 原创:94篇
    • 转载:10篇
    • 译文:0篇
    • 评论:383条
    我的微博
    微信公众号AndroidMax
    博客专栏
    最新评论