class Soundyoink::Downloader

Manages a queue of Audio instances to be downloaded, and knows how to create many Audio instances from a user profile link.

Public Class Methods

new(workers: 1) click to toggle source

@param workers [Integer] Number of download threads to run in parallel.

# File lib/soundyoink/downloader.rb, line 6
def initialize(workers: 1)
  @audios = Queue.new
  @workers = workers
end

Public Instance Methods

add(link) click to toggle source

If given a link to an audio page, adds it to the queue. If given a user profile link, adds each individual audio to the queue. @param link [String] Link to either an individual audio or a user profile.

# File lib/soundyoink/downloader.rb, line 14
def add(link)
  if link =~ %r{^https://.+[.net]/u/[^/]+$}
    add_profile(link)
  else
    @audios << Audio.new(link)
  end
end
add_profile(profile) click to toggle source

Find every audio link on a user's profile and add them to the queue. @param profile [String] Link to a user's profile page.

# File lib/soundyoink/downloader.rb, line 24
def add_profile(profile)
  open(profile).read
               .scan(%r{(https://[^.]+[.]net/u/[^"]+)})
               .flatten
               .map { |a| @audios << Audio.new(a) }
end
download() click to toggle source

+@workers+ number of threads work on the +@audios+ queue in parallel. @see Soundyoink::Audio#download

# File lib/soundyoink/downloader.rb, line 33
def download
  worker_threads = []
  @workers.times do
    worker_threads << Thread.new do
      @audios.pop.download until @audios.empty?
    end
  end
  worker_threads.map(&:join)
end