Class: GrooveDl::Downloader
- Inherits:
-
Object
- Object
- GrooveDl::Downloader
- Defined in:
- lib/groove-dl/downloader.rb
Overview
Downloader Class
Instance Attribute Summary collapse
-
#client ⇒ Object
writeonly
Sets the attribute client.
-
#download_count ⇒ Object
writeonly
Sets the attribute download_count.
-
#download_queue ⇒ false|Array
Download queue.
-
#download_skip ⇒ Object
writeonly
Sets the attribute download_skip.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#build_path(output_directory, song) ⇒ Object
Build path.
-
#download(song, object) ⇒ Net::HTTP
Download song.
-
#initialize(client, options = {}) ⇒ Downloader
constructor
Initialize download.
-
#playlist(playlist_id) ⇒ Array
Download playlist.
-
#process_cli_response(destination) ⇒ Proc
Process response to display a progress bar and download file into destination.
-
#process_gui_response(object) ⇒ Proc
Process response to pulse progressbar stored in the TreeIter object.
-
#song(song_id) ⇒ Array
Download song.
Constructor Details
#initialize(client, options = {}) ⇒ Downloader
Initialize download
16 17 18 19 20 21 22 23 |
# File 'lib/groove-dl/downloader.rb', line 16 def initialize(client, = {}) @client = client @output_directory = [:o] || Dir.tmpdir @queue = [] @count = 0 @skip = 0 @type = 'cli' end |
Instance Attribute Details
#client=(value) ⇒ Object (writeonly)
Sets the attribute client
6 7 8 |
# File 'lib/groove-dl/downloader.rb', line 6 def client=(value) @client = value end |
#download_count=(value) ⇒ Object (writeonly)
Sets the attribute download_count
8 9 10 |
# File 'lib/groove-dl/downloader.rb', line 8 def download_count=(value) @download_count = value end |
#download_queue ⇒ false|Array
Download queue
83 84 85 86 87 88 89 90 |
# File 'lib/groove-dl/downloader.rb', line 83 def download_queue return false if @queue.empty? @queue.each do |song| download(song, build_path(@output_directory, song)) end { skipped: @skip, downloaded: @count } end |
#download_skip=(value) ⇒ Object (writeonly)
Sets the attribute download_skip
9 10 11 |
# File 'lib/groove-dl/downloader.rb', line 9 def download_skip=(value) @download_skip = value end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/groove-dl/downloader.rb', line 5 def type @type end |
Instance Method Details
#build_path(output_directory, song) ⇒ Object
Build path
98 99 100 101 102 103 104 |
# File 'lib/groove-dl/downloader.rb', line 98 def build_path(output_directory, song) format('%s/%s/%s/%s.mp3', output_directory, song.artist, song.album, song.name) end |
#download(song, object) ⇒ Net::HTTP
Download song
function to execute during download
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/groove-dl/downloader.rb', line 65 def download(song, object) url = URI.parse(@client.get_song_url_by_id(song.id)) @client.get_stream_auth_by_songid(song.id) callback = process_gui_response(object) if @type == 'gui' callback = process_cli_response(object) if @type == 'cli' RestClient::Request .execute(method: :get, url: url.to_s, block_response: callback) end |
#playlist(playlist_id) ⇒ Array
Download playlist
46 47 48 49 50 51 52 53 54 |
# File 'lib/groove-dl/downloader.rb', line 46 def playlist(playlist_id) playlist = Grooveshark::Playlist.new(@client, 'playlist_id' => playlist_id) songs = playlist.load_songs return false unless songs @queue += songs download_queue end |
#process_cli_response(destination) ⇒ Proc
Process response to display a progress bar and download file into destination.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/groove-dl/downloader.rb', line 114 def process_cli_response(destination) proc do |response| total_size = response['content-length'].to_i if File.exist?(destination) && File.size?(destination) == total_size @skip += 1 fail Errors::AlreadyDownloaded, "#{destination} already downloaded" end = ProgressBar.create(title: destination.split('/').last, format: '%a |%b>>%i| %p%% %t', total: response['content-length'].to_i) File.open(destination, 'w') do |f| response.read_body do |chunk| f.write(chunk) .progress += chunk.length end end .finish @count += 1 end end |
#process_gui_response(object) ⇒ Proc
Process response to pulse progressbar stored in the TreeIter object.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/groove-dl/downloader.rb', line 147 def process_gui_response(object) proc do |response| = Widgets::Download::QUEUE_COLUMN_PGBAR_VALUE = Widgets::Download::QUEUE_COLUMN_PGBAR_TEXT total_size = response['content-length'].to_i path = object[Widgets::Download::QUEUE_COLUMN_PATH] if File.exist?(path) && File.size?(path) == total_size object[] = 100 object[] = 'Complete' fail Errors::AlreadyDownloaded, "#{path} already downloaded" end dirname = File.dirname(path) FileUtils.mkdir_p(dirname) unless File.directory?(dirname) File.open(path, 'w') do |f| file_size = 0 response.read_body do |chunk| f.write(chunk) file_size += chunk.length result = ((file_size * 100) / total_size).to_i object[] = result object[] = 'Complete' if object[] >= 100 end end end end |
#song(song_id) ⇒ Array
Download song
32 33 34 35 36 37 |
# File 'lib/groove-dl/downloader.rb', line 32 def song(song_id) @queue << Grooveshark::Song.new('song_id' => song_id, 'artist_name' => 'unknown', 'song_name' => 'unknown') download_queue end |