Class: Robro::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/robro/download.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

FILTERS =
[
  #--2020-11-10 22:30:31--  https://kubuntu.org/wp-content/uploads/2020/10/6ac0/GroovyBannerv4-1024x273.png
  /^--\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}--  http/,
  #Resolving kubuntu.org (kubuntu.org)... 162.213.33.214
  /^Resolving /,
  #Connecting to kubuntu.org (kubuntu.org)|162.213.33.214|:443... connected.
  /^Connecting /,
  #Reusing existing connection to kubuntu.org:443.
  /^Reusing /,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Download

Returns a new instance of Download.



9
10
11
12
# File 'lib/robro/download.rb', line 9

def initialize(url)
  @url = url.sub /^http:/, 'https:'
  Robro.logger.debug "Attempt to download: #{@url}"
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



7
8
9
# File 'lib/robro/download.rb', line 7

def details
  @details
end

Instance Method Details

#downloaded?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/robro/download.rb', line 56

def downloaded?
  success?
end

#errorObject



64
65
66
67
68
69
70
# File 'lib/robro/download.rb', line 64

def error
  return "#{details['error_code']}: #{details['error_text']}" unless details['error_code'].nil?
  return "#{details['error_text']}" unless details['error_text'].nil?
  return "Command execution failed with exit code: #{details[:exit_status].exitstatus}" unless details[:exit_status].success?

  nil
end

#run(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/robro/download.rb', line 25

def run(&block)
  @details = {}

  result = false

  Open3.popen2e(
    { "LANG" => 'C'},
    'wget', '--continue', '--content-disposition', '--progress=dot:mega', @url,
  ) do |stdin, stdout_and_err, thread|
    stdout_and_err.each do |line|
      line.chomp!
      next if line.empty?

      data = extract_details line

      unless data.nil?
        data['filename'] = eval "\"#{data['filename']}\"" unless data['filename'].nil? # Handle escaped UTF-8 chars
        @details.merge! data
        block.call data, @details
      else
        puts "==='#{line}'===" unless filtred?(line)
      end
    end

    exit_status = thread.value
    @details.merge!({ exit_status: exit_status })

    raise Error.new "Error while downloading: #{error}" unless error.nil?
  end
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/robro/download.rb', line 60

def success?
  error.nil?
end