lib/spotify_to_mp3/grooveshark.rb in spotify-to-mp3-0.6.1 vs lib/spotify_to_mp3/grooveshark.rb in spotify-to-mp3-0.7.0

- old
+ new

@@ -1,7 +1,6 @@ -require 'fileutils' -require 'rest-client' +require 'net/http' require 'spotify_to_mp3/grooveshark/track' module SpotifyToMp3 class Grooveshark def initialize(client) @@ -11,12 +10,25 @@ def get_track(query) client_track = @client.search_songs(query).first or raise "Track not found" Track.new(client_track) end - def download(track) - url = @client.get_song_url(track.client_track) - file = RestClient::Request.execute(:method => :post, :url => url, :raw_response => true).file - FileUtils.mv(file.path, track.filename) + 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