Greasy Fork

百度音乐下载

bdmusicdownload

目前为 2014-08-07 提交的版本。查看 最新版本

// ==UserScript==
// @name        百度音乐下载
// @namespace   [email protected]
// @description bdmusicdownload
// @author		  thunderhit
// @include     http://music.baidu.com/song/*
// @require     http://code.jquery.com/jquery-2.1.1.min.js
// @version     1.02
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==
$(document) .ready(function () {
  main = {
    _bdiduid: '',
    get_bdiduid: function () {
      if (this._bdiduid == '') {
        this._bdiduid = document.cookie.substring(8, 40);
      }
    },
    getsongid: function ()
    {
      if (location.pathname.indexOf('download') != - 1) {
        document.body.write('<p>BD音乐下载助手告诉你,暂时不支持本页面</p>');
      } 
      else {
        return location.pathname.substr(6);
      }
    },
    getcollection: function () {
      return 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.favorite.getCollectSong&format=json&from=bmpc&version=1.0.0&version_d=9.0.4.7&bdiduid=' + this._bdiduid + '&pn=0&rn=50';
    },
    favorite: function (songid) {
      return 'http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.favorite.addSongFavorite&format=json&from=bmpc&version=1.0.0&version_d=9.0.4.7&baiduid=' + this.bdiduid + '&songId=' + songid + '&time=' + (Math.round(new Date() .getTime() / 1000));
    },
    downloadurl: function (songid, rate) {
      //假设无损码率为999
      //if (rate == '999') {
      //  return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + songid + '&rate=' + rate + '&format=flac';
      //} 
      return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + songid + '&rate=' + rate + '&format=mp3';
    }
  };
  main.get_bdiduid();
  if (main._bdiduid == '') return
  //克隆
  var dlbutton = $('.add-song-btn') .clone();
  $('.btn.btn-b.down-song-btn') .after(dlbutton);
  $(dlbutton) .addClass('bdmdl');
  //替换图标
  $('.bdmdl i') .removeClass('icon btn-icon-add');
  $('.bdmdl i') .addClass('icon btn-icon-down');

  $('.addlayer', $('.bdmdl')) .addClass('bdmdl-hovermenu');
  $('.txt', $('.bdmdl')) .html('<font color=\'green\'>下载</font>');
  //移除原来标签
  $('a', $('.bdmdl-hovermenu')) .remove();
  $('.bdmdl-hovermenu') .append('<a class=\'bdmdl-rate\' id=\'128\' href=\'javascript:void(0);\'>128kbps</a><a class=\'bdmdl-rate\' id=\'256\' href=\'javascript:void(0);\'>256kbps</a><a class=\'bdmdl-rate\' id=\'320\' href=\'javascript:void(0);\'>320kbps</a> ');
  //<a class=\'bdmdl-rate\' id=\'999\' href=\'javascript:void(0);\'>无损</a>'
  $('.bdmdl') .hover(function () {
    $('.bdmdl-hovermenu') .toggle();
  });
  $('.bdmdl-rate') .click(function () {
    var songid = main.getsongid();
    var t = $(this);
    t.text('获取中...');
    var rate = t.attr('id');
    //add favorite
    GM_xmlhttpRequest({
      method: 'GET',
      url: main.favorite(songid),
      onload: function (response) {
        //延时执行,避免被识别
        setTimeout(function () {
          t.text(rate + 'kbps');
          var json = eval('(' + response.responseText + ')');
          if (json.error_code == '22322') {
            location.assign(main.downloadurl(songid, rate));
          } 
          else {
            alert('好像发生了什么错误。');
          }
          //等待时间
        }, 6540);
      },
      onerror: function (d) {
        alert('获取失败 #0');
      },
    });
  });
});