Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.71 KB | None | 0 0
  1. require 'nokogiri'
  2. require 'open-uri'
  3.    
  4. def distance(page_a, page_b)
  5.     return 0 if page_a == page_b
  6.     pages = [page_a]
  7.     dis(page_a, page_b, pages, 1)
  8. end
  9.  
  10. def dis(page_a, page_b, pages, depth)
  11.     new_links = []
  12.     if depth < 10000
  13.         pages.each do |page_name|
  14.             page = Nokogiri::HTML(open(page_name))  
  15.             links = page.css("a")
  16.             links.each do |x|
  17.                 if x["href"]
  18.                     if x["href"][0] == "/"
  19.                         puts page_name + x["href"].to_str
  20.                         return depth if page_name + x["href"].to_str == page_b
  21.                         new_links.push(page_name + x["href"].to_str) if x["href"].to_str != "/"
  22.                     end
  23.                 end
  24.             end
  25.         end
  26.         new_depth = depth + 1
  27.         return dis(page_a, page_b, new_links, new_depth)
  28.     else
  29.         puts "Wczerpano ilosc"
  30.         return depth
  31.     end
  32. end
  33.  
  34.  
  35. # czas1= Time.now
  36. # res1 = []
  37. # res1.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl"))
  38. # res1.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl/badania/zaklad/ZJP/granty"))
  39. # res1.push(distance("http://ruby.bastardsbook.com", "http://ruby.bastardsbook.com/toc/chapters/jail-logs-pcso"))
  40. # czas_po1 = Time.now
  41. # puts czas_po1 - czas1
  42.  
  43. threads = []
  44. res2 = []
  45. czas2 = Time.now
  46. res2 = []
  47. threads << Thread.new{res2.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl"))}
  48. threads << Thread.new{res2.push(distance("http://www.ii.uni.wroc.pl", "http://www.ii.uni.wroc.pl/badania/zaklad/ZJP/granty"))}
  49. threads << Thread.new{res2.push(distance("http://ruby.bastardsbook.com", "http://ruby.bastardsbook.com/toc/chapters/jail-logs-pcso"))}
  50. threads.join
  51. czas_po2 = Time.now
  52. # puts "Bez uzycia watkow"
  53. # puts czas_po1 - czas1
  54.  
  55. puts "Z uzyciem watkow"
  56. puts czas_po2 - czas2
  57.  
  58. res2.each do |x|
  59.     puts x
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement