Results 1 to 14 of 14

Thread: Dreambox Torrent Script

  1. #1
    Senior Member RHCP's Avatar
    Join Date
    Jan 2008
    Location
    Molesting a Cow
    Age
    38
    Posts
    740
    Thanks
    58
    Thanked 147 Times in 96 Posts
    Rep Power
    237
    Reputation
    728

    Default Dreambox Torrent Script

    Today i decided to write a script which would simplify the process of remotely initiating a torrent download on my dreambox. The script just cuts out the need to use telnet and the bash prompt. In the off chance that someone wants to do something similar, this is how i did it.
    I'm using the PLI Iolite 2008 Edition image on my 7020 and couldn't find a torrent plugin, so i had to use ctorrent.

    The script is launched via crond at midnight (to use up my off peak mbs) or any time via telnet. It checks if .torrent files exist in a specific folder on the HDD, then proceeds to download them according to the settings specified in the script. Hence, i FTP .torrent files to the dbox and the downloads start at midnight.

    ctorrent is available (only for the 7020).

    Either extract the contents of the 'ctorrent'.gz file to the /tmp directory, and then install via the 'software management/manual addons'
    or
    copy the .gz file to /tmp directory, then from telnet run
    Code:
    gunzip /tmp/ctorrent_1.3.4-r6_powerpc.ipk.gz
    ipkg install /tmp/ctorrent_1.3.4-r6_powerpc.ipk
    Here is my simple script

    Code:
    #!/bin/sh
    stime=1 	#Seed Time hrs
    port=1325 	#Port +1 for each simultaneous download (ie 5000, 5001, 5002...5010...)
    cache=1 	#MBs torrent cache
    URate=18 	#In KB/S Set to 10% lower than max Up rate - Split evenly amongst multiple downloads
    DRate=999 	#In KB/S Split evenly amongst multiple downloads - Set really high to max it
    MDownload=3 #Max Simultaneous downloads - any 'leftover' torrents will be kept and downloaded the next time
    TDir="/media/hdd/Torrents"				#Where .Torrent files are uploaded/kept
    TDown="/media/hdd/Torrents/Downloads" 	#Download dir
    LDir="/media/hdd/Torrents/Logs" 		#Log dir
    DelTor=Yes	# Yes or No - Delete .Torrent file after starting download
    Log=Yes		# Yes or No - Logs output of ctorrent for each download to a file in log dir
    
    mkdir -p "$TDir"
    mkdir -p "$TDown"
    if [ $Log = Yes ]
    then
    	mkdir -p "$LDir"
    	time=`date -I`
    fi
    
    cd "$TDir"
    ls -1 *.torrent > tor1_temp
    if [ $? = 0 ]
    then
    	TorData=`cat tor1_temp | tr '\n' '|'` 
    	lines=`wc -l tor1_temp| awk '{print $1}`
    	rm tor1_temp
    	if  [ $MDownload -lt $lines ] 
    	then
    		lines=$MDownload
    	fi
    
    	div=1
    	while [ $div -lt $lines ]
    	do
    		div=`expr $div + 1`
    	done
    	
    	DRate=`expr $DRate / $div`
    	URate=`expr $URate / $div`
    	count=1
    	cd "$TDown"
    	while [ $count -le $lines ]
    	do
    		Tor=`echo $TorData | cut -f$count -d"|"`
    		if [ $Log = Yes ]
    		then
    	 		ctorrent -e $stime -C $cache -p $port -D $DRate -U $URate "$TDir/$Tor" > "$LDir/$time.$Tor".txt &
    		else
    			ctorrent -e $stime -C $cache -p $port -D $DRate -U $URate "$TDir/$Tor" > /dev/null &
    	   	fi
     	   	
     	 	if [ $DelTor = Yes ]
     	    then
     	    	rm "$TDir/$Tor"
     	    fi
     	    port=`expr $count + $port`
    		count=`expr $count + 1`
    	done
    else
    	rm tor1_temp
    	exit
    fi
    Adjust the variables and save the script somewhere using a unix compatible editor, i call it dts.sh and keep it in /etc
    The only way i could successfully launch via crond was to edit /etc/cron/crontabs/root using a unix editor to
    Code:
    #SHELL=/bin/sh
    0 0 * * *  /etc/dts.sh
    from the telnet stop and start crond so the changes may take effect
    Code:
    killall crond
    crond
    This explains crond and the format it uses.

    I have no doubt there more efficient and correct ways of writing this script, but before today i had never touched a bash script and knew only a couple of unix commands...

    Cheers, RHCP.
    Democracy: Three wolves and a sheep voting on what's for lunch.



