In Geexbox, it is possible to use rtorrent to download files. Rtorrent is a service managed by systemd and, when it starts, open a session called ‘rtorrent’. Its config file is /etc/rtorrent.rc and it is possible to edit it to change default option values.
Terminal Connection
If you connect to Geexbox using SSH, you can see downlaoding files (using screen ) using this command :
screen -r rtorrent
This comand recover the session named ‘rtorrent’.
It is possible to show full session lists typing :
screen -ls
If you want to exit from current screen session you can use in screen session this keys:
CTRL+ a
d
This session can be (re)attached as shown before.
Web Management
The more comfortable way to manage rtorrent is using rutorrent (note the second char: ‘u’ ). You reach this frontend typing http://your_ip_address/rutorrent where your_ip_address is the IP address of your Geexbox machine.
This web tool allows to add torrents, see active/finished/inactive downloads, stop/suspend current downloads and more…
In Rutorrent, it is possible also to specify a path where you want to move files after download, like a folder in external hard disk.
How to add a security login for rutorrent
You can add a user/passowrd request when you access to rutorrent from web.
First of all you need to choose an username and a password. This login is independent from other login (ftp, samba, …)
In this example we use ‘john’ as username and ‘geexbox’ as password.
1) Go to lighttpd folder. Type:
# cd /etc/lighttpd/
2) Store in hash variable your crypted password. Type:
# hash=$(echo -n "john:rtorrent:geexbox" | md5sum | cut -b -32)
remember to replace user and password with your own.
3) Make a file where you store your crypted password. Type :
# echo "john:rtorrent:$hash" > /etc/lighttpd/lighttpd-htdigest.user
4) Type two commands. Type :
# chown john /etc/lighttpd/lighttpd-htdigest.user
# chmod 400 /etc/lighttpd/lighttpd-htdigest.user
5) make a copy of your original file . Type:
# cp lighttpd.conf lighttpd.conf_original
6) Add this code at the end of config file:
server.modules += ( "mod_auth" )
auth.debug = 0
auth.backend = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd/lighttpd-htdigest.user"
auth.require = ( "/rutorrent/" => (
"method" => "digest",
"realm" => "rtorrent",
"require" => "valid-user"
))
So, type:
# vi lighttpd.conf
and paste these rows after
server.modules += ( "mod_scgi" )
scgi.server = (
"/RPC2" =>
( "127.0.0.1" =>
(
"host" => "127.0.0.1",
"port" => 8000,
"check-local" => "disable"
)
)
)
in bottom part of file.
7) Restart lighttpd . Type :
# systemctl restart lighttpd
How to use rtorrent as download manager
Check requirements:
I) Installed python
Run this command :
opkg list_installed | grep python
output will be like:
python-lsb-release - 1.0-1
python-simplejson - 2.5.0-1
PART A: How to remove download torrent file from /session folder and have a notification by email.
1) Creating folders
First of all you need to check what folder is sat up as your directory folder
Open rtorrent.rc config file using :
vi /etc/rtorrent.rc
and look for directory parameter. Default value is :
directory = /rtorrent
Tip: Geexbox could runs on a Secure Digital memory card of 8, 16 or 32 Gb, specially in embedded systems. If you think to use rtorrent for download a big amount of data you can use all Secure Digital space in short time. I suggest you to remove /rtorrent folder and re-create a new one as symbolic link pointing to an external hard drive. rtorrent will follow this link and will store your file in a more sized external hard drive.
In order to do this you can type this commands:
rm /rtorrent
ln -s your_external_folder /rtorrent
remember to replace your_external_folder with your preferred path. I used /media/SAMSUNG/rtorrent of an HD Samsung of 2 TB.
in your /rtorrent folder you need to have completed, loading, session, temp and watch folders. When rtorrent start would create automatically session, temp and watch folders. You need to create manually completed, loading folders typing:
cd /rtorrent
mkdir completed
mkdir loading
2) Make script folder
You need to choose a convenient folder to store this script. I made /usr/myscript/ and I made it using :
mkdir /usr/myscript
3) Alter rtorrent.rc file
Open your rtorrent.rc file as shown before and add this row :
system.method.set_key = event.download.finished,write_log,"execute=/usr/myscript/torrent_complete_sendmail ,$d.get_hash=,$d.get_name="
just after
schedule = low_diskspace,60,60,close_low_diskspace=100M
row.
This row force rtorrent when a finished download event happens to execute torrent_complete_sendmail script (show next step for details) passing it two parameters: hashfile and file name.
Remember to restart rtorrent for enable this modification:
systemctl restart rtorrent
4) Make torrent_complete_sendmail file
Create with your preferred editor this file and name it ‘torrent_complete_sendmail’ (without ‘). I used this comand :
vi /usr/myscript/torrent_complete_sendmail
typed i for start to edit and paste into it following lines:
#!/bin/sh
mv -f /rtorrent/session/$1.torrent /rtorrent/completed
python /usr/myscript/sendmail.py -i "$2"
date >> /usr/myscript/rtorrent_completed.txt
echo /rtorrent/session/$1.torrent >> /usr/myscript/rtorrent_completed.txt
This file move end torrent file to /rtorrent/completed folder in first row, call a python script (sendmail.py) with -i parameter and rtorrent_file_name in second one and write a very simple log in rtorrent_completed.txt with date and hashcode in 3th and 4th rows.
5) Make sendmail.py script :
#!/usr/bin/python
import smtplib
import sys, getopt
inputfile=''
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:",["ifile="])
except getopt.GetoptError:
print 'test.py -i
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i
sys.exit()
elif opt in ("-i", "--ifile"):
global inputfile
inputfile = arg
# print 'Input file is "', inputfile
if __name__ == "__main__":
main(sys.argv[1:])
#print 'Input file is "', inputfile
# Specifying the from and to addresses
fromaddr = 'Notify Download Cubox [email protected]'
toaddrs = '[email protected]'
# Writing the message (this message will appear in the email)
#msg = 'Scaricato con successo il file '
SUBJECT = 'File downloaded: ' + inputfile
TEXT = 'The file ' + inputfile + ' had been download successfully.'
msg = 'Subject: %snn%s' % (SUBJECT, TEXT)
# Gmail Login
username = '[email protected]'
password = 'your_gmail_password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
In order to enable your gmail address to send email you need to do following steps:
5.1) Enable your google accoutn to use SMTP :
=> Login into your google account, go to gmail, go to setting, go to POP/IMAP options and enable it for all messages
5.2) Allow access to your google account
=> follow this link
https://accounts.google.com/b/0/DisplayUnlockCaptcha
and enable it.
Remember to replace [email protected] and your_gmail_password with your own.
PART B: How to fill /session folder with new torrent file
6) Scheduling rtorrent_check execution script
Edit tour root cron file:
vi /var/spool/cron/crontabs/root
and add this line :
*/5 * * * * python /usr/my_script/rtorrent_check.py &
this line execute rtorrent_check.py each 5 minutes.
Rememeber to restart cron after saving this file:
systemctl restart crond
7) Make rtorrent_check.py script
Edit the file :
vi /usr/my_script/rtorrent_check.py
press i to start edit and paste:
#!/usr/bin/env python
# Now you can dynamically change the maximum number of simultaneous
# downloads writing the new number to the file "max_downloads_file" and
# then sending a signal SIGHUP to the rtorrentqueuemanager process.
import glob
import stat
import os
import shutil
import time
import signal
watch = "/rtorrent/watch"
session = "/rtorrent/session"
queue = "/rtorrent/loading"
max_downloads_file = "/rtorrent/max_downloads"
max_downloads = 5
def handler_sighup(signum, frame):
f = open(max_downloads_file, "r")
max_downloads = int(f.readline())
f.close()
#### START
signal.signal(signal.SIGHUP, handler_sighup)
sfiles = glob.glob(session + "/*.torrent")
oldesttime = 0
oldestfile = ""
#print max_downloads
#print len(sfiles)
ftime = ""
oldesttime = ""
qfiles = glob.glob(queue + "/*.torrent")
if len(sfiles) < max_downloads :
for i in qfiles :
ftime = os.stat(i)[stat.ST_MTIME]
if oldesttime == 0 or ftime < oldesttime :
oldesttime = ftime
oldestfile = i
#print oldestfile
if oldestfile != "" :
shutil.move(oldestfile, watch)
This script count how many torrent file you have in your /session folder and if they are less than max_downloads it put the oldest one in /rtorrent/session folder from /rtorrent/loading folder.
PART C: How to purge pending downloads in /session folder
edit your cron root file
vi /var/spool/cron/crontabs/root
and add this line:
0 10 * * * systemctl restart rtorrent
This restart rtorrent teach day at 00:10 and when it restart it don’t find completed torrent in /session folder and so they not exist any more for it.
This is not a good method for purge session but I didn’t fint a better one.
Conclusion :
So, if you store your torrent file in /rtorrent/loading folder a script check if your download queue has free position. If yes it move oldest torrent file in /rtorrent/session folder and start to download it. When download finishes other script move torrent file in /rtorrent/completed folder and send you an email alert.
