批量下载豆瓣fm中收藏的音乐

本人是豆瓣fm的忠实用户,听的音乐多了,收藏的也比较多了,虽然豆瓣fm的app中可以离线下载收藏的音乐,但是还是希望能把音乐下载下来放在别的地方播放,比如放在U盘里在车里听。
过程很简单,在chrome、或者firefox中分析豆瓣账号的登录过程,然后再获取到收藏的音乐列表,然后获取到资源地址,就可以下载了。过程分析完了,用代码实现出来就完了。主要的点有如下几个:

  1. 账号登录,要下载自己收藏的音乐,登录是必不可少的,好在豆瓣的账号没有验证码,否则还得识别验证码,就比较麻烦了。
    HttpRequest login = HttpRequest.post("https://accounts.douban.com/j/popup/login/basic")
            .query("captcha_id", "")
            .query("captcha_solution", "")
            .query("ck", "")
            .query("name", username)
            .query("password", password)
            .query("referer", "https://douban.fm/")
            .query("source", "fm")
            .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
            .header("X-Requested-With", "XMLHttpRequest");
    
  2. 获取收藏的音乐列表,登录成功之后,把cookie保留下来,访问接口获取收藏列表
    HttpResponse getResponse = httpBrowser.sendRequest(HttpRequest.get("https://douban.fm/j/v2/redheart/basic"));
    String sidSongs = getResponse.bodyText();
    
  3. 获取音乐的真实地址
    HttpRequest request = HttpRequest.post("https://douban.fm/j/v2/redheart/songs")
            .header("Referer", "https://douban.fm/mine/")
            .form("sids", sids)
            .form("kbps", "320") //这是音乐的码率,直接写死,获取最高的
            .form("ck", ckValue);
    
  4. 真实地址获取到之后,直接下载保存就好了。

完整程序传到开源中国的git上了,地址:https://gitee.com/yeetrack/yinyueyun,具体用法可在README中看到。声明一下大家自己玩一玩就好了,禁止用于非法抓取,我自己是对豆瓣这个公司蛮有好感的,在充满欲望的互联网社会里,慢悠悠地做自己的产品。执行效果如下所示:

    0 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - login success
    137 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - song size: 307
    1099 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/df8698d865732dbed38312a6066f9bab/0/fm/song/p1827311_64k.mp3, Singin' In The Rain
    2379 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/39fd3ee6d6083c3f57a46fd61400f316/0/fm/song/p2236707_128k.mp3, 忆江南
    4255 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/6b7e9230f6b398e87e58359fa3c9609b/0/fm/song/p29191_128k.mp3, Angel Of Darkness
    6245 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/634dd44ec7c409204b019f86ca3da873/2/fm/song/p1769120_128k.mp3, 華
    9162 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/e0f8ef4af2500adbdf288a20f3e16168/0/fm/song/p253711_128k.mp3, Forever Now
    11026 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/b7a01d85b41b19f015f94f9ca10fc251/2/fm/song/p1809554_128k.mp3, Bamboo
    12471 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/daeccbb9a6f2750483a0b34fd5f7314c/3/fm/song/p1815970_128k.mp3, 里山
    14634 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/9501578f27462c45b815aadc8eee5a8e/0/fm/song/p1941988_128k.mp3, Craigie Hill
    16536 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/4164cdd37579ac1d6b744111e6a82810/0/fm/song/p1985709_128k.mp3, Lighthouse
    17891 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/6b060ab2148a8dd43dab9d29a71eef0e/0/fm/song/p1818274_64k.mp3, Summer (BONUS TRACK)
    18806 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/b24893872dca877af260fd8b05594bca/0/fm/song/p1452822_128k.mp3, 時の向こう 幻の空
    22142 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/10c4a8387d275afec303af13a61f399a/0/fm/song/p1532891_128k.mp3, It Is A Small World
    23798 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/88e505c964d03c042de7b7641b06cefa/1/fm/song/p1532892_128k.mp3, Mary Had A Little Lamb
    25491 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/1d391c8c5b29e08003433dbba7142381/0/fm/song/p580597_128k.mp3, 春よ?来い
    27099 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/550f62ae833a7e96b565a69300274025/1/fm/song/p1435805_128k.mp3, Позови меня тихо по имени
    28006 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr1.doubanio.com/b3fa3f936c1ddf7fa9f395cc0200fcff/0/fm/song/p230812_128k.mp3, Mae Bruxa
    30035 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/aca5f926b60c012ed08941a59befa048/0/fm/song/p1389939_128k.mp3, Hodoo
    30961 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/7374d7cfb03e1c61e427d4aa8a767a13/0/fm/song/p321214_128k.mp3, The Sound Of Silence
    32093 [main] INFO com.yeetrack.baidu.yinyueyun.DoubanMain  - http://mr3.doubanio.com/af7282d876820b7aff3848e2fde4f112/0/fm/song/p714967_128k.mp3, 百鬼夜行抄

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=1342