require 'rubygems' require 'mechanize' require 'nokogiri' require 'logger' require 'open-uri' require 'pp' require 'bencode' require 'json' class Example def initialize() #create the agent to search in the code @agent = Mechanize.new do |a| a.history.max_size = 1 a.user_agent_alias = 'Mac Safari' a.read_timeout = 60 end end def run time = Time.now logfile = "log_" + time.strftime("%Y-%m-%d") + ".txt" log = Logger.new(logfile) log.debug "***** Begin : " + time.inspect log.debug "-------------------------------------------------------------------------" torrent_info = {'68a5df15d2ac9b2f14ffe35ff907dd4ec5092264' => ["h%A5%DF%15%D2%AC%9B%2F%14%FF%E3_%F9%07%DDN%C5%09%22d", "[[\"http://tracker.publicbt.com/announce\"],[\"http://tracker.torrent.to:2710/announce\"],[\"http://tracker.istole.it/announce\"],[\"http://tracker2.istole.it:6969/announce\"],[\"http://announce.torrentsmd.com:8080/announce\"],[\"http://tracker.prq.to/announce\"],[\"http://exodus.1337x.org/announce\"],[\"http://genesis.1337x.org:1337/announce\"],[\"http://tracker.torrent.to:2710/announce\"],[\"http://tracker.torrentbay.to:6969/announce\"],[\"http://inferno.demonoid.com:3407/announce\"],[\"http://tracker.ex.ua/announce\"],[\"http://tracker.ilibr.org:6969/announce\"],[\"http://10.rarbg.com/announce\"],[\"http://tracker.ilibr.org:6969/announce\"],[\"udp://denis.stalker.h3q.com:6969/announce\"],[\"udp://tracker.publicbt.com:80/announce\"],[\"udp://tracker.openbittorrent.com:80/announce\"],[\"http://tracker4.finalgear.com/announce\"]]"] } if(torrent_info.size > 0) torrent_trackers = Array.new torrent_info.each { |hashcode, details| #clear all the lists torrent_trackers.clear binary_infohash = details[0] torrent_trackers = JSON.parse(details[1]) seeds = 0 peers = 0 #at this point I have the complete list of trackers for a specific torrent #and the next step is to send to each one of them a request with the infohash if(torrent_trackers.length > 0) torrent_trackers.each { |t| if(!t[0].eql?('')) url = t[0].to_s + '?info_hash=' + details[0].to_s if(url.include?("http://"))#http trackers #pp url tracker_seeds = -1 tracker_peers = -1 begin Timeout::timeout(50) do tracker_seeds, tracker_peers = extract_information(url, log) end rescue SystemCallError => e log.error 'Fail saving the file '+url+': SystemCallError' rescue Timeout::Error => e log.error 'Fail saving the file '+url+': Timeout' rescue Exception => e log.error 'General exception for url ' + url log.error e end if(tracker_seeds > -1 && !tracker_seeds.nil? && tracker_peers > -1 && !tracker_peers.nil?) seeds = seeds + tracker_seeds peers = peers + tracker_peers else log.debug 'The torrent is not present in ' + t[0] end else#udp trackers end end } log.debug 'Were found ' + seeds.to_s + ' seeds and ' + peers.to_s + ' peers for torrent ' + hashcode end } end log.debug "-------------------------------------------------------------------------" log.debug "***** End : " + Time.now.inspect end #end ############################################################################## ## send a request to the tracker, save the file and extract the information ## ############################################################################## def extract_information(url, log) seeds = -1 peers = -1 retries = 1 path = 'announce' begin Timeout::timeout(5) do log.debug 'calling ' + url res = @agent.get(url).save_as(path) if(res != nil && res != '' ) log.debug 'DONE' info = BEncode.load_file(path) seeds = info['complete'] peers = info['incomplete'] #delete the announce file, after extracting the information File.delete(path) else log.debug 'FAIL' end end rescue Exception => e log.error 'Fail saving the file '+url+': General exception' retries -= 1 if(retries > 0) retry else log.error 'Fail saving the file '+url+' (General Exception).' log.error e end rescue Timeout::Error => e log.error 'Fail saving the file '+url+': Timeout' retries -= 1 if(retries > 0) retry else log.error 'Fail saving the file '+url+' (Timeout::Error).' log.error e end end return seeds.to_i, peers.to_i end end