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 --passive-ftp --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
103
104
105
106
107
108
109
110
111
# File 'lib/purple/getter_wget.rb', line 27

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

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

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

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

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

        # Request
        puts "DEBUG Getter_wget#parse_input: Sending request" if $DEBUG
        line = pr.gets
        puts "->#{line}" if $DEBUG
        case line
        when /HTTP request sent, awaiting response\.\.\. (.*)/
            case $1
            when '200 OK'
                # Okay. Continue
            when /^3\d{2}/
                # Redirection
                pr.gets # Location line
                redo
            else
                @status = $1
                return
            end
        when /Logging in as anonymous \.\.\. (.*)/
            case $1
            when 'Logged in!'
                # Okay. Go ahead.
            else
                @status = $1
                return
            end
        end
        break
    end

    # Content details
    loop do
        line = pr.gets
        puts "->#{line}" if $DEBUG
        break if line =~ /^Length: /
    end
    #@length = md[1].gsub(',', '').to_i
    #@content_type = md[3]

    # Empty line
    pr.gets

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

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