List the files in a BitTorrent torrent with this quick Ruby script. Requires that you gem install bencode awesome_print before you run it.
#!/usr/bin/env ruby
#
# torrentdump - prints the contents of a torrent.
#
# Copyright (C) 2011 Paul Legato. All rights reserved.
# Licensed for personal, non-commercial use only.
# This code comes with NO WARRANTY, express or implied.
#
# Prerequisite gem setup:
# sudo gem install bencode awesome_print
#
require 'rubygems'
require 'bencode'
require 'awesome_print'
unless ARGV[0]
STDERR.puts <<END
Usage: torrentdump <filename> <-v>
If -v is given, prints the entire contents of the torrent except the binary "pieces" hash data.
If -vv is given, prints everything, including the binary hash data.
Otherwise, prints only the file list.
END
exit 1
end
# Method from http://stackoverflow.com/questions/3201890/is-there-an-elegant-way-to-remove-a-specific-key-from-a-hash-and-its-sub-hashes
def recursive_delete!(hash, to_remove)
hash.delete(to_remove)
hash.each_value do |value|
recursive_delete!(value, to_remove) if value.is_a? Hash
end
end
data = BEncode.load(File.read(ARGV[0]).force_encoding("binary"))
if ARGV[1] == "-vv"
ap data
elsif ARGV[1] == "-v"
recursive_delete!(data, "pieces")
ap data
else
if data["info"]["files"]
dir = data["info"]["name"] || ""
ap data["info"]["files"].map {|x| dir + "/" + x["path"].join("/") }
else
ap data["info"]["name"]
end
end
A quick example:
% torrentdump ubuntu-10.04.2-server-i386.iso.torrent "ubuntu-10.04.2-server-i386.iso"
Torrent dissection: of interest here..
- Ruby 1.9 imposes a character encoding on every string by default. This is fine for dealing with text, but caused errors in the BEncode library. (For some reason, the authors of BitTorrent chose to re-invent the wheel and developed their own binary encoding format.) We use the
#force_encodingmethod on the torrent data to make Ruby treat the data as raw binary data and not try to interpret it. - More than you ever wanted to know about what’s inside a
.torrent file
The Torrentdump source code is available on GitHub.
Popularity: 9% [?]
If you like this post and would like to receive updates from this blog, please subscribe our feed.
