Téléchargement de fichiers : alimenter une jauge de progression
Bonsoir !
Dans le cadre d’un projet perso (dont je vois le bout), j’essaye d’alimenter une jauge de progression de téléchargement de fichiers.
Pour cela, j’ai mélangé deux scripts :
1/ Le fichier gauge.rb de MrDialog
2/ Le fichier download-progress.rb (sur un autre Github)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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
|
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'net/http'
require 'mrdialog'
# --------------------------------------------------------------------
$height = 20
$width = 70
$percent = 0
$dialog = MRDialog.new
$dialog.clear = false
$dialog.title = "Titre de la fenètre"
$windowTitle = "Sous-titre de la fenètre."
# --------------------------------------------------------------------
def download(url)
Thread.new do
thread = Thread.current
thread[:body] = []
thread[:done] = 0
$url = URI(url)
Net::HTTP.new($url.host, $url.port).get2($url.path) do |res|
length = thread[:length] = res.content_length
open Final_name, 'w' do |file|
res.read_body do |segment|
file.write segment
$thread[:body] << segment
$thread[:done] += segment.length
$thread[:progress] = thread[:done].fdiv(length)
end
end
end
end
end
Final_name="UbuntuMini.iso"
$thread = download("http://archive.ubuntu.com/ubuntu/dists/wily/main/installer-amd64/current/images/netboot/mini.iso")
#puts "%.2f" % (thread[:progress]*100) until thread.join(1)
def fileDownloader
$dialog.gauge($windowTitle, $height, $width) do |p|
p.puts "XXX"
until $thread.join(1)
p.puts "%.2f" % ($thread[:progress]*100)
end
# puts "%.2f" % ($thread[:progress]*100) until $thread.join(1)
p.puts $windowTitle
p.puts "XXX"
end
end
fileDownloader |
Je pense que je suis sur le point d’y arriver mais je bloque sur comment terminer.
Si quelqu’un à une idée…