Look Here ->
  • #2
    Senior Member
    jonc's Avatar
    Join Date
    Jan 2008
    Location
    Bangkok
    Posts
    2,130
    Thanks
    401
    Thanked 836 Times in 335 Posts
    Rep Power
    429
    Reputation
    7951

    Default

    Quote Originally Posted by RHCP View Post
    Today i decided to write a script which would simplify the process of remotely initiating a torrent download on my dreambox.
    Well done indeed.. Thanks for this maybe you should also post this on the PLi site under 3rd party development -

    Great work..

  • #3
    Junior Member
    Join Date
    Aug 2008
    Age
    54
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default Torrent script improvements

    Hi,

    Thanks for the great script. I took it a made same improvements important for me:

    -Ability to handle torrents that take more than 24 hours to download. The previous script opens a new ctorrent process each time it is executed and the .torrent file exists
    -Connection to CTCS

    The new script

    Code:
    #!/bin/sh
    stime=1 	#Seed Time hrs
    port=1325 	#Port +1 for each simultaneous download (ie 5000, 5001, 5002...5010...)
    cache=1 	#MBs torrent cache
    URate=18 	#In KB/S Set to 10% lower than max Up rate - Split evenly amongst multiple downloads
    DRate=999 	#In KB/S Split evenly amongst multiple downloads - Set really high to max it
    MDownload=3 #Max Simultaneous downloads - any 'leftover' torrents will be kept and downloaded the next time
    TDir="/media/hdd/Torrents"				#Where .torrent files are uploaded/kept
    TDown="/media/hdd/Torrents/Downloads" 	#Download dir
    LDir="/tmp" 		#Log dir
    DelTor=No	# Yes or No - Delete .Torrent file after starting download
    Log=Yes		# Yes or No - Logs output of ctorrent for each download to a file in log dir
    CtcsURL=192.168.1.2:2780  #CTCS Server URL. If you what to control your torrents via the CTCS web interface
    
    mkdir -p "$TDir"
    mkdir -p "$TDown"
    
    cd "$TDir"
    ls -1 *.torrent > tor1_temp
    if [ $? = 0 ]
    then
    	TorData=`cat tor1_temp | tr '\n' '|'` 
    	lines=`wc -l tor1_temp| awk '{print $1}`
    	rm tor1_temp
    	if  [ $MDownload -lt $lines ] 
    	then
    		lines=$MDownload
    	fi
    
    	div=1
    	while [ $div -lt $lines ]
    	do
    		div=`expr $div + 1`
    	done
    	
    	DRate=`expr $DRate / $div`
    	URate=`expr $URate / $div`
    	count=1
    	cd "$TDown"
    	while [ $count -le $lines ]
    	do
    		Tor=`echo $TorData | cut -f$count -d"|"`
    		processName=`echo $Tor | cut -c 1-20`
    		resultcode=`ps | grep "$processName" |grep "ctorrent" |wc -l`
    		if [ $resultcode = 0 ]
    		then
    			if [ $Log = Yes ]
    			then
    				ctorrent -X "$processName" -d -e $stime -C $cache -p $port -D $DRate -U $URate -S $CtcsURL  "$TDir/$Tor" >> "$LDir/$Tor".txt &
    				echo Starting to download $Tor >> "$LDir/$Tor".txt 
    			else
    				ctorrent -X "$processName" -d -e $stime -C $cache -p $port -D $DRate -U $URate -S $CtcsURL  "$TDir/$Tor" > /dev/null &
    			fi
    		else     
    		       if [ $Log = Yes ]
    		       then
                              echo $Tor is allready being downloaded >> "$LDir/$Tor".txt 
    		       fi
    		fi
     	   	
     	    if [ $DelTor = Yes ]
     	    then
     	    	mv "$TDir/$Tor" "$TDir/$Tor.OLD"
     	    fi
     	    port=`expr $count + $port`
    		count=`expr $count + 1`
    	done
    else
    	rm tor1_temp
    	exit
    fi
    Thanks.

  • #4
    Senior Member RHCP's Avatar
    Join Date
    Jan 2008
    Location
    Molesting a Cow
    Age
    38
    Posts
    740
    Thanks
    58
    Thanked 147 Times in 96 Posts
    Rep Power
    237
    Reputation
    728

    Default

    Thanks for the improvements, i like what you have done. Couple of questions, you are obviously using a different ctorrent to me, as the
    Code:
    -X
    switch in the ctorrent (in first post) im using gives an error as it displays meta-tag info. In enhanced ctorrent it supposedly runs $processName as a command. What ctorrent are you using, and what does the -X switch do?

    What platform are you running this on? Dreambox? If your using enhanced ctorrent, i would love to get a copy of a compiled version (i have no linux setup atm and don't want to compile it).

    Also, what does the
    Code:
    ctorrent -X "$processName"
    do? If -X switch actually runs the $processName (which is first 20 letters of torrent title) as a command after download completion then i don't get what it does, or is -X used to give ctorrent process name (in ps) some part of the torrent name to identify it? Which would make sense as that's what
    Code:
    resultcode=`ps | grep "$processName" |grep "ctorrent" |wc -l`
    		if [ $resultcode = 0 ]
    seems to be looking for.

    Also, i had a problem with my script when downloading files which had long titles or certain characters...i did pinpoint the problem, but i haven't used it in ages and have now forgotten what caused it :P Have you experienced anything like this?

    Thanks, RHCP.
    Democracy: Three wolves and a sheep voting on what's for lunch.

  • #5
    Junior Member
    Join Date
    Aug 2008
    Age
    54
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default

    Hi,

    I use enhanced CTorrent on a Enigma 2 dreambox (DM800).


    The reason to use the -X parameter is to identify the different ctorrent processes when executing the ps command. The output of the ps command, when executed as a cron, is generated with a limitation inthe number of columns (at least on my dreambox)

    Code:
     ......
     2768 root       4244 S   /usr/bin/CCcam_2.0.9 
      864 root       9780 S   ctorrent -X GK.S01E01.HDTV.XviD- -d -e 48 -C 16 -p 1326 -D 199 -U 3 -S 192.168.
      885 root      15204 S   ctorrent -X GK.S01E02.HDTV.XviD- -d -e 48 -C 16 -p 1328 -D 199 -U 3 -S 192.168.
      906 root      19764 S   ctorrent -X GK.S01E03.HDTV.XviD- -d -e 48 -C 16 -p 1331 -D 199 -U 3 -S 192.168.
     2647 root        372 S   /bin/sh /usr/bin/enigma2.sh
    .....
     3256 root      12492 S   ctorrent -X GK.S01E04.HDTV.XviD- -d -e 48 -C 16 -p 1331 -D 249 -U 4 -S 192.168.
    10523 root        580 S   telnetd
    As you see, the right part of the ctorrent commans is not available. Very strange because happens to me only for ps commands executed in a cron. I googled but did not find a solution.

    So the only way to kwow the .torrent files currently being downloaded by ctorrent processes is identifying them at the left part of the process command.

    It's a pity because I was planning to use -X to run a script to delete *.torrent files but this was the only solution I found to associante .torrents to unix processes. Of course temp files could be used.... but I preffer current solution.

    To avoid problems with long names or certain characters all references to file names must be quoted ("file name").

    While coding the script I did not get any errors with the sample ctorrents I used, but reviewing the script I found a potential source of problems:

    Code:
    processName=`echo $Tor | cut -c 1-20`
    Should be changed to:
    Code:
    processName=`echo "$Tor" | cut -c 1-20`
    I am not at home now and I cannot test this, but I think this is safer.

    Lgarrido

  • #6
    Junior Member
    Join Date
    Jul 2009
    Age
    33
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default

    Hi, i'm having some probmelms to reach /etc/cron/crontabs/root my telnet session just says "permission denied" does anyone know how to solve this ?

    i've also tried to just type /etc/dts.sh, but then it says "-sh: /etc/dts.sh: not found
    "
    please help,
    Regards mr.Patel

  • #7
    Senior Member
    Join Date
    Feb 2008
    Posts
    693
    Thanks
    4
    Thanked 217 Times in 139 Posts
    Rep Power
    263
    Reputation
    2138

    Default

    Quote Originally Posted by hiren View Post
    Hi, i'm having some probmelms to reach /etc/cron/crontabs/root my telnet session just says "permission denied" does anyone know how to solve this ?
    Regards mr.Patel
    Try "crontab -e" to edit or "crontab -l" to list.

  • #8
    Junior Member
    Join Date
    Jul 2009
    Age
    33
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default

    Hi!

    thanks it worked, but i would like to start the scritp manually and not like a timer.

    so the app starts to download all torrents in the torrent folder and puts the files in the downloads folder. when i type a command in to the telnet box.

    I'm using pli jade3 if thats intressting.

    Thank you for the help
    edit:
    i have tried to save the
    #SHELL=/bin/sh
    ***** /etc/dts.sh

    but i don't know how to save after i've writtent crontab-e

    i'm using putty on windows xp
    Last edited by hiren; 07-07-09 at 02:49 AM.

  • #9
    Senior Member
    Join Date
    Feb 2008
    Posts
    693
    Thanks
    4
    Thanked 217 Times in 139 Posts
    Rep Power
    263
    Reputation
    2138

    Default

    Quote Originally Posted by hiren View Post
    Hi!

    thanks it worked, but i would like to start the scritp manually and not like a timer.
    To start it manually, just telnet to the box & enter "/etc/dts.sh"

  • #10
    Junior Member
    Join Date
    Jul 2009
    Age
    33
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default

    yeah, but my box just says that ther is no. /etc/dts.sh

    and i'm 100% shure that i've put it there.

  • #11
    Senior Member nfnovice's Avatar
    Join Date
    Jan 2008
    Posts
    1,430
    Thanks
    261
    Thanked 336 Times in 213 Posts
    Rep Power
    282
    Reputation
    1840

    Default

    chmod 755 /etc/dts.sh ???
    Dm500, DM5620, DM600 x2, DM7000 x1, DM7020, DM7025, DM800, VU+DUO and a partridge in a pear tree
    All it takes for evil to prevail is for good men to do nothing

  • #12
    Senior Member
    Join Date
    Feb 2008
    Posts
    693
    Thanks
    4
    Thanked 217 Times in 139 Posts
    Rep Power
    263
    Reputation
    2138

    Default

    Quote Originally Posted by hiren View Post
    yeah, but my box just says that ther is no. /etc/dts.sh

    and i'm 100% shure that i've put it there.
    Telnet into the box & enter "ll /etc"

    You should see a line something like:
    -rwxr-xr-x 1 root root 80 Jan 1 1970 dts.sh

    in the output. The important parts are -rwxr-xr-x (the x's show that it is executable by owner (root), member of the group (root) and everyone else) and the last (dts.sh).

  • #13
    Junior Member
    Join Date
    Jul 2009
    Age
    33
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Reputation
    10

    Default

    thanks, but yes i've already changed the chmod

    the line is

    -rwxr-xr-x 1 root root 1741 Jul 6 17:33 dts.sh*

    wich seems to be correct?

    but still when i write

    /etc/dts.sh

    the answear is -sh: /etc/dts.sh: not found

    thank you for all help untill now

  • #14
    Senior Member
    Join Date
    Feb 2008
    Posts
    693
    Thanks
    4
    Thanked 217 Times in 139 Posts
    Rep Power
    263
    Reputation
    2138

    Default

    Can you type in the following and show us the result:

    cd /
    find . -name dts.sh

  • Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •