{"id":836,"date":"2013-10-25T11:04:26","date_gmt":"2013-10-25T03:04:26","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=836"},"modified":"2013-10-25T11:06:48","modified_gmt":"2013-10-25T03:06:48","slug":"%e4%b8%8b%e8%bd%bd%e7%99%be%e5%ba%a6%e9%9f%b3%e4%b9%90%e6%97%a0%e6%8d%9f%e5%93%81%e8%b4%a8%e9%9f%b3%e4%b9%90","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=836","title":{"rendered":"\u4e0b\u8f7d\u767e\u5ea6\u97f3\u4e50\u65e0\u635f\u54c1\u8d28\u97f3\u4e50"},"content":{"rendered":"<p><meta http-equiv=\"content-type\" content=\"text\/html; charset=utf-8\" \/><br \/>\n\u4f7f\u7528\u767e\u5ea6\u97f3\u4e50\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6536\u85cf\uff0c\u6536\u85cf\u540e\u7684\u97f3\u4e50\u5217\u8868\u4f1a\u4fdd\u5b58\u5728http:\/\/yinyueyun.baidu.com\u4e2d\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e0b\u8f7d\u94fe\u63a5\u3002\u7528java\u7b80\u5355\u5199\u4e86\u4e2a\u4e0b\u8f7d\u81ea\u5df1\u6536\u85cf\u97f3\u4e50\u7684\u7a0b\u5e8f\uff0c\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u5bf9\u4e8eVIP\u7684\u65e0\u635f\u97f3\u4e50\uff0c\u4e0b\u8f7d\u94fe\u63a5\u5df2\u7ecf\u7ed9\u51fa\u6765\u4e86\uff0c\u53ea\u662f\u5728\u524d\u53f0\u505a\u4e86\u9650\u5236\uff0c\u8fd9\u4e2a\uff0c\u8fd9\u4e2a\u3002\u3002\u3002\u3002\uff08Bug\u5df2\u7ecf\u62a5\u7ed9\u767e\u5ea6\u97f3\u4e50\uff0c\u8fd9\u91cc\u7684\u4ee3\u7801\u53ea\u4e3a\u65b9\u4fbf\u81ea\u5df1\u548c\u4ee3\u7801\u5206\u4eab\uff09\u3002<\/p>\n<p>\u672c\u6765\u60f3\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u767b\u9646\u767e\u5ea6\u8d26\u53f7\uff0c\u53ef\u662f\u5728\u767b\u9646\u65f6\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53c2\u6570<code>callback<\/code>\uff0c\u8fd9\u4e2a\u53c2\u6570\u4e0d\u662f\u670d\u52a1\u5668\u53d1\u9001\u8fc7\u6765\u7684\uff0c\u6000\u7591\u662f\u6839\u636e\u4ec0\u4e48\u53c2\u6570\u5728\u672c\u5730\u8ba1\u7b97\u51fa\u6765\u7684\uff0c\u770b\u4e0d\u592a\u61c2js\uff0c\u8fd9\u4e2a\u529f\u80fd\u5c31\u6ca1\u5b9e\u73b0\u3002<\/p>\n<p>\u4f7f\u7528\u6b65\u9aa4\u5982\u4e0b\uff1a<!--more--><\/p>\n<ul>\n<li>\u4ee3\u7801jar\u5305\u4f7f\u7528maven\u7ba1\u7406\uff0c\u9700\u8981\u53bbapache\u7f51\u7ad9\u4e0b\u8f7dmaven\uff0c\u7136\u540e\u5728\u9879\u76ee\u6839\u8def\u5f84<code>mvn install<\/code><\/li>\n<li>\u6d4f\u89c8\u5668\u767b\u9646\u767e\u5ea6\u8d26\u53f7\uff0c\u67e5\u770b\u6d4f\u89c8\u5668\u4e2d\u7684cookie\uff0c\u53d6\u51fa\u540d\u79f0\u4e3a<code>BDUSS<\/code>\u7684cookie\u7684\u503c\uff0c\u628a\u5b83\u590d\u5236\u5230<code>com.yeetrack.yinyueyun<\/code>\u5305\u4e0b\u7684<code>HttpTool.java<\/code>\u4e2d\u7684<code>cookieValue<\/code>\u4e2d\u3002<\/li>\n<li>\u6267\u884c<code>MusicDownload.java<\/code>\u4e2d\u7684main\u65b9\u6cd5\uff0c\u5373\u53ef\u3002<\/li>\n<\/ul>\n<p>\u4e0b\u8f7d\u4e0b\u6765\u7684\u97f3\u4e50\uff0c\u4fdd\u5b58\u5728\u9879\u76ee\u6839\u8def\u5f84\u4e0b\u7684music\u6587\u4ef6\u5939\uff0c\u9ed8\u8ba4\u4e0b\u8f7d\u97f3\u8d28\u6700\u597d\u7684\u3002<br \/>\n\u5168\u90e8\u4ee3\u7801\u5730\u5740\uff1ahttp:\/\/www.oschina.net\/code\/snippet_147181_25883<br \/>\n\u4e0b\u9762\u662f\u4e3b\u51fd\u6570\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>    List&lt;String&gt; idList = new ArrayList&lt;String&gt;();\n\n    CloseableHttpClient httpClient = HttpTool.getHttpClient();\n\n    HttpGet get = new HttpGet(\"http:\/\/yinyueyun.baidu.com\/data\/cloud\/collection?type=song&amp;start=0&amp;size=2000&amp;_=\"+System.currentTimeMillis());\n\n    CloseableHttpResponse response = httpClient.execute(get);\n\n    String result = EntityUtils.toString(response.getEntity(), \"utf-8\");\n    get.releaseConnection();\n\n    StringBuffer songIds = new StringBuffer();\n    int start = result.indexOf(\"\\\"id\\\":\");\n    int end = -1;\n    if(start != -1)\n        end = result.indexOf(\",\", start);\n    while(start != -1 &amp;&amp; end != -1)\n    {\n        songIds.append(result.substring(start+5, end)+\",\");\n        idList.add(result.substring(start+5, end));\n        start = result.indexOf(\"\\\"id\\\":\", end);\n        if(start != -1)\n            end = result.indexOf(\",\", start);\n    }\n    String songidString = songIds.substring(0, songIds.length()-1);\n\n    \/\/\u83b7\u53d6\u97f3\u4e50\u7684\u8be6\u7ec6\u4fe1\u606f\n    HttpPost songInfoPost = new HttpPost(\"http:\/\/yinyueyun.baidu.com\/data\/cloud\/songinfo\");\n    List&lt;BasicNameValuePair&gt; params = new ArrayList&lt;BasicNameValuePair&gt;();\n    params.add(new BasicNameValuePair(\"songIds\", songidString));\n    params.add(new BasicNameValuePair(\"type\", \"\"));\n    params.add(new BasicNameValuePair(\"rate\", \"\"));\n    params.add(new BasicNameValuePair(\"pt\", \"0\"));\n    params.add(new BasicNameValuePair(\"flag\", \"\"));\n    params.add(new BasicNameValuePair(\"s2p\", \"\"));\n    params.add(new BasicNameValuePair(\"prerate\", \"\"));\n    params.add(new BasicNameValuePair(\"bwt\", \"\"));\n    params.add(new BasicNameValuePair(\"dur\", \"\"));\n    params.add(new BasicNameValuePair(\"bat\", \"\"));\n    params.add(new BasicNameValuePair(\"bp\", \"\"));\n    params.add(new BasicNameValuePair(\"pos\", \"\"));\n    params.add(new BasicNameValuePair(\"auto\", \"\"));\n    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, \"utf-8\");\n    songInfoPost.setEntity(formEntity);\n    CloseableHttpResponse songInfoResponse = httpClient.execute(songInfoPost);\n    String jsonResult = EntityUtils.toString(songInfoResponse.getEntity());\n    songInfoPost.releaseConnection();\n\n    SongInfoEntity songInfoEntity = JSON.parseObject(jsonResult, SongInfoEntity.class);\n\n    \/\/\u4e0b\u8f7d\u97f3\u4e50\n    HttpGet songGet = null;\n    for(SongEntity song : songInfoEntity.getData().getSongList())\n    {\n\n        \/\/\u9700\u8981\u6839\u636esong id\u6765\u83b7\u53d6\u6bcf\u9996\u6b4c\u66f2\u5b58\u5728\u7684\u7801\u7387\n        HttpGet songRateGet = new HttpGet(\"http:\/\/yinyueyun.baidu.com\/data\/cloud\/download?songIds=\"+song.getSongId());\n        CloseableHttpResponse songRateResponse = httpClient.execute(songRateGet);\n        Map&lt;Integer, String&gt; rateMap = SongDataUtil.getRateAndFormat(EntityUtils.toString(songRateResponse.getEntity()));\n        if(rateMap == null || rateMap.size()==0)\n            continue;\n        \/\/\u53ef\u4ee5\u904d\u5386treemap\uff0c\u6765\u4e0b\u8f7d\u591a\u79cd\u683c\u5f0f\u7684\u97f3\u4e50\uff0c\u8fd9\u91cc\u53ea\u4e0b\u8f7d\u6700\u4f18\u8d28\u7684\n        Integer rateMax = null;\n        String format = null;\n        Iterator&lt;Integer&gt; it = rateMap.keySet().iterator();\n        while (it.hasNext())\n        {\n            \/\/it.next()\u5f97\u5230\u7684\u662fkey\uff0ctm.get(key)\u5f97\u5230obj\n            rateMax = it.next();\n        }\n        format = rateMap.get(rateMax);\n\n        \/\/\u5f00\u59cb\u4e0b\u8f7d\u97f3\u4e50\n        songGet = new HttpGet(\"http:\/\/yinyueyun.baidu.com\/data\/cloud\/downloadsongfile?songIds=\"+song.getSongId()+\"&amp;rate=\"+rateMax.toString()+\"&amp;format=\"+format);\n        \/\/songGet = new HttpGet(\"http:\/\/yinyueyun.baidu.com\/data\/cloud\/downloadsongfile?songIds=83590931&amp;rate=941&amp;format=flac\");\n        songGet.addHeader(\"User-Agent\", \"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/30.0.1599.101 Safari\/537.36\");\n\n        InputStream inputStream = null;\n        FileOutputStream fileOutputStream = null;\n\n        CloseableHttpResponse songResponse = httpClient.execute(songGet);\n\n        if(!songResponse.getStatusLine().toString().contains(\"302\"))\n            continue;\n\n        \/\/\u83b7\u53d6\u91cd\u5b9a\u5411url,\u8be5url\u662f\u6700\u7ec8\u7684\u97f3\u4e50\u8d44\u6e90\u5730\u5740\n        String redictUrl = null;\n        for(Header header : songResponse.getAllHeaders())\n        {\n            if(\"Location\".equals(header.getName()))\n                redictUrl = header.getValue();\n        }\n        songGet.releaseConnection();\n\n        HttpGet mp3Get = new HttpGet(redictUrl);\n        System.out.println(redictUrl+\"---\"+song.getSongName()+\"---&gt;\u6b63\u5728\u4e0b\u8f7d...\");\n        CloseableHttpResponse mp3Response = httpClient.execute(mp3Get);\n\n            inputStream = mp3Response.getEntity().getContent();\n            fileOutputStream = new FileOutputStream(new File(\"music\/\"+song.getSongName()+\"-\"+song.getArtistName()+\".\"+format));\n\n                    byte[] bytes = new byte[102400];\n                    int len = 0;\n                    while((len=inputStream.read(bytes))!= -1)\n                    {\n                        fileOutputStream.write(bytes, 0 ,len);\n                        fileOutputStream.flush();\n                    }\n\n        System.out.println(song.getSongName()+\"---\u4e0b\u8f7d\u5b8c\u6bd5\");\n        fileOutputStream.close();\n        \/\/songGet.reset();\n\n    }\n    httpClient.close();\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u767e\u5ea6\u97f3\u4e50\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6536\u85cf\uff0c\u6536\u85cf\u540e\u7684\u97f3\u4e50\u5217\u8868\u4f1a\u4fdd\u5b58\u5728http:\/\/yinyueyun.baidu.com\u4e2d\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e0b\u8f7d\u94fe\u63a5\u3002\u7528java\u7b80\u5355\u5199\u4e86\u4e2a\u4e0b\u8f7d\u81ea\u5df1\u6536\u85cf\u97f3\u4e50\u7684\u7a0b\u5e8f\uff0c\u8fc7\u7a0b\u4e2d\u53d1\u73b0\u5bf9\u4e8eVIP\u7684\u65e0\u635f\u97f3&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,15,7,4],"class_list":["post-836","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-15","tag-7","tag-4"],"views":5357,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=836"}],"version-history":[{"count":2,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions"}],"predecessor-version":[{"id":838,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions\/838"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}