Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

67 рядки
1.6 KiB

  1. #!/usr/bin/env ruby
  2. require 'httparty'
  3. require 'ruby-progressbar'
  4. require 'uri'
  5. allowed_codes = [200, 302, 403]
  6. args = ARGV
  7. filename = args[0]
  8. contents = File.open(filename, 'rb') { |f| f.read }
  9. raw_links = URI.extract(contents, ['http', 'https'])
  10. # Remove trailing ')' from entry URLs
  11. links = []
  12. raw_links.each do |link|
  13. if link.end_with?(')')
  14. links.push(link[0...-1])
  15. else
  16. links.push(link)
  17. end
  18. end
  19. fails = []
  20. # Fail on any duplicate elements
  21. dup = links.select{|element| links.count(element) > 1}
  22. if dup.uniq.length > 0
  23. dup.uniq.each do |e|
  24. fails.push("(DUP): #{e}")
  25. end
  26. end
  27. # Remove any duplicates from array
  28. links = links.uniq
  29. count = 0
  30. total = links.length
  31. progressbar = ProgressBar.create(:total => total)
  32. # GET each link and check for valid response code from allowed_codes
  33. links.each do |link|
  34. begin
  35. count += 1
  36. res = HTTParty.get(link, timeout: 10)
  37. if res.code.nil?
  38. fails.push("(NIL): #{link}")
  39. next
  40. end
  41. if !allowed_codes.include?(res.code)
  42. fails.push("(#{res.code}): #{link}")
  43. end
  44. rescue Net::ReadTimeout
  45. fails.push("(TMO): #{link}")
  46. rescue OpenSSL::SSL::SSLError
  47. fails.push("(SSL): #{link}")
  48. rescue SocketError
  49. fails.push("(SOK): #{link}")
  50. rescue Errno::ECONNREFUSED
  51. fails.push("(CON): #{link}")
  52. end
  53. progressbar.increment
  54. end
  55. puts "#{count}/#{total} links checked"
  56. if fails.length <= 0
  57. puts "all links valid"
  58. exit(0)
  59. else
  60. puts "-- RESULTS --"
  61. fails.sort!
  62. fails.each do |e|
  63. puts e
  64. end
  65. exit(1)
  66. end