---
title: BTG
layout: default
created: 2008-10-10 22:41:02 +0200
updated: 2008-10-10 22:41:02 +0200
toc: false
tags:
  - know-how
  - software
  - linux
  - software
  - bittorrent
  - btg
---
**Homepage:** <del><http://btg.berlios.de/></del> <http://sourceforge.net/projects/btg.berlios/>


Incoming directory
==================

To have a directory scanned for `.torrent` files and let them be added to BTG, there is this nice bash script from
the [BTG HowTo](http://btg.berlios.de/howto.html#using-an-incoming-directory):

{% highlight bash %}
#!/bin/sh

CLIENT=btgcli
# The directory containing the torrent files.
INCOMING_DIR=~/btg/incomming
# The directory to which .torrent files are moved
# to after loading them into BTG.
DONE_DIR=~/btg/incomming/done

GOT_SESSION=0
$CLIENT -A -n -c "detach" &> /dev/null && GOT_SESSION=1

if [ $GOT_SESSION -eq 0 ]
then
  $CLIENT -n -c "detach" &> /dev/null && GOT_SESSION=1
fi

if [ $GOT_SESSION -eq 0 ]
then
  echo "Unable to attach or create a BTG session."
  exit -1
fi

TORRENT_ADDED=0

cd $INCOMING_DIR && \
for f in `ls -1 *.torrent 2> /dev/null` ; do
  echo "Loading file: $f" && \
  $CLIENT -A -n -c "detach" -o $f &> /dev/null && \
  TORRENT_ADDED=`expr $TORRENT_ADDED + 1` && \
  mv $f $DONE_DIR
done

if [ $TORRENT_ADDED -gt 0 ]
then
  echo "Added $TORRENT_ADDED torrents to BTG."
fi
{% endhighlight %}


Set Speedlimits based on time of day
====================================

This script also comes from the [BTG HowTo](http://btg.berlios.de/howto.html#setting-global-limits-based-on-the-time-of-the-day):

{% highlight bash %}
#!/bin/sh

# The location of the BTG client application.
CLIENT=btgcli

H=`date +%H`
O="none"

if [ "$H" -gt "0" ] || [ "$H" -lt "6" ]
then
    O="night"
fi

if [ "$H" -gt "6" ] || [ "$H" -lt "12" ]
then
    O="morning"
fi

if [ "$H" -gt "12" ] || [ "$H" -lt "16" ]
then
    O="midday"
fi

if [ "$H" -gt "16" ] || [ "$H" -lt "23" ]
then
    O="evening"
fi

# Max upload limit.
UL_MAX=75

# Global limits in KiB/sec.
UL=-1
DL=-1
SET_LIMIT=0

case "$O" in
    night)
       UL=$UL_MAX
       SET_LIMIT=1
       echo "Limit:$O:$UL:$DL"
       ;;
    morning)
       UL=`expr $UL_MAX - 20`
       SET_LIMIT=1
       echo "Limit:$O:$UL:$DL"
       ;;
    midday)
       UL=`expr $UL_MAX - 40`
       SET_LIMIT=1
       echo "Limit:$O:$UL:$DL"
       ;;
    evening)
       UL=`expr $UL_MAX - 70`
       SET_LIMIT=1
       echo "Limit:$O:$UL:$DL"
       ;;
    *)
       echo "Not setting limit."
       ;;
esac

if [ "$SET_LIMIT" -eq "0" ]
then
    exit 0
fi

GOT_SESSION=0
$CLIENT -A -n -c "detach" &> /dev/null && GOT_SESSION=1

if [ $GOT_SESSION -eq 0 ]
then
  $CLIENT -n -c "detach" &> /dev/null && GOT_SESSION=1
fi

if [ $GOT_SESSION -eq 0 ]
then
  echo "Unable to attach or create a BTG session."
  exit -1
fi

$CLIENT -A -n -c "glimit $UL $DL -1 -1;detach" &> /dev/null && \
echo "Limit set."
{% endhighlight %}
