Sha256: e33351d13ac6c6c10c007bca8b45be81ae14cd055d1412700f5233cdad786f60

Contents?: true

Size: 915 Bytes

Versions: 3

Compression:

Stored size: 915 Bytes

Contents

require 'net/http'
require 'spotify_to_mp3/grooveshark/track'

module SpotifyToMp3
  class Grooveshark
    def initialize(client)
      @client = client
    end

    def get_track(query)
      client_track = @client.search_songs(query).first or raise "Track not found"
      Track.new(client_track)
    end

    def download(options)
      track = options.fetch(:track)
      on_response = options.fetch(:on_response)
      on_body_chunk = options.fetch(:on_body_chunk)
      
      url = URI(@client.get_song_url(track.client_track))
      Net::HTTP.start(url.host) do |http|
        http.request_post("#{url.path}?#{url.query}", "") do |response|
          on_response.call(response)
          File.open(track.filename, 'w') do |f|
            response.read_body do |chunk|
              on_body_chunk.call(chunk)
              f.write(chunk)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spotify-to-mp3-0.7.2 lib/spotify_to_mp3/grooveshark.rb
spotify-to-mp3-0.7.1 lib/spotify_to_mp3/grooveshark.rb
spotify-to-mp3-0.7.0 lib/spotify_to_mp3/grooveshark.rb