Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'nokogiri'
- require 'open-uri'
- def distance(page_a, page_b)
- return 0 if page_a == page_b
- pages = [page_a]
- dis(page_a, page_b, pages, 1)
- end
- def dis(page_a, page_b, pages, depth)
- new_links = []
- if depth < 10000
- pages.each do |page_name|
- page = Nokogiri::HTML(open(page_name))
- links = page.css("a")
- links.each do |x|
- if x["href"]
- if x["href"][0] == "/"
- puts page_name + x["href"].to_str
- return depth if page_name + x["href"].to_str == page_b
- new_links.push(page_name + x["href"].to_str) if x["href"].to_str != "/"
- end
- end
- end
- end
- new_depth = depth + 1
- return dis(page_a, page_b, new_links, new_depth)
- else
- puts "Wczerpano ilosc"
- return depth
- end
- end
- # czas1= Time.now
- # res1 = []
- # res1.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl"))
- # res1.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl/badania/zaklad/ZJP/granty"))
- # res1.push(distance("http://ruby.bastardsbook.com", "http://ruby.bastardsbook.com/toc/chapters/jail-logs-pcso"))
- # czas_po1 = Time.now
- # puts czas_po1 - czas1
- threads = []
- res2 = []
- czas2 = Time.now
- res2 = []
- threads << Thread.new{res2.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl"))}
- threads << Thread.new{res2.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl/badania/zaklad/ZJP/granty"))}
- threads << Thread.new{res2.push(distance("http://ruby.bastardsbook.com", "http://ruby.bastardsbook.com/toc/chapters/jail-logs-pcso"))}
- threads.join
- czas_po2 = Time.now
- # puts "Bez uzycia watkow"
- # puts czas_po1 - czas1
- puts "Z uzyciem watkow"
- puts czas_po2 - czas2
- res2.each do |x|
- puts x
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement