require 'libhttpclient' want = ARGV.join '+' # target: url? search term? torrent file? if File.exist? want # torrent file file = want else if want =~ /^http.*:\/\// h = HttpClient.new want lnk = want else h = HttpClient.new 'http://btjunkie.org/' pg = h.get "/search?q=#{want}" lnk = h.links.grep(/dl.btjunkie.org.*#{want.gsub('+', '.*')}.*torrent$/i).first h = HttpClient.new lnk end p = h.get lnk # save tmp file Dir.chdir(ENV['DLDIR'] || '/home/shared/films/dl') file = "auto.#{want.gsub(/[^a-z0-9.-]/i, '_')}.torrent" File.open(file, 'w') { |fd| fd.write p.content } end # remove torrent from session dir sess = File.read(File.expand_path('~/.rtorrent.rc'))[/^session\s*=\s*(\/.*)/, 1] rescue nil # list of files before starting rtorrent prest = Dir[File.join(sess, '*.torrent')] if sess system 'rtorrent', file # remove torrent file File.unlink file rescue nil # cleanup session dir if only 1 file added st = Dir[File.join(sess, '*.torrent')] - prest if sess File.unlink st.first if st and st.length == 1