Method: SoundCloud#load_playlist

Defined in:
lib/soundcloud.rb

#load_playlist(search = nil, offset = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/soundcloud.rb', line 25

def load_playlist(search = nil, offset = 0)
  search = "" unless search && !search.empty?
  if search =~ /\s*http(s)?:\/\/(www.)?soundcloud.com.*/
    url = "https://api.soundcloud.com/resolve.json?url=%s&client_id=%s" % [CGI.escape(search), @cid]
  else
    url = "https://api.soundcloud.com/tracks.json?client_id=%s&filter=streamable&limit=%d&offset=%d&q=%s" \
      % [@cid, LIMIT, offset, CGI.escape(search)]
  end
  c = open(url) do |io|
    io.readlines
  end.join
  @tracks = JSON.parse c
  @tracks = [@tracks] if @tracks.is_a? Hash
  @tracks.map! do |t|
    t["mpg123url"] = client_url t['stream_url']
    t["download"] = client_url t['download_url']
    t["duration"] = t["duration"].nil? ? 0 : t["duration"].to_i/1000
    t["bpm"] = t["bpm"].nil? ? 0 : t["bpm"].to_i
    t[:error] = "Not streamable" if t["stream_url"].nil?
    t
  end
  changed
  notify_observers :state =>:load, :tracks => @tracks
  rescue => e
    @error = {:error => e}
end