Hi,
Recently I've wrote a small program in Ruby to manage my torrents.
UPDATE: please use the link above for an updated version.
I use it this way:
Recently I've wrote a small program in Ruby to manage my torrents.
UPDATE: please use the link above for an updated version.
I use it this way:
$ find . -name \*.torrent | launchtorrents -
It can read a list of .torrent files from standard input (e.g. terminal) or use command line arguments for that.
Here is the complete program listing:
#!/usr/bin/ruby
#
# launchtorrents -- a tiny program to launch multiple BitTorrent
# sessions
#
# Copyright (C) 2007 Alex Shulgin
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
$KCODE='utf-8'
$PROGNAME = File.basename($0)
if ARGV.empty?
STDERR.puts "usage: #$PROGNAME [options] [FILE]..."
STDERR.puts "options:"
STDERR.puts " - read file list from terminal"
exit 1
end
def fatal(msg)
STDERR.puts "#$PROGNAME: #{msg}"
exit 1
end
def warning(msg)
STDERR.puts "#$PROGNAME: #{msg}"
end
(ARGV[0] == '-' ? STDIN : ARGV).each do |torrent|
torrent = torrent.chomp
# parse torrent info: file set and sizes
dirname = "."
filename = nil
files = {}
multfiles = false
IO.popen("btshowmetainfo '#{torrent}'").each do |line|
case line
when /^directory name\.*: (.+)$/:
dirname = $1
when /^file name\.*: (.+)$/:
filename = $1
files[$1] = 0
when /^file size\.*: (\d+)/:
files[filename] = $1.to_i
when /^files\.*:/:
multfiles = true
when /^\s*(.+)\s+\((\d+)\)$/:
files[$1] = $2.to_i if multfiles
end
end
fatal "cannot query torrent file: #{torrent}" if files.empty?
# p dirname
# p files
# check sizes: leech if any file is unfinished or missing, else seed
begin
pwd = Dir.pwd
Dir.chdir(File.dirname(torrent))
cmd = "seed"
files.each do |file, size|
file = "#{dirname}/#{file}"
filesize = File.exist?(file) ? File.size(file) : 0
# p file
# p filesize
if filesize < size
cmd = "leech"
elsif filesize > size
warning "file size is greater than real size: #{file}"
throw
end
end
# puts "#{cmd} #{torrent}"
basename = File.basename(torrent)
system(cmd, basename) or warning "failed to #{cmd}: #{torrent}"
rescue
next
ensure
Dir.chdir(pwd)
end
end
As one can see, the program uses two auxiliary commands: leech and seed. These are the helpers I wrote for my convenience earlier; leech is used to start or continue torrent download, seed is used on finished torrents.
Here are the listings:
leech:
#!/bin/shseed:
for i in $@; do
sh -c "nohup btdownloadheadless '$i' &>/dev/null &";
done
#!/bin/sh
for i in $@; do
sh -c "nohup btdownloadheadless --check_hashes 0 '$i' &>/dev/null &";
done
The BitTorrent package in use is bittornado.
Listing coloring by GNU source-highlight.
See you. :-)
PS: something's wrong with blogspot editor, so I couldn't make all the indentation look quite right...
No comments:
Post a Comment