Class: SpeedStream

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

Direct Known Subclasses

YoutubeSpeedStream

Constant Summary collapse

USER_AGENT =
%{Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3}
BUF_SIZE =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, output_file) ⇒ SpeedStream

Returns a new instance of SpeedStream.



16
17
18
19
20
21
22
23
24
25
# File 'lib/speed_stream.rb', line 16

def initialize(uri, output_file)
  @uri = URI.parse(uri)
  @output_file = output_file
  @write_mutex = Mutex.new
  @finished = []
  @bytes_per_conn = 200_000
  @concurrent_connections = 15
  @cookies = {}
  @extra_file_length_headers = {'Range' => 'bytes=1-'}
end

Instance Attribute Details

#bytes_per_connObject (readonly)

Returns the value of attribute bytes_per_conn.



13
14
15
# File 'lib/speed_stream.rb', line 13

def bytes_per_conn
  @bytes_per_conn
end

#concurrent_connectionsObject (readonly)

Returns the value of attribute concurrent_connections.



13
14
15
# File 'lib/speed_stream.rb', line 13

def concurrent_connections
  @concurrent_connections
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



13
14
15
# File 'lib/speed_stream.rb', line 13

def cookies
  @cookies
end

#file_handleObject (readonly)

Returns the value of attribute file_handle.



13
14
15
# File 'lib/speed_stream.rb', line 13

def file_handle
  @file_handle
end

#on_progressObject

Returns the value of attribute on_progress.



14
15
16
# File 'lib/speed_stream.rb', line 14

def on_progress
  @on_progress
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



13
14
15
# File 'lib/speed_stream.rb', line 13

def output_file
  @output_file
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/speed_stream.rb', line 13

def uri
  @uri
end

Instance Method Details

#download!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/speed_stream.rb', line 27

def download!
  @start_time = Time.now
  @bytes_written = 0
  pool = ThreadPool.new(concurrent_connections, 999999)
  
  File.open(output_file, "wb") do |f|
    @file_handle = f

    begin
      ranges.each do |range|
        pool.add_work(range) do |drange|
          download_range drange
        end
      end
    ensure
      pool.shutdown
    end
  end
end