Archive | rtorrent RSS for this section

Bash custom functions and autocompletion

To easily upload torrents files to my VPS I wrote a custom function I put in my .bashrc :

upload_torrents () {
    for file; do
        filename=$(basename "$file")
        extension="${filename##*.}"
        if [ $extension = 'torrent' ]
            then
                rsync -a --partial --progress "$file" mybox:/home/downloads/torrents/watch/
        else
            echo "$file"" is not a torrent"
        fi
    done
}

This function is pretty straightforward, it uploads all the torrent files passed in arguments. Using it I realized it was lacking something: I wanted the bash autocompletion to only propose the torrent files instead of all the files in the directory. I added the following snippet to my .bashrc and it did the trick (found here):

complete -f -X '!*.@(torrent)' upload_torrents

Now that we’ve uploaded the torrents, we need to download the files from the remote server to our local machine. Here is another custom function I wrote:

download_from_mybox () {
    for file; do
        rsync -a --partial --progress --bwlimit=500 mybox:/home/downloads/torrents/"$file" .
    done
}

Even simpler than the first one. How about adding autocompletion to this command? Obviously we need to complete using the list of files located on the remote server in /home/downloads/torrents/. I had quite a lot of difficulties trying to figure this one out and I finally asked the fine people of unix.stackexchange. Here is how I finally did it:

In /etc/bash_completion.d/, I created a download_from_my_box file containing the following:

#!/bin/bash

_get_rsync_file_list()
{
    ssh mybox "cd /home/downloads/torrents/pierre && find *"
}

_GetOptSSH()
{
    local cur

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    case "$cur" in
    -*)
        COMPREPLY=( $( compgen -W "-h -l --help --list --" -- "$cur" ) );;
    *)
        local -a filelist=$(_get_rsync_file_list)
        COMPREPLY=( $( compgen -W "${filelist[@]}" -- "$cur" ) );;
    esac

    return 0
}

complete -F _GetOptSSH download_from_mybox

The case on $cur isn’t strictly necessary since I didn’t implement any option in my command but it’s good to know how to do it.

So that’s how you do custom remote autocompletion in bash! Pretty rad I know.

If you want to learn more on the matter, here is a very good article in two parts hosted on http://www.debian-administration.org

Quick edit before I post an article with the complete solution: this doesn’t work if you have spaces in your filenames/directories. I asked for help on the UnixStackExchange and got an awesome answer. I’m trying to find the time to implement a fully working solution and post it here.

Advertisements

rtorrent: Receiving an email when a download is complete

Since I had to get the information from different websites, I thought I would make a post compiling all the info necessary.
I’m using rtorrent on a remote server (running Ubuntu) and I wanted to receive a mail when rtorrent is done downloading a file, so that I wouldn’t have to check it by hand. I still have to download the files manually but that will be my next assignment!

First things first, you need a valid email address from which you will send these notifications. I created a gmail email address because it was the most convenient solution at the time. Let’s call it mybox@gmail.com and let’s say I wasn’t really concerned with security and used ‘qwerty’ as a password.

Next, you’ll need to install and configure a package to send email from the terminal. I’ve adapted this guide, which helped me greatly, to Debian based distros.

Install ssmtp with something like: sudo apt-get install ssmtp
Configure it by editing /etc/ssmtp/ssmtp.conf and change it to:

root=mybox@gmail.com
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=smtp.gmail.com:587
UseSTARTTLS=YES
UseTLS=YES
AuthUser=mybox
AuthPass=qwerty
AuthMethod=LOGIN

And in /etc/ssmtp/revaliases:

root:mybox@gmail.com:smtp.gmail.com:587
mainuser:mybox@gmail.com:smtp.gmail.com:587

Obviously, change the fields to match your own login credentials.

To test your configuration try this: echo test | mail -s "Hey this actually works!" your_personal@email.com
You should receive a mail from mybox@gmail.com. For more details on the mail command, see the man page.

Let’s now write a script that sends email to your personal address. Later on, we’ll tell rtorrent to execute this script when a download is completed.
Create a rtorrent_mail.sh file wherever you want and edit it like so:

#!/bin/sh
echo "$(date) : $1 - Download completed." | mail -s "[rtorrent] - Download completed : $1" your_personal@email.com

Don’t forget to make it executable (chmod +x rtorrent_mail.sh) and to give it the right permissions (the user launching rtorrent needs to be able to execute it).

Finally, we need to hook this script to rtorrent. In your .rtorrent.rc, add the following line:

system.method.set_key = event.download.finished,notify_me,"execute=/path/to/script/rtorrent_mail.sh,$d.get_name="

If I’m not mistaken, this hooks the notify_me receiver to the event.download.finished signal (or event). The last part between quotes is the content of the notify_me method.
Once again, put the path corresponding to where you put your script. You might need to restart rtorrent for these settings to be active.

Here you go! rtorrent will automatically send you emails when a download is completed.
This post doesn’t cover everything I did with rtorrent. I’m actually sharing my VPS with a very good friend of mine and we both get notified when a download is completed. This is great for tv shows we both watch. What if you want rtorrent to send an email to different users depending on the files it downloaded? (practical example: you don’t want your dear friend to know that you are very much into videos displaying women and horses in a lewd manner)
Well, that will be the object of a future post !