Friday, October 14, 2011

Add trackers to .torrent files in linux

Windows users have BEncode Editor to edit their torrent files without changing info hash of the torrent file. But, linux hasn't yet had a torrent editor.

I use rtorrent for downloading torrents and it doesn't have the feature of adding trackers. To me, that is very necessary because in my institute, many trackers are blocked and I have to reply on UDP trackers to get the seeds/peers stats.

There is a python library named bencode that helps you editing the torrent file but there is no usage given on the internet.

So, here is an example script to add torrents to a .torrent file using bencode.
  • This script assumes that you have a file that contains the list of trackers separated by new-lines. (first argument to the script)
  • This script creates a new torrent file in the current directory with the added trackers. 
  • The script takes the input torrent file as the second argument.
WARNING: Use it only on public trackers, otherwise it will get you banned.
#!/usr/bin/python2

"""
Author: shadyabhi (abhijeet[dot]1989[at]gmail[dot]com)

"""

import bencode
import sys
from os import path

if len(sys.argv) < 2:
    print "First argument: File containing trackers"
    print "Second argument: .torrent file"
    sys.exit(0)

tracker_file = open(sys.argv[1])
torrent_file = open(sys.argv[2])

trackers_list = []
#Creating the list of trackers.
trackers_list[:] = (value for value in tracker_file.read().split("\n") if value != '')
decoded_data = bencode.bdecode(torrent_file.read())

print "Trackers Before: "
for tracker in decoded_data['announce-list']:
    print tracker

for tracker in trackers_list:
    if tracker not in decoded_data['announce-list']:
        decoded_data['announce-list'].append([tracker])

print "Now, the trackers in the torrent are: "
for tracker in decoded_data['announce-list']:
    print tracker

#Writing the torrent file
f = open("new_"+path.basename(sys.argv[2]), "w")
f.write(bencode.bencode(decoded_data))
f.close() 

Sample Usage:

shadyabhi@archlinux ~/github/bencode_py $ python2 add_tracker.py /media/abhijeet/Misc/trackers ~/house.torrent 
Trackers Before: 
['http://10.rarbg.com:80/announce']
['http://9.rarbg.com:2710/announce']
['udp://11.rarbg.com:80/announce']
Now, the trackers in the torrent are: 
['http://10.rarbg.com:80/announce']
['http://9.rarbg.com:2710/announce']
['udp://11.rarbg.com:80/announce']
['udp://tracker.openbittorrent.com:80']
['udp://tracker.publicbt.com:80']
['udp://tracker.ccc.de:80']
['udp://tracker.istole.it:80']
['udp://tracker.1337x.org:80/announce']
['udp://tracker.torrentbox.com:2710']
['udp://tracker.openbittorrent.com:80']
['udp://tracker.torrentbox.com:2710']
['udp://tracker.openbittorrent.com:80/announce']
['udp://tracker.publicbt.com:80/announce']
shadyabhi@archlinux ~/github/bencode_py $ ls
total 36K
-rwxr-xr-x 1 shadyabhi users  960 Oct 14 02:20 add_tracker.py
-rw-r--r-- 1 shadyabhi users 3.2K Oct 13 20:12 bencode.py
-rw-r--r-- 1 shadyabhi users 4.4K Oct 13 20:12 bencode.pyc
-rw-r--r-- 1 shadyabhi users  786 Oct 14 02:05 check_trackers.py
-rw-r--r-- 1 shadyabhi users  15K Oct 14 02:51 new_house.torrent
shadyabhi@archlinux ~/github/bencode_py $

 You can also get the script at https://github.com/shadyabhi/Bencode-Torrent-Editor.

6 comments:

  1. Great if you are using public trackers...

    ReplyDelete
  2. Thanks! It might be useful to check for duplicates before adding the new trackers. Some of them might already be listed in the torrent file.

    ReplyDelete
  3. Check out http://www.trackon.org/ for the list of public trackers and their status.

    ReplyDelete
  4. Very nice! Was Anton´s proposal adopted in github?

    ReplyDelete
  5. I didn't think people would be reading this post :P. But, anyways if that's a wanted feature, I'll implement that update you by this weekend.

    ReplyDelete