Class: Downloader
- Inherits:
-
Object
- Object
- Downloader
- Defined in:
- lib/deadlist/cli/downloader.rb
Overview
A simple class to download files to a given directory. Expects details for the filename and a link. One Downloader should be created / show being downloaded. Downloaders can run on seperate threads for getting many hows at once.
Constant Summary collapse
- BASE_API_URL =
'https://archive.org'
Instance Method Summary collapse
- #download_url_for_show(show_id) ⇒ Object
-
#get(root_url, track_object) ⇒ Object
Goes to a link (assuming the format is already validated), and gets the file, saving with argument names.
-
#initialize(path, format, logger: Logger.new($stdout)) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(path, format, logger: Logger.new($stdout)) ⇒ Downloader
Returns a new instance of Downloader.
6 7 8 9 10 |
# File 'lib/deadlist/cli/downloader.rb', line 6 def initialize(path, format, logger: Logger.new($stdout)) @logger = logger @path = path @format = format end |
Instance Method Details
#download_url_for_show(show_id) ⇒ Object
12 13 14 |
# File 'lib/deadlist/cli/downloader.rb', line 12 def download_url_for_show(show_id) "#{BASE_API_URL}/download/#{show_id}/" end |
#get(root_url, track_object) ⇒ Object
Goes to a link (assuming the format is already validated), and gets the file, saving with argument names.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/deadlist/cli/downloader.rb', line 17 def get(root_url, track_object) uri = URI.parse(root_url + track_object.filename); raise ArgumentError, "Only HTTP(S) URLs allowed" unless uri.is_a?(URI::HTTP) download = uri.open # Extract disc number from filename disc_match = track_object.filename.match(/(?<!g)d(\d+)/) sanitized_title = track_object.title.gsub('/', '-') if disc_match # Multi-disc: use disc-track format (1-01, 2-01, etc.) disc_num = disc_match[1] padded_track = track_object.pos.rjust(2, '0') filename = "#{@path}/#{disc_num}-#{padded_track} -- #{sanitized_title}.#{@format}" else # Single disc: regular format filename = "#{@path}/#{track_object.pos} -- #{sanitized_title}.#{@format}" end IO.copy_stream(download, filename) true rescue => e @logger.error "❌ Download failed for '#{track_object.title}': #{e.}" false end |