Class: NetEase

Inherits:
Object
  • Object
show all
Defined in:
lib/mplug163/api.rb

Instance Method Summary collapse

Constructor Details

#initializeNetEase

Returns a new instance of NetEase.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mplug163/api.rb', line 6

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"
  }

  @cookies = {
    "appver" => "2.0.2"
  }

  @default_timeout = 10
  Unirest.timeout @default_timeout
end

Instance Method Details

#album(album_id) ⇒ Object

album id -> song id set



125
126
127
128
129
# File 'lib/mplug163/api.rb', line 125

def album(album_id)
  action = "http://music.163.com/api/album/#{album_id}"
  data = http_request('GET', action)
  return data['album']['songs']
end

#artists(artist_id) ⇒ Object

Songs to which a artist belongs.



118
119
120
121
122
# File 'lib/mplug163/api.rb', line 118

def artists(artist_id)
  action = "http://music.163.com/api/artist/#{artist_id}"
  data = http_request('GET', action)
  return data['hotSongs']
end

#channel_detail(channelids) ⇒ Object

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/mplug163/api.rb', line 158

def channel_detail(channelids)
  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) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/mplug163/api.rb', line 176

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
        song_info['artist'].join(',')
      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) ⇒ Object

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



148
149
150
151
152
153
154
# File 'lib/mplug163/api.rb', line 148

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+)/) || []
  return [] if channelids.empty?
  return channel_detail(channelids.uniq)
end

#http_request(method, action, query = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mplug163/api.rb', line 26

def http_request(method, action, query = nil)
  connection =
    if method == 'GET'
      url = (query.nil? ? action : "#{action}?#{query}")
      Unirest.get(url, headers: @header)
    elsif method == 'POST'
      Unirest.post(action, headers: @header, parameters: query)
    end

  connection.body
end

#login(username, password) ⇒ Object

Log in



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mplug163/api.rb', line 39

def (username, password)
  action = "http://music.163.com/api/login/"
  query = {
    "username" => username,
    "password" => Digest::MD5.hexdigest(password),
    "rememberLogin" => "true"
  }
  begin
    return http_request('POST', action, query)
  rescue => e
    return {"code" => 501}
  end
end

#new_albums(offset = 0, limit = 50) ⇒ Object



75
76
77
78
79
# File 'lib/mplug163/api.rb', line 75

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) ⇒ Object

Playlist’s details



93
94
95
96
97
# File 'lib/mplug163/api.rb', line 93

def playlist_detail(playlist_id)
  action = "http://music.163.com/api/playlist/detail?id=#{playlist_id}"
  data = http_request('GET', action)
  return data['result']['tracks']
end

#search(s, stype = 1, offset = 0, limit = 100) ⇒ Object

Search song(1),artist(100),album(10),playlist(1000),user(1002)



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mplug163/api.rb', line 61

def search(s, stype = 1, offset = 0, limit = 100)
  action = "http://music.163.com/api/search/get/web"
  query = {
    "s" => s,
    "type" => stype,
    "offset" => offset,
    "total" => true,
    "limit" => limit
  }
  http_request('POST', action, query)
end

#song_detail(music_id) ⇒ Object

song id -> song url (details)



140
141
142
143
144
145
# File 'lib/mplug163/api.rb', line 140

def song_detail(music_id)
  id = music_id.join(',')
  action = "http://music.163.com/api/song/detail/?id=#{id}&ids=[#{id}]"
  data = http_request('GET', action)
  return data['songs']
end

#songs_detail(ids, offset = 0) ⇒ Object

song ids -> song urls (details)



132
133
134
135
136
137
# File 'lib/mplug163/api.rb', line 132

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

#top_artists(offset = 0, limit = 100) ⇒ Object



101
102
103
104
105
# File 'lib/mplug163/api.rb', line 101

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)
  return data['artists']
end

#top_playlists(category = '%E5%85%A8%E9%83%A8', order = 'hot', offset = 0, limit = 100) ⇒ Object

‘全部’ => ‘%E5%85%A8%E9%83%A8’



85
86
87
88
89
90
# File 'lib/mplug163/api.rb', line 85

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)
  return data['playlists']
end

#top_songlistObject



109
110
111
112
113
114
115
# File 'lib/mplug163/api.rb', line 109

def top_songlist
  action = "http://music.163.com/discover/toplist"
  connection = http_request('GET', action)
  songids = connection.scan(/\/song\?id=(\d+)/)
  return [] if songids == []
  return songs_detail(songids.uniq)
end

#user_playlists(uid, offset = 0, limit = 100) ⇒ Object

User’s playlists



54
55
56
57
58
# File 'lib/mplug163/api.rb', line 54

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