Class: NetEase
- Inherits:
-
Object
- Object
- NetEase
- Defined in:
- lib/mdisc/api.rb
Constant Summary collapse
- TIMEOUT =
10
Instance Method Summary collapse
-
#album(album_id) ⇒ Object
album id -> song id set.
-
#artists(artist_id) ⇒ Object
Songs to which a artist belongs.
-
#channel_detail(channelids) ⇒ Object
DJchannel (id, channel_name) ids -> song urls (details) channels -> songs.
- #dig_info(data, dig_type) ⇒ Object
-
#djchannels(stype = 0, offset = 0, limit = 50) ⇒ Object
DJ channels: hot today(0), week(10), history(20), new(30).
-
#initialize ⇒ NetEase
constructor
A new instance of NetEase.
-
#login(username, password) ⇒ Object
Log in.
-
#new_albums(offset = 0, limit = 50) ⇒ Object
New albums music.163.com/#/discover/album/.
-
#playlist_detail(playlist_id) ⇒ Object
Playlist’s details.
-
#search(s, stype = 1, offset = 0, limit = 100) ⇒ Object
Search song(1),artist(100),album(10),playlist(1000),user(1002).
-
#song_detail(song_id) ⇒ Object
song id -> song url (details).
-
#songs_detail(ids, offset = 0) ⇒ Object
song ids -> song urls (details).
-
#top_artists(offset = 0, limit = 100) ⇒ Object
Top artists music.163.com/#/discover/artist/.
-
#top_playlists(category = '%E5%85%A8%E9%83%A8', order = 'hot', offset = 0, limit = 100) ⇒ Object
Top playlists hot||new music.163.com/#/discover/playlist/ ‘全部’ => ‘%E5%85%A8%E9%83%A8’.
-
#top_songlist ⇒ Object
Top songlist music.163.com/#/discover/toplist 100.
-
#user_playlists(uid, offset = 0, limit = 100) ⇒ Object
User’s playlists.
Constructor Details
#initialize ⇒ NetEase
Returns a new instance of NetEase.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Method Details
#album(album_id) ⇒ Object
album id -> song id set
115 116 117 118 119 120 |
# 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) ⇒ Object
Songs to which a artist belongs.
107 108 109 110 111 112 |
# 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) ⇒ Object
DJchannel (id, channel_name) ids -> song urls (details) channels -> songs
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# 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) ⇒ Object
173 174 175 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 |
# 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) ⇒ Object
DJ channels: hot today(0), week(10), history(20), new(30)
143 144 145 146 147 148 149 |
# 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) ⇒ Object
Log in
24 25 26 27 28 29 30 31 32 33 34 35 |
# 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) ⇒ Object
New albums music.163.com/#/discover/album/
61 62 63 64 65 66 |
# 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) ⇒ Object
Playlist’s details
80 81 82 83 84 85 |
# 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 |
#search(s, stype = 1, offset = 0, limit = 100) ⇒ Object
Search song(1),artist(100),album(10),playlist(1000),user(1002)
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mdisc/api.rb', line 46 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(song_id) ⇒ Object
song id -> song url (details)
134 135 136 137 138 139 140 |
# 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) ⇒ Object
song ids -> song urls (details)
123 124 125 126 127 128 129 130 131 |
# 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) ⇒ Object
Top artists music.163.com/#/discover/artist/
89 90 91 92 93 94 |
# 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) ⇒ Object
Top playlists hot||new music.163.com/#/discover/playlist/ ‘全部’ => ‘%E5%85%A8%E9%83%A8’
71 72 73 74 75 76 77 |
# 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 ⇒ Object
Top songlist music.163.com/#/discover/toplist 100
98 99 100 101 102 103 104 |
# 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) ⇒ Object
User’s playlists
38 39 40 41 42 43 |
# 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 |