Download events in aria2

In my previous post, I have demonstrated how to use aria2 as the default download manager on Unix/Linux and how to integrate aria2 into Conkeror. In this post, I’m going to show a simple tip that can cause aria2 to display notification when it finishes downloading. By default, aria2 provides some input arguments which are the event handlers for download hooks. Those events include --on-bt-download-complete, --on-download-complete, --on-download-error, --on-download-pause, --on-download-start, --on-download-stop. The solution is to pass a shell script to the event and use that bash script to activate notification program. For example

$ aria2c --on-download-complete="/path/to/notification/script.sh" http://example.com/file.iso

You can also pass the event handler command to the aria2 command for starting it as daemon or on RPC protocol. If you are using the command from my previous post, the command will look like this

$ touch /path/to/download/folder/session.txt && aria2c --enable-rpc --rpc-listen-all --save-session=/path/to/download/folder/session.txt --input-file=/path/to/download/folder/session.txt -x16 -s16 -k1M --dir=/path/to/download/folder --daemon --on-download-complete=/path/to/notification/script.sh

When aria2 finishes downloading, it will run the –on-download-complete script and pass 3 arguments, the third one is the path to the downloaded file. The content of the notification script.sh file should look similar to this

#!/bin/sh
notification-command "Download complete $3"

Notification on Linux

Most Linux distros come with the standard APIs for displaying notification. To activate it, simply execute the command notify-send "message" in terminal. The content of the notification script.sh file is similar to this

#!/bin/sh
notify-send "Download complete $3"

You can pass extra arguments to notify-send for specifying expiry time or notification icon.

Lubuntu

Xubuntu

Notification on Mac

Mac OS does not come with a shell application for activating notification. However, you can use Growl or terminal-notifier for displaying notification using Mac’s default Notification Center. terminal-notifier can be installed using Macports, ruby or homebrew

$ port install terminal-notifier
$ gem install terminal-notifier
$ brew install terminal-notifier

Next, edit the notification script.sh file like this

#!/bin/sh
terminal-notifier -message "Download completed - $3" -title "Aria2"

To set the icon for the notification, you need to create an empty aria2 .app wrapper.

$ sudo mkdir -p /Applications/aria2.app/Contents/MacOS
$ sudo touch /Applications/aria2.app/Contents/MacOS/aria2.sh
$ sudo touch /Applications/aria2.app/Contents/Info.plist

Edit the content Info.plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleExecutable</key>
	<string>aria2.sh</string>
	<key>CFBundleIdentifier</key>
	<string>com.example.aria2</string>
</dict>
</plist>

Open Applications folder in Finder, view info of aria2.app, drag and drop the image you want to the application icon

Alt Text

Now, back to the notification script.sh file and change it to

#!/bin/sh
terminal-notifier -message "Download completed - $3" -title "Aria2" -sender "com.example.aria2"

Notification Mac