Class: Purple::Getter::Getter_wget

Inherits:
Generic
  • Object
show all
Defined in:
lib/purple/getter_wget.rb

Instance Attribute Summary

Attributes inherited from Generic

#content_type, #length, #thread, #uri

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Getter_wget

Returns a new instance of Getter_wget.



6
7
8
9
# File 'lib/purple/getter_wget.rb', line 6

def initialize *args
    super
    @done = "0"
end

Instance Method Details

#getObject



11
12
13
14
15
16
17
18
19
# File 'lib/purple/getter_wget.rb', line 11

def get
    @thread = Thread.new do
        filename = File.basename uri.path
        Open3.popen3 "wget --progress dot -O '#{@destdir}/#{filename}' #{@uri.to_s} 2>&1" do
            |pw, pr, pe|
            parse_input pw, pr, pe
        end
    end
end

#get_statusObject



21
22
23
# File 'lib/purple/getter_wget.rb', line 21

def get_status
    [@status, @done]
end

#parse_input(pw, pr, pe) ⇒ Object

wget specific methods



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/purple/getter_wget.rb', line 27

def parse_input pw, pr, pe
    #puts "DEBUG Getter_wget#parse_input: Start"

    loop do
        # Start line
        2.times { pr.gets }

        # Host lookup
        #puts "DEBUG Getter_wget#parse_input: Host lookup"
        line = pr.gets
        #puts "->#{line}"
        if line =~ /failed:/
            @status = 'Host lookup failed'
            return
        end

        # Connecting
        #puts "DEBUG Getter_wget#parse_input: Connecting"
        line = pr.gets
        #puts "->#{line}"
        md = /^Connecting to (.*?)... (.*?)$/.match line
        #puts "DEBUG Getter_wget#parse_input: md[2]=#{md[2].inspect}"

        unless 'connected.' == md[2]
            @status = 'Connection failed'
            return
        end

        # Request
        #puts "DEBUG Getter_wget#parse_input: Sending request"
        line = pr.gets
        #puts "->#{line}"
        md = /HTTP request sent, awaiting response... (.*)/.match line
        case md[1]
        when '200 OK'
            # Okay. Continue
        when /^3\d{2}/
            # Redirection
            pr.gets # Location line
            redo
        else
            @status = md[1]
            return
        end

        break
    end

    # Content details
    line = pr.gets
    #puts "->#{line}"
    md = /Length: (.*?) \[(.*?)\]/.match line
    @length = md[1].gsub(',', '').to_i
    @content_type = md[2]

    # Empty line
    pr.gets

    # Data incoming
    pr.each do |line|
        #puts "->#{line}"
             
        md = / *([0-9]+)K[ \.]+([0-9]+)% +([0-9\.,]+ KB\/s)/.match line
        #puts (md ? "DEBUG #{md.to_a.inspect}" : "DEBUG nil")
        if md
            @done = md[2]
            @rate = md[3]
            next
        end

        if "100" == @done
            @status = "Done"
        end
        pr.read
    end
end