class NetEase

Constants

TIMEOUT

Public Class Methods

new() click to toggle source
# File lib/mdisc/api.rb, line 8
def initialize
  @header = {
    "Accept"          => "*/*",
    "Accept-Encoding" => "gzip,deflate,sdch",
    "Accept-Language" => "zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4",
    "Connection"      => "keep-alive",
    "Content-Type"    => "application/x-www-form-urlencoded",
    "Host"            => "music.163.com",
    "Referer"         => "http://music.163.com/",
    "User-Agent"      => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"
  }

  Unirest.timeout TIMEOUT
end

Public Instance Methods

album(album_id) click to toggle source

album id -> song id set

# File lib/mdisc/api.rb, line 115
def album(album_id)
  action = "http://music.163.com/api/album/#{album_id}"
  data = http_request('GET', action)

  data['album']['songs']
end
artists(artist_id) click to toggle source

Songs to which a artist belongs.

# File lib/mdisc/api.rb, line 107
def artists(artist_id)
  action = "http://music.163.com/api/artist/#{artist_id}"
  data = http_request('GET', action)

  data['hotSongs']
end
channel_detail(channelids) click to toggle source

DJchannel (id, channel_name) ids -> song urls (details) channels -> songs

# File lib/mdisc/api.rb, line 153
def channel_detail(channelids)
  return [] if channelids.empty?

  channels = []

  # ["xxxxxx"] -> "xxxxxx"
  channelids.each do |c|
    action = "http://music.163.com/api/dj/program/detail?id=#{c.join('')}"
    begin
      data = http_request('GET', action)
      channel = dig_info(data['program']['mainSong'], 'channels')
      channels.push channel
    rescue => e
      next
    end
  end

  channels
end
dig_info(data, dig_type) click to toggle source
# File lib/mdisc/api.rb, line 173
def dig_info(data, dig_type)
  tmp = []

  case dig_type
  when 'songs'
    data.each do |song|
      song_info = {
        "song_id" => song['id'],
        "artist" => [],
        "song_name" => song['name'],
        "album_name" => song['album']['name'],
        "mp3_url" => song['mp3Url']
      }

      if song.include? 'artist'
        song_info['artist'] = song['artist'].join('')
      elsif song.include? 'artists'
        song['artists'].each do |artist|
          song_info['artist'].push artist['name'].strip
        end
      else
        song_info['artist'] = '未知艺术家'
      end

      song_info['artist'] = song_info['artist'].join(',')
      tmp.push song_info
    end

  when 'artists'
    data.each do |artist|
      artists_info = {
        "artist_id" => artist['id'],
        "artists_name" => artist['name'],
        "alias" => artist['alias'].join('')
      }
      tmp.push artists_info
    end

  when 'albums'
    data.each do |album|
      albums_info = {
        "album_id" => album['id'],
        "albums_name" => album['name'],
        "artists_name" => album['artist']['name']
      }
      tmp.push albums_info
    end

  when 'playlists'
    data.each do |playlist|
      playlists_info = {
        "playlist_id" => playlist['id'],
        "playlists_name" => playlist['name'],
        "creator_name" => playlist['creator']['nickname']
      }
      tmp.push playlists_info
    end

  when 'channels'
    channel_info = {
      "song_id" => data['id'],
      "song_name" => data['name'],
      "artist" => data['artists'][0]['name'],
      "album_name" => 'DJ节目',
      "mp3_url" => data['mp3Url']
    }
    tmp.push channel_info
  end

  tmp
end
djchannels(stype = 0, offset = 0, limit = 50) click to toggle source

DJ channels: hot today(0), week(10), history(20), new(30)

# File lib/mdisc/api.rb, line 143
def djchannels(stype = 0, offset = 0, limit = 50)
  action = "http://music.163.com/discover/djchannel?type=#{stype}&offset=#{offset}&limit=#{limit}"
  connection = http_request('GET', action)
  channelids = connection.scan(/\/dj\?id=(\d+)/).uniq || []

  channel_detail channelids
