User Tools

Site Tools


wget-libnotify

This is a wrapper for wget which shows its stderr output in an updating system notification, so you can see download progress or errors. It also provides action buttons for opening the downloaded file and cancelling the download. Save the script in the executable path and make it executable. It requires Ruby, and the gems ruby-libnotify (which in turn requires a lot of Ruby/GTK+ libraries) and open4.

#!/usr/bin/env ruby
 
require 'RNotify'
require 'open4'
 
Gtk.init
Notify.init 'wget'
exit unless Notify.init?
 
URL = ARGV.last
ICON = nil
 
Open4.popen4 'wget', *ARGV do |pid, stdin, stdout, stderr|
 
  downloading = Notify::Notification.new 'Download started', URL, ICON, nil
  downloading.timeout= Notify::Notification::EXPIRES_NEVER
  downloading.category = 'transfer'
  downloading.add_action 'cancel', 'Cancel', 'cancel' do |action, data|
    Process.kill 'TERM', pid   
  end
  downloading.show
 
  stderr.each_line do |line|
    case line
    when /`([^']+)' saved/
      downloading.clear_actions
      downloading.close
 
      finished = Notify::Notification.new 'Download finished', "#{URL} saved to #{$1}", ICON, nil
      finished.timeout= Notify::Notification::EXPIRES_NEVER
      finished.signal_connect("closed") { Gtk.main_quit }
 
      finished.category = 'transfer.complete'
      finished.add_action 'open', 'Open', 'open' do |action, data|
        system 'xdg-open', $1
        Gtk.main_quit
      end
      finished.show
      break
    when /\S/
      downloading.update 'Downloading', line, ICON
      downloading.show
    end
  end
end
 
Gtk.main
 
downloading.clear_actions
downloading.close
if defined? finished
  finished.clear_actions
  finished.close
end
Notify.uninit

The script can be used outside of uzbl too, and it passes arguments to wget. To use it as uzbl's download handler, add this line in the config (based on dl-dbus):

@on_event   DOWNLOAD_REQUEST    sh 'wget-libnotify.rb --user-agent=Firefox --content-disposition --no-check-certificate --load-cookies $XDG_DATA_HOME/uzbl/cookies.txt --directory-prefix=$HOME/Downloads "$8"' %s
wget-libnotify.txt · Last modified: 2016/08/31 14:10 (external edit)