Class: ExternalDownload

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

Constant Summary collapse

CMD =
method :get_curl_cmd

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, filename) ⇒ ExternalDownload

Returns a new instance of ExternalDownload.



9
10
11
12
13
14
15
16
# File 'lib/ektoplayer/download/externaldownload.rb', line 9

def initialize(url, filename)
   @events   = Events.new(:completed, :failed)
   @url      = URI.parse(url)
   @filename = filename
   @progress = 0
   @error    = nil
   @tries    = 3
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#progressObject (readonly)

Returns the value of attribute progress.



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

def progress
  @progress
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.get_curl_cmd(url, file) ⇒ Object



22
23
24
# File 'lib/ektoplayer/download/externaldownload.rb', line 22

def self.get_curl_cmd(url, file)
   %w(curl -# -o) + [file, url]
end

.get_wget_cmd(url, file) ⇒ Object



18
19
20
# File 'lib/ektoplayer/download/externaldownload.rb', line 18

def self.get_wget_cmd(url, file)
   %w(wget -nv --show-progress --progress=dot:binary -O) + [file, url]
end

Instance Method Details

#start!Object



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
55
56
# File 'lib/ektoplayer/download/externaldownload.rb', line 26

def start!
   Thread.new do
      args = CMD.(@url.to_s, @filename)
      dl_in, dl_out, dl_err, @dl_proc = Open3.popen3(*args)

      begin
         while (line = dl_err.readpartial(1024))
            @last_line = line

            if (progress = line.scan(/(\d+(\.\d+)?%)/)[0][0].delete(?%).to_f rescue nil)
               @progress = progress
            end
         end
      rescue
         nil
      end

      begin
         @dl_proc.join
         raise if @dl_proc.value.exitstatus > 0
         @progress = 100.0
         @events.trigger(:completed)
      rescue
         @events.trigger(:failed, (@error = @last_line))
      end
   end

   sleep 0.1 while @dl_proc.nil?
   sleep 0.2
   self
end