end
login(username, password) click to toggle source

Log in

# File lib/mdisc/api.rb, line 24
def login(username, password)
  action = 'http://music.163.com/api/login/'
  query = {
    "username" => username,
    "password" => Digest::MD5.hexdigest(password),
    "rememberLogin" => "true"
  }

  http_request('POST', action, query)
rescue => e
  {"code" => 501}
end
new_albums(offset=0, limit=50) click to toggle source

New albums music.163.com/#/discover/album/

# File lib/mdisc/api.rb, line 61
def new_albums(offset=0, limit=50)
  action = "http://music.163.com/api/album/new?area=ALL&offset=#{offset}&total=true&limit=#{limit}"
  data = http_request('GET', action)

  data['albums']
end
playlist_detail(playlist_id) click to toggle source

Playlist's details

# File lib/mdisc/api.rb, line 80
def playlist_detail(playlist_id)
  action = "http://music.163.com/api/playlist/detail?id=#{playlist_id}"
  data = http_request('GET', action)

  data['result']['tracks']
end
song_detail(song_id) click to toggle source

song id -> song url (details)

# File lib/mdisc/api.rb, line 134
def song_detail(song_id)
  id = song_id.join(',')
  action = "http://music.163.com/api/song/detail/?id=#{id}&ids=[#{id}]"
  data = http_request('GET', action)

  data['songs']
end
songs_detail(ids, offset = 0) click to toggle source

song ids -> song urls (details)

# File lib/mdisc/api.rb, line 123
def songs_detail(ids, offset = 0)
  return [] if ids.empty?

  tmpids = ids[offset, 100]
  action = "http://music.163.com/api/song/detail?ids=[#{tmpids.join(',')}]"
  data = http_request('GET', action)

  data['songs']
end
top_artists(offset = 0, limit = 100) click to toggle source

Top artists music.163.com/#/discover/artist/

# File lib/mdisc/api.rb, line 89
def top_artists(offset = 0, limit = 100)
  action = "http://music.163.com/api/artist/top?offset=#{offset}&total=false&limit=#{limit}"
  data = http_request('GET', action)

  data['artists']
end
top_playlists(category = '%E5%85%A8%E9%83%A8', order = 'hot', offset = 0, limit = 100) click to toggle source

Top playlists hot||new music.163.com/#/discover/playlist/ '全部' => '%E5%85%A8%E9%83%A8'

# File lib/mdisc/api.rb, line 71
def top_playlists(category = '%E5%85%A8%E9%83%A8', order = 'hot', offset = 0, limit = 100)
  flag = offset > 0 ? true : false
  action = "http://music.163.com/api/playlist/list?cat=#{category}&order=#{order}&offset=#{offset}&total=#{flag}&limit=#{limit}"
  data = http_request('GET', action)

  data['playlists']
end
top_songlist() click to toggle source

Top songlist music.163.com/#/discover/toplist 100

# File lib/mdisc/api.rb, line 98
def top_songlist
  action = 'http://music.163.com/discover/toplist'
  connection = http_request('GET', action)
  songids = connection.scan(/\/song\?id=(\d+)/).uniq

  songs_detail songids
end
user_playlists(uid, offset = 0, limit = 100) click to toggle source

User's playlists

# File lib/mdisc/api.rb, line 38
def user_playlists(uid, offset = 0, limit = 100)
  action = "http://music.163.com/api/user/playlist/?offset=#{offset}&limit=#{limit}&uid=#{uid}"
  data = http_request('GET', action)

  data['playlist']
end

Private Instance Methods

http_request(method, action, query = nil) click to toggle source
# File lib/mdisc/api.rb, line 247
def http_request(method, action, query = nil)
  connection =
    if method == 'GET'
      Unirest.get(action, headers: @header)
    elsif method == 'POST'
      Unirest.post(action, headers: @header, parameters: query)
    end

  connection.body
rescue => e
  []
end