Greasy Fork

百度音乐下载

bdmusicdownload

当前为 2014-08-08 提交的版本,查看 最新版本

// ==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.05
// @grant       GM_xmlhttpRequest
// @run-at      document-end
// ==/UserScript==
$(document) .ready(function () {
  main = {
    _bdiduid: '',
    songid: '',
    get_bdiduid: function () {
      if (this._bdiduid == '') {
        this._bdiduid = document.cookie.substring(8, 40);
      }
    },
    getsongid: function () {
      this.songid = location.pathname.substr(6);
    },
    getsonginfo: function (callback) {
      this.callbackvar = null;
      //清空
      GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://yinyueyun.baidu.com/data/cloud/download?songIds=' + this.songid,
        onload: function (response) {
          var json = eval('(' + response.responseText + ')');
          if (json.data.data) {
            callback(json);
          }
        },
        onerror: function () {
          //alert("好像发生错误..");
        },
      });
    },
    getcollection: function (callback) {
      GM_xmlhttpRequest({
        method: 'GET',
        url: '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',
        onload: function (response) {
          json = eval('(' + response.responseText + ')');
          callback(json);
        },
        onerror: function () {
          //alert("好像发生错误..");
        },
      });
    },
    addfavorite: function (callback) {
      GM_xmlhttpRequest({
        method: 'GET',
        url: '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=' + this.songid + '&time=' + (Math.round(new Date() .getTime() / 1000)),
        onload: function (response) {
          var json = eval('(' + response.responseText + ')');
          if (json.error_code == '22322') {
            callback(json);
          } 
          else {
            callback(json);
          }
        },
        onerror: function () {
          //alert("好像发生错误..");
        },
      });
    },
    isfavorite: function (json) {
      if (json.total > 1500) {
        alert('你需要清理百度云音乐才可以继续使用');
        location.assign('http://yinyueyun.baidu.com');
        return
      }
      if (json.error_code == 22000) {
        json.result.find(function (item) {
          if (item.song_id == this.songid) {
            return true;
          }
        });
      }
    },
    getdownloadurl: function (rate) {
      //无损
      if (eval(rate) > 320) {
        return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + this.songid + '&rate=' + rate + '&format=flac';
      }
      return 'http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=' + this.songid + '&rate=' + rate + '&format=mp3';
    }
  };
  main.get_bdiduid();
  main.getsongid();
  if (main._bdiduid == '' || main.songid == '') return
  //main.getsonginfo();
  //克隆
  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=\'无损\' href=\'javascript:void(0);\'>无损</a>');
  $('.bdmdl') .hover(function () {
    $('.bdmdl-hovermenu') .toggle();
  });
  $('.bdmdl-rate') .click(function () {
    var t = $(this);
    var rate = t.attr('id');
    var result = null;
    //!!
    var timeout = 1500;
    var downloadurl = '';
    t.text('获取中...');
    main.getcollection(function (data) {
      result = data
    });
    setTimeout(function () {
      if (!main.isfavorite(result)) {
        main.addfavorite(function (data) {
          result = data
        });
        if (result.error_code == '22322') {
          // success
        }
      }
    }, timeout);
    setTimeout(function () {
      if (rate == '无损') {
        main.getsonginfo(function (data) {
          result = data
        });
        setTimeout(function () {
          if (result.data.data.flac) {
            downloadurl = main.getdownloadurl(result.data.data.flac.rate);
            location.assign(downloadurl);
            t.attr('href', downloadurl);
            t.unbind('click');
            t.text('无损');
          } 
          else {
            t.text('没有无损资源');
          }
          //原本承担更多时间,减去

        }, timeout - 500)
      } 
      else {
        downloadurl = main.getdownloadurl(rate);
        location.assign(downloadurl);
        t.attr('href', downloadurl);
        t.unbind('click');
        t.text(rate + 'kbps');
      }
    }, timeout + 2000);
  });
});