Class: Downloader

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(path, format) ⇒ Downloader

Returns a new instance of Downloader.



6
7
8
9
# File 'lib/deadlist/cli/downloader.rb', line 6

def initialize(path, format)
    @path = path
    @format = format
end

Instance Method Details

#download_url_for_show(show_id) ⇒ Object



11
12
13
# File 'lib/deadlist/cli/downloader.rb', line 11

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.



16
17
18
19
20
21
22
23
24
# File 'lib/deadlist/cli/downloader.rb', line 16

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
    filename = "#{@path}/#{track_object.pos} -- #{track_object.title}.#{@format}"

    IO.copy_stream(download, filename)
rescue => e
    puts "❌ Download failed: #{e.message}"
end