require 'libhttpclient' abort 'pattern needed' if not want = ARGV.shift want = want.sub('http://www.mininova.org', '') if want == 'killme' # rtorrent callback: kill parent Process.kill 'INT', Process.ppid rescue nil File.unlink ARGV.shift if not ARGV.empty? rescue nil exit end me = File.expand_path $0 # target: url? search term? torrent file? if File.exist? want # torrent file file = want else # http://www.mininova.org/search/house/seeds h = HttpClient.new 'http://www.mininova.org/' if want =~ /^\/?(tor|get)\/(\d+)$/ # mininova link lnk, want = want, $2 else # search term puts "searching.." p = h.get "/search/#{HttpServer.urlenc(([want]+ARGV).join(' '))}/seeds" # 01 Oct 08 TV Shows 121 House S05E03 HDTV XviD-LOL [eztv] in Other 349.78 MB 18322 17889 lnk = nil # parse results sorted by seed nr p.get_text.each_line { |l| case l when /\d\d ... \d\d (?:TV Shows|\w+) (?:\d+ )?(.*) in .* (\d+\.\d+ [MG]B) (\d+) \d+/ str, sz, seeds = $1, $2.to_f, $3.to_i sz *= 1024 if $2.include? 'GB' str = /#{str.gsub(/[^a-zA-Z0-9 ]/, '.*')}/ puts l, [str, sz, seeds].inspect # check target size if ARGV.include?('allsz') or (sz > 100 and sz < 1000) ostr = '' raise "failed #{str.inspect}" if p.parse.each { |e| case e.type when 'a'; lnk = e.attr['href'] ; ostr = '' when 'em', '/em'; ostr += ' ' when 'String' ostr += e.attr['content'] break if ostr =~ str else ostr = '' end } break end when /at the moment we are performing maintenance/i raise 'servfail' # TODO retry end } puts p.get_text if not lnk abort 'no search result' if not lnk puts lnk sleep 1 end if lnk =~ /tor\// # get the torrent file p = h.get lnk lnk = h.links.find { |lnk| lnk[0, 5] == '/get/' } puts lnk end begin p = h.get lnk end while p.content =~ /we are performing maintenance/ # weak # save tmp file Dir.chdir '/home/shared/films/dl' file = "auto.#{want}.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 # start rtorrent with us as on_finished callback system 'rtorrent', '-O', "on_finished=poil,\"execute=ruby,#{me},killme\"", 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